Marketplace has launched, further enabling blockchain developers! Learn more

How To Fork Ethereum Mainnet with Hardhat

September 23, 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 15

How to connect to Ethereum using .NET (Nethereum)
Originally Published On: Feb 20, 2021
Updated On: Sep 23, 2022

Dotnet or .NET is very popular... .NET is Microsoft’s alternative... C# is a modern Object-Oriented... Nethereum is the .Net... Nethereum requires .NET Core or... We could use pretty much any... Now go to your .NET app folder... Here we saw how we can connect...

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

Ethereum is a decentralized and... Ethereum is an open-sourced... Before we dive into Ethereum,... Ethereum... Ether (ETH) is the native... Kudos! You now have a better...

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

Transactions in Ethereum are... A transaction usually consists... Now let's take everything that... Start by opening up your... That's a wrap! In this guide, we...

Continue reading
How to build an Ethereum URL shortener dApp
Originally Published On: Jan 30, 2021
Updated On: Sep 23, 2022

dApps (decentralized... dApps or Decentralized... Since the dawn of the Internet,... For the purpose of this guide,... Make sure NodeJS and git are... Before building our dApp; first,... Start by creating a directory... Now, we need to set up our... Running the... Congratulations on building a...

Continue reading
How to integrate IPFS with Ethereum
Originally Published On: Apr 8, 2021
Updated On: Sep 23, 2022

It can be costly to store... The web that we use today is... Whenever someone wants to... Blockchains like Ethereum... Our first step here would be to... Now that we’ve published our... Head over to the To query the Ethereum... We’ll use Step 1: ipfs.js... So now that you know how to...

Continue reading
How to Fork Ethereum Blockchain with Ganache.
Originally Published On: Apr 8, 2021
Updated On: Sep 23, 2022

Forking and running a local... Ganache is an Ethereum developer... A fork in software development... We could use pretty much any... We’ll use To fork the mainnet, open your... Now, let’s get some information... Now that you have a local...

Continue reading
How to create and deploy a smart contract with Hardhat
Originally Published On: Jun 11, 2021
Updated On: Sep 23, 2022

Ethereum development... We’ll install hardhat using We’ll need an Ethereum... We’ll deploy our contract on the... To deploy our contract on... Open the hardhat.config.js file... Now, for our contract, create a... Now to deploy our contract,... Here we saw how to work with...

Continue reading
How to Fetch Ethereum Event Logs in Ruby
Originally Published On: Mar 6, 2021
Updated On: Sep 23, 2022

Ethereum log records are very... In event-driven languages like... eth.rb is a Ruby gem that makes... We’ll first deploy a smart... We could use pretty much any... Installing the eth.rb... Congrats! You've learned about...

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

While Ethereum has been trying... The first step on our journey to... We will need some Kovan test ETH... We will use the Create a javascript file named... I hope now you have a better...

Continue reading