-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransferer_test.go
More file actions
88 lines (68 loc) · 2.08 KB
/
Copy pathtransferer_test.go
File metadata and controls
88 lines (68 loc) · 2.08 KB
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
package ethereum
import (
"context"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
var ether = big.NewInt(1000000000000000000) // 1 ether in wei
func TestTransferer_Transfer(t *testing.T) {
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
alloc := core.GenesisAlloc{auth.From: {Balance: ether}}
sim := backends.NewSimulatedBackend(alloc, 10000000)
sim.Commit()
tr := Transferer{sim}
key2, _ := crypto.GenerateKey()
auth2 := bind.NewKeyedTransactor(key2)
amount := new(big.Int).Div(ether, big.NewInt(10))
to := auth2.From
auth.Value = amount
tx, err := tr.Transfer(auth, to, nil)
if err != nil {
t.Fatal(err)
}
sim.Commit()
ctx := context.TODO()
trr, err := sim.TransactionReceipt(ctx, tx.Hash())
if err != nil {
t.Fatal(err)
}
if trr.Status != types.ReceiptStatusSuccessful {
t.Errorf("unexpected transaction status: %v", trr.Status)
}
toBalance, err := sim.BalanceAt(ctx, to, nil)
if err != nil {
t.Fatal(err)
}
if amount.Cmp(toBalance) != 0 {
t.Errorf("expected balance after Transfer is %v, but got %v", amount, toBalance)
}
}
func TestTransferer_SuggestGasLimit(t *testing.T) {
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
alloc := core.GenesisAlloc{auth.From: {Balance: ether}}
sim := backends.NewSimulatedBackend(alloc, 10000000)
sim.Commit()
tr := Transferer{sim}
key2, _ := crypto.GenerateKey()
auth2 := bind.NewKeyedTransactor(key2)
amount := new(big.Int).Div(ether, big.NewInt(10))
to := auth2.From
auth.Value = amount
gasLimit, err := tr.SuggestGasLimit(auth, to, nil)
if err != nil {
t.Fatal(err)
}
expectedGasLimit := big.NewInt(21000)
if expectedGasLimit.Cmp(gasLimit) != 0 {
t.Errorf("expected gas limit is %v, but got %v", expectedGasLimit, gasLimit)
}
}