aave-protocol-v2/helpers/contracts-getters.ts

274 lines
9.4 KiB
TypeScript

import {
AaveProtocolTestHelpersFactory,
ATokenFactory,
ATokensAndRatesHelperFactory,
DefaultReserveInterestRateStrategyFactory,
GenericLogicFactory,
LendingPoolAddressesProviderFactory,
LendingPoolAddressesProviderRegistryFactory,
LendingPoolConfiguratorFactory,
LendingPoolFactory,
LendingRateOracleFactory,
MintableErc20Factory,
MockATokenFactory,
MockFlashLoanReceiverFactory,
MockStableDebtTokenFactory,
MockVariableDebtTokenFactory,
PriceOracleFactory,
ReserveLogicFactory,
SelfdestructTransferFactory,
StableAndVariableTokensHelperFactory,
StableDebtTokenFactory,
VariableDebtTokenFactory,
Weth9Factory,
Weth9MockedFactory,
WethGatewayFactory,
} from '../types';
import {Ierc20DetailedFactory} from '../types/Ierc20DetailedFactory';
import {MockTokenMap} from './contracts-helpers';
import {DRE, getDb} from './misc-utils';
import {eContractid, PoolConfiguration, tEthereumAddress, TokenContractId} from './types';
export const getFirstSigner = async () => (await DRE.ethers.getSigners())[0];
export const getLendingPoolAddressesProvider = async (address?: tEthereumAddress) =>
await LendingPoolAddressesProviderFactory.connect(
address ||
(await getDb().get(`${eContractid.LendingPoolAddressesProvider}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
export const getLendingPoolConfiguratorProxy = async (address?: tEthereumAddress) => {
return await LendingPoolConfiguratorFactory.connect(
address ||
(await getDb().get(`${eContractid.LendingPoolConfigurator}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
};
export const getLendingPool = async (address?: tEthereumAddress) =>
await LendingPoolFactory.connect(
address ||
(await getDb().get(`${eContractid.LendingPool}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getPriceOracle = async (address?: tEthereumAddress) =>
await PriceOracleFactory.connect(
address ||
(await getDb().get(`${eContractid.PriceOracle}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getAToken = async (address?: tEthereumAddress) =>
await ATokenFactory.connect(
address || (await getDb().get(`${eContractid.AToken}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getStableDebtToken = async (address?: tEthereumAddress) =>
await StableDebtTokenFactory.connect(
address ||
(await getDb().get(`${eContractid.StableDebtToken}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getVariableDebtToken = async (address?: tEthereumAddress) =>
await VariableDebtTokenFactory.connect(
address ||
(await getDb().get(`${eContractid.VariableDebtToken}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getMintableErc20 = async (address: tEthereumAddress) =>
await MintableErc20Factory.connect(
address ||
(await getDb().get(`${eContractid.MintableERC20}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getIErc20Detailed = async (address: tEthereumAddress) =>
await Ierc20DetailedFactory.connect(
address ||
(await getDb().get(`${eContractid.IERC20Detailed}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getAaveProtocolTestHelpers = async (address?: tEthereumAddress) =>
await AaveProtocolTestHelpersFactory.connect(
address ||
(await getDb().get(`${eContractid.AaveProtocolTestHelpers}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
export const getInterestRateStrategy = async (address?: tEthereumAddress) =>
await DefaultReserveInterestRateStrategyFactory.connect(
address ||
(
await getDb()
.get(`${eContractid.DefaultReserveInterestRateStrategy}.${DRE.network.name}`)
.value()
).address,
await getFirstSigner()
);
export const getMockFlashLoanReceiver = async (address?: tEthereumAddress) =>
await MockFlashLoanReceiverFactory.connect(
address ||
(await getDb().get(`${eContractid.MockFlashLoanReceiver}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
export const getLendingRateOracle = async (address?: tEthereumAddress) =>
await LendingRateOracleFactory.connect(
address ||
(await getDb().get(`${eContractid.LendingRateOracle}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getMockedTokens = async (config: PoolConfiguration) => {
const tokenSymbols = Object.keys(config.ReservesConfig);
const db = getDb();
const tokens: MockTokenMap = await tokenSymbols.reduce<Promise<MockTokenMap>>(
async (acc, tokenSymbol) => {
const accumulator = await acc;
const address = db.get(`${tokenSymbol.toUpperCase()}.${DRE.network.name}`).value().address;
accumulator[tokenSymbol] = await getMintableErc20(address);
return Promise.resolve(acc);
},
Promise.resolve({})
);
return tokens;
};
export const getAllMockedTokens = async () => {
const db = getDb();
const tokens: MockTokenMap = await Object.keys(TokenContractId).reduce<Promise<MockTokenMap>>(
async (acc, tokenSymbol) => {
const accumulator = await acc;
const address = db.get(`${tokenSymbol.toUpperCase()}.${DRE.network.name}`).value().address;
accumulator[tokenSymbol] = await getMintableErc20(address);
return Promise.resolve(acc);
},
Promise.resolve({})
);
return tokens;
};
export const getPairsTokenAggregator = (
allAssetsAddresses: {
[tokenSymbol: string]: tEthereumAddress;
},
aggregatorsAddresses: {[tokenSymbol: string]: tEthereumAddress}
): [string[], string[]] => {
const {ETH, USD, WETH, ...assetsAddressesWithoutEth} = allAssetsAddresses;
const pairs = Object.entries(assetsAddressesWithoutEth).map(([tokenSymbol, tokenAddress]) => {
if (tokenSymbol !== 'WETH' && tokenSymbol !== 'ETH') {
const aggregatorAddressIndex = Object.keys(aggregatorsAddresses).findIndex(
(value) => value === tokenSymbol
);
const [, aggregatorAddress] = (Object.entries(aggregatorsAddresses) as [
string,
tEthereumAddress
][])[aggregatorAddressIndex];
return [tokenAddress, aggregatorAddress];
}
}) as [string, string][];
const mappedPairs = pairs.map(([asset]) => asset);
const mappedAggregators = pairs.map(([, source]) => source);
return [mappedPairs, mappedAggregators];
};
export const getLendingPoolAddressesProviderRegistry = async (address?: tEthereumAddress) =>
await LendingPoolAddressesProviderRegistryFactory.connect(
address ||
(
await getDb()
.get(`${eContractid.LendingPoolAddressesProviderRegistry}.${DRE.network.name}`)
.value()
).address,
await getFirstSigner()
);
export const getReserveLogic = async (address?: tEthereumAddress) =>
await ReserveLogicFactory.connect(
address ||
(await getDb().get(`${eContractid.ReserveLogic}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getGenericLogic = async (address?: tEthereumAddress) =>
await GenericLogicFactory.connect(
address ||
(await getDb().get(`${eContractid.GenericLogic}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getStableAndVariableTokensHelper = async (address?: tEthereumAddress) =>
await StableAndVariableTokensHelperFactory.connect(
address ||
(
await getDb()
.get(`${eContractid.StableAndVariableTokensHelper}.${DRE.network.name}`)
.value()
).address,
await getFirstSigner()
);
export const getATokensAndRatesHelper = async (address?: tEthereumAddress) =>
await ATokensAndRatesHelperFactory.connect(
address ||
(await getDb().get(`${eContractid.ATokensAndRatesHelper}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
export const getWETHGateway = async (address?: tEthereumAddress) =>
await WethGatewayFactory.connect(
address ||
(await getDb().get(`${eContractid.WETHGateway}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getWETHMocked = async (address?: tEthereumAddress) =>
await Weth9MockedFactory.connect(
address || (await getDb().get(`${eContractid.WETHMocked}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getMockAToken = async (address?: tEthereumAddress) =>
await MockATokenFactory.connect(
address || (await getDb().get(`${eContractid.MockAToken}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getMockVariableDebtToken = async (address?: tEthereumAddress) =>
await MockVariableDebtTokenFactory.connect(
address ||
(await getDb().get(`${eContractid.MockVariableDebtToken}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);
export const getMockStableDebtToken = async (address?: tEthereumAddress) =>
await MockStableDebtTokenFactory.connect(
address ||
(await getDb().get(`${eContractid.MockStableDebtToken}.${DRE.network.name}`).value()).address,
await getFirstSigner()
);
export const getSelfdestructTransferMock = async (address?: tEthereumAddress) =>
await SelfdestructTransferFactory.connect(
address ||
(await getDb().get(`${eContractid.SelfdestructTransferMock}.${DRE.network.name}`).value())
.address,
await getFirstSigner()
);