Skip to content

Commit 9b06eb1

Browse files
committed
feat: Improve readability of fork configuration. Support FORK_BLOCK_NUMBER enviroment variable.
1 parent e15bdec commit 9b06eb1

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

hardhat.config.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { HardhatUserConfig } from 'hardhat/types';
55
import { accounts } from './test-wallets.js';
66
import { eEthereumNetwork, eNetwork, ePolygonNetwork, eXDaiNetwork } from './helpers/types';
77
import { BUIDLEREVM_CHAINID, COVERAGE_CHAINID } from './helpers/buidler-constants';
8-
import { NETWORKS_RPC_URL, NETWORKS_DEFAULT_GAS, BLOCK_TO_FORK } from './helper-hardhat-config';
8+
import {
9+
NETWORKS_RPC_URL,
10+
NETWORKS_DEFAULT_GAS,
11+
BLOCK_TO_FORK,
12+
buildForkConfig,
13+
} from './helper-hardhat-config';
914

1015
require('dotenv').config();
1116

@@ -16,6 +21,7 @@ import 'hardhat-gas-reporter';
1621
import 'hardhat-typechain';
1722
import '@tenderly/hardhat-tenderly';
1823
import 'solidity-coverage';
24+
import { fork } from 'child_process';
1925

2026
const SKIP_LOAD = process.env.SKIP_LOAD === 'true';
2127
const DEFAULT_BLOCK_GAS_LIMIT = 12450000;
@@ -24,7 +30,6 @@ const HARDFORK = 'istanbul';
2430
const ETHERSCAN_KEY = process.env.ETHERSCAN_KEY || '';
2531
const MNEMONIC_PATH = "m/44'/60'/0'/0";
2632
const MNEMONIC = process.env.MNEMONIC || '';
27-
const FORK = process.env.FORK || '';
2833

2934
// Prevent to load scripts before compilation and typechain
3035
if (!SKIP_LOAD) {
@@ -57,15 +62,7 @@ const getCommonNetworkConfig = (networkName: eNetwork, networkId: number) => ({
5762
},
5863
});
5964

60-
const forkMode = FORK
61-
? {
62-
url: NETWORKS_RPC_URL[FORK],
63-
...(FORK &&
64-
BLOCK_TO_FORK[FORK] && {
65-
blockNumber: BLOCK_TO_FORK[FORK],
66-
}),
67-
}
68-
: undefined;
65+
let forkMode;
6966

7067
const buidlerConfig: HardhatUserConfig = {
7168
solidity: {
@@ -114,7 +111,7 @@ const buidlerConfig: HardhatUserConfig = {
114111
privateKey: secretKey,
115112
balance,
116113
})),
117-
forking: forkMode,
114+
forking: buildForkConfig(),
118115
},
119116
buidlerevm_docker: {
120117
hardfork: 'berlin',

helper-hardhat-config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-ignore
2+
import { HardhatNetworkForkingUserConfig, HardhatUserConfig } from 'hardhat/types';
23
import {
34
eEthereumNetwork,
45
ePolygonNetwork,
@@ -11,9 +12,26 @@ require('dotenv').config();
1112
const INFURA_KEY = process.env.INFURA_KEY || '';
1213
const ALCHEMY_KEY = process.env.ALCHEMY_KEY || '';
1314
const TENDERLY_FORK_ID = process.env.TENDERLY_FORK_ID || '';
15+
const FORK = process.env.FORK || '';
16+
const FORK_BLOCK_NUMBER = process.env.FORK_BLOCK_NUMBER
17+
? parseInt(process.env.FORK_BLOCK_NUMBER)
18+
: 0;
1419

1520
const GWEI = 1000 * 1000 * 1000;
1621

22+
export const buildForkConfig = (): HardhatNetworkForkingUserConfig | undefined => {
23+
let forkMode;
24+
if (FORK) {
25+
forkMode = {
26+
url: NETWORKS_RPC_URL[FORK],
27+
};
28+
if (FORK_BLOCK_NUMBER || BLOCK_TO_FORK[FORK]) {
29+
forkMode.blockNumber = FORK_BLOCK_NUMBER || BLOCK_TO_FORK[FORK];
30+
}
31+
}
32+
return forkMode;
33+
};
34+
1735
export const NETWORKS_RPC_URL: iParamsPerNetwork<string> = {
1836
[eEthereumNetwork.kovan]: ALCHEMY_KEY
1937
? `https://eth-kovan.alchemyapi.io/v2/${ALCHEMY_KEY}`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"test-amm-scenarios": "npm run compile && TS_NODE_TRANSPILE_ONLY=1 hardhat test ./test-suites/test-amm/__setup.spec.ts test-suites/test-amm/scenario.spec.ts",
2424
"test-scenarios": "npm run compile && npx hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/scenario.spec.ts",
2525
"test-subgraph:scenarios": "npm run compile && hardhat --network hardhatevm_docker test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/subgraph-scenarios.spec.ts",
26-
"test:main:check-list": "npm run compile && FORK=true TS_NODE_TRANSPILE_ONLY=1 hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/mainnet/check-list.spec.ts",
26+
"test:main:check-list": "npm run compile && FORK=main TS_NODE_TRANSPILE_ONLY=1 hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/mainnet/check-list.spec.ts",
2727
"dev:coverage": "buidler compile --force && buidler coverage --network coverage",
2828
"aave:evm:dev:migration": "npm run compile && hardhat aave:dev",
2929
"aave:docker:full:migration": "npm run compile && npm run hardhat:docker -- aave:mainnet --skip-registry",

0 commit comments

Comments
 (0)