Skip to content

Commit c414ecb

Browse files
committed
split into client file
1 parent 937af19 commit c414ecb

File tree

3 files changed

+133
-11
lines changed

3 files changed

+133
-11
lines changed

client.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}

node.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"sync/atomic"
1111

12-
"github.com/ethereum/go-ethereum/common"
1312
"github.com/ethereum/go-ethereum/rpc"
1413
)
1514

@@ -20,8 +19,8 @@ type Node struct {
2019
cmd *exec.Cmd
2120
}
2221

23-
// New creates a new Node configured with the passed options
24-
func New(opts ...Option) *Node {
22+
// NewNode creates a new Node configured with the passed options
23+
func NewNode(opts ...Option) *Node {
2524
var args []string
2625
for _, opt := range opts {
2726
args = append(args, opt...)
@@ -80,11 +79,6 @@ func (n *Node) Start() error {
8079
return nil
8180
}
8281

83-
// SetBalance sets the balance of a given account.
84-
func (n *Node) SetBalance(account common.Address, balance *big.Int) error {
85-
return n.cli.Call(nil, "anvil_setBalance", account, "0x"+balance.Text(16))
86-
}
87-
8882
// Stop stops the anvil node
8983
func (n *Node) Stop() error {
9084
n.running.Store(false)

node_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/ethclient"
10+
"github.com/ethereum/go-ethereum/rpc"
1011
"github.com/lmittmann/w3"
1112
"github.com/sprintertech/go-anvil"
1213
)
@@ -15,7 +16,7 @@ func TestClient(t *testing.T) {
1516
port := 8547
1617
chainid := 13451
1718

18-
cli := anvil.New(
19+
cli := anvil.NewNode(
1920
anvil.WithPort(port),
2021
anvil.WithChainID(chainid),
2122
)
@@ -27,14 +28,17 @@ func TestClient(t *testing.T) {
2728

2829
defer cli.Stop()
2930

30-
ethcli, err := ethclient.Dial(fmt.Sprintf("http://127.0.0.1:%d", port))
31+
rpccli, err := rpc.Dial(fmt.Sprintf("http://127.0.0.1:%d", port))
3132
if err != nil {
3233
t.Fatal(err)
3334
}
3435

36+
ethcli := ethclient.NewClient(rpccli)
37+
acli := anvil.NewClient(rpccli)
38+
3539
addr := common.HexToAddress("0xc0de000000000000000000000000000000000000")
3640
balance := w3.I("53 eth")
37-
err = cli.SetBalance(addr, balance)
41+
err = acli.SetBalance(addr, balance)
3842
if err != nil {
3943
t.Fatal(err)
4044
}

0 commit comments

Comments
 (0)