feat: Add defender utils and use of signer

This commit is contained in:
miguelmtzinf 2021-05-17 14:35:21 +02:00
parent d6f92a6fad
commit 329a48cd03
11 changed files with 89 additions and 33 deletions

View File

@ -6,7 +6,7 @@ import {
ICommonConfiguration, ICommonConfiguration,
eNetwork, eNetwork,
} from './types'; } from './types';
import { getParamPerPool } from './contracts-helpers'; import { getEthersSignersAddresses, getParamPerPool } from './contracts-helpers';
import AaveConfig from '../markets/aave'; import AaveConfig from '../markets/aave';
import MaticConfig from '../markets/matic'; import MaticConfig from '../markets/matic';
import AmmConfig from '../markets/amm'; import AmmConfig from '../markets/amm';
@ -66,9 +66,7 @@ export const getGenesisPoolAdmin = async (
if (targetAddress) { if (targetAddress) {
return targetAddress; return targetAddress;
} }
const addressList = await Promise.all( const addressList = await getEthersSignersAddresses();
(await DRE.ethers.getSigners()).map((signer) => signer.getAddress())
);
const addressIndex = config.PoolAdminIndex; const addressIndex = config.PoolAdminIndex;
return addressList[addressIndex]; return addressList[addressIndex];
}; };
@ -81,9 +79,7 @@ export const getEmergencyAdmin = async (
if (targetAddress) { if (targetAddress) {
return targetAddress; return targetAddress;
} }
const addressList = await Promise.all( const addressList = await getEthersSignersAddresses();
(await DRE.ethers.getSigners()).map((signer) => signer.getAddress())
);
const addressIndex = config.EmergencyAdminIndex; const addressIndex = config.EmergencyAdminIndex;
return addressList[addressIndex]; return addressList[addressIndex];
}; };

View File

@ -137,7 +137,9 @@ export const deployGenericLogic = async (reserveLogic: Contract, verify?: boolea
linkedGenericLogicByteCode linkedGenericLogicByteCode
); );
const genericLogic = await (await genericLogicFactory.deploy()).deployed(); const genericLogic = await (
await genericLogicFactory.connect(await getFirstSigner()).deploy()
).deployed();
return withSaveAndVerify(genericLogic, eContractid.GenericLogic, [], verify); return withSaveAndVerify(genericLogic, eContractid.GenericLogic, [], verify);
}; };
@ -158,7 +160,9 @@ export const deployValidationLogic = async (
linkedValidationLogicByteCode linkedValidationLogicByteCode
); );
const validationLogic = await (await validationLogicFactory.deploy()).deployed(); const validationLogic = await (
await validationLogicFactory.connect(await getFirstSigner()).deploy()
).deployed();
return withSaveAndVerify(validationLogic, eContractid.ValidationLogic, [], verify); return withSaveAndVerify(validationLogic, eContractid.ValidationLogic, [], verify);
}; };

View File

@ -32,11 +32,11 @@ import {
FlashLiquidationAdapterFactory, FlashLiquidationAdapterFactory,
} from '../types'; } from '../types';
import { IERC20DetailedFactory } from '../types/IERC20DetailedFactory'; import { IERC20DetailedFactory } from '../types/IERC20DetailedFactory';
import { MockTokenMap } from './contracts-helpers'; import { getEthersSigners, MockTokenMap } from './contracts-helpers';
import { DRE, getDb, notFalsyOrZeroAddress } from './misc-utils'; import { DRE, getDb, notFalsyOrZeroAddress } from './misc-utils';
import { eContractid, PoolConfiguration, tEthereumAddress, TokenContractId } from './types'; import { eContractid, PoolConfiguration, tEthereumAddress, TokenContractId } from './types';
export const getFirstSigner = async () => (await DRE.ethers.getSigners())[0]; export const getFirstSigner = async () => (await getEthersSigners())[0];
export const getLendingPoolAddressesProvider = async (address?: tEthereumAddress) => { export const getLendingPoolAddressesProvider = async (address?: tEthereumAddress) => {
return await LendingPoolAddressesProviderFactory.connect( return await LendingPoolAddressesProviderFactory.connect(

View File

@ -23,9 +23,10 @@ import { MintableERC20 } from '../types/MintableERC20';
import { Artifact } from 'hardhat/types'; import { Artifact } from 'hardhat/types';
import { Artifact as BuidlerArtifact } from '@nomiclabs/buidler/types'; import { Artifact as BuidlerArtifact } from '@nomiclabs/buidler/types';
import { verifyEtherscanContract } from './etherscan-verification'; import { verifyEtherscanContract } from './etherscan-verification';
import { getIErc20Detailed } from './contracts-getters'; import { getFirstSigner, getIErc20Detailed } from './contracts-getters';
import { usingTenderly, verifyAtTenderly } from './tenderly-utils'; import { usingTenderly, verifyAtTenderly } from './tenderly-utils';
import { usingPolygon, verifyAtPolygon } from './polygon-utils'; import { usingPolygon, verifyAtPolygon } from './polygon-utils';
import { getDefenderRelaySigner, usingDefender } from './defender-utils';
export type MockTokenMap = { [symbol: string]: MintableERC20 }; export type MockTokenMap = { [symbol: string]: MintableERC20 };
@ -66,11 +67,18 @@ export const rawInsertContractAddressInDb = async (id: string, address: tEthereu
}) })
.write(); .write();
export const getEthersSigners = async (): Promise<Signer[]> => export const getEthersSigners = async (): Promise<Signer[]> => {
await Promise.all(await DRE.ethers.getSigners()); const ethersSigners = await Promise.all(await DRE.ethers.getSigners());
if (usingDefender()) {
const [, ...users] = ethersSigners;
return [await getDefenderRelaySigner(), ...users];
}
return ethersSigners;
};
export const getEthersSignersAddresses = async (): Promise<tEthereumAddress[]> => export const getEthersSignersAddresses = async (): Promise<tEthereumAddress[]> =>
await Promise.all((await DRE.ethers.getSigners()).map((signer) => signer.getAddress())); await Promise.all((await getEthersSigners()).map((signer) => signer.getAddress()));
export const getCurrentBlock = async () => { export const getCurrentBlock = async () => {
return DRE.ethers.provider.getBlockNumber(); return DRE.ethers.provider.getBlockNumber();
@ -83,9 +91,9 @@ export const deployContract = async <ContractType extends Contract>(
contractName: string, contractName: string,
args: any[] args: any[]
): Promise<ContractType> => { ): Promise<ContractType> => {
const contract = (await (await DRE.ethers.getContractFactory(contractName)).deploy( const contract = (await (await DRE.ethers.getContractFactory(contractName))
...args .connect(await getFirstSigner())
)) as ContractType; .deploy(...args)) as ContractType;
await waitForTx(contract.deployTransaction); await waitForTx(contract.deployTransaction);
await registerContractInJsonDb(<eContractid>contractName, contract); await registerContractInJsonDb(<eContractid>contractName, contract);
return contract; return contract;

41
helpers/defender-utils.ts Normal file
View File

@ -0,0 +1,41 @@
import { formatEther } from '@ethersproject/units';
import { DefenderRelaySigner, DefenderRelayProvider } from 'defender-relay-client/lib/ethers';
import { Signer } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { DRE, impersonateAccountsHardhat } from './misc-utils';
import { usingTenderly } from './tenderly-utils';
export const usingDefender = () => process.env.DEFENDER === 'true';
export const getDefenderRelaySigner = async () => {
const { DEFENDER_API_KEY, DEFENDER_SECRET_KEY } = process.env;
let defenderSigner: Signer;
if (!DEFENDER_API_KEY || !DEFENDER_SECRET_KEY) {
throw new Error('Defender secrets required');
}
const credentials = { apiKey: DEFENDER_API_KEY, apiSecret: DEFENDER_SECRET_KEY };
defenderSigner = new DefenderRelaySigner(credentials, new DefenderRelayProvider(credentials), {
speed: 'fast',
});
const defenderAddress = await defenderSigner.getAddress();
console.log(' - Using Defender Relay: ', defenderAddress);
// Reemplace signer if FORK=main is active
if (process.env.FORK === 'main') {
console.log(' - Impersonating Defender Relay');
await impersonateAccountsHardhat([defenderAddress]);
defenderSigner = await (DRE as HardhatRuntimeEnvironment).ethers.getSigner(defenderAddress);
}
// Reemplace signer if Tenderly network is active
if (usingTenderly()) {
console.log(' - Impersonating Defender Relay via Tenderly');
defenderSigner = await (DRE as HardhatRuntimeEnvironment).ethers.getSigner(defenderAddress);
}
console.log(' - Balance: ', formatEther(await defenderSigner.getBalance()));
return defenderSigner;
};

View File

@ -115,3 +115,17 @@ export const notFalsyOrZeroAddress = (address: tEthereumAddress | null | undefin
} }
return isAddress(address) && !isZeroAddress(address); return isAddress(address) && !isZeroAddress(address);
}; };
export const impersonateAccountsHardhat = async (accounts: string[]) => {
if (process.env.TENDERLY === 'true') {
return;
}
// eslint-disable-next-line no-restricted-syntax
for (const account of accounts) {
// eslint-disable-next-line no-await-in-loop
await (DRE as HardhatRuntimeEnvironment).network.provider.request({
method: 'hardhat_impersonateAccount',
params: [account],
});
}
};

View File

@ -3,6 +3,7 @@ import {
deployLendingPoolAddressesProvider, deployLendingPoolAddressesProvider,
deployLendingPoolAddressesProviderRegistry, deployLendingPoolAddressesProviderRegistry,
} from '../../helpers/contracts-deployments'; } from '../../helpers/contracts-deployments';
import { getEthersSigners } from '../../helpers/contracts-helpers';
import { waitForTx } from '../../helpers/misc-utils'; import { waitForTx } from '../../helpers/misc-utils';
import { AaveConfig } from '../../markets/aave'; import { AaveConfig } from '../../markets/aave';
@ -14,7 +15,7 @@ task(
.setAction(async ({ verify }, localBRE) => { .setAction(async ({ verify }, localBRE) => {
await localBRE.run('set-DRE'); await localBRE.run('set-DRE');
const admin = await (await localBRE.ethers.getSigners())[0].getAddress(); const admin = await (await getEthersSigners())[0].getAddress();
const addressesProvider = await deployLendingPoolAddressesProvider(AaveConfig.MarketId, verify); const addressesProvider = await deployLendingPoolAddressesProvider(AaveConfig.MarketId, verify);
await waitForTx(await addressesProvider.setPoolAdmin(admin)); await waitForTx(await addressesProvider.setPoolAdmin(admin));

View File

@ -3,7 +3,6 @@ import { DRE, setDRE } from '../../helpers/misc-utils';
import { EthereumNetworkNames } from '../../helpers/types'; import { EthereumNetworkNames } from '../../helpers/types';
import { usingTenderly } from '../../helpers/tenderly-utils'; import { usingTenderly } from '../../helpers/tenderly-utils';
import { HardhatRuntimeEnvironment } from 'hardhat/types'; import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { getFirstSigner } from '../../helpers/contracts-getters';
import { formatEther } from 'ethers/lib/utils'; import { formatEther } from 'ethers/lib/utils';
import { fork } from 'child_process'; import { fork } from 'child_process';
import { env } from 'process'; import { env } from 'process';
@ -26,16 +25,11 @@ task(`set-DRE`, `Inits the DRE, to have access to all the plugins' objects`).set
console.log('- Creating a new Tenderly Fork'); console.log('- Creating a new Tenderly Fork');
await _DRE.tenderlyRPC.initializeFork(); await _DRE.tenderlyRPC.initializeFork();
} }
const provider = new _DRE.ethers.providers.Web3Provider(_DRE.tenderlyRPC as any); const provider = new _DRE.ethers.providers.Web3Provider(_DRE.tenderlyRPC);
_DRE.ethers.provider = provider; _DRE.ethers.provider = provider;
console.log('- Initialized Tenderly fork:'); console.log('- Initialized Tenderly fork:');
console.log(' - Fork: ', _DRE.tenderlyRPC.getFork()); console.log(' - Fork: ', _DRE.tenderlyRPC.getFork());
console.log(' - Head: ', _DRE.tenderlyRPC.getHead()); console.log(' - Head: ', _DRE.tenderlyRPC.getHead());
console.log(' - First account:', await (await _DRE.ethers.getSigners())[0].getAddress());
console.log(
' - Balance:',
formatEther(await (await _DRE.ethers.getSigners())[0].getBalance())
);
} }
console.log('- Enviroment'); console.log('- Enviroment');

View File

@ -4,6 +4,7 @@ import {
insertContractAddressInDb, insertContractAddressInDb,
getEthersSigners, getEthersSigners,
registerContractInJsonDb, registerContractInJsonDb,
getEthersSignersAddresses,
} from '../../helpers/contracts-helpers'; } from '../../helpers/contracts-helpers';
import { import {
deployLendingPoolAddressesProvider, deployLendingPoolAddressesProvider,
@ -102,9 +103,7 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
await waitForTx(await addressesProvider.setPoolAdmin(aaveAdmin)); await waitForTx(await addressesProvider.setPoolAdmin(aaveAdmin));
//setting users[1] as emergency admin, which is in position 2 in the DRE addresses list //setting users[1] as emergency admin, which is in position 2 in the DRE addresses list
const addressList = await Promise.all( const addressList = await getEthersSignersAddresses();
(await DRE.ethers.getSigners()).map((signer) => signer.getAddress())
);
await waitForTx(await addressesProvider.setEmergencyAdmin(addressList[2])); await waitForTx(await addressesProvider.setEmergencyAdmin(addressList[2]));

View File

@ -4,6 +4,7 @@ import {
insertContractAddressInDb, insertContractAddressInDb,
getEthersSigners, getEthersSigners,
registerContractInJsonDb, registerContractInJsonDb,
getEthersSignersAddresses,
} from '../../helpers/contracts-helpers'; } from '../../helpers/contracts-helpers';
import { import {
deployLendingPoolAddressesProvider, deployLendingPoolAddressesProvider,
@ -101,9 +102,7 @@ const buildTestEnv = async (deployer: Signer, secondaryWallet: Signer) => {
await waitForTx(await addressesProvider.setPoolAdmin(aaveAdmin)); await waitForTx(await addressesProvider.setPoolAdmin(aaveAdmin));
//setting users[1] as emergency admin, which is in position 2 in the DRE addresses list //setting users[1] as emergency admin, which is in position 2 in the DRE addresses list
const addressList = await Promise.all( const addressList = await getEthersSignersAddresses();
(await DRE.ethers.getSigners()).map((signer) => signer.getAddress())
);
await waitForTx(await addressesProvider.setEmergencyAdmin(addressList[2])); await waitForTx(await addressesProvider.setEmergencyAdmin(addressList[2]));

View File

@ -8,7 +8,7 @@
"noImplicitAny": false, "noImplicitAny": false,
"resolveJsonModule": true "resolveJsonModule": true
}, },
"include": ["./scripts", "./test", "./tasks", "test-suites/test-aave/uniswapAdapters.repay.spec.ts", "test-suites/test-aave/upgradeability.spec.ts", "test-suites/test-aave/variable-debt-token.spec.ts", "test-suites/test-aave/weth-gateway.spec.ts"], "include": ["./scripts", "./test", "./tasks", "./helpers", "test-suites/test-aave/uniswapAdapters.repay.spec.ts", "test-suites/test-aave/upgradeability.spec.ts", "test-suites/test-aave/variable-debt-token.spec.ts", "test-suites/test-aave/weth-gateway.spec.ts"],
"files": [ "files": [
"./hardhat.config.ts", "./hardhat.config.ts",
"./modules/tenderly/tenderly.d.ts", "./modules/tenderly/tenderly.d.ts",