Skip to content

Commit 25c1b90

Browse files
committed
clean up, no longer require sleep
1 parent 9f9739e commit 25c1b90

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

client.go renamed to node.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package anvil
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
7+
"math/big"
68
"os/exec"
79
"sync/atomic"
10+
11+
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/rpc"
813
)
914

1015
// Node represents an active Anvil client
1116
type Node struct {
17+
cli *rpc.Client
1218
running atomic.Bool
1319
cmd *exec.Cmd
1420
}
@@ -36,7 +42,24 @@ func (n *Node) Start() error {
3642

3743
n.running.Store(true)
3844

39-
return n.cmd.Start()
45+
output, err := n.cmd.StdoutPipe()
46+
if err != nil {
47+
return err // @TODO return specific error
48+
}
49+
50+
scanner := bufio.NewScanner(output)
51+
52+
err = n.cmd.Start()
53+
if err != nil {
54+
return err
55+
}
56+
57+
scanner.Scan()
58+
return nil
59+
}
60+
61+
func (n *Node) SetBalance(account common.Address, balance *big.Int) error {
62+
return n.cli.Call(nil, "anvil_setBalance", account, balance)
4063
}
4164

4265
// Stop stops the anvil node
@@ -58,8 +81,8 @@ func WithBlockTime(seconds int) Option {
5881
// WithBalance sets the initial balance of accounts.
5982
//
6083
// Equivalent to the `--balance <BALANCE>` flag
61-
func WithBalance(balance int) Option {
62-
return []string{"--balance", fmt.Sprintf("%d", balance)}
84+
func WithBalance(balance *big.Int) Option {
85+
return []string{"--balance", fmt.Sprintf("%s", balance)}
6386
}
6487

6588
// WithDerivationPath sets the derivation path for HD wallets.
@@ -173,8 +196,8 @@ func WithForkURL(url string) Option {
173196
// WithForkBlockNumber forks from a specific block number.
174197
//
175198
// Equivalent to the `--fork-block-number <BLOCK>` flag
176-
func WithForkBlockNumber(block int) Option {
177-
return []string{"--fork-block-number", fmt.Sprintf("%d", block)}
199+
func WithForkBlockNumber(block *big.Int) Option {
200+
return []string{"--fork-block-number", fmt.Sprintf("%s", block)}
178201
}
179202

180203
// WithForkRetryBackoff sets initial retry backoff on fork errors.

client_test.go renamed to node_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"testing"
7-
"time"
87

98
"github.com/ethereum/go-ethereum/ethclient"
109
"github.com/sprintertech/go-anvil"
@@ -19,11 +18,12 @@ func TestClient(t *testing.T) {
1918
anvil.WithChainID(chainid),
2019
)
2120

22-
defer cli.Stop()
23-
24-
go cli.Start()
21+
err := cli.Start()
22+
if err != nil {
23+
t.Fatal(err)
24+
}
2525

26-
time.Sleep(1 * time.Second)
26+
defer cli.Stop()
2727

2828
ethcli, err := ethclient.Dial(fmt.Sprintf("http://127.0.0.1:%d", port))
2929
if err != nil {

0 commit comments

Comments
 (0)