Forking Ethereum Mainnet: A Comprehensive Guide for Testing with Hardhat

What, Why and How?

Aapsi Khaira
Coinmonks
Published in
3 min readMay 16, 2024

--

What is Mainnet Forking?

Mainnet forking involves creating a local copy of the Ethereum mainnet blockchain for development and testing purposes. Tools like Hardhat enable developers to replicate the mainnet’s state and behavior on their machines without affecting the actual network or spending real ether.

  1. Replicating Mainnet State: Duplicate the Ethereum mainnet’s state, including smart contracts, account balances, and transaction history, for experimentation and testing.
  2. Controlled Testing Environment: Gain full control over the blockchain environment to simulate different network conditions and test applications under various scenarios.

Why Fork Ethereum Mainnet?

Forking the Ethereum mainnet provides several advantages for smart contract development and review:

  1. Cost-Effective Testing: Conduct thorough testing and validation without incurring transaction costs, reducing development expenses.
  2. Enhanced Security: Test contracts against existing protocols without risking real funds, identifying and mitigating vulnerabilities before deployment.
  3. Streamlined Development Workflow: Seamlessly test and debug smart contracts, automate testing scenarios, and iterate rapidly on code without live network constraints.
  4. Signer Manipulation & Access Control Testing: Forking the mainnet allows developers to manipulate signers without needing private keys, facilitating thorough testing of contracts with access controls. This includes impersonating addresses to assess contract behavior under various permission scenarios and scripting interactions from mainnet accounts for comprehensive testing.

How to Fork Ethereum Mainnet using Hardhat?

Step 1 : Setting up a Hardhat Project

Create a directory where you will instantiate a Hardhat project

mkdir hardhatFork

cd hardhatFork

Now, install Hardhat using npm in the terminal:

npm install --save-dev hardhat

In the same directory where you installed Hardhat run:

npx hardhat

Now, select from the options provided in the terminal, to either create a Typescript or a Javascript project.

Step 2: Create RPC endpoint for Ethereum Mainnet.

To fork the mainnet for testing you need to connect to an archive node. You can either use Alchemy or Quicknode or Infura to create an Ethereum Endpoint.

Step 3: Fork the Network

Install dotenv

npm install --save-dev dotenv

Create .env file in the root of our hardhatFork project.

ALCHEMY_RPC_URL = <YOUR_ALCHEMY_URL_HERE>

// OR

QUICKNODE_RPC_URL = <YOUR_QUICKNODE_URL_HERE>

Next, we need to modify the hardhat.config.js file. We will be adding a new network.

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import * as dotenv from "dotenv";
dotenv.config();

const config: HardhatUserConfig = {
solidity: "0.8.25",
defaultNetwork: "hardhat",
networks: {
hardhat: {
forking: {
enabled: true,
url: `${process.env.ALCHEMY_RPC_URL}`,
blockNumber: 19832300,
},
chainId: 1,
},
},
};

export default config;

NOTE: If you do not have a blockNumber you can skip it. Also by default the enabled flag is set to “true” and hence can be skipped.

//Without blockNumber

networks: {
hardhat: {
forking: {
url: `${process.env.ALCHEMY_RPC_URL}`,
}
}
}

In your terminal, enter the command below to start hardhat node.

npx hardhat node

Yayy!! You have successfully forked ethereum mainnet in your local development network. You will see the following in your terminal.

In summary, with the mainnet forked locally, you can now explore additional functionalities such as deploying smart contracts or impersonating mainnet accounts using Hardhat. The possibilities are endless.

--

--

Aapsi Khaira
Coinmonks

Smart Contract Developer | Solidity | Ethereum