From dad66ef2d48eb302cf109a3729185fc3e053112f Mon Sep 17 00:00:00 2001 From: Zer0dot <zer0dot.dev@gmail.com> Date: Fri, 5 Feb 2021 11:19:40 -0500 Subject: [PATCH] Adapted deployments and tests --- helpers/contracts-deployments.ts | 180 +++++++++++++++++------- helpers/init-helpers.ts | 226 +++++++++++++++++++++--------- tasks/helpers/deploy-new-asset.ts | 2 +- 3 files changed, 288 insertions(+), 120 deletions(-) diff --git a/helpers/contracts-deployments.ts b/helpers/contracts-deployments.ts index fb284b35..a2b9b826 100644 --- a/helpers/contracts-deployments.ts +++ b/helpers/contracts-deployments.ts @@ -11,7 +11,6 @@ import { PoolConfiguration, eEthereumNetwork, } from './types'; - import { MintableERC20 } from '../types/MintableERC20'; import { MockContract } from 'ethereum-waffle'; import { getReservesConfigByPool } from './configuration'; @@ -69,6 +68,7 @@ const readArtifact = async (id: string) => { } return (DRE as HardhatRuntimeEnvironment).artifacts.readArtifact(id); }; + export const deployLendingPoolAddressesProvider = async (marketId: string, verify?: boolean) => withSaveAndVerify( await new LendingPoolAddressesProviderFactory(await getFirstSigner()).deploy(marketId), @@ -302,78 +302,111 @@ export const deployDefaultReserveInterestRateStrategy = async ( export const deployStableDebtToken = async ( args: [tEthereumAddress, tEthereumAddress, string, string, tEthereumAddress], verify: boolean -) => - withSaveAndVerify( - await new StableDebtTokenFactory(await getFirstSigner()).deploy(...args), +) => { + const instance = await withSaveAndVerify( + await new StableDebtTokenFactory(await getFirstSigner()).deploy(), eContractid.StableDebtToken, - args, + [], verify ); + await instance.initialize( + args[0], + args[1], + args[2], + "18", + args[3], + args[4] + ); + + return instance; +} + + export const deployVariableDebtToken = async ( args: [tEthereumAddress, tEthereumAddress, string, string, tEthereumAddress], verify: boolean -) => - withSaveAndVerify( - await new VariableDebtTokenFactory(await getFirstSigner()).deploy(...args), +) => { + const instance = await withSaveAndVerify( + await new VariableDebtTokenFactory(await getFirstSigner()).deploy(), eContractid.VariableDebtToken, - args, + [], verify ); + await instance.initialize( + args[0], + args[1], + args[2], + "18", + args[3], + args[4] + ); + + return instance; +} + + export const deployGenericAToken = async ( - [poolAddress, underlyingAssetAddress, treasuryAddress, name, symbol, incentivesController]: [ + [poolAddress, underlyingAssetAddress, treasuryAddress, incentivesController, name, symbol]: [ + tEthereumAddress, tEthereumAddress, tEthereumAddress, tEthereumAddress, string, - string, - tEthereumAddress + string ], verify: boolean ) => { - const args: [ - tEthereumAddress, - tEthereumAddress, - string, - string, - tEthereumAddress, - tEthereumAddress - ] = [poolAddress, underlyingAssetAddress, treasuryAddress, name, symbol, incentivesController]; - return withSaveAndVerify( - await new ATokenFactory(await getFirstSigner()).deploy(...args), + const instance = await withSaveAndVerify( + await new ATokenFactory(await getFirstSigner()).deploy(), eContractid.AToken, - args, + [], verify ); + + await instance.initialize( + poolAddress, + treasuryAddress, + underlyingAssetAddress, + incentivesController, + "18", + name, + symbol + ); + + return instance; }; export const deployDelegationAwareAToken = async ( - [poolAddress, underlyingAssetAddress, treasuryAddress, name, symbol, incentivesController]: [ + [pool, underlyingAssetAddress, treasuryAddress, incentivesController, name, symbol]: [ + tEthereumAddress, tEthereumAddress, tEthereumAddress, tEthereumAddress, string, - string, - tEthereumAddress + string ], verify: boolean ) => { - const args: [ - tEthereumAddress, - tEthereumAddress, - string, - string, - tEthereumAddress, - tEthereumAddress - ] = [poolAddress, underlyingAssetAddress, treasuryAddress, name, symbol, incentivesController]; - - return withSaveAndVerify( - await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(...args), + const instance = await withSaveAndVerify( + await new DelegationAwareATokenFactory(await getFirstSigner()).deploy(), eContractid.DelegationAwareAToken, - args, + [], verify ); + + await instance.initialize( + pool, + treasuryAddress, + underlyingAssetAddress, + incentivesController, + "18", + name, + symbol + ) + + return instance; }; export const deployAllMockTokens = async (verify?: boolean) => { @@ -390,6 +423,7 @@ export const deployAllMockTokens = async (verify?: boolean) => { [tokenSymbol, tokenSymbol, configData ? configData.reserveDecimals : decimals], verify ); + await registerContractInJsonDb(tokenSymbol.toUpperCase(), tokens[tokenSymbol]); } return tokens; }; @@ -449,16 +483,29 @@ export const deployWETHGateway = async ( ); export const deployMockStableDebtToken = async ( - args: [tEthereumAddress, tEthereumAddress, string, string, tEthereumAddress], + args: [tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string], verify?: boolean -) => - withSaveAndVerify( - await new MockStableDebtTokenFactory(await getFirstSigner()).deploy(...args), +) => { + const instance = await withSaveAndVerify( + await new MockStableDebtTokenFactory(await getFirstSigner()).deploy(), eContractid.MockStableDebtToken, - args, + [], verify ); + await instance.initialize( + args[0], + args[1], + args[2], + "18", + args[3], + args[4] + ); + + return instance; +} + + export const deployWETHMocked = async (verify?: boolean) => withSaveAndVerify( await new WETH9MockedFactory(await getFirstSigner()).deploy(), @@ -468,26 +515,53 @@ export const deployWETHMocked = async (verify?: boolean) => ); export const deployMockVariableDebtToken = async ( - args: [tEthereumAddress, tEthereumAddress, string, string, tEthereumAddress], + args: [tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string], verify?: boolean -) => - withSaveAndVerify( - await new MockVariableDebtTokenFactory(await getFirstSigner()).deploy(...args), +) => { + const instance = await withSaveAndVerify( + await new MockVariableDebtTokenFactory(await getFirstSigner()).deploy(), eContractid.MockVariableDebtToken, - args, + [], verify ); + await instance.initialize( + args[0], + args[1], + args[2], + "18", + args[3], + args[4] + ); + + return instance; +} + + export const deployMockAToken = async ( - args: [tEthereumAddress, tEthereumAddress, tEthereumAddress, string, string, tEthereumAddress], +args: [tEthereumAddress, tEthereumAddress, tEthereumAddress,tEthereumAddress, string, string], verify?: boolean -) => - withSaveAndVerify( - await new MockATokenFactory(await getFirstSigner()).deploy(...args), +) => { + const instance = await withSaveAndVerify( + await new MockATokenFactory(await getFirstSigner()).deploy(), eContractid.MockAToken, - args, + [], verify ); + + await instance.initialize( + args[0], + args[2], + args[1], + args[3], + "18", + args[4], + args[5], + ); + + return instance; +} + export const deploySelfdestructTransferMock = async (verify?: boolean) => withSaveAndVerify( diff --git a/helpers/init-helpers.ts b/helpers/init-helpers.ts index 55c372a7..39af12ed 100644 --- a/helpers/init-helpers.ts +++ b/helpers/init-helpers.ts @@ -12,6 +12,7 @@ import { getAToken, getATokensAndRatesHelper, getLendingPoolAddressesProvider, + getLendingPoolConfiguratorProxy, getStableAndVariableTokensHelper, } from './contracts-getters'; import { rawInsertContractAddressInDb } from './contracts-helpers'; @@ -75,11 +76,30 @@ export const initReservesByHelper = async ( let reserveInitDecimals: string[] = []; let reserveSymbols: string[] = []; + let initInputParams: { + aTokenImpl: string, + stableDebtTokenImpl: string, + variableDebtTokenImpl: string, + underlyingAssetDecimals: BigNumberish, + interestRateStrategyAddress: string, + underlyingAsset: string, + treasury: string, + incentivesController: string, + underlyingAssetName: string, + aTokenName: string, + aTokenSymbol: string, + variableDebtTokenName: string, + variableDebtTokenSymbol: string, + stableDebtTokenName: string, + stableDebtTokenSymbol: string, + }[] = []; + console.log( `- Token deployments in ${reservesChunks.length * 2} txs instead of ${ Object.entries(reservesParams).length * 4 } txs` ); + for (let reservesChunk of reservesChunks) { // Prepare data const tokens: string[] = []; @@ -94,6 +114,18 @@ export const initReservesByHelper = async ( ][] = []; const reservesDecimals: string[] = []; + const inputParams: { + asset: string, + rates: [ + BigNumberish, + BigNumberish, + BigNumberish, + BigNumberish, + BigNumberish, + BigNumberish + ] + }[] = []; + for (let [assetSymbol, { reserveDecimals }] of reservesChunk) { const assetAddressIndex = Object.keys(tokenAddresses).findIndex( (value) => value === assetSymbol @@ -128,11 +160,23 @@ export const initReservesByHelper = async ( stableRateSlope2, ]); reservesDecimals.push(reserveDecimals); + + inputParams.push({ + asset: tokenAddress, + rates: [ + optimalUtilizationRate, + baseVariableBorrowRate, + variableRateSlope1, + variableRateSlope2, + stableRateSlope1, + stableRateSlope2 + ] + }); } // Deploy stable and variable deployers and save implementations const tx1 = await waitForTx( - await stableAndVariableDeployer.initDeployment(tokens, symbols, incentivesController) + await stableAndVariableDeployer.initDeployment(tokens, symbols) ); tx1.events?.forEach((event, index) => { rawInsertContractAddressInDb(`stableDebt${symbols[index]}`, event?.args?.stableToken); @@ -141,13 +185,7 @@ export const initReservesByHelper = async ( // Deploy atokens and rate strategies and save implementations const tx2 = await waitForTx( - await atokenAndRatesDeployer.initDeployment( - tokens, - symbols, - strategyRates, - treasuryAddress, - incentivesController - ) + await atokenAndRatesDeployer.initDeployment(inputParams) ); tx2.events?.forEach((event, index) => { rawInsertContractAddressInDb(`a${symbols[index]}`, event?.args?.aToken); @@ -158,7 +196,7 @@ export const initReservesByHelper = async ( console.log(' * gasUsed: debtTokens batch', tx1.gasUsed.toString()); console.log(' * gasUsed: aTokens and Strategy batch', tx2.gasUsed.toString()); gasUsage = gasUsage.add(tx1.gasUsed).add(tx2.gasUsed); - + const stableTokens: string[] = tx1.events?.map((e) => e.args?.stableToken) || []; const variableTokens: string[] = tx1.events?.map((e) => e.args?.variableToken) || []; const aTokens: string[] = tx2.events?.map((e) => e.args?.aToken) || []; @@ -194,9 +232,9 @@ export const initReservesByHelper = async ( poolAddress, tokenAddresses[symbol], treasuryAddress, + ZERO_ADDRESS, `Aave interest bearing ${symbol}`, `a${symbol}`, - ZERO_ADDRESS, ], verify ); @@ -204,9 +242,9 @@ export const initReservesByHelper = async ( [ poolAddress, tokenAddresses[symbol], + ZERO_ADDRESS, // Incentives controller `Aave stable debt bearing ${symbol}`, - `stableDebt${symbol}`, - ZERO_ADDRESS, + `stableDebt${symbol}` ], verify ); @@ -214,9 +252,9 @@ export const initReservesByHelper = async ( [ poolAddress, tokenAddresses[symbol], + ZERO_ADDRESS, // Incentives controller `Aave variable debt bearing ${symbol}`, `variableDebt${symbol}`, - ZERO_ADDRESS, ], verify ); @@ -242,24 +280,37 @@ export const initReservesByHelper = async ( reserveSymbols.push(symbol); } - // Deploy init reserves per chunks - const chunkedStableTokens = chunk(deployedStableTokens, initChunks); - const chunkedVariableTokens = chunk(deployedVariableTokens, initChunks); - const chunkedAtokens = chunk(deployedATokens, initChunks); - const chunkedRates = chunk(deployedRates, initChunks); - const chunkedDecimals = chunk(reserveInitDecimals, initChunks); - const chunkedSymbols = chunk(reserveSymbols, initChunks); + for (let i = 0; i < deployedATokens.length; i ++) { + initInputParams.push({ + aTokenImpl: deployedATokens[i], + stableDebtTokenImpl: deployedStableTokens[i], + variableDebtTokenImpl: deployedVariableTokens[i], + underlyingAssetDecimals: reserveInitDecimals[i], + interestRateStrategyAddress: deployedRates[i], + underlyingAsset: reserveTokens[i], + treasury: treasuryAddress, + incentivesController: ZERO_ADDRESS, + underlyingAssetName: reserveSymbols[i], + aTokenName: `Aave interest bearing ${reserveSymbols[i]}`, + aTokenSymbol: `a${reserveSymbols[i]}`, + variableDebtTokenName: `Aave variable debt bearing ${reserveSymbols[i]}`, + variableDebtTokenSymbol: `variableDebt${reserveSymbols[i]}`, + stableDebtTokenName: `Aave stable debt bearing ${reserveSymbols[i]}`, + stableDebtTokenSymbol: `stableDebt${reserveSymbols[i]}` + }); + } - console.log(`- Reserves initialization in ${chunkedStableTokens.length} txs`); - for (let chunkIndex = 0; chunkIndex < chunkedDecimals.length; chunkIndex++) { + // Deploy init reserves per chunks + const chunkedSymbols = chunk(reserveSymbols, initChunks); + const chunkedInitInputParams = chunk(initInputParams, initChunks); + + const configurator = await getLendingPoolConfiguratorProxy(); + await waitForTx(await addressProvider.setPoolAdmin(admin)); + + console.log(`- Reserves initialization in ${chunkedInitInputParams.length} txs`); + for (let chunkIndex = 0; chunkIndex < chunkedInitInputParams.length; chunkIndex++) { const tx3 = await waitForTx( - await atokenAndRatesDeployer.initReserve( - chunkedStableTokens[chunkIndex], - chunkedVariableTokens[chunkIndex], - chunkedAtokens[chunkIndex], - chunkedRates[chunkIndex], - chunkedDecimals[chunkIndex] - ) + await configurator.batchInitReserve(chunkedInitInputParams[chunkIndex]) ); console.log(` - Reserve ready for: ${chunkedSymbols[chunkIndex].join(', ')}`); @@ -267,6 +318,7 @@ export const initReservesByHelper = async ( gasUsage = gasUsage.add(tx3.gasUsed); } + // Set deployer back as admin await waitForTx(await addressProvider.setPoolAdmin(admin)); return gasUsage; @@ -315,6 +367,15 @@ export const configureReservesByHelper = async ( const reserveFactors: string[] = []; const stableRatesEnabled: boolean[] = []; + const inputParams : { + asset: string; + baseLTV: BigNumberish; + liquidationThreshold: BigNumberish; + liquidationBonus: BigNumberish; + reserveFactor: BigNumberish; + stableBorrowingEnabled: boolean; + }[] = []; + for (const [ assetSymbol, { @@ -342,6 +403,16 @@ export const configureReservesByHelper = async ( continue; } // Push data + + inputParams.push({ + asset: tokenAddress, + baseLTV: baseLTVAsCollateral, + liquidationThreshold: liquidationThreshold, + liquidationBonus: liquidationBonus, + reserveFactor: reserveFactor, + stableBorrowingEnabled: stableBorrowRateEnabled + }); + tokens.push(tokenAddress); symbols.push(assetSymbol); baseLTVA.push(baseLTVAsCollateral); @@ -356,24 +427,14 @@ export const configureReservesByHelper = async ( // Deploy init per chunks const enableChunks = 20; - const chunkedTokens = chunk(tokens, enableChunks); const chunkedSymbols = chunk(symbols, enableChunks); - const chunkedBase = chunk(baseLTVA, enableChunks); - const chunkedliquidationThresholds = chunk(liquidationThresholds, enableChunks); - const chunkedliquidationBonuses = chunk(liquidationBonuses, enableChunks); - const chunkedReserveFactors = chunk(reserveFactors, enableChunks); - const chunkedStableRatesEnabled = chunk(stableRatesEnabled, enableChunks); + const chunkedInputParams = chunk(inputParams, enableChunks); - console.log(`- Configure reserves in ${chunkedTokens.length} txs`); - for (let chunkIndex = 0; chunkIndex < chunkedTokens.length; chunkIndex++) { + console.log(`- Configure reserves in ${chunkedInputParams.length} txs`); + for (let chunkIndex = 0; chunkIndex < chunkedInputParams.length; chunkIndex++) { await waitForTx( await atokenAndRatesDeployer.configureReserves( - chunkedTokens[chunkIndex], - chunkedBase[chunkIndex], - chunkedliquidationThresholds[chunkIndex], - chunkedliquidationBonuses[chunkIndex], - chunkedReserveFactors[chunkIndex], - chunkedStableRatesEnabled[chunkIndex], + chunkedInputParams[chunkIndex], { gasLimit: 12000000 } ) ); @@ -425,8 +486,28 @@ export const initTokenReservesByHelper = async ( let deployedVariableTokens: string[] = []; let deployedATokens: string[] = []; let deployedRates: string[] = []; + let reserveTokens: string[] = []; let reserveInitDecimals: string[] = []; let reserveSymbols: string[] = []; + + let initInputParams: { + aTokenImpl: string, + stableDebtTokenImpl: string, + variableDebtTokenImpl: string, + underlyingAssetDecimals: BigNumberish, + interestRateStrategyAddress: string, + underlyingAsset: string, + treasury: string, + incentivesController: string, + underlyingAssetName: string, + aTokenName: string, + aTokenSymbol: string, + variableDebtTokenName: string, + variableDebtTokenSymbol: string, + stableDebtTokenName: string, + stableDebtTokenSymbol: string, + }[] = []; + const network = process.env.MAINNET_FORK === 'true' ? eEthereumNetwork.main @@ -454,9 +535,9 @@ export const initTokenReservesByHelper = async ( [ poolAddress, tokenAddresses[symbol], + ZERO_ADDRESS, // Incentives controller `Aave stable debt bearing ${symbol}`, - `stableDebt${symbol}`, - ZERO_ADDRESS, + `stableDebt${symbol}` ], verify ); @@ -467,9 +548,9 @@ export const initTokenReservesByHelper = async ( [ poolAddress, tokenAddresses[symbol], + ZERO_ADDRESS, // Incentives Controller `Aave variable debt bearing ${symbol}`, - `variableDebt${symbol}`, - ZERO_ADDRESS, + `variableDebt${symbol}` ], verify ); @@ -485,9 +566,9 @@ export const initTokenReservesByHelper = async ( poolAddress, tokenAddresses[symbol], treasuryAddress, - `Aave interest bearing ${symbol}`, - `a${symbol}`, ZERO_ADDRESS, + `Aave interest bearing ${symbol}`, + `a${symbol}` ], verify ); @@ -531,34 +612,47 @@ export const initTokenReservesByHelper = async ( deployedStableTokens.push(stableTokenImpl); deployedVariableTokens.push(variableTokenImpl); deployedATokens.push(aTokenImplementation); + reserveTokens.push(); deployedRates.push(strategyImpl); reserveInitDecimals.push(decimals.toString()); reserveSymbols.push(symbol); } - // Deploy init reserves per chunks - const chunkedStableTokens = chunk(deployedStableTokens, initChunks); - const chunkedVariableTokens = chunk(deployedVariableTokens, initChunks); - const chunkedAtokens = chunk(deployedATokens, initChunks); - const chunkedRates = chunk(deployedRates, initChunks); - const chunkedDecimals = chunk(reserveInitDecimals, initChunks); - const chunkedSymbols = chunk(reserveSymbols, initChunks); + for (let i = 0; i < deployedATokens.length; i ++) { + initInputParams.push({ + aTokenImpl: deployedATokens[i], + stableDebtTokenImpl: deployedStableTokens[i], + variableDebtTokenImpl: deployedVariableTokens[i], + underlyingAssetDecimals: reserveInitDecimals[i], + interestRateStrategyAddress: deployedRates[i], + underlyingAsset: tokenAddresses[reserveSymbols[i]], + treasury: treasuryAddress, + incentivesController: ZERO_ADDRESS, + underlyingAssetName: reserveSymbols[i], + aTokenName: `Aave interest bearing ${reserveSymbols[i]}`, + aTokenSymbol: `a${reserveSymbols[i]}`, + variableDebtTokenName: `Aave variable debt bearing ${reserveSymbols[i]}`, + variableDebtTokenSymbol: `variableDebt${reserveSymbols[i]}`, + stableDebtTokenName: `Aave stable debt bearing ${reserveSymbols[i]}`, + stableDebtTokenSymbol: `stableDebt${reserveSymbols[i]}` + }); + } - console.log(`- Reserves initialization in ${chunkedStableTokens.length} txs`); - for (let chunkIndex = 0; chunkIndex < chunkedDecimals.length; chunkIndex++) { + // Deploy init reserves per chunks + const chunkedSymbols = chunk(reserveSymbols, initChunks); + const chunkedInitInputParams = chunk(initInputParams, initChunks); + + const configurator = await getLendingPoolConfiguratorProxy(); + await waitForTx(await addressProvider.setPoolAdmin(admin)); + + console.log(`- Reserves initialization in ${chunkedInitInputParams.length} txs`); + for (let chunkIndex = 0; chunkIndex < chunkedInitInputParams.length; chunkIndex++) { const tx3 = await waitForTx( - await atokenAndRatesDeployer.initReserve( - chunkedStableTokens[chunkIndex], - chunkedVariableTokens[chunkIndex], - chunkedAtokens[chunkIndex], - chunkedRates[chunkIndex], - chunkedDecimals[chunkIndex] - ) + await configurator.batchInitReserve(chunkedInitInputParams[chunkIndex]) ); console.log(` - Reserve ready for: ${chunkedSymbols[chunkIndex].join(', ')}`); console.log(' * gasUsed', tx3.gasUsed.toString()); - gasUsage = gasUsage.add(tx3.gasUsed); } // Set deployer back as admin diff --git a/tasks/helpers/deploy-new-asset.ts b/tasks/helpers/deploy-new-asset.ts index 912ebff1..5df35fdc 100644 --- a/tasks/helpers/deploy-new-asset.ts +++ b/tasks/helpers/deploy-new-asset.ts @@ -53,9 +53,9 @@ WRONG RESERVE ASSET SETUP: poolAddress, reserveAssetAddress, treasuryAddress, + ZERO_ADDRESS, `Aave interest bearing ${symbol}`, `a${symbol}`, - ZERO_ADDRESS, ], verify );