2020-07-13 08:54:08 +00:00
|
|
|
import {Contract, Signer, utils, ethers} from 'ethers';
|
2020-05-29 14:55:31 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
import {getDb, BRE} from './misc-utils';
|
2020-06-08 12:03:40 +00:00
|
|
|
import {
|
|
|
|
tEthereumAddress,
|
|
|
|
eContractid,
|
|
|
|
tStringTokenSmallUnits,
|
|
|
|
eEthereumNetwork,
|
|
|
|
AavePools,
|
|
|
|
iParamsPerNetwork,
|
|
|
|
iParamsPerPool,
|
2020-07-13 08:54:08 +00:00
|
|
|
} from './types';
|
|
|
|
import {LendingPoolAddressesProvider} from '../types/LendingPoolAddressesProvider';
|
|
|
|
import {MintableErc20} from '../types/MintableErc20';
|
|
|
|
import {LendingPoolAddressesProviderRegistry} from '../types/LendingPoolAddressesProviderRegistry';
|
|
|
|
import {LendingPoolConfigurator} from '../types/LendingPoolConfigurator';
|
|
|
|
import {readArtifact} from '@nomiclabs/buidler/plugins';
|
|
|
|
import {Artifact} from '@nomiclabs/buidler/types';
|
|
|
|
import {LendingPool} from '../types/LendingPool';
|
|
|
|
import {PriceOracle} from '../types/PriceOracle';
|
|
|
|
import {MockAggregator} from '../types/MockAggregator';
|
|
|
|
import {LendingRateOracle} from '../types/LendingRateOracle';
|
|
|
|
import {DefaultReserveInterestRateStrategy} from '../types/DefaultReserveInterestRateStrategy';
|
|
|
|
import {LendingPoolLiquidationManager} from '../types/LendingPoolLiquidationManager';
|
|
|
|
import {InitializableAdminUpgradeabilityProxy} from '../types/InitializableAdminUpgradeabilityProxy';
|
|
|
|
import {MockFlashLoanReceiver} from '../types/MockFlashLoanReceiver';
|
|
|
|
import {WalletBalanceProvider} from '../types/WalletBalanceProvider';
|
|
|
|
import {AToken} from '../types/AToken';
|
|
|
|
import {AaveProtocolTestHelpers} from '../types/AaveProtocolTestHelpers';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import {Ierc20Detailed} from '../types/Ierc20Detailed';
|
|
|
|
import {StableDebtToken} from '../types/StableDebtToken';
|
|
|
|
import {VariableDebtToken} from '../types/VariableDebtToken';
|
2020-09-09 19:24:20 +00:00
|
|
|
import {MockSwapAdapter} from '../types/MockSwapAdapter';
|
2020-09-14 17:59:00 +00:00
|
|
|
import {signTypedData_v4, TypedData} from 'eth-sig-util';
|
|
|
|
import {fromRpcSig, ECDSASignature} from 'ethereumjs-util';
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
export const registerContractInJsonDb = async (contractId: string, contractInstance: Contract) => {
|
2020-05-29 14:55:31 +00:00
|
|
|
const currentNetwork = BRE.network.name;
|
2020-09-09 19:24:20 +00:00
|
|
|
if (currentNetwork !== 'buidlerevm' && !currentNetwork.includes('coverage')) {
|
2020-05-29 14:55:31 +00:00
|
|
|
console.log(`*** ${contractId} ***\n`);
|
|
|
|
console.log(`Network: ${currentNetwork}`);
|
|
|
|
console.log(`tx: ${contractInstance.deployTransaction.hash}`);
|
|
|
|
console.log(`contract address: ${contractInstance.address}`);
|
|
|
|
console.log(`deployer address: ${contractInstance.deployTransaction.from}`);
|
|
|
|
console.log(`gas price: ${contractInstance.deployTransaction.gasPrice}`);
|
|
|
|
console.log(`gas used: ${contractInstance.deployTransaction.gasLimit}`);
|
|
|
|
console.log(`\n******`);
|
|
|
|
console.log();
|
|
|
|
}
|
|
|
|
|
|
|
|
await getDb()
|
|
|
|
.set(`${contractId}.${currentNetwork}`, {
|
|
|
|
address: contractInstance.address,
|
|
|
|
deployer: contractInstance.deployTransaction.from,
|
|
|
|
})
|
|
|
|
.write();
|
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const insertContractAddressInDb = async (id: eContractid, address: tEthereumAddress) =>
|
2020-06-08 12:03:40 +00:00
|
|
|
await getDb()
|
|
|
|
.set(`${id}.${BRE.network.name}`, {
|
|
|
|
address,
|
|
|
|
})
|
|
|
|
.write();
|
|
|
|
|
2020-05-29 14:55:31 +00:00
|
|
|
export const getEthersSigners = async (): Promise<Signer[]> =>
|
2020-08-19 12:23:41 +00:00
|
|
|
await Promise.all(await BRE.ethers.getSigners());
|
2020-05-29 14:55:31 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const getEthersSignersAddresses = async (): Promise<tEthereumAddress[]> =>
|
2020-08-19 12:23:41 +00:00
|
|
|
await Promise.all((await BRE.ethers.getSigners()).map((signer) => signer.getAddress()));
|
2020-05-29 14:55:31 +00:00
|
|
|
|
|
|
|
export const getCurrentBlock = async () => {
|
|
|
|
return BRE.ethers.provider.getBlockNumber();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const decodeAbiNumber = (data: string): number =>
|
2020-07-13 08:54:08 +00:00
|
|
|
parseInt(utils.defaultAbiCoder.decode(['uint256'], data).toString());
|
2020-05-29 14:55:31 +00:00
|
|
|
|
2020-08-10 18:20:08 +00:00
|
|
|
export const deployContract = async <ContractType extends Contract>(
|
2020-05-29 14:55:31 +00:00
|
|
|
contractName: string,
|
|
|
|
args: any[]
|
2020-06-08 12:03:40 +00:00
|
|
|
): Promise<ContractType> => {
|
2020-08-19 12:23:41 +00:00
|
|
|
const contract = (await (await BRE.ethers.getContractFactory(contractName)).deploy(
|
2020-05-29 14:55:31 +00:00
|
|
|
...args
|
|
|
|
)) as ContractType;
|
|
|
|
|
2020-06-08 12:03:40 +00:00
|
|
|
await registerContractInJsonDb(<eContractid>contractName, contract);
|
|
|
|
return contract;
|
|
|
|
};
|
|
|
|
|
2020-07-09 14:27:19 +00:00
|
|
|
export const getContract = async <ContractType extends Contract>(
|
2020-05-29 14:55:31 +00:00
|
|
|
contractName: string,
|
|
|
|
address: string
|
2020-07-13 08:54:08 +00:00
|
|
|
): Promise<ContractType> => (await BRE.ethers.getContractAt(contractName, address)) as ContractType;
|
2020-05-29 14:55:31 +00:00
|
|
|
|
2020-06-03 10:44:10 +00:00
|
|
|
export const deployLendingPoolAddressesProvider = async () =>
|
2020-07-13 08:54:08 +00:00
|
|
|
await deployContract<LendingPoolAddressesProvider>(eContractid.LendingPoolAddressesProvider, []);
|
2020-05-29 14:55:31 +00:00
|
|
|
|
2020-06-08 12:03:40 +00:00
|
|
|
export const deployLendingPoolAddressesProviderRegistry = async () =>
|
|
|
|
await deployContract<LendingPoolAddressesProviderRegistry>(
|
|
|
|
eContractid.LendingPoolAddressesProviderRegistry,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
export const deployLendingPoolConfigurator = async () =>
|
2020-07-13 08:54:08 +00:00
|
|
|
await deployContract<LendingPoolConfigurator>(eContractid.LendingPoolConfigurator, []);
|
2020-06-08 12:03:40 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
const deployLibrary = async (libraryId: eContractid) => {
|
|
|
|
const factory = await BRE.ethers.getContractFactory(libraryId);
|
2020-06-20 23:40:03 +00:00
|
|
|
const library = await factory.deploy();
|
|
|
|
await library.deployed();
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return library;
|
|
|
|
};
|
2020-06-20 23:40:03 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const linkLibrariesToArtifact = async (artifact: Artifact) => {
|
2020-06-20 23:40:03 +00:00
|
|
|
const reserveLogic = await deployLibrary(eContractid.ReserveLogic);
|
2020-06-08 12:03:40 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const genericLogicArtifact = await readArtifact(
|
2020-06-08 12:03:40 +00:00
|
|
|
BRE.config.paths.artifacts,
|
2020-06-20 23:40:03 +00:00
|
|
|
eContractid.GenericLogic
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
2020-06-20 23:40:03 +00:00
|
|
|
|
|
|
|
const linkedGenericLogicByteCode = linkBytecode(genericLogicArtifact, {
|
|
|
|
[eContractid.ReserveLogic]: reserveLogic.address,
|
2020-06-08 12:03:40 +00:00
|
|
|
});
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const genericLogicFactory = await BRE.ethers.getContractFactory(
|
|
|
|
genericLogicArtifact.abi,
|
|
|
|
linkedGenericLogicByteCode
|
|
|
|
);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const genericLogic = await (await genericLogicFactory.deploy()).deployed();
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const validationLogicArtifact = await readArtifact(
|
|
|
|
BRE.config.paths.artifacts,
|
|
|
|
eContractid.ValidationLogic
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
|
|
|
|
2020-06-25 22:39:28 +00:00
|
|
|
const linkedValidationLogicByteCode = linkBytecode(validationLogicArtifact, {
|
2020-06-20 23:40:03 +00:00
|
|
|
[eContractid.ReserveLogic]: reserveLogic.address,
|
|
|
|
[eContractid.GenericLogic]: genericLogic.address,
|
|
|
|
});
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const validationLogicFactory = await BRE.ethers.getContractFactory(
|
|
|
|
validationLogicArtifact.abi,
|
|
|
|
linkedValidationLogicByteCode
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-20 23:40:03 +00:00
|
|
|
const validationLogic = await (await validationLogicFactory.deploy()).deployed();
|
2020-06-08 12:03:40 +00:00
|
|
|
|
2020-06-26 12:34:44 +00:00
|
|
|
const linkedBytecode = linkBytecode(artifact, {
|
2020-07-13 08:54:08 +00:00
|
|
|
[eContractid.ReserveLogic]: reserveLogic.address,
|
|
|
|
[eContractid.GenericLogic]: genericLogic.address,
|
|
|
|
[eContractid.ValidationLogic]: validationLogic.address,
|
2020-06-20 23:40:03 +00:00
|
|
|
});
|
2020-07-13 08:54:08 +00:00
|
|
|
const factory = await BRE.ethers.getContractFactory(artifact.abi, linkedBytecode);
|
2020-06-26 12:34:44 +00:00
|
|
|
|
|
|
|
return factory;
|
2020-07-13 08:54:08 +00:00
|
|
|
};
|
2020-06-26 12:34:44 +00:00
|
|
|
|
|
|
|
export const deployLendingPool = async () => {
|
|
|
|
const lendingPoolArtifact = await readArtifact(
|
|
|
|
BRE.config.paths.artifacts,
|
|
|
|
eContractid.LendingPool
|
|
|
|
);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-26 12:34:44 +00:00
|
|
|
const factory = await linkLibrariesToArtifact(lendingPoolArtifact);
|
|
|
|
|
|
|
|
const lendingPool = await factory.deploy();
|
2020-06-20 23:40:03 +00:00
|
|
|
return (await lendingPool.deployed()) as LendingPool;
|
2020-07-13 08:54:08 +00:00
|
|
|
};
|
2020-06-08 12:03:40 +00:00
|
|
|
|
|
|
|
export const deployPriceOracle = async () =>
|
|
|
|
await deployContract<PriceOracle>(eContractid.PriceOracle, []);
|
|
|
|
|
|
|
|
export const deployMockAggregator = async (price: tStringTokenSmallUnits) =>
|
|
|
|
await deployContract<MockAggregator>(eContractid.MockAggregator, [price]);
|
|
|
|
|
|
|
|
export const deployChainlinkProxyPriceProvider = async ([
|
|
|
|
assetsAddresses,
|
|
|
|
sourcesAddresses,
|
|
|
|
fallbackOracleAddress,
|
|
|
|
]: [tEthereumAddress[], tEthereumAddress[], tEthereumAddress]) =>
|
2020-07-13 08:54:08 +00:00
|
|
|
await deployContract<MockAggregator>(eContractid.ChainlinkProxyPriceProvider, [
|
|
|
|
assetsAddresses,
|
|
|
|
sourcesAddresses,
|
|
|
|
fallbackOracleAddress,
|
|
|
|
]);
|
2020-06-08 12:03:40 +00:00
|
|
|
|
|
|
|
export const deployLendingRateOracle = async () =>
|
|
|
|
await deployContract<LendingRateOracle>(eContractid.LendingRateOracle, []);
|
|
|
|
|
2020-06-26 12:34:44 +00:00
|
|
|
export const deployLendingPoolLiquidationManager = async () => {
|
|
|
|
const liquidationManagerArtifact = await readArtifact(
|
|
|
|
BRE.config.paths.artifacts,
|
|
|
|
eContractid.LendingPoolLiquidationManager
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-26 12:34:44 +00:00
|
|
|
const factory = await linkLibrariesToArtifact(liquidationManagerArtifact);
|
|
|
|
|
|
|
|
const liquidationManager = await factory.deploy();
|
|
|
|
return (await liquidationManager.deployed()) as LendingPoolLiquidationManager;
|
2020-07-13 08:54:08 +00:00
|
|
|
};
|
2020-06-08 12:03:40 +00:00
|
|
|
|
|
|
|
export const deployInitializableAdminUpgradeabilityProxy = async () =>
|
|
|
|
await deployContract<InitializableAdminUpgradeabilityProxy>(
|
|
|
|
eContractid.InitializableAdminUpgradeabilityProxy,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const deployMockFlashLoanReceiver = async (addressesProvider: tEthereumAddress) =>
|
|
|
|
await deployContract<MockFlashLoanReceiver>(eContractid.MockFlashLoanReceiver, [
|
|
|
|
addressesProvider,
|
|
|
|
]);
|
2020-06-08 12:03:40 +00:00
|
|
|
|
2020-09-08 13:05:53 +00:00
|
|
|
export const deployMockSwapAdapter = async (addressesProvider: tEthereumAddress) =>
|
2020-09-09 19:24:20 +00:00
|
|
|
await deployContract<MockSwapAdapter>(eContractid.MockSwapAdapter, [addressesProvider]);
|
2020-09-08 13:05:53 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const deployWalletBalancerProvider = async (addressesProvider: tEthereumAddress) =>
|
|
|
|
await deployContract<WalletBalanceProvider>(eContractid.WalletBalanceProvider, [
|
|
|
|
addressesProvider,
|
|
|
|
]);
|
2020-06-08 15:36:40 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const deployAaveProtocolTestHelpers = async (addressesProvider: tEthereumAddress) =>
|
|
|
|
await deployContract<AaveProtocolTestHelpers>(eContractid.AaveProtocolTestHelpers, [
|
|
|
|
addressesProvider,
|
2020-06-08 12:03:40 +00:00
|
|
|
]);
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const deployMintableErc20 = async ([name, symbol, decimals]: [string, string, number]) =>
|
|
|
|
await deployContract<MintableErc20>(eContractid.MintableERC20, [name, symbol, decimals]);
|
|
|
|
|
2020-06-08 12:03:40 +00:00
|
|
|
export const deployDefaultReserveInterestRateStrategy = async ([
|
|
|
|
addressesProvider,
|
|
|
|
baseVariableBorrowRate,
|
|
|
|
variableSlope1,
|
|
|
|
variableSlope2,
|
|
|
|
stableSlope1,
|
|
|
|
stableSlope2,
|
2020-07-13 08:54:08 +00:00
|
|
|
]: [tEthereumAddress, string, string, string, string, string]) =>
|
2020-06-08 12:03:40 +00:00
|
|
|
await deployContract<DefaultReserveInterestRateStrategy>(
|
|
|
|
eContractid.DefaultReserveInterestRateStrategy,
|
|
|
|
[
|
|
|
|
addressesProvider,
|
|
|
|
baseVariableBorrowRate,
|
|
|
|
variableSlope1,
|
|
|
|
variableSlope2,
|
|
|
|
stableSlope1,
|
|
|
|
stableSlope2,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-08-19 12:23:41 +00:00
|
|
|
export const deployStableDebtToken = async ([name, symbol, underlyingAsset, poolAddress]: [
|
|
|
|
string,
|
|
|
|
string,
|
|
|
|
tEthereumAddress,
|
|
|
|
tEthereumAddress
|
|
|
|
]) => {
|
2020-08-13 11:06:23 +00:00
|
|
|
const token = await deployContract<StableDebtToken>(eContractid.StableDebtToken, [
|
|
|
|
poolAddress,
|
|
|
|
underlyingAsset,
|
2020-08-19 12:23:41 +00:00
|
|
|
name,
|
|
|
|
symbol,
|
2020-08-13 11:06:23 +00:00
|
|
|
]);
|
2020-06-30 12:09:28 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return token;
|
|
|
|
};
|
|
|
|
|
2020-08-19 12:23:41 +00:00
|
|
|
export const deployVariableDebtToken = async ([name, symbol, underlyingAsset, poolAddress]: [
|
|
|
|
string,
|
|
|
|
string,
|
|
|
|
tEthereumAddress,
|
|
|
|
tEthereumAddress
|
|
|
|
]) => {
|
2020-08-13 11:06:23 +00:00
|
|
|
const token = await deployContract<VariableDebtToken>(eContractid.VariableDebtToken, [
|
|
|
|
poolAddress,
|
|
|
|
underlyingAsset,
|
2020-08-19 12:23:41 +00:00
|
|
|
name,
|
|
|
|
symbol,
|
2020-08-13 11:06:23 +00:00
|
|
|
]);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return token;
|
|
|
|
};
|
|
|
|
|
2020-08-10 18:20:08 +00:00
|
|
|
export const deployGenericAToken = async ([poolAddress, underlyingAssetAddress, name, symbol]: [
|
|
|
|
tEthereumAddress,
|
|
|
|
tEthereumAddress,
|
|
|
|
string,
|
|
|
|
string
|
|
|
|
]) => {
|
|
|
|
const token = await deployContract<AToken>(eContractid.AToken, [
|
|
|
|
poolAddress,
|
|
|
|
underlyingAssetAddress,
|
|
|
|
name,
|
|
|
|
symbol,
|
|
|
|
]);
|
2020-08-07 17:29:13 +00:00
|
|
|
|
|
|
|
return token;
|
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const getLendingPoolAddressesProvider = async (address?: tEthereumAddress) => {
|
2020-06-08 12:03:40 +00:00
|
|
|
return await getContract<LendingPoolAddressesProvider>(
|
|
|
|
eContractid.LendingPoolAddressesProvider,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.LendingPoolAddressesProvider}.${BRE.network.name}`).value())
|
|
|
|
.address
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const getLendingPoolConfiguratorProxy = async (address?: tEthereumAddress) => {
|
2020-06-08 12:03:40 +00:00
|
|
|
return await getContract<LendingPoolConfigurator>(
|
|
|
|
eContractid.LendingPoolConfigurator,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.LendingPoolConfigurator}.${BRE.network.name}`).value())
|
|
|
|
.address
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-09 14:36:33 +00:00
|
|
|
export const getLendingPool = async (address?: tEthereumAddress) => {
|
2020-06-26 12:34:44 +00:00
|
|
|
const lendingPoolArtifact = await readArtifact(
|
|
|
|
BRE.config.paths.artifacts,
|
|
|
|
eContractid.LendingPool
|
|
|
|
);
|
2020-07-03 09:15:30 +00:00
|
|
|
|
2020-06-26 12:34:44 +00:00
|
|
|
const factory = await linkLibrariesToArtifact(lendingPoolArtifact);
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return <LendingPool>(
|
|
|
|
await factory.attach(
|
|
|
|
address ||
|
|
|
|
(await getDb().get(`${eContractid.LendingPool}.${BRE.network.name}`).value()).address
|
|
|
|
)
|
|
|
|
);
|
2020-06-08 12:03:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getPriceOracle = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<PriceOracle>(
|
|
|
|
eContractid.PriceOracle,
|
2020-07-13 08:54:08 +00:00
|
|
|
address || (await getDb().get(`${eContractid.PriceOracle}.${BRE.network.name}`).value()).address
|
2020-06-08 12:03:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-08 15:36:40 +00:00
|
|
|
export const getAToken = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<AToken>(
|
|
|
|
eContractid.AToken,
|
2020-07-13 08:54:08 +00:00
|
|
|
address || (await getDb().get(`${eContractid.AToken}.${BRE.network.name}`).value()).address
|
2020-06-08 15:36:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
export const getMintableErc20 = async (address: tEthereumAddress) => {
|
2020-06-09 09:11:19 +00:00
|
|
|
return await getContract<MintableErc20>(
|
|
|
|
eContractid.MintableERC20,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.MintableERC20}.${BRE.network.name}`).value()).address
|
2020-06-09 09:11:19 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
export const getIErc20Detailed = async (address: tEthereumAddress) => {
|
2020-06-09 09:11:19 +00:00
|
|
|
return await getContract<Ierc20Detailed>(
|
|
|
|
eContractid.IERC20Detailed,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.IERC20Detailed}.${BRE.network.name}`).value()).address
|
2020-06-09 09:11:19 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const getAaveProtocolTestHelpers = async (address?: tEthereumAddress) => {
|
2020-06-08 15:36:40 +00:00
|
|
|
return await getContract<AaveProtocolTestHelpers>(
|
|
|
|
eContractid.AaveProtocolTestHelpers,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.AaveProtocolTestHelpers}.${BRE.network.name}`).value())
|
|
|
|
.address
|
2020-06-08 15:36:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-10 10:31:08 +00:00
|
|
|
export const getInterestRateStrategy = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<DefaultReserveInterestRateStrategy>(
|
|
|
|
eContractid.DefaultReserveInterestRateStrategy,
|
|
|
|
address ||
|
|
|
|
(
|
|
|
|
await getDb()
|
2020-07-13 08:54:08 +00:00
|
|
|
.get(`${eContractid.DefaultReserveInterestRateStrategy}.${BRE.network.name}`)
|
2020-06-10 10:31:08 +00:00
|
|
|
.value()
|
|
|
|
).address
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-12 13:41:52 +00:00
|
|
|
export const getMockFlashLoanReceiver = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<MockFlashLoanReceiver>(
|
|
|
|
eContractid.MockFlashLoanReceiver,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.MockFlashLoanReceiver}.${BRE.network.name}`).value())
|
|
|
|
.address
|
2020-06-12 13:41:52 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-08 13:05:53 +00:00
|
|
|
export const getMockSwapAdapter = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<MockSwapAdapter>(
|
|
|
|
eContractid.MockSwapAdapter,
|
|
|
|
address ||
|
2020-09-09 19:24:20 +00:00
|
|
|
(await getDb().get(`${eContractid.MockSwapAdapter}.${BRE.network.name}`).value()).address
|
2020-09-08 13:05:53 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
export const getLendingRateOracle = async (address?: tEthereumAddress) => {
|
|
|
|
return await getContract<LendingRateOracle>(
|
|
|
|
eContractid.LendingRateOracle,
|
|
|
|
address ||
|
2020-07-13 08:54:08 +00:00
|
|
|
(await getDb().get(`${eContractid.LendingRateOracle}.${BRE.network.name}`).value()).address
|
2020-06-12 20:12:53 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-08 12:03:40 +00:00
|
|
|
const linkBytecode = (artifact: Artifact, libraries: any) => {
|
|
|
|
let bytecode = artifact.bytecode;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
for (const [fileName, fileReferences] of Object.entries(artifact.linkReferences)) {
|
2020-06-08 12:03:40 +00:00
|
|
|
for (const [libName, fixups] of Object.entries(fileReferences)) {
|
|
|
|
const addr = libraries[libName];
|
|
|
|
|
|
|
|
if (addr === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const fixup of fixups) {
|
|
|
|
bytecode =
|
|
|
|
bytecode.substr(0, 2 + fixup.start * 2) +
|
|
|
|
addr.substr(2) +
|
|
|
|
bytecode.substr(2 + (fixup.start + fixup.length) * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytecode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getParamPerNetwork = <T>(
|
2020-09-14 13:57:11 +00:00
|
|
|
{kovan, ropsten, main, buidlerevm, coverage}: iParamsPerNetwork<T>,
|
2020-06-08 12:03:40 +00:00
|
|
|
network: eEthereumNetwork
|
|
|
|
) => {
|
|
|
|
switch (network) {
|
2020-09-14 13:57:11 +00:00
|
|
|
case eEthereumNetwork.coverage:
|
|
|
|
return coverage;
|
|
|
|
case eEthereumNetwork.buidlerevm:
|
|
|
|
return buidlerevm;
|
2020-06-08 12:03:40 +00:00
|
|
|
case eEthereumNetwork.kovan:
|
|
|
|
return kovan;
|
|
|
|
case eEthereumNetwork.ropsten:
|
|
|
|
return ropsten;
|
|
|
|
case eEthereumNetwork.main:
|
|
|
|
return main;
|
|
|
|
default:
|
|
|
|
return main;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const getParamPerPool = <T>({proto, secondary}: iParamsPerPool<T>, pool: AavePools) => {
|
2020-06-08 12:03:40 +00:00
|
|
|
switch (pool) {
|
|
|
|
case AavePools.proto:
|
|
|
|
return proto;
|
|
|
|
case AavePools.secondary:
|
|
|
|
return secondary;
|
|
|
|
default:
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
};
|
2020-06-09 09:11:19 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const convertToCurrencyDecimals = async (tokenAddress: tEthereumAddress, amount: string) => {
|
2020-08-13 11:06:23 +00:00
|
|
|
const token = await getIErc20Detailed(tokenAddress);
|
|
|
|
let decimals = (await token.decimals()).toString();
|
2020-06-09 09:11:19 +00:00
|
|
|
|
2020-06-12 20:12:53 +00:00
|
|
|
return ethers.utils.parseUnits(amount, decimals);
|
2020-06-09 09:11:19 +00:00
|
|
|
};
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
export const convertToCurrencyUnits = async (tokenAddress: string, amount: string) => {
|
2020-08-13 11:06:23 +00:00
|
|
|
const token = await getIErc20Detailed(tokenAddress);
|
|
|
|
let decimals = new BigNumber(await token.decimals());
|
2020-06-09 09:11:19 +00:00
|
|
|
const currencyUnit = new BigNumber(10).pow(decimals);
|
|
|
|
const amountInCurrencyUnits = new BigNumber(amount).div(currencyUnit);
|
|
|
|
return amountInCurrencyUnits.toFixed();
|
|
|
|
};
|
2020-09-14 13:57:11 +00:00
|
|
|
|
|
|
|
export const buildPermitParams = (
|
|
|
|
chainId: number,
|
|
|
|
token: tEthereumAddress,
|
|
|
|
revision: string,
|
|
|
|
tokenName: string,
|
|
|
|
owner: tEthereumAddress,
|
|
|
|
spender: tEthereumAddress,
|
|
|
|
nonce: number,
|
|
|
|
deadline: string,
|
|
|
|
value: tStringTokenSmallUnits
|
|
|
|
) => ({
|
|
|
|
types: {
|
|
|
|
EIP712Domain: [
|
2020-09-14 17:59:00 +00:00
|
|
|
{name: 'name', type: 'string'},
|
|
|
|
{name: 'version', type: 'string'},
|
|
|
|
{name: 'chainId', type: 'uint256'},
|
|
|
|
{name: 'verifyingContract', type: 'address'},
|
2020-09-14 13:57:11 +00:00
|
|
|
],
|
|
|
|
Permit: [
|
2020-09-14 17:59:00 +00:00
|
|
|
{name: 'owner', type: 'address'},
|
|
|
|
{name: 'spender', type: 'address'},
|
|
|
|
{name: 'value', type: 'uint256'},
|
|
|
|
{name: 'nonce', type: 'uint256'},
|
|
|
|
{name: 'deadline', type: 'uint256'},
|
2020-09-14 13:57:11 +00:00
|
|
|
],
|
|
|
|
},
|
2020-09-14 17:59:00 +00:00
|
|
|
primaryType: 'Permit' as const,
|
2020-09-14 13:57:11 +00:00
|
|
|
domain: {
|
|
|
|
name: tokenName,
|
|
|
|
version: revision,
|
|
|
|
chainId: chainId,
|
|
|
|
verifyingContract: token,
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
owner,
|
|
|
|
spender,
|
|
|
|
value,
|
|
|
|
nonce,
|
|
|
|
deadline,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const getSignatureFromTypedData = (
|
|
|
|
privateKey: string,
|
|
|
|
typedData: any // TODO: should be TypedData, from eth-sig-utils, but TS doesn't accept it
|
|
|
|
): ECDSASignature => {
|
2020-09-14 17:59:00 +00:00
|
|
|
const signature = signTypedData_v4(Buffer.from(privateKey.substring(2, 66), 'hex'), {
|
|
|
|
data: typedData,
|
|
|
|
});
|
2020-09-14 13:57:11 +00:00
|
|
|
return fromRpcSig(signature);
|
2020-09-14 17:59:00 +00:00
|
|
|
};
|