-
Notifications
You must be signed in to change notification settings - Fork 6
/
send_test.go
148 lines (133 loc) · 5.11 KB
/
send_test.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
package seth
import (
"encoding/json"
"math/big"
"testing"
)
func BenchmarkEncodeCall(b *testing.B) {
var to Address
var amount big.Int
var c CallOpts
to.FromString("0x78bbe6a0fb1a07fd078bf634dcf2a7d0f444d845")
amount.SetString("111f904273b", 16)
a := Int(amount)
for i := 0; i < b.N; i++ {
c.EncodeCall("transfer(address,uint256)", &to, &a)
}
}
func TestArgumentEncoding(t *testing.T) {
var from, to Address
var amount big.Int
var c CallOpts
from.FromString("0x7b79d72f7eb12b62e1d2e95860b7062dd63f7b7a")
to.FromString("0x78bbe6a0fb1a07fd078bf634dcf2a7d0f444d845")
amount.SetString("111f904273b", 16)
a := Int(amount)
c.From = &from
c.EncodeCall("transfer(address,uint256)", &to, &a)
const want = `"0xa9059cbb00000000000000000000000078bbe6a0fb1a07fd078bf634dcf2a7d0f444d84500000000000000000000000000000000000000000000000000000111f904273b"`
b, _ := json.Marshal(c.Data)
if string(b) != want {
t.Errorf("wanted %q\ngot%q", want, b)
}
// now go the opposite direction
const wantret = `"0x00000000000000000000000078bbe6a0fb1a07fd078bf634dcf2a7d0f444d84500000000000000000000000000000000000000000000000000000111f904273b"`
to = Address{}
amount.SetString("0", 10)
d := NewABIDecoder(&to, &amount)
err := json.Unmarshal([]byte(wantret), d)
if err != nil {
t.Fatal(err)
}
if to.String() != "0x78bbe6a0fb1a07fd078bf634dcf2a7d0f444d845" {
t.Error("bad unmarshal of addr")
}
}
func TestArgumentEncoding2(t *testing.T) {
words := []string{
"0x008eb4da847d0d5cfc908959df5b4e9d52492fd20000000000033e07b4d9d580",
"0x0035a1bbacd4f771b215715cdb108be436418713000000000000acec45ad61cf",
"0x00de5f818693eb9a0b38f78866c43d1ff67218a40000000000000b8737d85bda",
"0x41beed1b9e7d78da2f180364f252eaeb2027c30100000000000114ad3c489c80",
}
const want = `"0x9a0e4ebb00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004008eb4da847d0d5cfc908959df5b4e9d52492fd20000000000033e07b4d9d5800035a1bbacd4f771b215715cdb108be436418713000000000000acec45ad61cf00de5f818693eb9a0b38f78866c43d1ff67218a40000000000000b8737d85bda41beed1b9e7d78da2f180364f252eaeb2027c30100000000000114ad3c489c80"`
sl := make(IntSlice, len(words))
for i := range words {
if err := sl[i].FromString(words[i]); err != nil {
t.Fatal(err)
}
}
var c CallOpts
c.EncodeCall("multiMint(uint256[])", &sl)
b, _ := json.Marshal(c.Data)
if string(b) != want {
t.Errorf("wanted %q\ngot%q", want, b)
}
}
func TestArgumentEncoding3(t *testing.T) {
a1 := IntSlice{*NewInt(1), *NewInt(2)}
a2 := IntSlice{*NewInt(3), *NewInt(4)}
const want = `"0x3f8fc7ea` +
`0000000000000000000000000000000000000000000000000000000000000040` +
`00000000000000000000000000000000000000000000000000000000000000a0` +
`0000000000000000000000000000000000000000000000000000000000000002` +
`0000000000000000000000000000000000000000000000000000000000000001` +
`0000000000000000000000000000000000000000000000000000000000000002` +
`0000000000000000000000000000000000000000000000000000000000000002` +
`0000000000000000000000000000000000000000000000000000000000000003` +
`0000000000000000000000000000000000000000000000000000000000000004"`
var c CallOpts
c.EncodeCall("foo(uint256[],uint256[])", &a1, &a2)
b, _ := json.Marshal(c.Data)
if string(b) != want {
t.Errorf("wanted %q\ngot%q", want, b)
}
// input w/o function selector
stripped := `"0x` + want[11:]
var r0, r1 IntSlice
d := NewABIDecoder(&r0, &r1)
err := json.Unmarshal([]byte(stripped), d)
if err != nil {
t.Fatal(err)
}
if len(r0) != 2 {
t.Errorf("bad length %d r0", len(r0))
}
if len(r1) != 2 {
t.Errorf("bad length %d r1", len(r1))
}
if r0[0].Int64() != 1 || r0[1].Int64() != 2 || r1[0].Int64() != 3 || r1[1].Int64() != 4 {
t.Errorf("bad values: %v %v %v %v", &r0[0], &r0[1], &r1[0], &r1[1])
}
}
func TestArgumentEncoding4(t *testing.T) {
txdata := Bytes{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
const want = `"0x30c8d1da` +
`0000000000000000000000000000000000000000000000000000000000000020` +
`0000000000000000000000000000000000000000000000000000000000000032` +
`0102030405060708090a0102030405060708090a0102030405060708090a0102` +
`030405060708090a0102030405060708090a0000000000000000000000000000"`
var c CallOpts
c.EncodeCall("foo(bytes)", &txdata)
b, _ := json.Marshal(c.Data)
if string(b) != want {
t.Errorf("wanted %q\ngot%q", want, b)
}
}
func TestArgumentEncoding5(t *testing.T) {
a1 := Bytes{1, 2, 3}
a2 := Bytes{4, 5, 6}
const want = `"0x65837adb` +
`0000000000000000000000000000000000000000000000000000000000000040` +
`0000000000000000000000000000000000000000000000000000000000000080` +
`0000000000000000000000000000000000000000000000000000000000000003` +
`0102030000000000000000000000000000000000000000000000000000000000` +
`0000000000000000000000000000000000000000000000000000000000000003` +
`0405060000000000000000000000000000000000000000000000000000000000"`
var c CallOpts
c.EncodeCall("foo(bytes,bytes)", &a1, &a2)
b, _ := json.Marshal(c.Data)
if string(b) != want {
t.Errorf("wanted %q\ngot%q", want, b)
}
}