How to Fork Ethereum Mainnet With Hardhat
Samson Adesanoye
Posted on February 8, 2023
During the development or review of smart contracts, it may be necessary to evaluate your smart contracts against pre-existing smart contracts such as decentralized exchanges or flash loans without the cost of spending real ether.
In this article, you will learn how to fork Ethereum mainnet using Hardhat.
What is Hardhat?
Hardhat is an Ethereum development environment for professionals, it comes with tools such as Hardhat runner, network, and VSCode extension that make development flexible, extensible, and fast.
Why Fork Ethereum Mainnet?
- It allows manipulating the signer even if you don’t have access to its private key.
- It allows testing a contract with access controls, address is all needed to impersonate it.
- It allows testing mainnet smart contracts without using real ethers.
- It allows you to write script impersonating mainnet account.
Prerequisite:
Install node and npm
Create New Hardhat Project
We will be using a hardhat project, you can feel free to skip this if you already have.
- Create folder project: Open your terminal and enter the command below.
mkdir mainnet-fork-tutorial
cd mainnet-fork-tutorial
- Initiate npm in the project: Initialize npm in the created project.
npm init -y
- Install hardhat
npm install --save-dev hardhat
- In the same directory where you installed Hardhat run:
npx hardhat
Select create a JavaScript project
with your keyboard and hit enter, to create a fresh hardhat project; this might take time depending on your internet connection.
- Install dotenv: we will be using dotenv to store environment variables.
npm i dotenv
Create API Key
We need an remote procedure call (RPC) node, Hardhat recommends we make use Alchemy because they offer Full archive nodes; which are a type of node that contains all the information about a blockchain from the genesis or original block.
- Create an alchemy account: Create your free alchemy account at https://auth.alchemy.com/signup and confirm your email.
- Create an app: In your alchemy dashboard, click on + CREATE APP button. This brings a popup to create a new app. Fill in the name and description in the form and click CREATE APP button. Example:
- Name: mainnet-fork
- Description: Description of what your app does so you can keep track.
- Chain: Ethereum (default selection)
- Network: Mainnet (default selection)
- View key: Once your new app is created, the pop disappears. The app appeared under the Personal Apps table. Locate the newly created app in the table and click on the view key button. Copy the API KEY.
Fork Ethereum mainnet
Open the newly created mainnet-fork-tutorial project with your favorite editor.
- Create .env file: create a .env file in the root of mainnet-fork-tutorial project.
MAINNET_RPC_URL=your_app_api_url_from_alchemy
-
Edit hardhat.config.js: Edit
hardhat.config.js
we will be adding a new network called hardhat in module.exports object.
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
module.exports = {
solidity: "0.8.17",
networks: {
hardhat: {
forking: {
url: process.env.MAINNET_RPC_URL,
},
chainId: 1,
},
},
};
You can also pin the block number:
networks: {
hardhat: {
forking: {
url: process.env.MAINNET_RPC_URL,
blockNumber: 14390000
}
}
}
- Run Network: In your terminal, enter the command below to start hardhat node.
npx hardhat node
Conclusion
Now you have forked mainnet locally, you can take it a step further by deploying smart contract or Impersonate a mainnet Accounts With Hardhat, the use case is unlimited.
Resources:
Forking other networks | Ethereum development environment for professionals by Nomic Foundation
Posted on February 8, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.