Skip to main content

StartedAnvilContainer

Returned by AnvilContainer.start() (and proxied by useAnvil()). StartedAnvilContainer<TAccount> extends Testcontainers' AbstractStartedContainer (getHost(), getMappedPort(), …).

Test client​

MemberDescription
testClientthe test client (alias: client)
cheatsa new cheats chain on testClient
testClientFor(account)a client with the same settings acting as an address (anvil signs) or a viem LocalAccount (TestClientAccountInput)
createTestClient(options)a client with its own TestClientOptions, validated against the node
testClientConfigurationthe test client settings in effect

Connection​

MemberDescription
rpcUrl / wsUrlHTTP / WebSocket JSON-RPC URLs reachable from the host
chainId / chainchain id (follows setChainId()) and a viem Chain for your own clients
configurationthe AnvilConfiguration anvil was started with
rpc(method, params?)raw JSON-RPC request

Accounts​

MemberDescription
addresses()dev account addresses (eth_accounts), checksummed
accounts()AnvilAccount[]: index, address, privateKey when known
getBalance(address)balance in wei
setBalance(address, wei) / setBalanceEther(address, ether)anvil_setBalance
impersonate(address) / stopImpersonating(address)anvil_impersonateAccount / anvil_stopImpersonatingAccount
withImpersonation(address, fn)runs fn(client) while impersonating; always stops
setAutoImpersonate(enabled)anvil_autoImpersonateAccount

Transactions​

MemberDescription
confirm(hash | Promise<hash>, { throwOnRevert?, timeout? })receipt; mines first when automine is off; throws AnvilTransactionRevertedError
sendEthTransaction(from, to, ether)sends Ether (decimal string) and returns the receipt
deployContract(abi, bytecode, from, args?)deploys and returns the receipt (contractAddress)

Mining and time​

MemberDescription
mine(blocks?, interval?)anvil_mine
getAutomine() / setAutomine(enabled)anvil_getAutomine / evm_setAutomine
setIntervalMining(duration)evm_setIntervalMining; 0 stops it
increaseTime(duration)evm_increaseTime; resolves to the total offset in seconds
setNextBlockTimestamp(timestamp)evm_setNextBlockTimestamp
latestTimestamp()timestamp of the latest block

Fees and limits​

MemberDescription
setMinGasPrice(wei)anvil_setMinGasPrice (pre-London hardforks only)
setNextBlockBaseFeePerGas(wei)anvil_setNextBlockBaseFeePerGas
setBlockGasLimit(gas)evm_setBlockGasLimit

State​

MemberDescription
snapshot() / revert(id)evm_snapshot / strict evm_revert (SNAPSHOT_NOT_FOUND)
withSnapshot(fn)runs fn(client) on a snapshot and always reverts
checkpoint()AnvilCheckpoint: { id, restore() }, restorable any number of times
reset(options?)fixed anvil_reset (anvil extras); discards snapshots
dumpState() / loadState(hex)anvil_dumpState / anvil_loadState
dumpStateToFile(path) / loadStateFromFile(path)the --load-state JSON format, host paths

Diagnostics​

MemberDescription
nodeInfo() / metadata()anvil_nodeInfo / anvil_metadata
setLoggingEnabled(enabled)anvil_setLoggingEnabled
logText(tail?)anvil's output so far

Artifacts​

MemberDescription
contractAbi(location) / contractBytecode(location)loaders relative to withArtifactsDir()

Lifecycle​

MemberDescription
stop(options?)stops; with a state dump configured, gracefully (DEFAULT_GRACEFUL_STOP_TIMEOUT_MS) and copies it to the host; closes WebSockets
isStoppedtrue after stop(); clients then reject with CONTAINER_STOPPED
removeStoppedContainer()protected: removes a container stopped with remove: false; override for non-Docker runtimes

Durations: seconds or { weeks, days, hours, minutes, seconds }; timestamps: seconds or a Date (Duration, Timestamp).