-
Notifications
You must be signed in to change notification settings - Fork 18
/
testchain.go
348 lines (292 loc) · 8.39 KB
/
testchain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package ethtest
import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/0xsequence/ethkit"
"github.com/0xsequence/ethkit/ethcontract"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/ethtxn"
"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/core/types"
)
type Testchain struct {
options TestchainOptions
chainID *big.Int // chainID determined by the test chain
walletMnemonic string // test wallet mnemonic parsed from package.json
Provider *ethrpc.Provider // provider rpc to the test chain
}
type TestchainOptions struct {
NodeURL string
}
var DefaultTestchainOptions = TestchainOptions{
NodeURL: "http://localhost:8545",
}
func NewTestchain(opts ...TestchainOptions) (*Testchain, error) {
var err error
tc := &Testchain{}
// set options
if len(opts) > 0 {
tc.options = opts[0]
} else {
tc.options = DefaultTestchainOptions
}
// provider
tc.Provider, err = ethrpc.NewProvider(tc.options.NodeURL)
if err != nil {
return nil, err
}
// connect to the test-chain or error out if fail to communicate
if err := tc.connect(); err != nil {
return nil, err
}
return tc, nil
}
func (c *Testchain) connect() error {
numAttempts := 6
for i := 0; i < numAttempts; i++ {
chainID, err := c.Provider.ChainID(context.Background())
if err != nil || chainID == nil {
time.Sleep(1 * time.Second)
continue
}
c.chainID = chainID
}
if c.chainID == nil {
return fmt.Errorf("ethtest: unable to connect to testchain")
}
return nil
}
func (c *Testchain) ChainID() *big.Int {
return c.chainID
}
func (c *Testchain) Wallet() (*ethwallet.Wallet, error) {
var err error
if c.walletMnemonic == "" {
c.walletMnemonic, err = parseTestWalletMnemonic()
if err != nil {
return nil, err
}
}
// we create a new instance each time so we don't change the account indexes
// on the wallet across consumers
wallet, err := ethwallet.NewWalletFromMnemonic(c.walletMnemonic)
if err != nil {
return nil, err
}
wallet.SetProvider(c.Provider)
err = c.FundAddress(wallet.Address())
if err != nil {
return nil, err
}
return wallet, nil
}
func (c *Testchain) MustWallet(optAccountIndex ...uint32) *ethwallet.Wallet {
wallet, err := c.Wallet()
if err != nil {
panic(err)
}
if len(optAccountIndex) > 0 {
_, err = wallet.SelfDeriveAccountIndex(optAccountIndex[0])
if err != nil {
panic(err)
}
}
err = c.FundAddress(wallet.Address())
if err != nil {
panic(err)
}
return wallet
}
func (c *Testchain) DummyWallet(seed uint64) (*ethwallet.Wallet, error) {
wallet, err := ethwallet.NewWalletFromPrivateKey(DummyPrivateKey(seed))
if err != nil {
return nil, err
}
wallet.SetProvider(c.Provider)
return wallet, nil
}
func (c *Testchain) DummyWallets(nWallets uint64, startingSeed uint64) ([]*ethwallet.Wallet, error) {
var wallets []*ethwallet.Wallet
for i := uint64(0); i < nWallets; i++ {
wallet, err := c.DummyWallet(startingSeed + i*1000)
if err != nil {
return nil, err
}
wallets = append(wallets, wallet)
}
return wallets, nil
}
func (c *Testchain) FundWallets(minBalance float64, wallets ...*ethwallet.Wallet) error {
minTarget := ETHValue(minBalance)
fundAddresses := []ethkit.Address{}
for _, wallet := range wallets {
balance, err := c.Provider.BalanceAt(context.Background(), wallet.Address(), nil)
if err != nil {
return err
}
if balance.Cmp(minTarget) < 0 {
fundAddresses = append(fundAddresses, wallet.Address())
}
}
return c.FundAddresses(fundAddresses, minBalance)
}
func (c *Testchain) FundAddress(addr common.Address, optBalanceTarget ...float64) error {
target := ETHValue(100)
if len(optBalanceTarget) > 0 {
target = ETHValue(optBalanceTarget[0])
}
balance, err := c.Provider.BalanceAt(context.Background(), addr, nil)
if err != nil {
return err
}
if balance.Cmp(target) >= 0 {
// skip, we have enough funds in this wallet for the target
return nil
}
var accounts []common.Address
call := ethrpc.NewCallBuilder[[]common.Address]("eth_accounts", nil)
err = c.Provider.Do(context.Background(), call.Into(&accounts))
if err != nil {
return err
}
type SendTx struct {
From *common.Address `json:"from"`
To *common.Address `json:"to"`
Value string `json:"value"`
}
amount := big.NewInt(0)
amount.Sub(target, balance)
// if balance.Cmp(target) < 0 {
// // top up to the target
// amount.Sub(target, balance)
// } else {
// // already at the target, add same target quantity
// amount.Set(target)
// }
tx := &SendTx{
From: &accounts[0],
To: &addr,
Value: "0x" + amount.Text(16),
}
err = c.Provider.Do(context.Background(), ethrpc.NewCall("eth_sendTransaction", tx))
if err != nil {
return err
}
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
balance, err = c.Provider.BalanceAt(context.Background(), addr, nil)
if err != nil {
return err
}
if balance.Cmp(target) >= 0 {
return nil
}
}
return fmt.Errorf("test wallet failed to fund")
}
func (c *Testchain) MustFundAddress(addr common.Address, optBalanceTarget ...float64) {
err := c.FundAddress(addr, optBalanceTarget...)
if err != nil {
panic(err)
}
}
func (c *Testchain) FundAddresses(addrs []common.Address, optBalanceTarget ...float64) error {
for _, addr := range addrs {
err := c.FundAddress(addr)
if err != nil {
return err
}
}
return nil
}
func (c *Testchain) GetDeployWallet() *ethwallet.Wallet {
return c.MustWallet(5)
}
// GetDeployTransactor returns a account transactor typically used for deploying contracts
func (c *Testchain) GetDeployTransactor() (*bind.TransactOpts, error) {
return c.GetDeployWallet().Transactor(context.Background())
}
// GetRelayerWallet is the wallet dedicated EOA wallet to relaying transactions
func (c *Testchain) GetRelayerWallet() *ethwallet.Wallet {
return c.MustWallet(6)
}
// Deploy will deploy a contract registered in `Contracts` registry using the standard deployment method. Each Deploy call
// will instanitate a new contract on the test chain.
func (c *Testchain) Deploy(t *testing.T, contractName string, contractConstructorArgs ...interface{}) (*ethcontract.Contract, *types.Receipt) {
artifact, ok := Contracts.Get(contractName)
if !ok {
t.Fatal(fmt.Errorf("contract abi not found for name %s", contractName))
}
data := make([]byte, len(artifact.Bin))
copy(data, artifact.Bin)
var input []byte
var err error
// encode constructor call
if len(contractConstructorArgs) > 0 && len(artifact.ABI.Constructor.Inputs) > 0 {
input, err = artifact.ABI.Pack("", contractConstructorArgs...)
} else {
input, err = artifact.ABI.Pack("")
}
if err != nil {
t.Fatal(fmt.Errorf("contract constructor pack failed: %w", err))
}
// append constructor calldata at end of the contract bin
data = append(data, input...)
wallet := c.GetDeployWallet()
signedTxn, err := wallet.NewTransaction(context.Background(), ðtxn.TransactionRequest{
Data: data,
})
if err != nil {
t.Fatal(err)
}
_, waitTx, err := wallet.SendTransaction(context.Background(), signedTxn)
if err != nil {
t.Fatal(err)
}
receipt, err := waitTx(context.Background())
if err != nil {
t.Fatal(err)
}
if receipt.Status != types.ReceiptStatusSuccessful {
t.Fatal(fmt.Errorf("txn failed: %w", err))
}
return ethcontract.NewContractCaller(receipt.ContractAddress, artifact.ABI, c.Provider), receipt
}
func (c *Testchain) WaitMined(txn common.Hash) error {
_, err := ethrpc.WaitForTxnReceipt(context.Background(), c.Provider, txn)
return err
}
func (c *Testchain) RandomNonce() *big.Int {
space := big.NewInt(int64(time.Now().Nanosecond()))
return space
}
// parseTestWalletMnemonic parses the wallet mnemonic from ./package.json, the same
// key used to start the test chain server.
func parseTestWalletMnemonic() (string, error) {
_, filename, _, _ := runtime.Caller(0)
cwd := filepath.Dir(filename)
packageJSONFile := filepath.Join(cwd, "./testchain/package.json")
data, err := os.ReadFile(packageJSONFile)
if err != nil {
return "", fmt.Errorf("ParseTestWalletMnemonic, read: %w", err)
}
var dict struct {
Config struct {
Mnemonic string `json:"mnemonic"`
} `json:"config"`
}
err = json.Unmarshal(data, &dict)
if err != nil {
return "", fmt.Errorf("ParseTestWalletMnemonic, unmarshal: %w", err)
}
return dict.Config.Mnemonic, nil
}