Forking Mainnet or Testnets in Foundry: Writing Real-World Tests & Upgradable Smart Contracts Best Guide
Forking Mainnet or Testnets in Foundry: Writing Real-World Tests & Upgradable Smart Contracts Best Guide

Chapter 0x15: Forking Mainnet or Testnets in Foundry: Writing Real-World Tests & Upgradable Smart Contracts Best Guide

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.

So, let’s get started!

Forking Mainnet or Testnets in Foundry

Forking Mainnet or Testnets in Foundry
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

How to Fork Ethereum Blockchain with Foundry
How to Fork Ethereum Blockchain with Foundry

Step 1: Install Foundry

If you haven’t installed Foundry yet, you can do it by running:

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:

This command starts a local fork of Ethereum mainnet.

If you need a specific block, use:

Step 3: Interact with the Forked Chain

Now, we can use Cast to interact with contracts already deployed on Ethereum:

This queries the USDC contract for your balance – just like in a real Ethereum environment!

Writing Foundry Tests That Simulate Real-World Scenarios

Writing Foundry Tests That Simulate Real-World Scenarios
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:

Run the test with:

This ensures your smart contract behaves as expected in a real-world environment.

Upgradable Smart Contracts in Foundry

Upgradable Smart Contracts in Foundry
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:

  1. Proxy Pattern (UUPS Upgradeable)
  2. Transparent Proxy Pattern

Deploying an Upgradeable Smart Contract with Foundry

Let’s deploy an Upgradeable ERC20 Token using UUPS Upgradeability:

Upgrading a Smart Contract

When you want to upgrade the contract:

  1. Deploy the new implementation.
  2. Call the upgrade function from the proxy.

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.

Spread the love