Skip to content

Commit

Permalink
Merge pull request #294 from eonian-core/ssolovyev/aave-strategy-for-bsc
Browse files Browse the repository at this point in the history
Add AAVE to deployment plan on BSC
  • Loading branch information
solovev authored Feb 15, 2025
2 parents 45c7915 + 4ef991b commit add48bb
Show file tree
Hide file tree
Showing 10 changed files with 672 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class AaveV3LikePool extends BaseAddresses {
[Chain.ETH]: {
ANY_ENVIRONMENT: '0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2',
},
[Chain.BSC]: {
ANY_ENVIRONMENT: '0x6807dc923806fE8Fd134338EABCA509979a7e0cB',
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export async function attachToVault(

console.log(`Strategy attached successfully with ratio: ${debtRatio}!`)
if (debtRatio === 0n) {
console.warn('Debt ratio of the strategy is 0, so it\'s not active. You should adjust it manually!')
console.log('Debt ratio of the strategy is 0, so it\'s not active. You should adjust it manually!')
}
}
69 changes: 41 additions & 28 deletions packages/contracts/hardhat/tasks/deploy/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
import { task } from 'hardhat/config';
import type { HardhatRuntimeEnvironment } from 'hardhat/types';
import { execute } from '@eonian/upgradeable';
import deployHealthCheck from '../../deployment/deployHealthCheck';
import deployVault from '../../deployment/deployVault';
import deployVFT from '../../deployment/deployVFT';
import { getStrategyDeploymentPlan, Strategy, getStrategyDeployer } from './strategy-deployment-plan';
import _ from 'lodash';
import { task } from 'hardhat/config'
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
import { execute } from '@eonian/upgradeable'
import _ from 'lodash'
import deployHealthCheck from '../../deployment/deployHealthCheck'
import deployVault from '../../deployment/deployVault'
import deployVFT from '../../deployment/deployVFT'
import type { Strategy, StrategyDeploymentPlan } from './strategy-deployment-plan'
import { getStrategyDeployer, getStrategyDeploymentPlan } from './strategy-deployment-plan'

export const deployTask = task('deploy', 'Deploy (or upgade) production contracts', async (args, hre) => {
return await deployTaskAction(hre);
});
const strategyDeploymentPlan = getPlanFromArguments(args) ?? getStrategyDeploymentPlan(hre)
return await deployTaskAction(hre, strategyDeploymentPlan)
})

export async function deployTaskAction(
hre: HardhatRuntimeEnvironment,
strategyDeploymentPlan = getStrategyDeploymentPlan(hre)
strategyDeploymentPlan: StrategyDeploymentPlan,
) {
console.log(`Strategy deployment plan: ${JSON.stringify(strategyDeploymentPlan)}`);
console.log(`Strategy deployment plan: ${JSON.stringify(strategyDeploymentPlan)}`)

const tokens = new Set(Object.values(strategyDeploymentPlan).flat());
const tokens = new Set(Object.values(strategyDeploymentPlan).flat())
if (tokens.size <= 0) {
console.log('No contracts to deploy, aborted');
return;
console.log('No contracts to deploy, aborted')
return
}

console.log('\nDeploying common contracts for...\n');
console.log('\nDeploying common contracts for...\n')

await execute(deployHealthCheck, hre);
await execute(deployHealthCheck, hre)

for (const token of tokens) {
console.log(`\nDeploying vault-related contracts for ${token}...\n`);
await execute(deployVault, token, hre);
await execute(deployVFT, token, hre);
console.log(`\nDeploying vault-related contracts for ${token}...\n`)
await execute(deployVault, token, hre)
await execute(deployVFT, token, hre)
}

const strategies = Object.keys(strategyDeploymentPlan) as Strategy[];
const strategies = Object.keys(strategyDeploymentPlan) as Strategy[]
for (const strategy of strategies) {
console.log(`\nStarting to deploy ${strategy} strategy...`);
console.log(`\nStarting to deploy ${strategy} strategy...`)

const strategyTokens = strategyDeploymentPlan[strategy]!;
const strategyTokens = strategyDeploymentPlan[strategy]!
for (const token of strategyTokens) {
console.log(`Deploying ${strategy} strategy for ${token} token...`);
console.log(`Deploying ${strategy} strategy for ${token} token...`)

const deployer = getStrategyDeployer(strategy, token);
await execute(deployer, hre);
const deployer = getStrategyDeployer(strategy, token)
await execute(deployer, hre)
}
}

await hre.proxyValidator.validateLastDeployments();
await hre.proxyValidator.validateLastDeployments()

console.log('\nDeployment is done!\n');
console.log('\nDeployment is done!\n')
}

/**
* Gets strategy deployment plan from the hardhat task arguments. Used in tests only.
* Production plan is declared in separate file (strategy-deployment-plan.ts).
*/
function getPlanFromArguments(args: unknown): StrategyDeploymentPlan | null {
if (typeof args === 'object' && !!args && 'plan' in args && typeof args.plan === 'string') {
return JSON.parse(args.plan) as StrategyDeploymentPlan
}
return null
}
26 changes: 15 additions & 11 deletions packages/contracts/hardhat/tasks/deploy/strategy-deployment-plan.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { Chain, DeployResult, resolveChain, TokenSymbol } from '@eonian/upgradeable';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import deployAaveSupplyStrategy from '../../deployment/deployAaveSupplyStrategy';
import deployApeLendingStrategy from '../../deployment/deployApeLendingStrategy';
import type { DeployResult } from '@eonian/upgradeable'
import { Chain, TokenSymbol, resolveChain } from '@eonian/upgradeable'
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
import deployAaveSupplyStrategy from '../../deployment/deployAaveSupplyStrategy'
import deployApeLendingStrategy from '../../deployment/deployApeLendingStrategy'

export enum Strategy {
APESWAP = 'APESWAP',
AAVE_V2 = 'AAVE_V2',
AAVE_V3 = 'AAVE_V3',
}

export type StrategyDeploymentPlan = Partial<Record<Strategy, TokenSymbol[]>>

/**
* Add here all the new strategies that are planned to be deployed in production.
*/
const strategyDeploymentPlan: Partial<Record<Chain, Partial<Record<Strategy, TokenSymbol[]>>>> = {
const strategyDeploymentPlan: Partial<Record<Chain, StrategyDeploymentPlan>> = {
[Chain.BSC]: {
[Strategy.APESWAP]: [TokenSymbol.USDC, TokenSymbol.USDT, TokenSymbol.BTCB, TokenSymbol.WETH],
[Strategy.AAVE_V3]: [TokenSymbol.USDC, TokenSymbol.USDT, TokenSymbol.BTCB, TokenSymbol.WETH],
},
};
}

/**
* Deployment functions for each strategy.
Expand All @@ -31,11 +35,11 @@ export function getStrategyDeployer(strategy: Strategy, token: TokenSymbol) {
return (hre: HardhatRuntimeEnvironment) => deployers[strategy](token, hre)
}

export function getStrategyDeploymentPlan(hre: HardhatRuntimeEnvironment) {
const chain = resolveChain(hre);
const strategies = strategyDeploymentPlan[chain];
export function getStrategyDeploymentPlan(hre: HardhatRuntimeEnvironment): StrategyDeploymentPlan {
const chain = resolveChain(hre)
const strategies = strategyDeploymentPlan[chain]
if (!strategies) {
throw new Error(`No strategies to deploy for "${chain}" chain!`);
}
throw new Error(`No strategies to deploy for "${chain}" chain!`)
}
return strategies
}
67 changes: 63 additions & 4 deletions packages/contracts/test/integration/bsc/bsc-deploy-task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import hre from 'hardhat'
import * as helpers from '@nomicfoundation/hardhat-network-helpers'
import { expect } from 'chai'
import { type DeployResult, DeployStatus, TokenSymbol } from '@eonian/upgradeable'
import type { ContractName } from 'hardhat/types'
import { deleteErrorFile } from '../../../hardhat/tasks/deploy-error-catcher'
import { clearDeployments } from '../../deploy/helpers'
import { Addresses } from '../../../hardhat/deployment'
import { getStrategyDeploymentPlan } from '../../../hardhat/tasks/deploy/strategy-deployment-plan'
import type { Strategy, StrategyDeploymentPlan } from '../../../hardhat/tasks/deploy/strategy-deployment-plan'

describe('BSC Deploy Task', () => {
clearDeployments(hre)
Expand Down Expand Up @@ -41,7 +43,13 @@ describe('BSC Deploy Task', () => {
}
}

await hre.run('deploy', { tokens: [TokenSymbol.USDC].join(), strategies: [Addresses.APESWAP].join() })
const deployTaskArgs = {
plan: JSON.stringify({
APESWAP: [TokenSymbol.USDC],
} satisfies StrategyDeploymentPlan),
}

await hre.run('deploy', deployTaskArgs)

await expect(hre.run('check-deploy-error')).to.be.rejectedWith(Error, 'sender doesn\'t have enough funds to send tx')
})
Expand All @@ -56,8 +64,14 @@ describe('BSC Deploy Task', () => {
}
}

const deployTaskArgs = {
plan: JSON.stringify({
APESWAP: [TokenSymbol.USDC],
} satisfies StrategyDeploymentPlan),
}

// First deploy. Only the first proxy should be deployed.
await hre.run('deploy', { tokens: [TokenSymbol.USDC].join(), strategies: [Addresses.APESWAP].join() })
await hre.run('deploy', deployTaskArgs)
const [firstDeployedProxy, ...restProxies] = getDeployments(TokenSymbol.USDC)
expect(firstDeployedProxy.status).to.be.equal(DeployStatus.DEPLOYED)
expect(restProxies.length).to.be.equal(0)
Expand All @@ -66,21 +80,66 @@ describe('BSC Deploy Task', () => {

// Second deploy. The first proxy should be skipped, but the rest ones are deployed.
await setDeployerBalance(100n * 10n ** 18n)
await hre.run('deploy', { tokens: [TokenSymbol.USDC].join(), strategies: [Addresses.APESWAP].join() })
await hre.run('deploy', deployTaskArgs)
const [firstProxy, ...restDeployedProxies] = getDeployments(TokenSymbol.USDC)
expect(firstProxy.status).to.be.equal(DeployStatus.NONE)
expect(restDeployedProxies.every(deployment => deployment.status === DeployStatus.DEPLOYED)).to.be.equal(true)

// No errors should be thrown.
await expect(hre.run('check-deploy-error')).not.to.be.rejectedWith(Error)
})

it('Should deploy every strategy from deployment-plan', async () => {
process.env.CI = 'true'

await setDeployerBalance(100n * 10n ** 18n)
await hre.run('deploy')

const plan = getStrategyDeploymentPlan(hre)
const strategyToContractNameLookup: Partial<Record<Strategy, ContractName>> = {
AAVE_V3: 'AaveSupplyStrategy',
APESWAP: 'ApeLendingStrategy',
}

const vaultAddresses = new Set()

const strategies = Object.keys(plan) as Strategy[]
for (const strategy of strategies) {
const contractName = strategyToContractNameLookup[strategy]!
const tokens = plan[strategy]!
for (const token of tokens) {
const deployment = getDeployment(contractName, token)
const strategyContract = await hre.ethers.getContractAt('BaseStrategy', deployment.proxyAddress)

const vaultAddress = await strategyContract.lender()
const vaultDeployment = getDeployment('Vault', token)
expect(vaultAddress, 'Different vault').to.be.eq(vaultDeployment.proxyAddress)

const vault = await hre.ethers.getContractAt('Vault', vaultAddress)
const strategyData = await vault.borrowersData(deployment.proxyAddress)

expect(strategyData.activationTimestamp, 'Strategy is not active!').to.be.greaterThan(0)

vaultAddresses.add(vaultAddress)
}
}

expect(vaultAddresses.size).to.be.eq(4)
})
})

function getDeployments(token: TokenSymbol): DeployResult[] {
const deployments = Object.values(hre.lastDeployments)
return deployments.filter(deployment => deployment.deploymentId === token || !deployment.deploymentId)
}

function getDeployment(contractName: ContractName, token: TokenSymbol): DeployResult {
const deployments = Object.values(hre.lastDeployments)
const deployment = deployments.find(deployment => deployment.deploymentId === token && deployment.contractName === contractName)
expect(deployment, `Missing deployment for ${contractName} (${token})`).not.to.be.eq(undefined)
return deployment!
}

async function setDeployerBalance(balance: bigint) {
const [deployer] = await hre.ethers.getSigners()
await helpers.setBalance(deployer.address, balance)
Expand Down
Loading

0 comments on commit add48bb

Please sign in to comment.