Skip to content

Commit 77e12d5

Browse files
committed
fix: forking URL interference with unit tests
1 parent 9a5a20a commit 77e12d5

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ MAINNET_RPC_URL=http://localhost:8545
2828

2929
# RPC URL for Hardhat Network forking, required for running tests on mainnet fork with tracing (Infura, Alchemy, etc.)
3030
# https://hardhat.org/hardhat-network/docs/guides/forking-other-networks#forking-other-networks
31-
HARDHAT_FORKING_URL=https://eth.drpc.org
31+
FORK_RPC_URL=https://eth.drpc.org
3232

3333
# https://docs.lido.fi/deployed-contracts
3434
MAINNET_LOCATOR_ADDRESS=0xC1d0b3DE6792Bf6b4b37EccdcC24e45978Cfd2Eb

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ This is the most common method for running integration tests. It uses an instanc
327327
mainnet environment, allowing you to run integration tests with trace logging.
328328
329329
> [!NOTE]
330-
> Ensure that `HARDHAT_FORKING_URL` is set to Ethereum Mainnet RPC and `MAINNET_*` environment variables are set in the
330+
> Ensure that `FORK_RPC_URL` is set to Ethereum Mainnet RPC and `MAINNET_*` environment variables are set in the
331331
> `.env` file (refer to `.env.example` for guidance). Otherwise, the tests will run against the Scratch deployment.
332332
333333
```bash

globals.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
declare namespace NodeJS {
22
export interface ProcessEnv {
3-
/* iternal logging verbosity (used in scratch deploy / integration tests) */
3+
/* internal logging verbosity (used in scratch deploy / integration tests) */
44
LOG_LEVEL?: "all" | "debug" | "info" | "warn" | "error" | "none"; // default: "info"
55

66
/**
77
* Flags for changing the behavior of the Hardhat Network
88
*/
99

10-
/* RPC URL for Hardhat Network forking, required for running tests on mainnet fork with tracing */
11-
HARDHAT_FORKING_URL?: string;
10+
/* Test execution mode: 'scratch' for fresh network, 'fork' for forked network */
11+
MODE?: "scratch" | "forking"; // default: "scratch"
12+
13+
/* URL of the network to fork from */
14+
FORK_RPC_URL?: string; // default: "https://eth.drpc.org"
1215

1316
/**
1417
* Flags for changing the behavior of the integration tests

hardhat.helpers.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ import { existsSync, readFileSync } from "node:fs";
22

33
/* Determines the forking configuration for Hardhat */
44
export function getHardhatForkingConfig() {
5-
const forkingUrl = process.env.HARDHAT_FORKING_URL || "";
5+
const mode = process.env.MODE || "scratch";
66

7-
if (!forkingUrl) {
8-
// Scratch deploy, need to disable CSM
9-
process.env.INTEGRATION_ON_SCRATCH = "on";
10-
process.env.INTEGRATION_WITH_CSM = "off";
11-
return undefined;
12-
}
7+
switch (mode) {
8+
case "scratch":
9+
process.env.INTEGRATION_ON_SCRATCH = "on";
10+
process.env.INTEGRATION_WITH_CSM = "off";
11+
return undefined;
12+
13+
case "forking":
14+
if (!process.env.FORK_RPC_URL) {
15+
throw new Error("FORK_RPC_URL must be set when MODE=forking");
16+
}
17+
return { url: process.env.FORK_RPC_URL };
1318

14-
return { url: forkingUrl };
19+
default:
20+
throw new Error("MODE must be either 'scratch' or 'forking'");
21+
}
1522
}
1623

1724
// TODO: this plaintext accounts.json private keys management is a subject

lib/protocol/context.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import hre from "hardhat";
44
import { deployScratchProtocol, deployUpgrade, ether, findEventsWithInterfaces, impersonate, log } from "lib";
55

66
import { discover } from "./discover";
7-
import { isNonForkingHardhatNetwork } from "./networks";
87
import { provision } from "./provision";
98
import { ProtocolContext, ProtocolContextFlags, ProtocolSigners, Signer } from "./types";
109

@@ -14,8 +13,11 @@ const getSigner = async (signer: Signer, balance = ether("100"), signers: Protoc
1413
};
1514

1615
export const getProtocolContext = async (): Promise<ProtocolContext> => {
17-
if (isNonForkingHardhatNetwork()) {
18-
await deployScratchProtocol(hre.network.name);
16+
if (hre.network.name === "hardhat") {
17+
const networkConfig = hre.config.networks[hre.network.name];
18+
if (!networkConfig.forking?.enabled) {
19+
await deployScratchProtocol(hre.network.name);
20+
}
1921
} else {
2022
await deployUpgrade(hre.network.name);
2123
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
"test:trace": "hardhat test test/**/*.test.ts --trace --disabletracer",
2323
"test:fulltrace": "hardhat test test/**/*.test.ts --fulltrace --disabletracer",
2424
"test:watch": "hardhat watch",
25-
"test:integration": "hardhat test test/integration/**/*.ts",
26-
"test:integration:trace": "hardhat test test/integration/**/*.ts --trace --disabletracer",
27-
"test:integration:fulltrace": "hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
28-
"test:integration:scratch": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts",
29-
"test:integration:scratch:trace": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts --trace --disabletracer",
30-
"test:integration:scratch:fulltrace": "HARDHAT_FORKING_URL= INTEGRATION_WITH_CSM=off INTEGRATION_WITH_SCRATCH_DEPLOY=on hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
25+
"test:integration": "MODE=forking hardhat test test/integration/**/*.ts",
26+
"test:integration:trace": "MODE=forking hardhat test test/integration/**/*.ts --trace --disabletracer",
27+
"test:integration:fulltrace": "MODE=forking hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
28+
"test:integration:scratch": "MODE=scratch hardhat test test/integration/**/*.ts",
29+
"test:integration:scratch:trace": "MODE=scratch hardhat test test/integration/**/*.ts --trace --disabletracer",
30+
"test:integration:scratch:fulltrace": "MODE=scratch hardhat test test/integration/**/*.ts --fulltrace --disabletracer",
3131
"test:integration:fork:local": "hardhat test test/integration/**/*.ts --network local",
3232
"test:integration:fork:mainnet": "hardhat test test/integration/**/*.ts --network mainnet-fork",
3333
"test:integration:fork:mainnet:custom": "hardhat test --network mainnet-fork",

0 commit comments

Comments
 (0)