TLDR:Documentation Index
Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
Use this file to discover all available pages before exploring further.
- Hardhat can spin up a local EVM node that mirrors a live mainnet’s state — perfect for testing DeFi interactions against real contracts without spending real funds.
- You need an archive node endpoint for the fork; full nodes don’t retain enough history.
- In Hardhat 3, declare the fork in
hardhat.config.tsunder a network of typeedr-simulatedand runnpx hardhat node. - Point MetaMask at
http://127.0.0.1:8545with chain ID31337to interact with the fork in a browser wallet.
Why fork mainnet?
A mainnet fork gives you a local EVM that starts from the real mainnet state — same contracts, same balances, same storage slots — and from that point onward runs only on your machine. It’s the standard way to:- Test DeFi interactions against deployed contracts (Uniswap, Aave, Curve, etc.) without paying gas.
- Reproduce a specific on-chain bug at the exact block where it happened.
- Simulate upgrades or whale movements before deploying changes.
Before you start
You need:- An archive node endpoint on the network you want to fork. Deploy an archive-mode node on Chainstack and copy the HTTPS endpoint. See View node access and credentials.
- Node.js installed locally.
-
A Hardhat project. If you don’t have one yet:
Configure the fork
In Hardhat 3, forking is declared as a network of typeedr-simulated in your config file:
Run the fork as a standalone node
Start a local JSON-RPC node that wallets and external scripts can connect to:8545. Any transaction you send updates only the local state; the live mainnet sees nothing.
Fork at a specific block
To reproduce historical state — for example, debugging an exploit at a known block — pin the fork by settingblockNumber in the forking config:
Run tests against the fork
To run your tests against the forked network, pass it with--network:
Connect MetaMask to the fork
Once the Hardhat node is running, point MetaMask at it:- Open MetaMask, click the network selector, and choose Add a network → Add a network manually.
-
Fill in:
Field Value Network name Hardhat forkNew RPC URL http://127.0.0.1:8545Chain ID 31337Currency symbol ETH - Click Save, then switch MetaMask to the Hardhat fork network.
Hardhat 2 syntax (legacy)
If your project is still on Hardhat 2, forking uses a CLI flag instead of the config-drivenedr-simulated network type:
http://127.0.0.1:8545, chain ID 31337).
Common gotchas
Error: missing trie node— your endpoint isn’t an archive node, or it doesn’t retain state at the block you’re forking from. Switch to an archive endpoint. See EVM node returns “Missing trie node”.- Slow fork startup. Hardhat fetches state on demand. Forking at HEAD means every test run racks up RPC calls. Pin to a fixed block whenever possible.
- Nonce errors after restart. Hardhat resets state when you stop the node — but MetaMask remembers nonces per-account. Reset the MetaMask activity (Settings → Advanced → Clear activity tab data) after each fork restart.