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.
- 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
- Chainstack account with a Monad node endpoint
- Foundry installed
- Basic Solidity and command-line knowledge
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
- 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: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: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
Createtest/ForkTest.t.sol:
test/ForkTest.t.sol
Configure fork in foundry.toml
Add your RPC endpoint tofoundry.toml:
foundry.toml
Run fork tests
Fork from a specific block
For reproducible tests, pin to a specific block: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
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: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