anvil

package module
v0.0.0-...-c2eae0a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2025 License: MIT Imports: 9 Imported by: 0

README

go-anvil: Go Bindings for Anvil

Go Reference Go Report Card

go-anvil provides an easy way to use Anvil nodes from Go.

[!NOTE] go-anvil requires the foundy binary to be installed. See the foundry repository for installation instructions.

go get github.com/sprintertech/go-anvil

Getting Started

cli := anvil.New(
    anvil.WithPort(8545),
)

err := cli.Run()
if err != nil {
    print(err)	
} 

[!WARNING] This package is pre-1.0. There might be breaking changes between minor versions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an RPC client for anvil specific functions

func Dial

func Dial(addr string) (*Client, error)

Dial creates a new client for the given URL.

func NewClient

func NewClient(cli *rpc.Client) *Client

NewClient creates a new client with the given RPC client

func (*Client) DumpState

func (c *Client) DumpState() ([]byte, error)

DumpState returns a hex-encoded snapshot of the entire chain state.

Equivalent to the `anvil_dumpState` RPC call.

func (*Client) LoadState

func (c *Client) LoadState(state string) error

LoadState merges a previously dumped state into the current chain state.

Equivalent to the `anvil_loadState` RPC call.

func (*Client) NodeInfo

func (c *Client) NodeInfo() (*NodeInfo, error)

NodeInfo retrieves the current node configuration parameters.

Equivalent to the `anvil_nodeInfo` RPC call.

func (*Client) Reset

func (c *Client) Reset(forkURL string, block *big.Int) error

Reset resets the node state to the original or a new forked state.

Equivalent to the `anvil_reset` RPC call.

func (*Client) SetBalance

func (c *Client) SetBalance(account common.Address, balance *big.Int) error

SetBalance sets the balance of a given account.

Equivalent to the `anvil_setBalance` RPC call.

func (*Client) SetChainId

func (c *Client) SetChainId(id uint64) error

SetChainId sets the chain ID of the node.

Equivalent to the `anvil_setChainId` RPC call.

func (*Client) SetCode

func (c *Client) SetCode(account common.Address, code []byte) error

SetCode sets the EVM bytecode at the specified account.

Equivalent to the `anvil_setCode` RPC call.

func (*Client) SetCoinbase

func (c *Client) SetCoinbase(addr common.Address) error

SetCoinbase sets the coinbase address for block rewards.

Equivalent to the `anvil_setCoinbase` RPC call.

func (*Client) SetLoggingEnabled

func (c *Client) SetLoggingEnabled(enable bool) error

SetLoggingEnabled enables or disables logging output.

Equivalent to the `anvil_setLoggingEnabled` RPC call.

func (*Client) SetMinGasPrice

func (c *Client) SetMinGasPrice(price *big.Int) error

SetMinGasPrice sets the minimum gas price for the node.

Equivalent to the `anvil_setMinGasPrice` RPC call.

func (*Client) SetNextBlockBaseFeePerGas

func (c *Client) SetNextBlockBaseFeePerGas(fee *big.Int) error

SetNextBlockBaseFeePerGas sets the base fee for the next block.

Equivalent to the `anvil_setNextBlockBaseFeePerGas` RPC call.

func (*Client) SetNonce

func (c *Client) SetNonce(account common.Address, nonce uint64) error

SetNonce sets the transaction nonce for a given account.

Equivalent to the `anvil_setNonce` RPC call.

func (*Client) SetStorageAt

func (c *Client) SetStorageAt(account common.Address, slot common.Hash, value []byte) error

SetStorageAt writes a single storage slot at a given account.

Equivalent to the `anvil_setStorageAt` RPC call.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents an active Anvil client

func NewNode

func NewNode(opts ...Option) *Node

NewNode creates a new Node configured with the passed options

func (*Node) Start

func (n *Node) Start() error

Start starts the anvil node @TODO: should return available accounts on startup or store them

func (*Node) Stop

func (n *Node) Stop() error

Stop stops the anvil node

type NodeInfo

type NodeInfo struct {
	AvailableAccounts []string `json:"available_accounts"`
	BaseFee           string   `json:"base_fee"`
	GasLimit          string   `json:"gas_limit"`
	GasPrice          string   `json:"gas_price"`
	PrivateKeys       []string `json:"private_keys"`
	Wallet            Wallet   `json:"wallet"`
}

type Option

type Option []string

An Option configures a Node

func WithAllowOrigin

func WithAllowOrigin(origin string) Option

WithAllowOrigin sets the CORS allowed origin.

Equivalent to the `--allow-origin <ALLOW-ORIGIN>` flag

func WithBalance

func WithBalance(balance *big.Int) Option

WithBalance sets the initial balance of accounts.

Equivalent to the `--balance <BALANCE>` flag

func WithBaseFee

func WithBaseFee(fee int) Option

WithBaseFee sets the base fee in a block.

Equivalent to the `--base-fee <FEE>` flag

func WithBlockBaseFeePerGas

func WithBlockBaseFeePerGas(fee int) Option

WithBlockBaseFeePerGas sets the block base fee per gas.

Equivalent to the `--block-base-fee-per-gas <FEE>` flag

func WithBlockTime

func WithBlockTime(seconds int) Option

WithBlockTime sets the block time in seconds for interval mining.

Equivalent to the `-b, --block-time <BLOCK-TIME>` flag

func WithChainID

func WithChainID(id int) Option

WithChainID sets the chain ID.

Equivalent to the `--chain-id <CHAIN_ID>` flag

func WithCodeSizeLimit

func WithCodeSizeLimit(limit int) Option

WithCodeSizeLimit sets the EIP-170 code size limit in bytes.

Equivalent to the `--code-size-limit <CODE_SIZE>` flag

func WithComputeUnitsPerSecond

func WithComputeUnitsPerSecond(cups int) Option

WithComputeUnitsPerSecond sets the assumed compute units per second.

Equivalent to the `--compute-units-per-second <CUPS>` flag

func WithConfigOut

func WithConfigOut(path string) Option

WithConfigOut writes the Anvil output to a given file as JSON.

Equivalent to the `--config-out <OUT_FILE>` flag

func WithDerivationPath

func WithDerivationPath(path string) Option

WithDerivationPath sets the derivation path for HD wallets.

Equivalent to the `--derivation-path <DERIVATION_PATH>` flag

func WithDisableDefaultCreate2Deployer

func WithDisableDefaultCreate2Deployer() Option

WithDisableDefaultCreate2Deployer disables the default CREATE2 factory.

Equivalent to the `--disable-default-create2-deployer` flag

func WithForkBlockNumber

func WithForkBlockNumber(block *big.Int) Option

WithForkBlockNumber forks from a specific block number.

Equivalent to the `--fork-block-number <BLOCK>` flag

func WithForkRetryBackoff

func WithForkRetryBackoff(backoff int) Option

WithForkRetryBackoff sets initial retry backoff on fork errors.

Equivalent to the `--fork-retry-backoff <BACKOFF>` flag

func WithForkTransactionHash

func WithForkTransactionHash(tx string) Option

WithForkTransactionHash forks state from a specific transaction hash.

Equivalent to the `--fork-transaction-hash <TRANSACTION>` flag

func WithForkURL

func WithForkURL(url string) Option

WithForkURL enables state forking from a remote endpoint.

Equivalent to the `-f, --fork-url <URL>` flag

func WithGasLimit

func WithGasLimit(limit int) Option

WithGasLimit sets the block gas limit.

Equivalent to the `--gas-limit <GAS_LIMIT>` flag

func WithGasPrice

func WithGasPrice(price int) Option

WithGasPrice sets the gas price.

Equivalent to the `--gas-price <GAS_PRICE>` flag

func WithHardfork

func WithHardfork(name string) Option

WithHardfork sets the EVM hardfork to use.

Equivalent to the `--hardfork <HARDFORK>` flag

func WithHelp

func WithHelp() Option

WithHelp prints help information.

Equivalent to the `-h, --help` flag

func WithHost

func WithHost(host string) Option

WithHost sets the host address to listen on.

Equivalent to the `--host <HOST>` flag

func WithIPC

func WithIPC(path string) Option

WithIPC starts an IPC endpoint at a given path (optional).

Equivalent to the `--ipc [<PATH>]` flag

func WithInit

func WithInit(path string) Option

WithInit initializes the genesis block using a genesis.json file.

Equivalent to the `--init <PATH>` flag

func WithMnemonic

func WithMnemonic(mnemonic string) Option

WithMnemonic sets the BIP39 mnemonic phrase for generating accounts.

Equivalent to the `-m, --mnemonic <MNEMONIC>` flag

func WithNoCORS

func WithNoCORS() Option

WithNoCORS disables CORS support.

Equivalent to the `--no-cors` flag

func WithNoMining

func WithNoMining() Option

WithNoMining disables auto and interval mining.

Equivalent to the `--no-mining` flag

func WithNoRateLimit

func WithNoRateLimit() Option

WithNoRateLimit disables rate limiting for the node’s provider.

Equivalent to the `--no-rate-limit` flag

func WithNoRequestSizeLimit

func WithNoRequestSizeLimit() Option

WithNoRequestSizeLimit disables the default request size limit (2MB).

Equivalent to the `--no-request-size-limit` flag

func WithNoStorageCaching

func WithNoStorageCaching() Option

WithNoStorageCaching disables RPC storage slot caching.

Equivalent to the `--no-storage-caching` flag

func WithOrder

func WithOrder(order string) Option

WithOrder sets the transaction ordering strategy in the mempool.

Equivalent to the `--order <ORDER>` flag

func WithPort

func WithPort(port int) Option

WithPort sets the listening port.

Equivalent to the `-p, --port <PORT>` flag

func WithPruneHistory

func WithPruneHistory() Option

WithPruneHistory disables full chain history retention.

Equivalent to the `--prune-history` flag

func WithRetries

func WithRetries(count int) Option

WithRetries sets the number of retry attempts for network issues.

Equivalent to the `--retries <RETRIES>` flag

func WithSilent

func WithSilent() Option

WithSilent disables all startup logs.

Equivalent to the `--silent` flag

func WithStepsTracing

func WithStepsTracing() Option

WithStepsTracing enables steps tracing for geth-style traces.

Equivalent to the `--steps-tracing` flag (alias: `--tracing`)

func WithTimeout

func WithTimeout(ms int) Option

WithTimeout sets the request timeout for forking mode in ms.

Equivalent to the `--timeout <TIMEOUT>` flag

func WithTimestamp

func WithTimestamp(ts int64) Option

WithTimestamp sets the timestamp of the genesis block.

Equivalent to the `--timestamp <TIMESTAMP>` flag

func WithVersion

func WithVersion() Option

WithVersion prints version information.

Equivalent to the `-V, --version` flag

type Wallet

type Wallet struct {
	DerivationPath string `json:"derivation_path"`
	Mnemonic       string `json:"mnemonic"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL