aave-protocol-v2/test/helpers/make-suite.ts

151 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-07-13 08:54:08 +00:00
import {evmRevert, evmSnapshot, BRE} from '../../helpers/misc-utils';
import {Signer} from 'ethers';
import {
getEthersSigners,
getLendingPool,
getLendingPoolAddressesProvider,
getAaveProtocolTestHelpers,
getAToken,
getMintableErc20,
2020-07-13 08:54:08 +00:00
getLendingPoolConfiguratorProxy,
getPriceOracle,
2020-09-23 09:21:49 +00:00
getMockSwapAdapter, getLendingPoolAddressesProviderRegistry
} from '../../helpers/contracts-helpers';
2020-07-13 08:54:08 +00:00
import {tEthereumAddress} from '../../helpers/types';
import {LendingPool} from '../../types/LendingPool';
import {AaveProtocolTestHelpers} from '../../types/AaveProtocolTestHelpers';
import {MintableErc20} from '../../types/MintableErc20';
import {AToken} from '../../types/AToken';
import {LendingPoolConfigurator} from '../../types/LendingPoolConfigurator';
2020-07-13 08:54:08 +00:00
import chai from 'chai';
// @ts-ignore
2020-07-13 08:54:08 +00:00
import bignumberChai from 'chai-bignumber';
import {almostEqual} from './almost-equal';
2020-07-13 08:54:08 +00:00
import {PriceOracle} from '../../types/PriceOracle';
import {LendingPoolAddressesProvider} from '../../types/LendingPoolAddressesProvider';
import { MockSwapAdapter } from '../../types/MockSwapAdapter';
2020-09-23 09:21:49 +00:00
import { LendingPoolAddressesProviderRegistry } from '../../types/LendingPoolAddressesProviderRegistry';
chai.use(bignumberChai());
chai.use(almostEqual());
2020-06-10 12:47:39 +00:00
export interface SignerWithAddress {
signer: Signer;
address: tEthereumAddress;
}
export interface TestEnv {
deployer: SignerWithAddress;
users: SignerWithAddress[];
pool: LendingPool;
configurator: LendingPoolConfigurator;
oracle: PriceOracle;
helpersContract: AaveProtocolTestHelpers;
weth: MintableErc20;
aEth: AToken;
dai: MintableErc20;
aDai: AToken;
usdc: MintableErc20;
2020-06-27 02:13:32 +00:00
lend: MintableErc20;
2020-07-13 08:54:08 +00:00
addressesProvider: LendingPoolAddressesProvider;
mockSwapAdapter: MockSwapAdapter;
2020-09-23 09:21:49 +00:00
registry: LendingPoolAddressesProviderRegistry;
}
2020-07-13 08:54:08 +00:00
let buidlerevmSnapshotId: string = '0x1';
const setBuidlerevmSnapshotId = (id: string) => {
2020-07-13 08:54:08 +00:00
if (BRE.network.name === 'buidlerevm') {
buidlerevmSnapshotId = id;
}
};
const testEnv: TestEnv = {
deployer: {} as SignerWithAddress,
users: [] as SignerWithAddress[],
pool: {} as LendingPool,
configurator: {} as LendingPoolConfigurator,
helpersContract: {} as AaveProtocolTestHelpers,
oracle: {} as PriceOracle,
weth: {} as MintableErc20,
aEth: {} as AToken,
dai: {} as MintableErc20,
aDai: {} as AToken,
usdc: {} as MintableErc20,
2020-06-27 02:13:32 +00:00
lend: {} as MintableErc20,
2020-07-13 08:54:08 +00:00
addressesProvider: {} as LendingPoolAddressesProvider,
2020-09-23 09:21:49 +00:00
mockSwapAdapter: {} as MockSwapAdapter,
registry: {} as LendingPoolAddressesProviderRegistry
} as TestEnv;
export async function initializeMakeSuite() {
const [_deployer, ...restSigners] = await getEthersSigners();
const deployer: SignerWithAddress = {
address: await _deployer.getAddress(),
signer: _deployer,
};
for (const signer of restSigners) {
testEnv.users.push({
signer,
address: await signer.getAddress(),
});
}
testEnv.deployer = deployer;
testEnv.pool = await getLendingPool();
2020-07-13 08:54:08 +00:00
console.log('Pool loaded');
2020-06-26 12:34:44 +00:00
testEnv.configurator = await getLendingPoolConfiguratorProxy();
2020-07-13 08:54:08 +00:00
console.log('Configurator loaded');
testEnv.oracle = await getPriceOracle();
testEnv.addressesProvider = await getLendingPoolAddressesProvider();
2020-09-23 09:21:49 +00:00
testEnv.registry = await getLendingPoolAddressesProviderRegistry();
2020-07-13 08:54:08 +00:00
testEnv.helpersContract = await getAaveProtocolTestHelpers();
2020-07-13 08:54:08 +00:00
const aDaiAddress = (await testEnv.helpersContract.getAllATokens()).find(
2020-07-13 08:54:08 +00:00
(aToken) => aToken.symbol === 'aDAI'
)?.tokenAddress;
const aEthAddress = (await testEnv.helpersContract.getAllATokens()).find(
(aToken) => aToken.symbol === 'aETH'
)?.tokenAddress;
const reservesTokens = await testEnv.helpersContract.getAllReservesTokens();
2020-06-26 12:34:44 +00:00
2020-07-13 08:54:08 +00:00
const daiAddress = reservesTokens.find((token) => token.symbol === 'DAI')?.tokenAddress;
const usdcAddress = reservesTokens.find((token) => token.symbol === 'USDC')?.tokenAddress;
const lendAddress = reservesTokens.find((token) => token.symbol === 'LEND')?.tokenAddress;
const wethAddress = reservesTokens.find((token) => token.symbol === 'WETH')?.tokenAddress;
2020-07-13 08:54:08 +00:00
if (!aDaiAddress || !aEthAddress) {
console.log(`atoken-modifiers.spec: aTokens not correctly initialized`);
process.exit(1);
}
if (!daiAddress || !usdcAddress || !lendAddress || !wethAddress) {
console.log(`atoken-modifiers.spec: USDC or DAI not correctly initialized`);
process.exit(1);
}
testEnv.aDai = await getAToken(aDaiAddress);
testEnv.aEth = await getAToken(aEthAddress);
testEnv.dai = await getMintableErc20(daiAddress);
testEnv.usdc = await getMintableErc20(usdcAddress);
2020-06-27 02:13:32 +00:00
testEnv.lend = await getMintableErc20(lendAddress);
testEnv.weth = await getMintableErc20(wethAddress);
testEnv.mockSwapAdapter = await getMockSwapAdapter()
}
export function makeSuite(name: string, tests: (testEnv: TestEnv) => void) {
describe(name, () => {
2020-06-10 12:47:39 +00:00
before(async () => {
setBuidlerevmSnapshotId(await evmSnapshot());
2020-06-10 12:47:39 +00:00
});
tests(testEnv);
2020-06-10 12:47:39 +00:00
after(async () => {
await evmRevert(buidlerevmSnapshotId);
2020-06-10 12:47:39 +00:00
});
});
}