| name | anvil-node |
| description | Run local Ethereum nodes with Anvil. Use when setting up local development environments, forking mainnet for testing, or running integration tests against a local node. |
Anvil Local Node
Anvil is Foundry's fast local Ethereum node for development and testing.
When to Use
- Local development and testing
- Forking mainnet or other networks
- Integration testing with frontend applications
- Testing transaction flows before mainnet
- Simulating complex scenarios
Quick Start
anvil
anvil --port 8545
anvil --accounts 5 --balance 1000
Default Configuration
When started without flags:
| Setting | Value |
|---|
| Port | 8545 |
| Accounts | 10 |
| Balance | 10000 ETH each |
| Chain ID | 31337 |
| Block time | Instant (mines on each tx) |
Pre-Funded Accounts
Default test accounts (deterministic):
Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
Account #2: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
Forking
Fork Mainnet
anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/$API_KEY
anvil --fork-url $MAINNET_RPC --fork-block-number 18000000
Fork Other Networks
anvil --fork-url https://arb1.arbitrum.io/rpc
anvil --fork-url https://mainnet.optimism.io
anvil --fork-url https://mainnet.base.org
anvil --fork-url https://polygon-rpc.com
Configuration Options
Account Settings
anvil --accounts 20
anvil --balance 100000
anvil --mnemonic "test test test test test test test test test test test junk"
anvil --derivation-path "m/44'/60'/0'/0/"
Network Settings
anvil --chain-id 1337
anvil --port 8546
anvil --host 0.0.0.0
anvil --gas-limit 30000000
anvil --gas-price 1000000000
Mining Settings
anvil
anvil --block-time 12
anvil --no-mining
State Management
anvil --load-state state.json
anvil --dump-state state.json
anvil --state ./anvil-state
Forking Features
See forking.md for detailed patterns.
Impersonating Accounts
anvil --fork-url $RPC --unlocked 0xWhaleAddress
Or via RPC:
cast rpc anvil_impersonateAccount 0xWhaleAddress
cast send 0xRecipient --value 100ether \
--from 0xWhaleAddress \
--unlocked
cast rpc anvil_stopImpersonatingAccount 0xWhaleAddress
Time Manipulation
cast rpc evm_increaseTime 86400
cast rpc evm_setNextBlockTimestamp 1700000000
cast rpc evm_mine
Block Manipulation
cast rpc anvil_mine 10
cast rpc evm_setBlockGasLimit 50000000
RPC Methods
Standard Ethereum RPC
All standard Ethereum JSON-RPC methods are supported.
Anvil-Specific Methods
cast rpc anvil_mine [numBlocks]
cast rpc anvil_setBalance 0xAddress 0x8ac7230489e80000
cast rpc anvil_setCode 0xAddress 0x608060...
cast rpc anvil_setStorageAt 0xContract 0x0 0x1
cast rpc anvil_impersonateAccount 0xAddress
SNAPSHOT_ID=$(cast rpc evm_snapshot)
cast rpc evm_revert $SNAPSHOT_ID
cast rpc anvil_setNonce 0xAddress 100
cast rpc anvil_setChainId 1
Integration with Forge
Fork Testing
contract ForkTest is Test {
function setUp() public {
// Fork mainnet at specific block
vm.createSelectFork("mainnet", 18000000);
}
function test_ForkState() public {
// Tests run against forked state
IERC20 usdc = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
assertGt(usdc.totalSupply(), 0);
}
}
Running Against Anvil
anvil --fork-url $MAINNET_RPC
forge test --rpc-url http://localhost:8545
Common Use Cases
Frontend Development
anvil --fork-url $MAINNET_RPC --dump-state ./state.json
Integration Testing
anvil --fork-url $MAINNET_RPC &
forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --broadcast
forge test --rpc-url http://localhost:8545
pkill anvil
Debugging Transactions
anvil --fork-url $RPC --fork-block-number 17999999
cast send --rpc-url http://localhost:8545 ...
cast run $TX_HASH --rpc-url http://localhost:8545
Best Practices
- Use specific fork blocks for reproducible tests
- Save state for faster restarts during development
- Use unlocked accounts for impersonation testing
- Set appropriate block time for time-sensitive tests
- Monitor memory usage when forking large state
Troubleshooting
Connection Issues
curl http://localhost:8545 -X POST -H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
Memory Issues
anvil --fork-url $RPC --fork-retry-backoff 1000
anvil --fork-url $RPC --fork-block-number 18000000
RPC Rate Limits
anvil --fork-url $RPC --fork-retry-backoff 1000