Marketplace has launched, further enabling blockchain developers! Learn more

How To Fork Ethereum Mainnet with Hardhat

October 18, 2022

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

Booting Our Ethereum Archive Node

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. It's better to get an Ethereum node from QuickNode with archive mode add on; it gives us access to Full Archive Ethereum node instantly.

Sign up for a free trial QuickNode, select ETH as the chain, and Mainnet as the network. Don't forget to choose the archive mode to add on.

Screenshot of Ethereum Archive Add On


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:

installing dependencies

Copy
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:

installing dependencies

Copy
mkdir hardhat_demo

cd hardhat_demo
Now to install Hardhat, use the following command:

installing dependencies

Copy
npm install -d hardhat
After this, we'll need to install some dependencies that hardhat requires:

installing dependencies

Copy
npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
Let's initialize our Hardhat project:

installing dependencies

Copy
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.

installing dependencies

Copy
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/



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.



installing dependencies

Copy
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:

querying the forked chain

Copy
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.

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 :)

Related articles 17

Pending and Queued Transactions Explained
Published: Mar 18, 2022
Updated: Sep 23, 2022

The Ethereum network processes over one million transactions a day. These transactions originate from users wanting to transfer Ether or interact with smart contracts. In this guide, we will...

Continue reading
What is Ethereum?
Published: Sep 23, 2022
Updated: Sep 26, 2022

Ethereum is a decentralized and open-sourced blockchain network that allows anyone in the world to participate and interact with it. In this guide, you will learn what Ethereum is, how it...

Continue reading
What are Ethereum Transactions?
Published: Mar 28, 2022
Updated: Sep 23, 2022

Transactions in Ethereum are cryptographically signed data messages that contain a set of instructions. These instructions can interpret to sending Ether from one Ethereum account to another...

Continue reading
How to Send an EIP-1559 Transaction
Published: Dec 3, 2021
Updated: Sep 23, 2022

While Ethereum has been trying to scale, it has encountered some gas price issues. Many layer 2 solutions and sidechains sprang into existence to solve this problem, but Ethereum is the main...

Continue reading