📖 Scenario: You are a blockchain developer who wants to test smart contract interactions safely without spending real money or affecting the live network. You will create a simple script to connect to a mainnet fork, check an account balance, and simulate a transaction.
🎯 Goal: Build a Python script that connects to a mainnet fork using web3.py, sets up an account, checks its balance, and simulates sending Ether to another address.
📋 What You'll Learn
Create a Web3 connection to a local mainnet fork RPC URL
Set up an account with a private key
Check the account's Ether balance
Simulate sending 0.01 Ether to another address
Print the transaction hash of the simulated transaction
💡 Why This Matters
🌍 Real World
Mainnet forks let blockchain developers test their code exactly as if on the real Ethereum network but without spending real money or risking real assets.
💼 Career
Blockchain developers and smart contract engineers use mainnet forks daily to debug, test upgrades, and verify contract behavior before deploying to live networks.
Progress0 / 4 steps
1
Set up Web3 connection to mainnet fork
Import Web3 from web3 and create a w3 variable that connects to the local mainnet fork RPC URL http://127.0.0.1:8545 using Web3.HTTPProvider.
Blockchain / Solidity
Hint
Use Web3.HTTPProvider with the URL 'http://127.0.0.1:8545' to connect.
2
Set up account with private key
Create a variable private_key with the exact string '0x4c0883a69102937d6231471b5dbb6204fe512961708279a1a3e7e7a7a4e9f7a3'. Then create an account variable by calling w3.eth.account.from_key(private_key).
Blockchain / Solidity
Hint
Use the exact private key string and w3.eth.account.from_key to create the account.
3
Check account balance and prepare transaction
Get the balance of account.address using w3.eth.get_balance(account.address) and store it in balance. Then create a tx dictionary with keys: to set to '0x000000000000000000000000000000000000dead', value set to w3.to_wei(0.01, 'ether'), gas set to 21000, and gasPrice set to w3.to_wei(50, 'gwei'). Also add nonce with w3.eth.get_transaction_count(account.address).
Blockchain / Solidity
Hint
Use w3.eth.get_balance and build the transaction dictionary with the exact keys and values.
4
Sign and print transaction hash
Sign the transaction tx with account.sign_transaction(tx) and store it in signed_tx. Then print the transaction hash as a hex string using print(signed_tx.hash.hex()).
Blockchain / Solidity
Hint
Use account.sign_transaction(tx) and print the hex string of the hash with .hash.hex().
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
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 A
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
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.
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.
Final Answer:
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY -> Option A
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: