This article applies to:
- Ethereum
- Polygon
- BNB Smart Chain
- Avalanche
- Fantom
- Harmony
Introduction
Hardhat can fork an EVM network and run a local simulated EVM node based on EthereumJS.
This can be especially useful if you want to interact with the complex decentralized finance (DeFi) contracts deployed on the EVM mainnet in a local simulated environment.
You need an archive node to fork the mainnet.
Step-by-step
Deploy an archive node with Chainstack
Deploy the node as described in Join a public network.
Get the HTTO endpoint of your deployed node as described in View node access and credentials.
Install Hardhat
Run:
npm install hardhat
Fork the mainnet through the Chainstack node at the latest block
Run:
npx hardhat node --fork HTTPS_ENDPOINT
where HTTPS_ENDPOINT is your node HTTPS endpoint.
Example:
npx hardhat node --fork https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531
A forked mainnet node is now running locally and you can interact with it.
Fork the mainnet through the Chainstack node at a specific block
Run:
npx hardhat node --fork HTTPS_ENDPOINT --fork-block-number BLOCK_NUMBER
where
- HTTPS_ENDPOINT — your node HTTPS endpoint.
- BLOCK_NUMBER — the block number at which you want to fork.
Example:
npx hardhat node --fork https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531 --fork-block-number 8116664
A forked mainnet node is now running locally and you can interact with it.
See also:
If you have any questions, feel free to contact Chainstack support by emailing support@chainstack.com or by submitting this form.
Comments
0 comments
Please sign in to leave a comment.