Forking Mainnet or Testnets in Foundry : If you’re diving deep into Ethereum development with Foundry, you’ve probably come across the concept of mainnet forking and writing tests that simulate real-world scenarios. In this guide, we’ll break down everything you need to know about forking the mainnet or testnets in Foundry, how to write effective tests, and deploy upgradable smart contracts.
Table of Contents
So, let’s get started!
Forking Mainnet or Testnets in Foundry

What is Mainnet Forking?
Before we jump into Foundry, let’s understand what mainnet forking is and why it’s useful.
Mainnet forking allows you to create a local copy of the Ethereum blockchain at a specific block. This enables developers to:
- Test their smart contracts using real-world state and data.
- Interact with live protocols like Uniswap, Aave, or Compound in a safe, simulated environment.
- Debug and analyze contract interactions before deploying on the mainnet.
With Foundry, we can fork the Ethereum blockchain in a few simple commands using Anvil and Cast.
How to Fork Ethereum Blockchain with Foundry

Step 1: Install Foundry
If you haven’t installed Foundry yet, you can do it by running:
curl -L https://foundry.paradigm.xyz | bash
foundryup
This installs Forge, Cast, and Anvil – essential tools for smart contract development and testing.
Step 2: Fork Ethereum Mainnet
To fork the Ethereum mainnet using Anvil, run:
anvil --fork-url https://mainnet.infura.io/v3/YOUR_INFURA_KEY
This command starts a local fork of Ethereum mainnet.
If you need a specific block, use:
anvil --fork-url https://mainnet.infura.io/v3/YOUR_INFURA_KEY --fork-block-number 17000000
Step 3: Interact with the Forked Chain
Now, we can use Cast to interact with contracts already deployed on Ethereum:
cast call 0xA0b86991C6218b36c1d19D4a2e9Eb0cE3606eB48 "balanceOf(address)" 0xYourAddress --rpc-url http://localhost:8545
This queries the USDC contract for your balance – just like in a real Ethereum environment!
Writing Foundry Tests That Simulate Real-World Scenarios

Why Write Forked Tests?
Writing tests on a forked mainnet allows you to:
- Simulate transactions on live DeFi protocols.
- Test how your smart contracts interact with real-world data.
- Ensure security and functionality before deploying.
Writing a Test in Foundry
Create a test file, e.g., ForkTest.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/Test.sol";
contract ForkTest is Test {
function setUp() public {
vm.createSelectFork("https://mainnet.infura.io/v3/YOUR_INFURA_KEY");
}
function testForkBalance() public {
address usdc = 0xA0b86991C6218b36c1d19D4a2e9Eb0cE3606eB48;
uint256 balance = IERC20(usdc).balanceOf(msg.sender);
console.log(balance);
}
}
Run the test with:
forge test
This ensures your smart contract behaves as expected in a real-world environment.
Upgradable Smart Contracts in Foundry

Why Use Upgradable Smart Contracts?
Upgradable contracts allow developers to fix bugs, enhance functionality, and add new features without requiring users to migrate assets manually.
The two most common upgradeability patterns are:
- Proxy Pattern (UUPS Upgradeable)
- Transparent Proxy Pattern
Deploying an Upgradeable Smart Contract with Foundry
Let’s deploy an Upgradeable ERC20 Token using UUPS Upgradeability:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
contract MyToken is UUPSUpgradeable {
function initialize() initializer public {
__UUPSUpgradeable_init();
}
function _authorizeUpgrade(address) internal override {}
}
Upgrading a Smart Contract
When you want to upgrade the contract:
- Deploy the new implementation.
- Call the upgrade function from the proxy.
function upgradeTo(address newImplementation) external {
_upgradeTo(newImplementation);
}
Now, your contract is safely upgraded without redeploying!
Conclusion
In this tutorial, we covered:
- Forking Ethereum mainnet with Foundry using Anvil and Cast.
- Writing tests that simulate real-world scenarios.
- Deploying and upgrading smart contracts using Foundry.
With this knowledge, you can confidently test your contracts in a real-world environment, improve security, and deploy upgradeable smart contracts with ease!
FAQs
How to fork a deployed smart contract on mainnet using Foundry?
Use vm.createSelectFork("RPC_URL")
in your Foundry test setup to fork the mainnet and interact with existing smart contracts.
How to test ERC1155Upgradeable contract in Foundry?
Use OpenZeppelin’s ERC1155Upgradeable contract and write tests using forge-std in Foundry.
What is the best way to upgrade a contract in Foundry?
The UUPS proxy pattern is efficient and commonly used. OpenZeppelin provides a secure implementation.
Is Foundry better than Hardhat for testing?
Foundry is faster and provides native Solidity debugging, making it an excellent choice for testing smart contracts.