Skip to main content

anvil behaviours worth knowing

Reset​

A bare anvil_reset detaches a forked node, and viem's reset() fails on a non-forked one. The library's reset() returns to genesis or re-forks at the fork block; both discard snapshots.

Forks​

A fork's hardfork follows the chain id and block: a local "mainnet" (id 1) at block 3 gets an ancient one, so pin evm.withHardfork().

Chain id​

After setChainId(), anvil's dev-account signer keeps the old chain id: node-signed transactions fail; local, impersonated and unsigned ones work.

Watchers​

Watchers subscribe asynchronously with no "ready" signal: repeat the trigger until observed. watchBlocks over WebSocket may report blocks out of order.

More​

BehaviourConsequence
Writes signed by anvil are mined even when they revertcheck the receipt (confirm() throws) or simulate first
With automine, anvil returns the hash just before the block is committedconfirm() re-reads the receipt with a short backoff
Writes signed locally are estimated firstthe revert is thrown before anything is sent
--order fees ranks pending transactions by maxFeePerGasraise the fee cap, not only the tip, to be mined first
Reverting a snapshot keeps pending transactionsuseAnvil() empties the pool; do it yourself around withSnapshot()
Snapshots don't restore node settingsrestore them in finally
setMinGasPrice() is rejected while EIP-1559 is activeuse a pre-London hardfork, or setNextBlockBaseFeePerGas()
Receipts carry contractAddress in lower casenormalise with getAddress() before comparing
Two copies of viem (e.g. npm link)match errors by name, not instanceof
A transaction with a nonce gap waits in the queued poolit is mined once the gap is filled
Mixed mining reports getAutomine() === falsetransactions are still mined on arrival
JSON ABIs are typed as Abinarrow readContract results, or use parseAbiItem
Every genesis alloc entry needs a balanceadd "balance": "0x0" to contract entries
EIP-5792 sendCalls, simulateCalls asset tracingnot supported by anvil 1.6