# Table of Contents - [Anvil – foundry - Ethereum Development Framework](#anvil-foundry-ethereum-development-framework) - [Forking – foundry - Ethereum Development Framework](#forking-foundry-ethereum-development-framework) - [State management – foundry - Ethereum Development Framework](#state-management-foundry-ethereum-development-framework) - [Custom methods – foundry - Ethereum Development Framework](#custom-methods-foundry-ethereum-development-framework) --- # Anvil – foundry - Ethereum Development Framework [Skip to content](https://www.getfoundry.sh/anvil#vocs-content) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) Search... ⌘ K Anvil[](https://www.getfoundry.sh/anvil#anvil) ----------------------------------------------- Anvil is a fast local Ethereum node for development and testing. It runs entirely in-memory and supports forking from any EVM-compatible chain. ### Key capabilities[](https://www.getfoundry.sh/anvil#key-capabilities) | Feature | Description | | --- | --- | | **Local development** | Start a local node with pre-funded accounts and instant mining | | **Forking** | Fork mainnet or any chain at a specific block | | **State management** | Dump and load chain state for reproducible testing | | **Custom RPC methods** | Impersonate accounts, manipulate time, and control mining | | **Tracing** | Debug transactions with full execution traces | ### Common workflows[](https://www.getfoundry.sh/anvil#common-workflows) Start local nodeCustom accounts $ anvil Fork mainnetFork at block $ anvil --fork-url https://ethereum.reth.rs/rpc Auto-impersonate $ anvil --auto-impersonate ### Default accounts[](https://www.getfoundry.sh/anvil#default-accounts) Anvil generates 10 development accounts with 10,000 ETH each. The default mnemonic is: test test test test test test test test test test test junk You can customize accounts with `--accounts`, `--balance`, and `--mnemonic`. ### Learn more[](https://www.getfoundry.sh/anvil#learn-more) * [Forking](https://www.getfoundry.sh/anvil/forking) — Fork mainnet and other chains * [State management](https://www.getfoundry.sh/anvil/state-management) — Dump and load chain state * [Custom methods](https://www.getfoundry.sh/anvil/custom-methods) — Impersonation, mining control, and time manipulation Was this helpful? [Suggest changes on GitHub](https://github.com/foundry-rs/book/edit/master/vocs/src/pages/anvil/index.mdx) Copy page for AI Ask AI... ⌘ I --- # Forking – foundry - Ethereum Development Framework [Skip to content](https://www.getfoundry.sh/anvil/forking#vocs-content) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) Search... ⌘ K Forking[](https://www.getfoundry.sh/anvil/forking#forking) ----------------------------------------------------------- Anvil can fork any EVM-compatible chain, allowing you to test against real-world state without deploying to a live network. ### Basic forking[](https://www.getfoundry.sh/anvil/forking#basic-forking) $ anvil --fork-url https://ethereum.reth.rs/rpc You can also fork from a specific point in the chain's history: At blockAt transaction $ anvil --fork-url https://ethereum.reth.rs/rpc --fork-block-number 18000000 ### Chain configuration[](https://www.getfoundry.sh/anvil/forking#chain-configuration) When forking, Anvil inherits the chain ID from the remote endpoint by default. Override chain ID $ anvil --fork-url https://ethereum.reth.rs/rpc --chain-id 1337 Specify hardfork $ anvil --fork-url https://ethereum.reth.rs/rpc --hardfork cancun ### Rate limiting[](https://www.getfoundry.sh/anvil/forking#rate-limiting) RPC providers often rate-limit requests. Anvil handles this automatically but you can tune the behavior: Set compute units per second $ anvil --fork-url $RPC_URL --compute-units-per-second 100 Disable rate limiting $ anvil --fork-url $RPC_URL --no-rate-limit Set retry count $ anvil --fork-url $RPC_URL --retries 10 ### Caching[](https://www.getfoundry.sh/anvil/forking#caching) Anvil caches forked state to disk at `~/.foundry/cache/rpc///` to speed up subsequent runs. Disable storage caching $ anvil --fork-url $RPC_URL --no-storage-caching ### Custom headers[](https://www.getfoundry.sh/anvil/forking#custom-headers) Some RPC providers require authentication headers: Add custom header $ anvil --fork-url $RPC_URL --fork-header "Authorization: Bearer $TOKEN" ### L2 support[](https://www.getfoundry.sh/anvil/forking#l2-support) Anvil supports L2-specific features: Enable Optimism features $ anvil --fork-url https://mainnet.optimism.io --optimism Enable Celo features $ anvil --fork-url https://forno.celo.org --celo Was this helpful? [Suggest changes on GitHub](https://github.com/foundry-rs/book/edit/master/vocs/src/pages/anvil/forking.mdx) Copy page for AI Ask AI... ⌘ I --- # State management – foundry - Ethereum Development Framework [Skip to content](https://www.getfoundry.sh/anvil/state-management#vocs-content) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) Search... ⌘ K State management[](https://www.getfoundry.sh/anvil/state-management#state-management) -------------------------------------------------------------------------------------- Anvil can save and restore chain state, enabling reproducible test scenarios and faster development workflows. ### Dumping state[](https://www.getfoundry.sh/anvil/state-management#dumping-state) Save the current state when Anvil exits: Dump state on exit $ anvil --dump-state state.json Dump to a directory $ anvil --dump-state ./states/ ### Loading state[](https://www.getfoundry.sh/anvil/state-management#loading-state) Start from a previously saved state: Load from state file $ anvil --load-state state.json ### Combined state file[](https://www.getfoundry.sh/anvil/state-management#combined-state-file) Use `--state` to both load and dump from the same file: Persist state across restarts $ anvil --state state.json This loads the state on startup (if the file exists) and saves it on exit. ### Periodic snapshots[](https://www.getfoundry.sh/anvil/state-management#periodic-snapshots) Save state at regular intervals to prevent data loss: Dump state every 60 seconds $ anvil --state state.json --state-interval 60 ### Historical states[](https://www.getfoundry.sh/anvil/state-management#historical-states) By default, Anvil only keeps the latest state. Enable historical state preservation for debugging: Preserve historical states $ anvil --preserve-historical-states --dump-state state.json Limit persisted states $ anvil --max-persisted-states 100 ### Memory management[](https://www.getfoundry.sh/anvil/state-management#memory-management) For long-running instances, prune historical state to reduce memory usage: Prune history (keep 10 states) $ anvil --prune-history 10 Prune all history $ anvil --prune-history ### Genesis configuration[](https://www.getfoundry.sh/anvil/state-management#genesis-configuration) Initialize Anvil with a custom genesis file: Use custom genesis $ anvil --init genesis.json The genesis file follows the standard Ethereum genesis format with `alloc`, `difficulty`, `gasLimit`, and other fields. Was this helpful? [Suggest changes on GitHub](https://github.com/foundry-rs/book/edit/master/vocs/src/pages/anvil/state-management.mdx) Copy page for AI Ask AI... ⌘ I --- # Custom methods – foundry - Ethereum Development Framework [Skip to content](https://www.getfoundry.sh/anvil/custom-methods#vocs-content) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) [![Logo](https://www.getfoundry.sh/foundry-logo.png)](https://www.getfoundry.sh/) Search... ⌘ K Custom methods[](https://www.getfoundry.sh/anvil/custom-methods#custom-methods) -------------------------------------------------------------------------------- Anvil exposes custom RPC methods beyond the standard Ethereum JSON-RPC API. These methods are useful for testing scenarios that require account impersonation, time manipulation, or mining control. ### Account impersonation[](https://www.getfoundry.sh/anvil/custom-methods#account-impersonation) Impersonate any account to send transactions without its private key: Impersonate an account $ cast rpc anvil_impersonateAccount $ADDRESS Send transaction as impersonated account $ cast send $CONTRACT "transfer(address,uint256)" $TO $AMOUNT --from $ADDRESS --unlocked Stop impersonating $ cast rpc anvil_stopImpersonatingAccount $ADDRESS Start Anvil with auto-impersonation enabled: Enable auto-impersonation $ anvil --auto-impersonate ### Mining control[](https://www.getfoundry.sh/anvil/custom-methods#mining-control) Control when blocks are mined: Disable automatic mining $ anvil --no-mining Mine a single block $ cast rpc anvil_mine Mine multiple blocks $ cast rpc anvil_mine 10 Set block interval (seconds) $ anvil --block-time 12 Enable interval mining via RPC $ cast rpc evm_setIntervalMining 12 Resume automatic mining $ cast rpc evm_setAutomine true ### Time manipulation[](https://www.getfoundry.sh/anvil/custom-methods#time-manipulation) Control the blockchain's perception of time: Increase time by seconds $ cast rpc evm_increaseTime 3600 Set next block timestamp $ cast rpc evm_setNextBlockTimestamp 1700000000 Mine block with new timestamp $ cast rpc evm_mine ### State snapshots[](https://www.getfoundry.sh/anvil/custom-methods#state-snapshots) Create and restore chain state snapshots during a session: Create snapshot $ cast rpc evm_snapshot Revert to snapshot $ cast rpc evm_revert $SNAPSHOT_ID Snapshots return an ID that you use to revert. Reverting removes the snapshot. ### Balance and code manipulation[](https://www.getfoundry.sh/anvil/custom-methods#balance-and-code-manipulation) Modify account state directly: Set account balance $ cast rpc anvil_setBalance $ADDRESS 0xDE0B6B3A7640000 Set account nonce $ cast rpc anvil_setNonce $ADDRESS 0x10 Set contract code $ cast rpc anvil_setCode $ADDRESS $BYTECODE Set storage slot $ cast rpc anvil_setStorageAt $ADDRESS $SLOT $VALUE ### Chain configuration[](https://www.getfoundry.sh/anvil/custom-methods#chain-configuration) Modify chain parameters at runtime: Set chain ID $ cast rpc anvil_setChainId 31337 Set block gas limit $ cast rpc evm_setBlockGasLimit 0x1C9C380 Set next block base fee $ cast rpc anvil_setNextBlockBaseFeePerGas 0x5F5E100 ### Transaction pool[](https://www.getfoundry.sh/anvil/custom-methods#transaction-pool) Inspect and manage pending transactions: Get pending transactions $ cast rpc txpool_content Drop a transaction $ cast rpc anvil_dropTransaction $TX_HASH Drop all pending transactions $ cast rpc anvil_dropAllTransactions ### Tracing[](https://www.getfoundry.sh/anvil/custom-methods#tracing) Enable execution traces for debugging: Enable trace printing $ anvil --print-traces Enable step tracing for debug\_\* methods $ anvil --steps-tracing Was this helpful? [Suggest changes on GitHub](https://github.com/foundry-rs/book/edit/master/vocs/src/pages/anvil/custom-methods.mdx) Copy page for AI Ask AI... ⌘ I ---