|
| 1 | +package anvil |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/ethereum/go-ethereum/rpc" |
| 9 | +) |
| 10 | + |
| 11 | +// Client is an RPC client for anvil specific functions |
| 12 | +type Client struct { |
| 13 | + cli *rpc.Client |
| 14 | +} |
| 15 | + |
| 16 | +// NewClient creates a new client with the given RPC client |
| 17 | +func NewClient(cli *rpc.Client) *Client { |
| 18 | + return &Client{cli: cli} |
| 19 | +} |
| 20 | + |
| 21 | +// Dial creates a new client for the given URL. |
| 22 | +func Dial(addr string) (*Client, error) { |
| 23 | + cli, err := rpc.Dial(addr) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + return NewClient(cli), nil |
| 29 | +} |
| 30 | + |
| 31 | +// SetBalance sets the balance of a given account. |
| 32 | +// |
| 33 | +// Equivalent to the `anvil_setBalance` RPC call. |
| 34 | +func (c *Client) SetBalance(account common.Address, balance *big.Int) error { |
| 35 | + return c.cli.Call(nil, "anvil_setBalance", account, "0x"+balance.Text(16)) |
| 36 | +} |
| 37 | + |
| 38 | +// SetNonce sets the transaction nonce for a given account. |
| 39 | +// |
| 40 | +// Equivalent to the `anvil_setNonce` RPC call. |
| 41 | +func (c *Client) SetNonce(account common.Address, nonce uint64) error { |
| 42 | + return c.cli.Call(nil, "anvil_setNonce", account, fmt.Sprintf("0x%x", nonce)) |
| 43 | +} |
| 44 | + |
| 45 | +// SetCode sets the EVM bytecode at the specified account. |
| 46 | +// |
| 47 | +// Equivalent to the `anvil_setCode` RPC call. |
| 48 | +func (c *Client) SetCode(account common.Address, code []byte) error { |
| 49 | + return c.cli.Call(nil, "anvil_setCode", account, fmt.Sprintf("0x%x", code)) |
| 50 | +} |
| 51 | + |
| 52 | +// SetStorageAt writes a single storage slot at a given account. |
| 53 | +// |
| 54 | +// Equivalent to the `anvil_setStorageAt` RPC call. |
| 55 | +func (c *Client) SetStorageAt(account common.Address, slot string, value string) error { |
| 56 | + return c.cli.Call(nil, "anvil_setStorageAt", account, slot, value) |
| 57 | +} |
| 58 | + |
| 59 | +// SetMinGasPrice sets the minimum gas price for the node. |
| 60 | +// |
| 61 | +// Equivalent to the `anvil_setMinGasPrice` RPC call. |
| 62 | +func (c *Client) SetMinGasPrice(price *big.Int) error { |
| 63 | + return c.cli.Call(nil, "anvil_setMinGasPrice", "0x"+price.Text(16)) |
| 64 | +} |
| 65 | + |
| 66 | +// SetNextBlockBaseFeePerGas sets the base fee for the next block. |
| 67 | +// |
| 68 | +// Equivalent to the `anvil_setNextBlockBaseFeePerGas` RPC call. |
| 69 | +func (c *Client) SetNextBlockBaseFeePerGas(fee *big.Int) error { |
| 70 | + return c.cli.Call(nil, "anvil_setNextBlockBaseFeePerGas", "0x"+fee.Text(16)) |
| 71 | +} |
| 72 | + |
| 73 | +// SetChainId sets the chain ID of the node. |
| 74 | +// |
| 75 | +// Equivalent to the `anvil_setChainId` RPC call. |
| 76 | +func (c *Client) SetChainId(id uint64) error { |
| 77 | + return c.cli.Call(nil, "anvil_setChainId", fmt.Sprintf("0x%x", id)) |
| 78 | +} |
| 79 | + |
| 80 | +// SetCoinbase sets the coinbase address for block rewards. |
| 81 | +// |
| 82 | +// Equivalent to the `anvil_setCoinbase` RPC call. |
| 83 | +func (c *Client) SetCoinbase(addr common.Address) error { |
| 84 | + return c.cli.Call(nil, "anvil_setCoinbase", addr) |
| 85 | +} |
| 86 | + |
| 87 | +// SetLoggingEnabled enables or disables logging output. |
| 88 | +// |
| 89 | +// Equivalent to the `anvil_setLoggingEnabled` RPC call. |
| 90 | +func (c *Client) SetLoggingEnabled(enable bool) error { |
| 91 | + return c.cli.Call(nil, "anvil_setLoggingEnabled", enable) |
| 92 | +} |
| 93 | + |
| 94 | +// Reset resets the node state to the original or a new forked state. |
| 95 | +// |
| 96 | +// Equivalent to the `anvil_reset` RPC call. |
| 97 | +func (c *Client) Reset(forkURL string) error { |
| 98 | + if forkURL == "" { |
| 99 | + return c.cli.Call(nil, "anvil_reset") |
| 100 | + } |
| 101 | + config := map[string]interface{}{"forking": map[string]string{"jsonRpcUrl": forkURL}} |
| 102 | + return c.cli.Call(nil, "anvil_reset", config) |
| 103 | +} |
| 104 | + |
| 105 | +// DumpState returns a hex-encoded snapshot of the entire chain state. |
| 106 | +// |
| 107 | +// Equivalent to the `anvil_dumpState` RPC call. |
| 108 | +func (c *Client) DumpState(out *string) error { |
| 109 | + return c.cli.Call(out, "anvil_dumpState") |
| 110 | +} |
| 111 | + |
| 112 | +// LoadState merges a previously dumped state into the current chain state. |
| 113 | +// |
| 114 | +// Equivalent to the `anvil_loadState` RPC call. |
| 115 | +func (c *Client) LoadState(state string) error { |
| 116 | + return c.cli.Call(nil, "anvil_loadState", state) |
| 117 | +} |
| 118 | + |
| 119 | +// NodeInfo retrieves the current node configuration parameters. |
| 120 | +// |
| 121 | +// Equivalent to the `anvil_nodeInfo` RPC call. |
| 122 | +func (c *Client) NodeInfo(info *map[string]interface{}) error { |
| 123 | + return c.cli.Call(info, "anvil_nodeInfo") |
| 124 | +} |
0 commit comments