Skip to main content
This tutorial teaches you how to set up a local Monad fork using Foundry’s Anvil. You’ll learn to test smart contracts against real mainnet state without spending gas or relying on testnet availability.
Get your own node endpoint todayStart for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.
TLDR:
  • Fork Monad mainnet locally using Anvil and a Chainstack RPC endpoint
  • Run tests against real on-chain state without spending gas
  • Impersonate any account to test interactions with deployed contracts
  • Avoid testnet instability and re-genesis disruptions

Prerequisites

Why fork locally?

Testing on public testnets comes with challenges:
  • Testnet resets: Networks occasionally undergo re-genesis, wiping all deployed contracts and balances
  • Faucet limitations: Getting test tokens can be slow or rate-limited
  • Shared state: Other developers’ transactions can interfere with your tests
  • Network issues: Testnets may experience downtime or congestion
A local fork solves these problems by copying the mainnet state to your machine. You get:
  • Reproducible tests: Same state every time you run tests
  • Instant execution: No network latency, no waiting for blocks
  • Free transactions: No gas costs for testing
  • Account impersonation: Test as any address, including whales and protocols

Set up the fork

Install Foundry

If you haven’t installed Foundry yet:

Start the fork

Launch Anvil with your Chainstack Monad endpoint:
You’ll see output like:
Anvil provides 10 pre-funded accounts with 10,000 ETH each (displayed as ETH but these are MON on Monad forks).

Fork configuration options

Customize your fork with additional flags:

Interact with forked state

With the fork running, you can query real mainnet data using the local RPC.

Query account balances

Read contract state

Send transactions

Use one of Anvil’s pre-funded accounts:
The private key above is Anvil’s first default account—safe to use in local testing.

Impersonate accounts

One of the most powerful features of local forking is account impersonation. You can send transactions as any address without needing its private key.

Impersonate a whale

Test protocol interactions

Impersonation is useful for testing how your contract interacts with existing protocols:

Write fork tests with Forge

Foundry’s Forge test framework has built-in fork testing support.

Create a test file

Create test/ForkTest.t.sol:
test/ForkTest.t.sol

Configure fork in foundry.toml

Add your RPC endpoint to foundry.toml:
foundry.toml

Run fork tests

Fork from a specific block

For reproducible tests, pin to a specific block:
Or in foundry.toml:
foundry.toml

Advanced fork techniques

Snapshot and revert

Save and restore fork state during tests:

Manipulate block properties

Mock contract calls

Example: Testing a DEX interaction

Here’s a complete example testing a swap on a forked DEX:
test/DexForkTest.t.sol
Run the test:

Troubleshooting

Fork is slow to start

Large state can take time to cache. Subsequent runs are faster. You can also:

RPC rate limiting

If you hit rate limits, Chainstack paid plans offer higher limits. You can also:

State mismatch errors

If contract state doesn’t match expectations, ensure you’re forking from the correct block:

Transaction reverts unexpectedly

Enable tracing to debug:
The extra v flags show detailed call traces.

Next steps

Now that you can test against forked mainnet state, you can:
  • Build integration tests for complex protocol interactions
  • Test upgrades against production contract state
  • Debug mainnet transaction failures locally
Last modified on June 22, 2026