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

251 lines
8.0 KiB
TypeScript
Raw Normal View History

2020-07-13 08:54:08 +00:00
import rawBRE from '@nomiclabs/buidler';
import {MockContract} from 'ethereum-waffle';
import {
deployLendingPoolAddressesProvider,
deployMintableErc20,
deployLendingPoolAddressesProviderRegistry,
deployLendingPoolConfigurator,
deployLendingPool,
deployPriceOracle,
getLendingPoolConfiguratorProxy,
deployChainlinkProxyPriceProvider,
deployLendingRateOracle,
deployLendingPoolLiquidationManager,
deployMockFlashLoanReceiver,
deployWalletBalancerProvider,
getLendingPool,
insertContractAddressInDb,
2020-06-08 15:36:40 +00:00
deployAaveProtocolTestHelpers,
getEthersSigners,
registerContractInJsonDb,
2020-08-20 15:38:57 +00:00
getPairsTokenAggregator,
2020-08-21 11:07:32 +00:00
initReserves,
2020-07-13 08:54:08 +00:00
} from '../helpers/contracts-helpers';
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-07-13 08:54:08 +00:00
import {MintableErc20} from '../types/MintableErc20';
import {
MOCK_USD_PRICE_IN_WEI,
ALL_ASSETS_INITIAL_PRICES,
USD_ADDRESS,
MOCK_CHAINLINK_AGGREGATORS_PRICES,
LENDING_RATE_ORACLE_RATES_COMMON,
getReservesConfigByPool,
2020-07-13 08:54:08 +00:00
} from '../helpers/constants';
import {initializeMakeSuite} from './helpers/make-suite';
2020-08-19 14:10:08 +00:00
2020-08-20 15:38:57 +00:00
import {
setInitialAssetPricesInOracle,
setInitialMarketRatesInRatesOracle,
deployAllMockAggregators,
} from '../helpers/oracles-helpers';
import {waitForTx} from '../helpers/misc-utils';
2020-08-21 11:07:32 +00:00
import {enableReservesToBorrow, enableReservesAsCollateral} from '../helpers/init-helpers';
const deployAllMockTokens = async (deployer: Signer) => {
const tokens: {[symbol: string]: MockContract | MintableErc20} = {};
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-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
2020-08-19 12:23:41 +00:00
tokens[tokenSymbol] = await deployMintableErc20([
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');
const lendingPoolManager = await deployer.getAddress();
2020-06-08 15:36:40 +00:00
const mockTokens = await deployAllMockTokens(deployer);
const addressesProvider = await deployLendingPoolAddressesProvider();
2020-07-13 08:54:08 +00:00
await waitForTx(await addressesProvider.setLendingPoolManager(lendingPoolManager));
const addressesProviderRegistry = await deployLendingPoolAddressesProviderRegistry();
await waitForTx(
2020-07-13 08:54:08 +00:00
await addressesProviderRegistry.registerAddressesProvider(addressesProvider.address, 0)
);
const lendingPoolImpl = await deployLendingPool();
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
console.log('Deployed lending pool, address:', lendingPoolImpl.address);
await waitForTx(await addressesProvider.setLendingPoolImpl(lendingPoolImpl.address));
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
console.log('Added pool to addresses provider');
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
const address = await addressesProvider.getLendingPool();
console.log('Address is ', address);
const lendingPoolProxy = await getLendingPool(address);
2020-06-20 23:40:03 +00:00
2020-07-13 08:54:08 +00:00
console.log('implementation set, address:', lendingPoolProxy.address);
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
);
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,
LEND: mockTokens.LEND.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,
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,
]);
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 setInitialMarketRatesInRatesOracle(
LENDING_RATE_ORACLE_RATES_COMMON,
allReservesAddresses,
lendingRateOracle
);
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-07-13 08:54:08 +00:00
console.log('Initialize configuration');
await initReserves(
reservesParams,
protoPoolReservesAddresses,
addressesProvider,
2020-06-20 23:40:03 +00:00
lendingPoolProxy,
lendingPoolConfiguratorProxy,
AavePools.proto
);
await enableReservesToBorrow(
reservesParams,
protoPoolReservesAddresses,
2020-06-20 23:40:03 +00:00
lendingPoolProxy,
lendingPoolConfiguratorProxy
);
await enableReservesAsCollateral(
reservesParams,
protoPoolReservesAddresses,
2020-06-20 23:40:03 +00:00
lendingPoolProxy,
lendingPoolConfiguratorProxy
);
const liquidationManager = await deployLendingPoolLiquidationManager();
await waitForTx(
2020-07-13 08:54:08 +00:00
await addressesProvider.setLendingPoolLiquidationManager(liquidationManager.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);
2020-07-13 08:54:08 +00:00
const testHelpers = await deployAaveProtocolTestHelpers(addressesProvider.address);
2020-06-08 15:36:40 +00:00
2020-07-13 08:54:08 +00:00
await insertContractAddressInDb(eContractid.AaveProtocolTestHelpers, testHelpers.address);
2020-06-08 15:36:40 +00:00
2020-07-13 08:54:08 +00:00
console.timeEnd('setup');
};
before(async () => {
2020-07-13 08:54:08 +00:00
await rawBRE.run('set-bre');
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');
});