Skip to main content

How To Fork Ethereum Mainnet with Hardhat

Updated on
Dec 11, 2023

4 min read

Overview

Forking the chain at an older block of the blockchain is helpful if you want to simulate the blockchain’s state at that block; Hardhat has this functionality built in. In this guide, let’s go through the process of forking the Ethereum Mainnet at an older block.

Prerequisites

  • An Ethereum Full Archive node.
  • js installed on your system.
  • Any text editor.
  • CLI (Terminal/cmd).
  • Curiosity (assuming you don't have a cat).

Why Fork with Hardhat?

Hardhat is a one-stop solution for simulating an Ethereum environment locally; we can fork the Ethereum Mainnet using Hardhat and simulate the blockchain's complex operations at any given block. Using Hardhat, we can also develop smart contracts. It is a great tool to compile, test, debug, and deploy smart contracts. Learn how to write and deploy smart contracts in our in-house guide.

Because of the blockchain's rapid movement, testing can be complex. To circumvent this problem, you can fork the chain at a particular block before running any tests.

Every Ethereum client that runs a full node stores only the 128 most recent blocks. We need to fork the chain at older blocks for our purpose today. We'll need an Ethereum Full Archive node for this. See Full Node vs. Archive Node

Set Up Your QuickNode Ethereum Archive Endpoint

We could use any Ethereum client to boot our node, but Ethereum Archive data is enormous and may take days or even weeks to sync the whole chain data depending on the resources used. Instead, we will create a free QuickNode account here and easily create an Ethereum endpoint.

Select ETH as the chain, and Mainnet as the network. We will get Archive data out of the box with this endpoint.

Screenshot of Quicknode Ethereum endpoint

Copy and save the HTTP URL, we'll need it later.

Installing dependencies

Hardhat can be installed through npm, which comes with node.js.

To get started, first, let’s make sure we have node.js installed; type the following in your terminal/cmd:

node -v

It should return the installed version of node.js; if not installed, download it from node.js’s official website.

Create a new directory hardhat_demo, and make that directory your working directory:

mkdir hardhat_demo

cd hardhat_demo

Now to install Hardhat, use the following command:

npm install -d hardhat

After this, we'll need to install some dependencies that hardhat requires:

npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai

Let's initialize our Hardhat project:

npx hardhat

You’ll be greeted with a welcome message; select the second option, "Create an empty hardhat.config.js". This will create an empty Hardhat config file in your directory.

Note: If you want to develop scripts, and contracts select the first option. It'll create the necessary files.

Now that we have our Hardhat project set up let’s fork the Ethereum Mainnet using Hardhat.

npx hardhat node --fork <YOUR_QUICKNODE_URL_HERE>

Replace YOUR_QUICKNODE_URL_HERE with the node's HTTP URL, which we got in the previous section, and run the command. This will fork the Mainnet and expose the new JSON RPC HTTP and WebSocket endpoints at [http://127.0.0.1:8545/

](http://127.0.0.1:8545/)

[

](http://127.0.0.1:8545/)The other option here is to fork the chain at a particular block; this functionality here requires archive capabilities of the node. Let's fork the chain at block 12799760.

npx hardhat node --fork <YOUR_QUICKNODE_URL_HERE> --fork-block-number 12799760

Querying the Forked Chain

The forked chain's RPC server is listening at http://127.0.0.1:8545/. Let's try to make a cURL to the RPC to get the block number. Open a new terminal/cmd window and use the following command:

curl --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545 

As the above image shows, the output is 0xc34f10 which is the same block number at which we forked the chain, but in hexadecimal format. To convert it in decimal format, remove the 0x prefix and convert it on this website. The resulting conversion is 12799760.

Why Quicknode?

QuickNode offers access to Full Archive nodes with Archive mode add on, Following are more reasons to use QuickNode:

  1. A lot of other providers offer free calls up to 1M with archive data, but it is often seen that the performance is not up to the mark. According to our tests, where we forked the chain repeatedly, QuickNode consistently had the most performant results.
  2. As we move up towards higher volumes of calls QuickNode costs significantly less than other providers. For instance, QuickNode offers 20 million calls at $99 + $ 250 (for archive add on) = $349, whereas this cost for other providers run up a bill approximating $1134.

Conclusion

In this tutorial, we learned about Hardhat, how to install, make a new project with Hardhat, and how to fork Mainnet with Hardhat. Explore more Hardhat methods in their official documentation.

Learn how to fork Ethereum Mainnet with ganache

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)

Share this guide