Fork testing lets you try out changes on a copy of the real blockchain. This helps you check if your code works without risking real money.
Fork testing (mainnet fork) in Blockchain / Solidity
npx hardhat node --fork <MAINNET_RPC_URL> --fork-block-number <BLOCK_NUMBER>
Replace <MAINNET_RPC_URL> with your Ethereum mainnet provider URL.
The --fork-block-number option lets you pick a specific block to fork from.
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_INFURA_KEY
npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/YOUR_ALCHEMY_KEY --fork-block-number 17000000This script runs on a forked mainnet. It shows your account address, balance, and reads the name of the DAI token from the real contract.
import { ethers } from "hardhat"; async function main() { const [deployer] = await ethers.getSigners(); console.log("Using account:", deployer.address); // Check balance on forked mainnet const balance = await deployer.getBalance(); console.log("Balance:", ethers.utils.formatEther(balance), "ETH"); // Example: Interact with an existing contract on fork const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI token const daiAbi = ["function name() view returns (string)"]; const dai = new ethers.Contract(daiAddress, daiAbi, deployer); const name = await dai.name(); console.log("DAI token name:", name); } main().catch((error) => { console.error(error); process.exitCode = 1; });
Fork testing requires a mainnet RPC URL from providers like Infura or Alchemy.
Balances and contract states reflect the chosen block number at fork time.
Transactions on the fork do not affect the real mainnet and cost no real ETH.
Fork testing creates a safe copy of the mainnet blockchain for testing.
It helps you try code with real blockchain data without risk.
Use it to debug, test upgrades, and simulate real user interactions.
Practice
mainnet fork in blockchain development?Solution
Step 1: Understand what a mainnet fork is
A mainnet fork is a copy of the live blockchain used for testing.Step 2: Identify the purpose of testing on a fork
It allows developers to try code changes safely without risking the real network.Final Answer:
To safely test code using real blockchain data without affecting the live network -> Option AQuick Check:
Fork testing = safe real-data testing [OK]
- Thinking a fork changes the live blockchain
- Confusing fork with creating a new coin
- Assuming fork speeds up mining
Solution
Step 1: Recall Hardhat command for forking
The command to start a local node forked from mainnet includesnode --forkwith a mainnet RPC URL.Step 2: Match the correct syntax
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY usesnpx hardhat node --forkwith a mainnet URL, which is correct.Final Answer:
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY -> Option AQuick Check:
Fork command = hardhat node --fork [OK]
- Using compile or deploy commands to fork
- Missing the --fork flag
- Not providing a mainnet RPC URL
const { ethers } = require("hardhat");
async function main() {
const balance = await ethers.provider.getBalance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e");
console.log(ethers.utils.formatEther(balance));
}
main();What will this script output when run on a mainnet fork?
Solution
Step 1: Understand mainnet fork data availability
A mainnet fork copies the live blockchain state, including balances.Step 2: Analyze the script's balance query
The script queries the balance of a known mainnet address, so it returns the real Ether amount.Final Answer:
The Ether balance of the address on the live mainnet -> Option BQuick Check:
Fork has live data = real balance [OK]
- Assuming fork has empty state
- Confusing mainnet fork with testnet
- Thinking address format is invalid
Invalid JSON RPC response. What is the most likely cause?Solution
Step 1: Understand the error context
"Invalid JSON RPC response" usually means the node cannot connect to the RPC endpoint.Step 2: Identify common causes
Not providing a valid mainnet RPC URL or missing API key causes this error.Final Answer:
You forgot to provide a valid mainnet RPC URL with an API key -> Option DQuick Check:
Missing RPC URL/API key = JSON RPC error [OK]
- Blaming Hardhat version without checking URL
- Assuming address errors cause RPC failure
- Ignoring internet connectivity issues
Solution
Step 1: Fork mainnet and deploy old contract
Start by creating a mainnet fork and deploying the current contract version to replicate live state.Step 2: Run upgrade scripts and test new behavior
Apply upgrade scripts on the fork and test the new contract to ensure correctness without risk.Final Answer:
Fork mainnet locally, deploy old contract, run upgrade scripts, test new contract behavior -> Option CQuick Check:
Fork + old contract + upgrade + test = safe upgrade [OK]
- Deploying new contract directly on mainnet
- Ignoring old contract state in tests
- Skipping mainnet fork and testing only on testnet
Ready to prove it?
Pass the test and this pattern moves unknown
