Skip to content

The anvil_ methods (Anvil)

The anvil_ namespace: what a local test node offers to stage a chain, by setting accounts and state, mining, moving the clock and rewriting history.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const program = Effect.gen(function* () {
// an account anvil holds no key for and does not fund
yield* Anvil.setBalance(ADDRESS, 10n ** 18n);
yield* Anvil.mine(3);
const balance = yield* Eth.getBalance(ADDRESS, "latest");
const block = yield* Eth.blockNumber();
return { balance, block };
});
const staged = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// { balance: 1000000000000000000n, block: 3n }

The namespace is for a local test node: anvil serves it, a production node does not. A test gets a fresh node from AnvilNode: see Testing.

A method takes its JSON-RPC params in order and returns an Effect of the decoded result. The name is the wire name without its prefix: anvil_setBalance is Anvil.setBalance. Two names differ: anvil_mine_detailed is Anvil.mineDetailed, and Anvil.sendUnsignedTransaction is eth_sendUnsignedTransaction on the wire.

The names Ganache gave to some of these methods are in Evm.

Method JSON-RPC Returns Description
impersonateAccount anvil_impersonateAccount null Lets transactions be sent from an account the node has no key for.
stopImpersonatingAccount anvil_stopImpersonatingAccount null Stops impersonating an account.
autoImpersonateAccount anvil_autoImpersonateAccount null Impersonates every account, or stops doing so.
impersonateSignature anvil_impersonateSignature null Makes ecrecover answer an address for a signature.
sendUnsignedTransaction eth_sendUnsignedTransaction Hash Sends a transaction from any account, without a signature.
Method JSON-RPC Returns Description
setBalance anvil_setBalance null Sets the balance of an account, in wei.
addBalance anvil_addBalance null Adds to the balance of an account, in wei.
dealERC20 anvil_dealERC20 null Sets an account’s balance of an ERC-20 token.
setERC20Balance anvil_setERC20Balance null The same, under its other name.
setERC20Allowance anvil_setERC20Allowance null Sets an ERC-20 allowance.
setCode anvil_setCode null Sets the code of an account.
setNonce anvil_setNonce null Sets the nonce of an account.
setStorageAt anvil_setStorageAt boolean Writes one storage slot of an account.
Method JSON-RPC Returns Description
setChainId anvil_setChainId null Sets the chain id.
setCoinbase anvil_setCoinbase null Sets the address that receives block rewards.
setBlockGasLimit anvil_setBlockGasLimit boolean Sets the gas limit of the blocks to come.
setMinGasPrice anvil_setMinGasPrice null Sets the lowest gas price the node accepts. Refused once EIP-1559 is active.
setNextBlockBaseFeePerGas anvil_setNextBlockBaseFeePerGas null Sets the base fee of the next block.
setNextBlockPrevRandao anvil_setNextBlockPrevRandao null Sets the prevRandao of the next block.
setNextBlockParentBeaconBlockRoot anvil_setNextBlockParentBeaconBlockRoot null Sets the parent beacon block root of the next block.
Method JSON-RPC Returns Description
getAutomine anvil_getAutomine boolean Whether a block is mined as soon as a transaction arrives.
setAutomine anvil_setAutomine null Turns mining a block per transaction on or off.
getIntervalMining anvil_getIntervalMining number | null The seconds between two blocks, when blocks are mined on a timer.
setIntervalMining anvil_setIntervalMining null Mines a block every given number of seconds.
getMiningPaused anvil_getMiningPaused boolean Whether mining is paused.
setMiningPaused anvil_setMiningPaused null Pauses or resumes mining.
mine anvil_mine null Mines blocks, one by default.
mineDetailed anvil_mine_detailed MinedBlock[] Mines blocks and returns them, with what each transaction did.
Method JSON-RPC Returns Description
dropTransaction anvil_dropTransaction Hash | null Removes a transaction from the pool.
dropAllTransactions anvil_dropAllTransactions null Removes every transaction from the pool.
removePoolTransactions anvil_removePoolTransactions null Removes the transactions of one sender from the pool.

What waits in the pool is read with Txpool.

Method JSON-RPC Returns Description
getGenesisTime anvil_getGenesisTime bigint The timestamp of the genesis block, in seconds.
getLastBlockWallTime anvil_getLastBlockWallTime number The real time at which the latest block became the head, in milliseconds.
increaseTime anvil_increaseTime number Moves the clock forward by a number of seconds.
setTime anvil_setTime number Sets the clock to a timestamp.
setNextBlockTimestamp anvil_setNextBlockTimestamp null Sets the timestamp of the next block.
setBlockTimestampInterval anvil_setBlockTimestampInterval null Makes every block’s timestamp a fixed number of seconds after the one before.
removeBlockTimestampInterval anvil_removeBlockTimestampInterval boolean Removes that interval.
Method JSON-RPC Returns Description
snapshot anvil_snapshot Hex Saves the state of the chain and returns an id.
revert anvil_revert boolean Brings the chain back to a snapshot.
dumpState anvil_dumpState Hex The state of the chain as one blob.
loadState anvil_loadState boolean Merges a dumped state into the chain.
rollback anvil_rollback null Removes the latest blocks, one by default.
reorg anvil_reorg null Replaces the latest blocks with new ones.
Method JSON-RPC Returns Description
nodeInfo anvil_nodeInfo NodeInfo How the node is set up: its head, hard fork and fees, and the endpoint it forks.
metadata anvil_metadata NodeMetadata What identifies the node: its version, chain and head, its fork, its snapshots.
setLoggingEnabled anvil_setLoggingEnabled null Turns the node’s logging of requests on or off.
Method JSON-RPC Returns Description
setRpcUrl anvil_setRpcUrl null Sets the endpoint the node forks from.
reset anvil_reset null Starts the chain anew, as a fork or not.
Method JSON-RPC Returns Description
getBlobByHash anvil_getBlobByHash Hex | null A blob the node has seen, by its versioned hash.
getBlobsByTransactionHash anvil_getBlobsByTransactionHash Hex[] | null The blobs a transaction carried.
  • A setter mines nothing. setBalance, setCode, setNonce, setStorageAt and the chain and clock setters change the node at once and add no block.
  • State set by hand belongs to no block. rollback and reorg bring the state back to the last block they keep, and what a setter changed since that block is lost.
  • Most setters answer null. The effect succeeds with null when the node accepted the change. setStorageAt and setBlockGasLimit answer true instead.
  • Block numbers and timestamps are bigints, here as everywhere, also where anvil sends them as JSON numbers: getGenesisTime, currentBlockTimestamp in NodeInfo, latestBlockNumber in NodeMetadata.