Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Add inflation attack check and prevention to deploy scripts #209

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions packages/perennial-vaults/deploy/01_deploy_vault_alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import 'hardhat-deploy'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { DeployFunction } from 'hardhat-deploy/types'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { BalancedVault__factory } from '../types/generated'
import { SingleBalancedVault__factory, IERC20__factory } from '../types/generated'
import { utils } from 'ethers'

const VAULT_TOKEN_NAME = 'Perennial Vault Alpha'
const VAULT_TOKEN_SYMBOL = 'PVA'
const SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT = utils.parseEther('1')

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getNamedAccounts, ethers } = hre
const { deploy, get } = deployments
const { deploy, get, getOrNull } = deployments
const { deployer } = await getNamedAccounts()
const deployerSigner: SignerWithAddress = await ethers.getSigner(deployer)

Expand All @@ -22,6 +24,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const short = (await get('Product_ShortEther')).address
const targetLeverage = ethers.utils.parseEther('1.5')
const maxCollateral = ethers.utils.parseEther('6000000')
const dsuERC20 = IERC20__factory.connect(dsu, deployerSigner)

const vaultImpl = await deploy('PerennialVaultAlpha_Impl', {
contract: 'BalancedVault',
Expand All @@ -32,6 +35,17 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
autoMine: true,
})

// If no deployment detected
const newProxyDeployment = (await getOrNull('PerennialVaultAlpha_Proxy')) === null
if (newProxyDeployment) {
if ((await dsuERC20.balanceOf(deployer)).lt(SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT))
throw new Error(
`Insufficient DSU balance to prevent inflation attack. Please deposit at least ${utils.formatEther(
SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT,
)} DSU to your deployer account.`,
)
}

await deploy('PerennialVaultAlpha_Proxy', {
contract: 'TransparentUpgradeableProxy',
args: [vaultImpl.address, proxyAdminAddress, '0x'],
Expand All @@ -42,14 +56,27 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
})

// Initialize
const vault = new BalancedVault__factory(deployerSigner).attach((await get('PerennialVaultAlpha_Proxy')).address)
const vault = new SingleBalancedVault__factory(deployerSigner).attach(
(await get('PerennialVaultAlpha_Proxy')).address,
)
if ((await vault.name()) === VAULT_TOKEN_NAME) {
console.log('PerennialVaultAlpha already initialized.')
} else {
process.stdout.write('initializing PerennialVaultAlpha...')
await (await vault.initialize(VAULT_TOKEN_NAME, VAULT_TOKEN_SYMBOL)).wait(2)
process.stdout.write('complete.\n')
}

if (newProxyDeployment) {
process.stdout.write('checking if inflation attack has occurred...')
const currentDSUBalance = dsuERC20.balanceOf(vault.address)
if ((await currentDSUBalance).gt(0)) throw new Error('WARNING: Share inflation attack detected!')
process.stdout.write('complete.\n')

process.stdout.write('depositing DSU to prevent inflation attack...')
await vault.deposit(SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT, deployer)
process.stdout.write('checking if inflation attack has occurred...')
}
}

export default func
Expand Down
33 changes: 30 additions & 3 deletions packages/perennial-vaults/deploy/02_deploy_vault_bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import 'hardhat-deploy'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { DeployFunction } from 'hardhat-deploy/types'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { BalancedVault__factory } from '../types/generated'
import { SingleBalancedVault__factory, IERC20__factory } from '../types/generated'
import { utils } from 'ethers'

const VAULT_TOKEN_NAME = 'Perennial Vault Bravo'
const VAULT_TOKEN_SYMBOL = 'PVB'
const SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT = utils.parseEther('1')

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getNamedAccounts, ethers } = hre
const { deploy, get } = deployments
const { deploy, get, getOrNull } = deployments
const { deployer } = await getNamedAccounts()
const deployerSigner: SignerWithAddress = await ethers.getSigner(deployer)

Expand All @@ -22,6 +24,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const short = (await get('Product_ShortArbitrum')).address
const targetLeverage = ethers.utils.parseEther('1')
const maxCollateral = ethers.utils.parseEther('500000')
const dsuERC20 = IERC20__factory.connect(dsu, deployerSigner)

const vaultImpl = await deploy('PerennialVaultBravo_Impl', {
contract: 'BalancedVault',
Expand All @@ -32,6 +35,17 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
autoMine: true,
})

// If no deployment detected
const newProxyDeployment = (await getOrNull('PerennialVaultBravo_Proxy')) === null
if (newProxyDeployment) {
if ((await dsuERC20.balanceOf(deployer)).lt(SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT))
throw new Error(
`Insufficient DSU balance to prevent inflation attack. Please deposit at least ${utils.formatEther(
SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT,
)} DSU to your deployer account.`,
)
}

await deploy('PerennialVaultBravo_Proxy', {
contract: 'TransparentUpgradeableProxy',
args: [vaultImpl.address, proxyAdminAddress, '0x'],
Expand All @@ -42,14 +56,27 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
})

// Initialize
const vault = new BalancedVault__factory(deployerSigner).attach((await get('PerennialVaultBravo_Proxy')).address)
const vault = new SingleBalancedVault__factory(deployerSigner).attach(
(await get('PerennialVaultBravo_Proxy')).address,
)
if ((await vault.name()) === VAULT_TOKEN_NAME) {
console.log('PerennialVaultBravo already initialized.')
} else {
process.stdout.write('initializing PerennialVaultBravo...')
await (await vault.initialize(VAULT_TOKEN_NAME, VAULT_TOKEN_SYMBOL)).wait(2)
process.stdout.write('complete.\n')
}

if (newProxyDeployment) {
process.stdout.write('checking if inflation attack has occurred...')
const currentDSUBalance = dsuERC20.balanceOf(vault.address)
if ((await currentDSUBalance).gt(0)) throw new Error('WARNING: Share inflation attack detected!')
process.stdout.write('complete.\n')

process.stdout.write('depositing DSU to prevent inflation attack...')
await vault.deposit(SHARE_INFLATION_PREVENTION_DEPOSIT_AMOUNT, deployer)
process.stdout.write('checking if inflation attack has occurred...')
}
}

export default func
Expand Down
Loading