Introduction
Hardhat can fork an Ethereum network and run a local simulated Ethereum node based on EthereumJS.
This can be especially useful if you want to interact with the complex decentralized finance (DeFi) contracts deployed on the Ethereum mainnet in a local simulated environment.
Step-by-step
Deploy an Ethereum mainnet node with Chainstack
Deploy the node as described in Join a public network.
Get the RPC endpoint and access credentials of your deployed node as described in View node access and credentials.
Install Hardhat
Run:
npm install hardhat
Fork the Ethereum mainnet through the Chainstack node at the latest block
Run:
npx hardhat node --fork https://USERNAME:PASSWORD@RPC_ENDPOINT
where
- USERNAME — your Ethereum node access username.
- PASSWORD — your Ethereum node access password.
- RPC_ENDPOINT — your Ethereum node RPC endpoint.
Example:
npx hardhat node --fork https://user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com
A forked Ethereum mainnet node is now running locally and you can interact with it.
Fork the Ethereum mainnet through the Chainstack node at a specific block
Run:
npx hardhat node --fork https://USERNAME:PASSWORD@RPC_ENDPOINT --fork-block-number BLOCK_NUMBER
where
- USERNAME — your Ethereum node access username.
- PASSWORD — your Ethereum node access password.
- RPC_ENDPOINT — your Ethereum node RPC endpoint.
- BLOCK_NUMBER — the block number at which you want to fork.
Example:
npx hardhat node --fork https://user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com --fork-block-number 8116664
A forked Ethereum mainnet node is now running locally and you can interact with it.
See also Hardhat documentation.
Comments
0 comments
Please sign in to leave a comment.