aave-protocol-v2/test/__setup.spec.ts

283 lines
9.5 KiB
TypeScript
Raw Normal View History

2020-11-05 11:18:20 +00:00
import rawBRE from 'hardhat';
2020-07-13 08:54:08 +00:00
import {MockContract} from 'ethereum-waffle';
import {
insertContractAddressInDb,
getEthersSigners,
registerContractInJsonDb,
} from '../helpers/contracts-helpers';
import {
deployLendingPoolAddressesProvider,
deployMintableERC20,
deployLendingPoolAddressesProviderRegistry,
deployLendingPoolConfigurator,
deployLendingPool,
deployPriceOracle,
deployChainlinkProxyPriceProvider,
deployLendingPoolCollateralManager,
deployMockFlashLoanReceiver,
deployWalletBalancerProvider,
2020-06-08 15:36:40 +00:00
deployAaveProtocolTestHelpers,
2020-09-24 15:48:29 +00:00
deployLendingRateOracle,
deployStableAndVariableTokensHelper,
deployATokensAndRatesHelper,
2020-10-29 11:44:21 +00:00
deployWETHGateway,
deployWETHMocked,
} from '../helpers/contracts-deployments';
2020-08-20 15:38:57 +00:00
import {Signer} from 'ethers';
2020-08-21 15:19:25 +00:00
import {TokenContractId, eContractid, tEthereumAddress, AavePools} from '../helpers/types';
2020-08-25 12:15:35 +00:00
import {MintableErc20 as MintableERC20} from '../types/MintableErc20';
2020-11-05 11:35:50 +00:00
import {getEmergencyAdmin, getReservesConfigByPool} from '../helpers/configuration';
2020-07-13 08:54:08 +00:00
import {initializeMakeSuite} from './helpers/make-suite';
2020-08-19 14:10:08 +00:00
2020-08-20 15:38:57 +00:00
import {
setInitialAssetPricesInOracle,
deployAllMockAggregators,
setInitialMarketRatesInRatesOracleByHelper,
2020-08-20 15:38:57 +00:00
} from '../helpers/oracles-helpers';
2020-11-05 15:15:52 +00:00
import {DRE, waitForTx} from '../helpers/misc-utils';
import {
initReservesByHelper,
enableReservesToBorrowByHelper,
enableReservesAsCollateralByHelper,
} from '../helpers/init-helpers';
2020-08-25 12:15:35 +00:00
import {AaveConfig} from '../config/aave';
2020-09-24 15:48:29 +00:00
import {ZERO_ADDRESS} from '../helpers/constants';
import {
getLendingPool,
getLendingPoolConfiguratorProxy,
getPairsTokenAggregator,
} from '../helpers/contracts-getters';
2020-10-29 11:44:21 +00:00
import {Weth9Mocked} from '../types/Weth9Mocked';
2020-08-25 12:15:35 +00:00
const MOCK_USD_PRICE_IN_WEI = AaveConfig.ProtocolGlobalParams.MockUsdPriceInWei;
const ALL_ASSETS_INITIAL_PRICES = AaveConfig.Mocks.AllAssetsInitialPrices;
const USD_ADDRESS = AaveConfig.ProtocolGlobalParams.UsdAddress;
const MOCK_CHAINLINK_AGGREGATORS_PRICES = AaveConfig.Mocks.ChainlinkAggregatorPrices;
const LENDING_RATE_ORACLE_RATES_COMMON = AaveConfig.LendingRateOracleRatesCommon;
const deployAllMockTokens = async (deployer: Signer) => {
2020-10-29 11:44:21 +00:00
const tokens: {[symbol: string]: MockContract | MintableERC20 | Weth9Mocked} = {};
2020-06-26 19:28:18 +00:00
const protoConfigData = getReservesConfigByPool(AavePools.proto);
const secondaryConfigData = getReservesConfigByPool(AavePools.secondary);
for (const tokenSymbol of Object.keys(TokenContractId)) {
2020-10-29 11:44:21 +00:00
if (tokenSymbol === 'WETH') {
tokens[tokenSymbol] = await deployWETHMocked();
await registerContractInJsonDb(tokenSymbol.toUpperCase(), tokens[tokenSymbol]);
continue;
}
2020-08-19 12:23:41 +00:00
let decimals = 18;
2020-07-03 09:15:30 +00:00
2020-08-19 12:23:41 +00:00
let configData = (<any>protoConfigData)[tokenSymbol];
2020-07-03 09:15:30 +00:00
2020-08-19 12:23:41 +00:00
if (!configData) {
configData = (<any>secondaryConfigData)[tokenSymbol];
}
2020-06-26 19:28:18 +00:00
2020-08-19 12:23:41 +00:00
if (!configData) {
decimals = 18;
}
2020-07-03 09:15:30 +00:00
tokens[tokenSymbol] = await deployMintableERC20([
2020-08-19 12:23:41 +00:00
tokenSymbol,
tokenSymbol,
configData ? configData.reserveDecimals : 18,
]);
await registerContractInJsonDb(tokenSymbol.toUpperCase(), tokens[tokenSymbol]);
}
return tokens;
};
const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
2020-07-13 08:54:08 +00:00
console.time('setup');
2020-09-16 12:09:42 +00:00
const aaveAdmin = await deployer.getAddress();
2020-06-08 15:36:40 +00:00
const mockTokens = await deployAllMockTokens(deployer);
2020-11-04 10:47:06 +00:00
const mockTokenAddress = Object.keys(mockTokens).reduce<{[key: string]: string}>((acc, key) => {
acc[key] = mockTokens[key].address;
return acc;
}, {});
const addressesProvider = await deployLendingPoolAddressesProvider();
2020-11-05 11:35:50 +00:00
await waitForTx(await addressesProvider.setPoolAdmin(aaveAdmin));
2020-11-05 15:15:52 +00:00
//setting users[1] as emergency admin, which is in position 2 in the DRE addresses list
2020-11-05 11:35:50 +00:00
const addressList = await Promise.all(
2020-11-05 15:15:52 +00:00
(await DRE.ethers.getSigners()).map((signer) => signer.getAddress())
2020-11-05 11:35:50 +00:00
);
await waitForTx(await addressesProvider.setEmergencyAdmin(addressList[2]));
const addressesProviderRegistry = await deployLendingPoolAddressesProviderRegistry();
await waitForTx(
await addressesProviderRegistry.registerAddressesProvider(addressesProvider.address, 1)
);
const lendingPoolImpl = await deployLendingPool();
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
await waitForTx(await addressesProvider.setLendingPoolImpl(lendingPoolImpl.address));
2020-06-20 23:40:03 +00:00
const lendingPoolAddress = await addressesProvider.getLendingPool();
const lendingPoolProxy = await getLendingPool(lendingPoolAddress);
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
await insertContractAddressInDb(eContractid.LendingPool, lendingPoolProxy.address);
2020-07-23 15:18:06 +00:00
const lendingPoolConfiguratorImpl = await deployLendingPoolConfigurator();
await waitForTx(
await addressesProvider.setLendingPoolConfiguratorImpl(lendingPoolConfiguratorImpl.address)
);
const lendingPoolConfiguratorProxy = await getLendingPoolConfiguratorProxy(
await addressesProvider.getLendingPoolConfigurator()
);
await insertContractAddressInDb(
eContractid.LendingPoolConfigurator,
lendingPoolConfiguratorProxy.address
);
// Deploy deployment helpers
await deployStableAndVariableTokensHelper([lendingPoolProxy.address, addressesProvider.address]);
await deployATokensAndRatesHelper([
lendingPoolProxy.address,
addressesProvider.address,
lendingPoolConfiguratorProxy.address,
]);
const fallbackOracle = await deployPriceOracle();
await waitForTx(await fallbackOracle.setEthUsdPrice(MOCK_USD_PRICE_IN_WEI));
await setInitialAssetPricesInOracle(
ALL_ASSETS_INITIAL_PRICES,
{
WETH: mockTokens.WETH.address,
DAI: mockTokens.DAI.address,
TUSD: mockTokens.TUSD.address,
USDC: mockTokens.USDC.address,
USDT: mockTokens.USDT.address,
SUSD: mockTokens.SUSD.address,
AAVE: mockTokens.AAVE.address,
BAT: mockTokens.BAT.address,
REP: mockTokens.REP.address,
MKR: mockTokens.MKR.address,
LINK: mockTokens.LINK.address,
KNC: mockTokens.KNC.address,
WBTC: mockTokens.WBTC.address,
MANA: mockTokens.MANA.address,
ZRX: mockTokens.ZRX.address,
SNX: mockTokens.SNX.address,
BUSD: mockTokens.BUSD.address,
2020-11-04 10:47:06 +00:00
YFI: mockTokens.BUSD.address,
REN: mockTokens.REN.address,
UNI: mockTokens.UNI.address,
2020-11-04 20:03:58 +00:00
ENJ: mockTokens.ENJ.address,
USD: USD_ADDRESS,
UNI_DAI_ETH: mockTokens.UNI_DAI_ETH.address,
UNI_USDC_ETH: mockTokens.UNI_USDC_ETH.address,
UNI_SETH_ETH: mockTokens.UNI_SETH_ETH.address,
UNI_LEND_ETH: mockTokens.UNI_LEND_ETH.address,
UNI_MKR_ETH: mockTokens.UNI_MKR_ETH.address,
UNI_LINK_ETH: mockTokens.UNI_LINK_ETH.address,
},
fallbackOracle
);
2020-07-13 08:54:08 +00:00
const mockAggregators = await deployAllMockAggregators(MOCK_CHAINLINK_AGGREGATORS_PRICES);
const allTokenAddresses = Object.entries(mockTokens).reduce(
2020-07-13 08:54:08 +00:00
(accum: {[tokenSymbol: string]: tEthereumAddress}, [tokenSymbol, tokenContract]) => ({
...accum,
[tokenSymbol]: tokenContract.address,
}),
{}
);
const allAggregatorsAddresses = Object.entries(mockAggregators).reduce(
2020-07-13 08:54:08 +00:00
(accum: {[tokenSymbol: string]: tEthereumAddress}, [tokenSymbol, aggregator]) => ({
...accum,
[tokenSymbol]: aggregator.address,
}),
{}
);
2020-07-13 08:54:08 +00:00
const [tokens, aggregators] = getPairsTokenAggregator(allTokenAddresses, allAggregatorsAddresses);
const chainlinkProxyPriceProvider = await deployChainlinkProxyPriceProvider([
tokens,
aggregators,
fallbackOracle.address,
mockTokens.WETH.address,
]);
2020-07-13 08:54:08 +00:00
await waitForTx(await addressesProvider.setPriceOracle(fallbackOracle.address));
const lendingRateOracle = await deployLendingRateOracle();
2020-07-13 08:54:08 +00:00
await waitForTx(await addressesProvider.setLendingRateOracle(lendingRateOracle.address));
const {USD, ...tokensAddressesWithoutUsd} = allTokenAddresses;
const allReservesAddresses = {
...tokensAddressesWithoutUsd,
};
await setInitialMarketRatesInRatesOracleByHelper(
LENDING_RATE_ORACLE_RATES_COMMON,
allReservesAddresses,
lendingRateOracle,
aaveAdmin
);
const {
UNI_DAI_ETH,
UNI_USDC_ETH,
UNI_SETH_ETH,
UNI_LINK_ETH,
UNI_MKR_ETH,
UNI_LEND_ETH,
...protoPoolReservesAddresses
} = <{[symbol: string]: tEthereumAddress}>allReservesAddresses;
const reservesParams = getReservesConfigByPool(AavePools.proto);
2020-10-12 18:07:17 +00:00
const testHelpers = await deployAaveProtocolTestHelpers(addressesProvider.address);
await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address);
const admin = await deployer.getAddress();
2020-10-12 18:07:17 +00:00
2020-07-13 08:54:08 +00:00
console.log('Initialize configuration');
await initReservesByHelper(reservesParams, protoPoolReservesAddresses, admin, ZERO_ADDRESS);
await enableReservesToBorrowByHelper(
reservesParams,
protoPoolReservesAddresses,
2020-10-12 18:07:17 +00:00
testHelpers,
admin
);
await enableReservesAsCollateralByHelper(
reservesParams,
protoPoolReservesAddresses,
2020-10-12 18:07:17 +00:00
testHelpers,
admin
);
const collateralManager = await deployLendingPoolCollateralManager();
await waitForTx(
await addressesProvider.setLendingPoolCollateralManager(collateralManager.address)
);
2020-07-13 08:54:08 +00:00
const mockFlashLoanReceiver = await deployMockFlashLoanReceiver(addressesProvider.address);
await insertContractAddressInDb(eContractid.MockFlashLoanReceiver, mockFlashLoanReceiver.address);
await deployWalletBalancerProvider(addressesProvider.address);
await deployWETHGateway([mockTokens.WETH.address, lendingPoolAddress]);
2020-10-29 11:44:21 +00:00
2020-07-13 08:54:08 +00:00
console.timeEnd('setup');
};
before(async () => {
await rawBRE.run('set-DRE');
const [deployer, secondaryWallet] = await getEthersSigners();
2020-07-13 08:54:08 +00:00
console.log('-> Deploying test environment...');
await buildTestEnv(deployer, secondaryWallet);
await initializeMakeSuite();
2020-07-13 08:54:08 +00:00
console.log('\n***************');
console.log('Setup and snapshot finished');
console.log('***************\n');
});