Skip to main content

LocalNodeManager Class

Manages the lifecycle and state of a local Anvil Ethereum node for testing. Handles dynamic port allocation, process management, and exposes a rich set of methods for manipulating the blockchain state.

Constructor

Creates a new instance with the specified configuration.
NodeConfig
Optional node configuration options. See Configuration for details.

Properties

number
The allocated port number, or -1 if not started.
string
The RPC URL for the running node (e.g., http://localhost:12345).

Lifecycle Methods

start()

Starts the Anvil node with the configured options. Allocates a port, spawns the process, and waits for readiness.

stop()

Stops the running Anvil node and cleans up resources.

getPort()

Returns the allocated port number for this node instance, or null if not started.

State Management Methods

snapshot()

Takes a snapshot of the current chain state. Returns a snapshot ID for later use with revert().

revert()

Reverts the chain state to a previous snapshot.
string
required
The ID returned from snapshot()

reset()

Resets the chain state to the initial state or a specified fork block.
bigint
Optional block number to reset to (when in fork mode)

Mining Methods

mine()

Mines a specified number of blocks.
number
default:"1"
Number of blocks to mine

setAutomine()

Enables or disables automatic block mining.
boolean
required
Whether to enable automatic mining

Time Manipulation Methods

setNextBlockTimestamp()

Sets the timestamp for the next block.
number
required
Unix timestamp in seconds

increaseTime()

Increases chain time by the specified number of seconds.
number
required
Number of seconds to advance

setTime()

Sets absolute chain time.
number
required
Unix timestamp in seconds

Account Management Methods

getAccounts()

Returns an array of available account addresses.

setBalance()

Sets the balance for the specified address.
string
required
The address to modify
bigint
required
The new balance in wei

setNonce()

Sets the nonce for the specified address.
string
required
The address to modify
number
required
The new nonce value

impersonateAccount()

Enables impersonation of the specified account.
string
required
The address to impersonate

stopImpersonatingAccount()

Disables impersonation of the specified account.
string
required
The address to stop impersonating

Contract Methods

setCode()

Sets the contract code at the specified address.
string
required
The contract address
string
required
The contract bytecode (hex string)

setStorageAt()

Sets the storage value at the specified slot for a contract.
string
required
The contract address
string
required
The storage slot (hex string)
string
required
The value to store (hex string)

Gas Configuration Methods

setNextBlockBaseFeePerGas()

Sets the base fee for the next block (EIP-1559).
bigint
required
The base fee in wei

setMinGasPrice()

Sets the minimum gas price.
bigint
required
The minimum gas price in wei

Network Methods

setChainId()

Sets the chain ID.
number
required
The new chain ID

Next Steps