import { AaveProtocolDataProviderFactory, ATokenFactory, ATokensAndRatesHelperFactory, DefaultReserveInterestRateStrategyFactory, GenericLogicFactory, InitializableAdminUpgradeabilityProxyFactory, LendingPoolAddressesProviderFactory, LendingPoolAddressesProviderRegistryFactory, LendingPoolCollateralManagerFactory, LendingPoolConfiguratorFactory, LendingPoolFactory, LendingRateOracleFactory, MintableErc20Factory, MockATokenFactory, MockFlashLoanReceiverFactory, MockStableDebtTokenFactory, MockVariableDebtTokenFactory, PriceOracleFactory, ReserveLogicFactory, SelfdestructTransferFactory, StableAndVariableTokensHelperFactory, StableDebtTokenFactory, VariableDebtTokenFactory, WalletBalanceProviderFactory, Weth9Factory, Weth9MockedFactory, WethGatewayFactory, } from '../types'; import {Ierc20DetailedFactory} from '../types/Ierc20DetailedFactory'; import {UpgradeabilityProxy} from '../types/UpgradeabilityProxy'; 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 getAaveProtocolDataProvider = async (address?: tEthereumAddress) => await AaveProtocolDataProviderFactory.connect( address || (await getDb().get(`${eContractid.AaveProtocolDataProvider}.${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>( 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>( 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() ); export const getProxy = async (address: tEthereumAddress) => await InitializableAdminUpgradeabilityProxyFactory.connect(address, await getFirstSigner()); export const getLendingPoolImpl = async (address?: tEthereumAddress) => await LendingPoolFactory.connect( address || (await getDb().get(`${eContractid.LendingPoolImpl}.${DRE.network.name}`).value()).address, await getFirstSigner() ); export const getLendingPoolConfiguratorImpl = async (address?: tEthereumAddress) => await LendingPoolConfiguratorFactory.connect( address || (await getDb().get(`${eContractid.LendingPoolConfiguratorImpl}.${DRE.network.name}`).value()) .address, await getFirstSigner() ); export const getLendingPoolCollateralManagerImpl = async (address?: tEthereumAddress) => await LendingPoolCollateralManagerFactory.connect( address || ( await getDb() .get(`${eContractid.LendingPoolCollateralManagerImpl}.${DRE.network.name}`) .value() ).address, await getFirstSigner() ); export const getWalletProvider = async (address?: tEthereumAddress) => await WalletBalanceProviderFactory.connect( address || (await getDb().get(`${eContractid.WalletBalanceProvider}.${DRE.network.name}`).value()) .address, await getFirstSigner() ); export const getLendingPoolCollateralManager = async (address?: tEthereumAddress) => await LendingPoolCollateralManagerFactory.connect( address || (await getDb().get(`${eContractid.LendingPoolCollateralManager}.${DRE.network.name}`).value()) .address, await getFirstSigner() ); export const getAddressById = async (id: string) => (await getDb().get(`${id}.${DRE.network.name}`).value()).address;