aave-protocol-v2/helpers/configuration.ts

137 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-08-25 12:15:35 +00:00
import {
AavePools,
iMultiPoolsAssets,
IReserveParams,
PoolConfiguration,
2020-09-15 15:24:50 +00:00
ICommonConfiguration,
eNetwork,
2020-08-25 12:15:35 +00:00
} from './types';
import { getEthersSignersAddresses, getParamPerPool } from './contracts-helpers';
import AaveConfig from '../markets/aave';
import MaticConfig from '../markets/matic';
import AmmConfig from '../markets/amm';
import { CommonsConfig } from '../markets/aave/commons';
import { DRE, filterMapBy } from './misc-utils';
import { tEthereumAddress } from './types';
import { getParamPerNetwork } from './contracts-helpers';
import { deployWETHMocked } from './contracts-deployments';
2020-08-25 12:15:35 +00:00
export enum ConfigNames {
Commons = 'Commons',
Aave = 'Aave',
Matic = 'Matic',
Amm = 'Amm',
}
2020-08-25 12:15:35 +00:00
export const loadPoolConfig = (configName: ConfigNames): PoolConfiguration => {
switch (configName) {
case ConfigNames.Aave:
return AaveConfig;
case ConfigNames.Matic:
return MaticConfig;
case ConfigNames.Amm:
return AmmConfig;
2020-09-15 15:24:50 +00:00
case ConfigNames.Commons:
return CommonsConfig;
default:
throw new Error(`Unsupported pool configuration: ${Object.values(ConfigNames)}`);
}
};
2020-08-25 12:15:35 +00:00
// ----------------
// PROTOCOL PARAMS PER POOL
// ----------------
export const getReservesConfigByPool = (pool: AavePools): iMultiPoolsAssets<IReserveParams> =>
getParamPerPool<iMultiPoolsAssets<IReserveParams>>(
{
[AavePools.proto]: {
...AaveConfig.ReservesConfig,
},
[AavePools.amm]: {
...AmmConfig.ReservesConfig,
2020-12-25 05:47:30 +00:00
},
[AavePools.matic]: {
...MaticConfig.ReservesConfig,
},
2020-08-25 12:15:35 +00:00
},
pool
);
export const getGenesisPoolAdmin = async (
config: ICommonConfiguration
): Promise<tEthereumAddress> => {
const currentNetwork = process.env.FORK ? process.env.FORK : DRE.network.name;
const targetAddress = getParamPerNetwork(config.PoolAdmin, <eNetwork>currentNetwork);
2020-09-15 15:24:50 +00:00
if (targetAddress) {
return targetAddress;
}
const addressList = await getEthersSignersAddresses();
2020-11-05 11:35:50 +00:00
const addressIndex = config.PoolAdminIndex;
return addressList[addressIndex];
};
export const getEmergencyAdmin = async (
config: ICommonConfiguration
): Promise<tEthereumAddress> => {
const currentNetwork = process.env.FORK ? process.env.FORK : DRE.network.name;
const targetAddress = getParamPerNetwork(config.EmergencyAdmin, <eNetwork>currentNetwork);
2020-11-05 11:35:50 +00:00
if (targetAddress) {
return targetAddress;
}
const addressList = await getEthersSignersAddresses();
2020-11-05 11:35:50 +00:00
const addressIndex = config.EmergencyAdminIndex;
2020-09-15 15:24:50 +00:00
return addressList[addressIndex];
};
2020-11-27 15:40:00 +00:00
export const getTreasuryAddress = async (
config: ICommonConfiguration
): Promise<tEthereumAddress> => {
const currentNetwork = process.env.FORK ? process.env.FORK : DRE.network.name;
return getParamPerNetwork(config.ReserveFactorTreasuryAddress, <eNetwork>currentNetwork);
2020-11-27 15:40:00 +00:00
};
2020-09-15 15:24:50 +00:00
export const getATokenDomainSeparatorPerNetwork = (
network: eNetwork,
2020-09-15 15:24:50 +00:00
config: ICommonConfiguration
): tEthereumAddress => getParamPerNetwork<tEthereumAddress>(config.ATokenDomainSeparator, network);
export const getWethAddress = async (config: ICommonConfiguration) => {
const currentNetwork = process.env.FORK ? process.env.FORK : DRE.network.name;
const wethAddress = getParamPerNetwork(config.WETH, <eNetwork>currentNetwork);
if (wethAddress) {
return wethAddress;
}
if (currentNetwork.includes('main')) {
throw new Error('WETH not set at mainnet configuration.');
}
const weth = await deployWETHMocked();
return weth.address;
};
export const getWrappedNativeTokenAddress = async (config: ICommonConfiguration) => {
const currentNetwork = process.env.MAINNET_FORK === 'true' ? 'main' : DRE.network.name;
const wethAddress = getParamPerNetwork(config.WrappedNativeToken, <eNetwork>currentNetwork);
if (wethAddress) {
return wethAddress;
}
if (currentNetwork.includes('main')) {
throw new Error('WETH not set at mainnet configuration.');
}
const weth = await deployWETHMocked();
return weth.address;
};
export const getLendingRateOracles = (poolConfig: ICommonConfiguration) => {
const {
ProtocolGlobalParams: { UsdAddress },
LendingRateOracleRatesCommon,
ReserveAssets,
} = poolConfig;
const network = process.env.FORK ? process.env.FORK : DRE.network.name;
return filterMapBy(LendingRateOracleRatesCommon, (key) =>
Object.keys(ReserveAssets[network]).includes(key)
);
};