Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Fork testing (mainnet fork) in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fork Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Hardhat fork setup code?
Consider this Hardhat config snippet for mainnet forking. What will be the output when running a script that fetches the balance of an address on the forked network?
Blockchain / Solidity
import { ethers } from "hardhat";

async function main() {
  const provider = ethers.provider;
  const balance = await provider.getBalance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e");
  console.log(ethers.formatEther(balance));
}

main();
ADisplays the ETH balance of the address on mainnet at the fork block
BThrows an error because the provider is not connected
CDisplays zero because the forked network is empty
DDisplays the balance in wei without formatting
Attempts:
2 left
💡 Hint
Remember that mainnet fork replicates the state at a specific block.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes mainnet fork testing?
Choose the correct description of mainnet fork testing in blockchain development.
AIt forks the mainnet to create a new public blockchain network.
BIt deploys contracts directly on the mainnet for live testing.
CIt simulates transactions without any real blockchain state.
DIt creates a local blockchain that replicates the mainnet state at a specific block for testing.
Attempts:
2 left
💡 Hint
Think about how developers test with real mainnet data safely.
🔧 Debug
advanced
2:00remaining
Why does this fork test script fail with 'invalid JSON RPC response'?
Given this Hardhat fork config and script, why does the script fail with 'invalid JSON RPC response' error? Config snippet: { forking: { url: "https://mainnet.infura.io/v3/INVALID_KEY", blockNumber: 15000000 } } Script tries to get block number from provider.
Blockchain / Solidity
const blockNumber = await ethers.provider.getBlockNumber();
console.log(blockNumber);
AThe script is missing await keyword causing a promise error.
BThe blockNumber is set to a future block that does not exist yet.
CThe Infura URL contains an invalid API key causing connection failure.
DThe provider is not initialized before calling getBlockNumber.
Attempts:
2 left
💡 Hint
Check the RPC URL and credentials.
📝 Syntax
advanced
1:30remaining
Which option correctly sets up a mainnet fork in Hardhat config?
Select the correct Hardhat config snippet to enable mainnet forking at block 16000000 using Alchemy RPC.
A{ forking: { url: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", blockNumber: 16000000 } }
B{ forking: { url: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", blockNumber: "16000000" } }
C{ forking: { endpoint: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", block: 16000000 } }
D{ fork: { url: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", block: 16000000 } }
Attempts:
2 left
💡 Hint
Check property names and syntax carefully.
🚀 Application
expert
2:30remaining
How many transactions are recorded after this fork test script runs?
You run a script on a mainnet fork that sends 3 transactions: 1. Transfer 1 ETH from account A to B 2. Deploy a contract 3. Call a function that reverts intentionally After the script finishes, how many transactions remain on the forked chain?
ANo transactions are recorded because it's a fork
BAll 3 transactions are recorded regardless of revert
COnly the deploy contract transaction is recorded
D2 transactions are recorded; the reverted one is discarded
Attempts:
2 left
💡 Hint
Think about how reverted transactions affect blockchain state.

Practice

(1/5)
1. What is the main purpose of creating a mainnet fork in blockchain development?
easy
A. To safely test code using real blockchain data without affecting the live network
B. To permanently change the main blockchain rules
C. To mine new blocks faster than the main network
D. To create a new cryptocurrency from scratch

Solution

  1. Step 1: Understand what a mainnet fork is

    A mainnet fork is a copy of the live blockchain used for testing.
  2. Step 2: Identify the purpose of testing on a fork

    It allows developers to try code changes safely without risking the real network.
  3. Final Answer:

    To safely test code using real blockchain data without affecting the live network -> Option A
  4. Quick Check:

    Fork testing = safe real-data testing [OK]
Hint: Mainnet fork = safe playground for real blockchain data [OK]
Common Mistakes:
  • Thinking a fork changes the live blockchain
  • Confusing fork with creating a new coin
  • Assuming fork speeds up mining
2. Which of the following commands is commonly used to create a mainnet fork with Hardhat?
easy
A. npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY
B. npx hardhat compile --fork mainnet
C. npx hardhat deploy --network mainnet
D. npx hardhat test --fork mainnet

Solution

  1. Step 1: Recall Hardhat command for forking

    The command to start a local node forked from mainnet includes node --fork with a mainnet RPC URL.
  2. Step 2: Match the correct syntax

    npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY uses npx hardhat node --fork with a mainnet URL, which is correct.
  3. Final Answer:

    npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY -> Option A
  4. Quick Check:

    Fork command = hardhat node --fork [OK]
Hint: Fork with 'hardhat node --fork' plus mainnet URL [OK]
Common Mistakes:
  • Using compile or deploy commands to fork
  • Missing the --fork flag
  • Not providing a mainnet RPC URL
3. Given this Hardhat script snippet for mainnet fork testing:
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?
medium
A. The balance of the address on a testnet like Ropsten
B. The Ether balance of the address on the live mainnet
C. An error because the address is invalid
D. Zero, because the fork has no data

Solution

  1. Step 1: Understand mainnet fork data availability

    A mainnet fork copies the live blockchain state, including balances.
  2. 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.
  3. Final Answer:

    The Ether balance of the address on the live mainnet -> Option B
  4. Quick Check:

    Fork has live data = real balance [OK]
Hint: Fork shows real mainnet balances, not zero or errors [OK]
Common Mistakes:
  • Assuming fork has empty state
  • Confusing mainnet fork with testnet
  • Thinking address format is invalid
4. You tried to fork mainnet using Hardhat but got the error: Invalid JSON RPC response. What is the most likely cause?
medium
A. You ran the fork command without internet connection
B. Your Hardhat version does not support forking
C. The address you queried does not exist
D. You forgot to provide a valid mainnet RPC URL with an API key

Solution

  1. Step 1: Understand the error context

    "Invalid JSON RPC response" usually means the node cannot connect to the RPC endpoint.
  2. Step 2: Identify common causes

    Not providing a valid mainnet RPC URL or missing API key causes this error.
  3. Final Answer:

    You forgot to provide a valid mainnet RPC URL with an API key -> Option D
  4. Quick Check:

    Missing RPC URL/API key = JSON RPC error [OK]
Hint: Check RPC URL and API key first for JSON RPC errors [OK]
Common Mistakes:
  • Blaming Hardhat version without checking URL
  • Assuming address errors cause RPC failure
  • Ignoring internet connectivity issues
5. You want to test a smart contract upgrade on a mainnet fork. Which sequence correctly describes the steps to do this safely?
hard
A. Fork mainnet, deploy new contract only, ignore old contract state
B. Deploy new contract on mainnet directly, then fork mainnet to test
C. Fork mainnet locally, deploy old contract, run upgrade scripts, test new contract behavior
D. Test upgrade only on testnet, no need to fork mainnet

Solution

  1. Step 1: Fork mainnet and deploy old contract

    Start by creating a mainnet fork and deploying the current contract version to replicate live state.
  2. 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.
  3. Final Answer:

    Fork mainnet locally, deploy old contract, run upgrade scripts, test new contract behavior -> Option C
  4. Quick Check:

    Fork + old contract + upgrade + test = safe upgrade [OK]
Hint: Fork, deploy old, upgrade, then test for safe contract upgrades [OK]
Common Mistakes:
  • Deploying new contract directly on mainnet
  • Ignoring old contract state in tests
  • Skipping mainnet fork and testing only on testnet
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard