forked from lmittmann/w3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
225 lines (215 loc) · 7.93 KB
/
event_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
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
package w3_test
import (
"fmt"
"math/big"
"strconv"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3"
)
func ExampleEvent_DecodeArgs() {
var (
eventTransfer = w3.MustNewEvent("Transfer(address indexed from, address indexed to, uint256 value)")
log = &types.Log{
Address: w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"),
},
Data: w3.B("0x0000000000000000000000000000000000000000000000001111d67bb1bb0000"),
}
from common.Address
to common.Address
value big.Int
)
if err := eventTransfer.DecodeArgs(log, &from, &to, &value); err != nil {
fmt.Printf("Failed to decode event log: %v\n", err)
return
}
fmt.Printf("Transferred %s WETH9 from %s to %s", w3.FromWei(&value, 18), from, to)
// Output:
// Transferred 1.23 WETH9 from 0x000000000000000000000000000000000000c0Fe to 0x000000000000000000000000000000000000dEaD
}
func TestNewEvent(t *testing.T) {
tests := []struct {
Signature string
WantEvent *w3.Event
}{
{
Signature: "Transfer(address,address,uint256)",
WantEvent: &w3.Event{
Signature: "Transfer(address,address,uint256)",
Topic0: w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
},
},
{
Signature: "Transfer(address from, address to, uint256 value)",
WantEvent: &w3.Event{
Signature: "Transfer(address,address,uint256)",
Topic0: w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
},
},
{
Signature: "Approval(address,address,uint256)",
WantEvent: &w3.Event{
Signature: "Approval(address,address,uint256)",
Topic0: w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"),
},
},
{
Signature: "Approval(address owner, address spender, uint256 value)",
WantEvent: &w3.Event{
Signature: "Approval(address,address,uint256)",
Topic0: w3.H("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"),
},
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
gotEvent, err := w3.NewEvent(test.Signature)
if err != nil {
t.Fatalf("Failed to create new FUnc: %v", err)
}
if diff := cmp.Diff(test.WantEvent, gotEvent,
cmpopts.IgnoreUnexported(w3.Event{}),
cmpopts.IgnoreFields(w3.Event{}, "Args"),
); diff != "" {
t.Fatalf("(-want, +got)\n%s", diff)
}
})
}
}
func TestEventDecodeArgs(t *testing.T) {
tests := []struct {
Event *w3.Event
Log *types.Log
Args []any
WantArgs []any
}{
{
Event: w3.MustNewEvent("Transfer(address,address,uint256)"),
Log: &types.Log{
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
},
Data: w3.B("0x" +
"000000000000000000000000000000000000000000000000000000000000c0fe" +
"000000000000000000000000000000000000000000000000000000000000dead" +
"000000000000000000000000000000000000000000000000000000000000002a"),
},
Args: []any{new(common.Address), new(common.Address), new(big.Int)},
WantArgs: []any{
w3.APtr("0x000000000000000000000000000000000000c0Fe"),
w3.APtr("0x000000000000000000000000000000000000dEaD"),
big.NewInt(42),
},
},
{
Event: w3.MustNewEvent("Transfer(address indexed, address, uint256)"),
Log: &types.Log{
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"),
},
Data: w3.B("0x" +
"000000000000000000000000000000000000000000000000000000000000dead" +
"000000000000000000000000000000000000000000000000000000000000002a"),
},
Args: []any{new(common.Address), new(common.Address), new(big.Int)},
WantArgs: []any{
w3.APtr("0x000000000000000000000000000000000000c0Fe"),
w3.APtr("0x000000000000000000000000000000000000dEaD"),
big.NewInt(42),
},
},
{
Event: w3.MustNewEvent("Transfer(address, address indexed, uint256)"),
Log: &types.Log{
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"),
},
Data: w3.B("0x" +
"000000000000000000000000000000000000000000000000000000000000c0fe" +
"000000000000000000000000000000000000000000000000000000000000002a"),
},
Args: []any{new(common.Address), new(common.Address), new(big.Int)},
WantArgs: []any{
w3.APtr("0x000000000000000000000000000000000000c0Fe"),
w3.APtr("0x000000000000000000000000000000000000dEaD"),
big.NewInt(42),
},
},
{
Event: w3.MustNewEvent("Transfer(address indexed, address indexed, uint256)"),
Log: &types.Log{
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"),
},
Data: w3.B("0x000000000000000000000000000000000000000000000000000000000000002a"),
},
Args: []any{new(common.Address), new(common.Address), new(big.Int)},
WantArgs: []any{
w3.APtr("0x000000000000000000000000000000000000c0Fe"),
w3.APtr("0x000000000000000000000000000000000000dEaD"),
big.NewInt(42),
},
},
{
Event: w3.MustNewEvent("Transfer(address indexed, address indexed, uint256 indexed)"),
Log: &types.Log{
Topics: []common.Hash{
w3.H("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000c0fe"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000dead"),
w3.H("0x000000000000000000000000000000000000000000000000000000000000002a"),
},
},
Args: []any{new(common.Address), new(common.Address), new(big.Int)},
WantArgs: []any{
w3.APtr("0x000000000000000000000000000000000000c0Fe"),
w3.APtr("0x000000000000000000000000000000000000dEaD"),
big.NewInt(42),
},
},
{ // https://github.com/lmittmann/w3/issues/15
Event: w3.MustNewEvent("NameRegistered(string name, bytes32 indexed label, address indexed owner, uint cost, uint expires)"),
Log: &types.Log{
Address: w3.A("0x283Af0B28c62C092C9727F1Ee09c02CA627EB7F5"),
Topics: []common.Hash{
w3.H("0xca6abbe9d7f11422cb6ca7629fbf6fe9efb1c621f71ce8f02b9f2a230097404f"),
w3.H("0x4e59ffc7ae105a2b19f7f29b63e9f9c5ac28e27bce744a330804c6a89269cec0"),
w3.H("0x000000000000000000000000bd08f39b2523426cc1d6961e2d6a9744b3b432b5"),
},
Data: w3.B("0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000165a7d3a3e48ad0000000000000000000000000000000000000000000000000000000066e4d0a50000000000000000000000000000000000000000000000000000000000000009656c666172657373690000000000000000000000000000000000000000000000"),
},
Args: []any{new(string), new(common.Hash), new(common.Address), new(big.Int), new(big.Int)},
WantArgs: []any{
ptr("elfaressi"),
ptr(w3.H("0x4e59ffc7ae105a2b19f7f29b63e9f9c5ac28e27bce744a330804c6a89269cec0")),
w3.APtr("0xbD08F39B2523426Cc1d6961e2d6A9744B3B432b5"),
big.NewInt(6291943382206637),
big.NewInt(1726271653),
},
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if err := test.Event.DecodeArgs(test.Log, test.Args...); err != nil {
t.Fatalf("Failed to decode args: %v", err)
}
if diff := cmp.Diff(test.WantArgs, test.Args,
cmp.AllowUnexported(big.Int{}),
cmpopts.IgnoreUnexported(w3.Event{}),
); diff != "" {
t.Fatalf("(-want, +got)\n%s", diff)
}
})
}
}