-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvisitors.go
466 lines (455 loc) · 16.8 KB
/
visitors.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package runtime
import (
"bytes"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/accounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/consensusaccounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/core"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
sdkTypes "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/oasisprotocol/nexus/analyzer/evmabi"
"github.com/oasisprotocol/nexus/analyzer/runtime/abiparse"
"github.com/oasisprotocol/nexus/storage/oasis/nodeapi"
)
type CallHandler struct {
AccountsTransfer func(body *accounts.Transfer) error
ConsensusAccountsDeposit func(body *consensusaccounts.Deposit) error
ConsensusAccountsWithdraw func(body *consensusaccounts.Withdraw) error
ConsensusAccountsDelegate func(body *consensusaccounts.Delegate) error
ConsensusAccountsUndelegate func(body *consensusaccounts.Undelegate) error
EVMCreate func(body *evm.Create, ok *[]byte) error
EVMCall func(body *evm.Call, ok *[]byte) error
RoflCreate func(body *rofl.Create) error
RoflUpdate func(body *rofl.Update) error
RoflRemove func(body *rofl.Remove) error
RoflRegister func(body *rofl.Register) error
UnknownMethod func(methodName string) error // Invoked for a tx call that doesn't map to any of the above method names.
}
//nolint:nestif,gocyclo
func VisitCall(call *sdkTypes.Call, result *sdkTypes.CallResult, handler *CallHandler) error {
// List of methods: See each of the SDK modules, example for consensus_accounts:
// https://github.com/oasisprotocol/oasis-sdk/blob/client-sdk%2Fgo%2Fv0.6.0/client-sdk/go/modules/consensusaccounts/consensus_accounts.go#L16-L20
switch call.Method {
case "accounts.Transfer":
if handler.AccountsTransfer != nil {
var body accounts.Transfer
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal accounts transfer: %w", err)
}
if err := handler.AccountsTransfer(&body); err != nil {
return fmt.Errorf("accounts transfer: %w", err)
}
}
case "consensus.Deposit":
if handler.ConsensusAccountsDeposit != nil {
var body consensusaccounts.Deposit
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal consensus accounts deposit: %w", err)
}
if err := handler.ConsensusAccountsDeposit(&body); err != nil {
return fmt.Errorf("consensus accounts deposit: %w", err)
}
}
case "consensus.Withdraw":
if handler.ConsensusAccountsWithdraw != nil {
var body consensusaccounts.Withdraw
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal consensus accounts withdraw: %w", err)
}
if err := handler.ConsensusAccountsWithdraw(&body); err != nil {
return fmt.Errorf("consensus accounts withdraw: %w", err)
}
}
case "consensus.Delegate":
if handler.ConsensusAccountsDelegate != nil {
var body consensusaccounts.Delegate
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal consensus accounts delegate: %w", err)
}
if err := handler.ConsensusAccountsDelegate(&body); err != nil {
return fmt.Errorf("consensus accounts delegate: %w", err)
}
}
case "consensus.Undelegate":
if handler.ConsensusAccountsUndelegate != nil {
var body consensusaccounts.Undelegate
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal consensus accounts undelegate: %w", err)
}
if err := handler.ConsensusAccountsUndelegate(&body); err != nil {
return fmt.Errorf("consensus accounts undelegate: %w", err)
}
}
case "evm.Create":
if handler.EVMCreate != nil {
var body evm.Create
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal evm create: %w", err)
}
var okP *[]byte
if !result.IsUnknown() && result.IsSuccess() {
var ok []byte
if err := cbor.Unmarshal(result.Ok, &ok); err != nil {
return fmt.Errorf("unmarshal evm create result: %w", err)
}
okP = &ok
}
if err := handler.EVMCreate(&body, okP); err != nil {
return fmt.Errorf("evm create: %w", err)
}
}
case "evm.Call":
if handler.EVMCall != nil {
var body evm.Call
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal evm call: %w", err)
}
var okP *[]byte
if !result.IsUnknown() && result.IsSuccess() {
var ok []byte
if err := cbor.Unmarshal(result.Ok, &ok); err != nil {
return fmt.Errorf("unmarshal evm call result: %w", err)
}
okP = &ok
}
if err := handler.EVMCall(&body, okP); err != nil {
return fmt.Errorf("evm call: %w", err)
}
}
case "rofl.Create":
if handler.RoflCreate != nil {
var body rofl.Create
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal rofl create: %w", err)
}
if err := handler.RoflCreate(&body); err != nil {
return fmt.Errorf("rofl create: %w", err)
}
}
case "rofl.Update":
if handler.RoflUpdate != nil {
var body rofl.Update
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal rofl update: %w", err)
}
if err := handler.RoflUpdate(&body); err != nil {
return fmt.Errorf("rofl update: %w", err)
}
}
case "rofl.Remove":
if handler.RoflRemove != nil {
var body rofl.Remove
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal rofl remove: %w", err)
}
if err := handler.RoflRemove(&body); err != nil {
return fmt.Errorf("rofl remove: %w", err)
}
}
case "rofl.Register":
if handler.RoflRegister != nil {
var body rofl.Register
if err := cbor.Unmarshal(call.Body, &body); err != nil {
return fmt.Errorf("unmarshal rofl register: %w", err)
}
if err := handler.RoflRegister(&body); err != nil {
return fmt.Errorf("rofl register: %w", err)
}
}
default:
if handler.UnknownMethod != nil {
return handler.UnknownMethod(string(call.Method))
}
}
return nil
}
type SdkEventHandler struct {
Core func(event *core.Event) error
Accounts func(event *accounts.Event) error
ConsensusAccounts func(event *consensusaccounts.Event) error
EVM func(event *evm.Event) error
Rofl func(event *rofl.Event) error
}
func VisitSdkEvent(event *nodeapi.RuntimeEvent, handler *SdkEventHandler) error {
if handler.Core != nil {
coreEvents, err := DecodeCoreEvent(event)
if err != nil {
return fmt.Errorf("decode core: %w", err)
}
for i := range coreEvents {
if err = handler.Core(&coreEvents[i]); err != nil {
return fmt.Errorf("decoded event %d core: %w", i, err)
}
}
}
if handler.Accounts != nil {
accountEvents, err := DecodeAccountsEvent(event)
if err != nil {
return fmt.Errorf("decode accounts: %w", err)
}
for i := range accountEvents {
if err = handler.Accounts(&accountEvents[i]); err != nil {
return fmt.Errorf("decoded event %d accounts: %w", i, err)
}
}
}
if handler.ConsensusAccounts != nil {
consensusAccountsEvents, err := DecodeConsensusAccountsEvent(event)
if err != nil {
return fmt.Errorf("decode consensus accounts: %w", err)
}
for i := range consensusAccountsEvents {
if err = handler.ConsensusAccounts(&consensusAccountsEvents[i]); err != nil {
return fmt.Errorf("decoded event %d consensus accounts: %w", i, err)
}
}
}
if handler.EVM != nil {
evmEvents, err := DecodeEVMEvent(event)
if err != nil {
return fmt.Errorf("decode evm: %w", err)
}
for i := range evmEvents {
if err = handler.EVM(&evmEvents[i]); err != nil {
return fmt.Errorf("decoded event %d evm: %w", i, err)
}
}
}
if handler.Rofl != nil {
roflEvents, err := DecodeRoflEvent(event)
if err != nil {
return fmt.Errorf("decode rofl: %w", err)
}
for i := range roflEvents {
if err = handler.Rofl(&roflEvents[i]); err != nil {
return fmt.Errorf("decoded event %d rofl: %w", i, err)
}
}
}
return nil
}
func VisitSdkEvents(events []nodeapi.RuntimeEvent, handler *SdkEventHandler) error {
for i := range events {
if err := VisitSdkEvent(&events[i], handler); err != nil {
return fmt.Errorf("event %d: %w; raw event: %+v", i, err, events[i])
}
}
return nil
}
type EVMEventHandler struct {
ERC20Transfer func(from ethCommon.Address, to ethCommon.Address, value *big.Int) error
ERC20Approval func(owner ethCommon.Address, spender ethCommon.Address, value *big.Int) error
ERC721Transfer func(from ethCommon.Address, to ethCommon.Address, tokenID *big.Int) error
ERC721Approval func(owner ethCommon.Address, approved ethCommon.Address, tokenID *big.Int) error
ERC721ApprovalForAll func(owner ethCommon.Address, operator ethCommon.Address, approved bool) error
IUniswapV2FactoryPairCreated func(token0 ethCommon.Address, token1 ethCommon.Address, pair ethCommon.Address, allPairsLength *big.Int) error
IUniswapV2PairMint func(sender ethCommon.Address, amount0 *big.Int, amount1 *big.Int) error
IUniswapV2PairBurn func(sender ethCommon.Address, amount0 *big.Int, amount1 *big.Int, to ethCommon.Address) error
IUniswapV2PairSwap func(sender ethCommon.Address, amount0In *big.Int, amount1In *big.Int, amount0Out *big.Int, amount1Out *big.Int, to ethCommon.Address) error
IUniswapV2PairSync func(reserve0 *big.Int, reserve1 *big.Int) error
// `owner` wrapped/deposited runtime's native token (ROSE) into the wrapper contract (creating WROSE).
// `value` ROSE is transferred from `owner` to the wrapper (= event-emitting contract). Caller's WROSE balance increases by `value`.
WROSEDeposit func(owner ethCommon.Address, value *big.Int) error
// `owner` unwrapped/withdrew runtime's native token (ROSE) into the wrapper contract (burning WROSE).
// Caller's WROSE balance decreases by `value`. `value` ROSE is transferred from the wrapper (= event-emitting contract) to `owner`.
WROSEWithdrawal func(owner ethCommon.Address, value *big.Int) error
}
func eventMatches(evmEvent *evm.Event, ethEvent abi.Event) bool {
if len(evmEvent.Topics) == 0 || !bytes.Equal(evmEvent.Topics[0], ethEvent.ID.Bytes()) {
return false
}
// Thanks, ERC-721 and ERC-20 having the same signature for Transfer.
// Check if it has the right number of topics.
numTopics := 0
if !ethEvent.Anonymous {
numTopics++
}
for _, input := range ethEvent.Inputs {
if input.Indexed {
numTopics++
}
}
return len(evmEvent.Topics) == numTopics
}
func VisitEVMEvent(event *evm.Event, handler *EVMEventHandler) error { //nolint:gocyclo
switch {
case eventMatches(event, evmabi.ERC20.Events["Transfer"]):
if handler.ERC20Transfer != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.ERC20)
if err != nil {
return fmt.Errorf("parse erc20 transfer: %w", err)
}
if err = handler.ERC20Transfer(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(*big.Int),
); err != nil {
return fmt.Errorf("handle erc20 transfer: %w", err)
}
}
case eventMatches(event, evmabi.ERC20.Events["Approval"]):
if handler.ERC20Approval != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.ERC20)
if err != nil {
return fmt.Errorf("parse erc20 approval: %w", err)
}
if err = handler.ERC20Approval(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(*big.Int),
); err != nil {
return fmt.Errorf("handle erc20 approval: %w", err)
}
}
case eventMatches(event, evmabi.ERC721.Events["Transfer"]):
if handler.ERC721Transfer != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.ERC721)
if err != nil {
return fmt.Errorf("parse erc721 transfer: %w", err)
}
if err = handler.ERC721Transfer(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(*big.Int),
); err != nil {
return fmt.Errorf("handle erc721 transfer: %w", err)
}
}
case eventMatches(event, evmabi.ERC721.Events["Approval"]):
if handler.ERC721Approval != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.ERC721)
if err != nil {
return fmt.Errorf("parse erc721 approval: %w", err)
}
if err = handler.ERC721Approval(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(*big.Int),
); err != nil {
return fmt.Errorf("handle erc721 approval: %w", err)
}
}
case eventMatches(event, evmabi.ERC721.Events["ApprovalForAll"]):
if handler.ERC721ApprovalForAll != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.ERC721)
if err != nil {
return fmt.Errorf("parse erc721 approval for all: %w", err)
}
if err = handler.ERC721ApprovalForAll(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(bool),
); err != nil {
return fmt.Errorf("handle erc721 approval for all: %w", err)
}
}
case eventMatches(event, evmabi.IUniswapV2Factory.Events["PairCreated"]):
if handler.IUniswapV2FactoryPairCreated != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.IUniswapV2Factory)
if err != nil {
return fmt.Errorf("parse uniswap v2 factory pair created: %w", err)
}
if err = handler.IUniswapV2FactoryPairCreated(
args[0].(ethCommon.Address),
args[1].(ethCommon.Address),
args[2].(ethCommon.Address),
args[3].(*big.Int),
); err != nil {
return fmt.Errorf("handle uniswap v2 factory pair created: %w", err)
}
}
case eventMatches(event, evmabi.IUniswapV2Pair.Events["Mint"]):
if handler.IUniswapV2PairMint != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.IUniswapV2Pair)
if err != nil {
return fmt.Errorf("parse uniswap v2 pair mint: %w", err)
}
if err = handler.IUniswapV2PairMint(
args[0].(ethCommon.Address),
args[1].(*big.Int),
args[2].(*big.Int),
); err != nil {
return fmt.Errorf("handle uniswap v2 pair mint: %w", err)
}
}
case eventMatches(event, evmabi.IUniswapV2Pair.Events["Burn"]):
if handler.IUniswapV2PairBurn != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.IUniswapV2Pair)
if err != nil {
return fmt.Errorf("parse uniswap v2 pair burn: %w", err)
}
if err = handler.IUniswapV2PairBurn(
args[0].(ethCommon.Address),
args[1].(*big.Int),
args[2].(*big.Int),
args[3].(ethCommon.Address),
); err != nil {
return fmt.Errorf("handle uniswap v2 pair burn: %w", err)
}
}
case eventMatches(event, evmabi.IUniswapV2Pair.Events["Swap"]):
if handler.IUniswapV2PairSwap != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.IUniswapV2Pair)
if err != nil {
return fmt.Errorf("parse uniswap v2 pair swap: %w", err)
}
if err = handler.IUniswapV2PairSwap(
args[0].(ethCommon.Address),
args[1].(*big.Int),
args[2].(*big.Int),
args[3].(*big.Int),
args[4].(*big.Int),
args[5].(ethCommon.Address),
); err != nil {
return fmt.Errorf("handle uniswap v2 pair swap: %w", err)
}
}
case eventMatches(event, evmabi.IUniswapV2Pair.Events["Sync"]):
if handler.IUniswapV2PairSync != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.IUniswapV2Pair)
if err != nil {
return fmt.Errorf("parse uniswap v2 pair sync: %w", err)
}
if err = handler.IUniswapV2PairSync(
args[0].(*big.Int),
args[1].(*big.Int),
); err != nil {
return fmt.Errorf("handle uniswap v2 pair sync: %w", err)
}
}
// Signature: 0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c (hex) or 4f/8xJI9BLVZ9NKai/xs2gTrWw08RgdRwkAsXFzJEJw= (base64)
case eventMatches(event, evmabi.WROSE.Events["Deposit"]):
if handler.WROSEDeposit != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.WROSE)
if err != nil {
return fmt.Errorf("parse wrose deposit: %w", err)
}
if err = handler.WROSEDeposit(
args[0].(ethCommon.Address),
args[1].(*big.Int),
); err != nil {
return fmt.Errorf("handle wrose deposit: %w", err)
}
}
// Signature: 0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65 (hex) or f89TLBXwptsL1tDgOL6nHTDYCMfZjLO/cmipW/UIG2U= (base64)
case eventMatches(event, evmabi.WROSE.Events["Withdrawal"]):
if handler.WROSEWithdrawal != nil {
_, args, err := abiparse.ParseEvent(event.Topics, event.Data, evmabi.WROSE)
if err != nil {
return fmt.Errorf("parse wrose withdrawal: %w", err)
}
if err = handler.WROSEWithdrawal(
args[0].(ethCommon.Address),
args[1].(*big.Int),
); err != nil {
return fmt.Errorf("handle wrose withdrawal: %w", err)
}
}
}
return nil
}