Repository navigation
Expand file tree
/
Copy pathtypes.go
More file actions
519 lines (448 loc) · 11.6 KB
/
Copy pathtypes.go
File metadata and controls
519 lines (448 loc) · 11.6 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package multicall
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/omnes-tech/abi"
)
type TxOrCall struct {
From common.Address
To *common.Address
Gas uint64
GasPrice *big.Int
GasFeeCap *big.Int
GasTipCap *big.Int
Value *big.Int
Data []byte
Nonce uint64
BlockNumber *big.Int
AccessList types.AccessList
Overrides StateOverride
}
func (t *TxOrCall) String() string {
return fmt.Sprintf(`TxOrCall{
From: %s,
To: %s,
Gas: %d,
GasPrice: %s,
Value: %s,
Data: %s,
Nonce: %d,
BlockNumber: %s,
AccessList: %v,
Overrides: %v,
}
`,
t.From.Hex(),
t.To.Hex(),
t.Gas,
t.GasPrice.String(),
t.Value.String(),
common.Bytes2Hex(t.Data),
t.Nonce,
t.BlockNumber.String(),
t.AccessList,
t.Overrides,
)
}
func FromTxToTxOrCall(tx *types.Transaction, from common.Address, blockNumber *big.Int, overrides StateOverride) TxOrCall {
return TxOrCall{
From: from,
To: tx.To(),
Gas: tx.Gas(),
GasPrice: tx.GasPrice(),
Value: tx.Value(),
Data: tx.Data(),
Nonce: tx.Nonce(),
AccessList: tx.AccessList(),
BlockNumber: blockNumber,
Overrides: overrides,
}
}
func FromCallToTxOrCall(call *ethereum.CallMsg, blockNumber *big.Int, overrides StateOverride) TxOrCall {
return TxOrCall{
From: call.From,
To: call.To,
Gas: call.Gas,
GasPrice: call.GasPrice,
Value: call.Value,
Data: call.Data,
BlockNumber: blockNumber,
Overrides: overrides,
}
}
type Result struct {
Success bool
Result any
Error error
TxOrCall TxOrCall
}
func (r *Result) Description(full bool) string {
if full {
return fmt.Sprintf("%+v", r)
} else {
return fmt.Sprintf("Success: %+v, Result: %+v", r.Success, r.Result)
}
}
func (r *Result) Print(full bool) {
fmt.Println(r.Description(full))
}
type commonCall struct {
Target common.Address
FuncSignature string
Args []interface{}
ReturnTypes []string
CallData []byte
}
type Call struct {
commonCall
Value *big.Int
}
type CallWithFailure struct {
Call
RequireSuccess bool
}
type StaticCall commonCall
type StaticCallWithFailure struct {
StaticCall
RequireSuccess bool
}
type CallsInterface interface {
GetTarget(i int) *common.Address
GetFuncSignature(i int) string
GetArgs(i int) []interface{}
GetCallData(i int) []byte
GetReturnTypes(i int) []string
GetValue(i int) *big.Int
Len() int
ToArray(withValue bool, isMultiCall3Type bool) ([]any, *big.Int, error)
}
type CallsWithFailureInterface interface {
CallsInterface
GetRequireSuccess(i int) bool
}
type Calls []Call
type CallsWithFailure []CallWithFailure
func NewCall(
target common.Address, funcSignature string,
args []interface{}, callData []byte, returnTypes []string, value *big.Int,
) Call {
return Call{
commonCall: commonCall{
Target: target,
FuncSignature: funcSignature,
Args: args,
ReturnTypes: returnTypes,
CallData: callData,
},
Value: value,
}
}
func NewCalls(
targets []common.Address, funcSignatures []string,
argss [][]interface{}, callDatas [][]byte, returnTypess [][]string, values []*big.Int,
) Calls {
calls := make(Calls, len(targets))
for i, target := range targets {
args, callData, returnTypes, value := setParameters(i, argss, callDatas, returnTypess, values)
if funcSignatures == nil {
calls[i] = NewCall(target, "", args, callData, returnTypes, value)
} else {
calls[i] = NewCall(target, funcSignatures[i], args, callData, returnTypes, value)
}
}
return calls
}
func NewCallWithFailure(
target common.Address, funcSignature string, args []interface{},
callData []byte, returnTypes []string, value *big.Int, requireSuccess bool,
) CallWithFailure {
return CallWithFailure{
Call: Call{
commonCall: commonCall{
Target: target,
FuncSignature: funcSignature,
Args: args,
ReturnTypes: returnTypes,
},
Value: value,
},
RequireSuccess: requireSuccess,
}
}
func NewCallsWithFailure(
targets []common.Address, funcSignatures []string, argss [][]interface{},
callDatas [][]byte, returnTypess [][]string, values []*big.Int, requireSuccesss []bool,
) CallsWithFailure {
calls := make(CallsWithFailure, len(targets))
for i, target := range targets {
args, callData, returnTypes, value := setParameters(i, argss, callDatas, returnTypess, values)
if funcSignatures == nil {
calls[i] = NewCallWithFailure(target, "", args, callData, returnTypes, value, requireSuccesss[i])
} else {
calls[i] = NewCallWithFailure(target, funcSignatures[i], args, callData, returnTypes, value, requireSuccesss[i])
}
}
return calls
}
func ParseCallToCalls(calls []Call) Calls {
result := make(Calls, len(calls))
for i, c := range calls {
result[i] = Call{
commonCall: commonCall{
Target: c.Target,
FuncSignature: c.FuncSignature,
Args: c.Args,
ReturnTypes: c.ReturnTypes,
},
Value: c.Value,
}
}
return result
}
func ParseCallWithFailureToCallsWithFailure(calls []CallWithFailure) CallsWithFailure {
result := make(CallsWithFailure, len(calls))
for i, c := range calls {
result[i] = CallWithFailure{
Call: Call{
commonCall: commonCall{
Target: c.Target,
FuncSignature: c.FuncSignature,
Args: c.Args,
ReturnTypes: c.ReturnTypes,
},
Value: c.Value,
},
RequireSuccess: c.RequireSuccess,
}
}
return result
}
func setParameters(
index int, argss [][]interface{}, callDatas [][]byte, returnTypess [][]string, values []*big.Int,
) ([]any, []byte, []string, *big.Int) {
var args []any
if argss == nil {
args = nil
} else {
args = argss[index]
}
var callData []byte
if callDatas == nil {
callData = nil
} else {
callData = callDatas[index]
}
var returnTypes []string
if returnTypess == nil {
returnTypes = nil
} else {
returnTypes = returnTypess[index]
}
var value *big.Int
if values == nil {
value = nil
} else {
value = values[index]
}
return args, callData, returnTypes, value
}
func (c Calls) GetTarget(i int) *common.Address {
return &c[i].Target
}
func (c Calls) GetFuncSignature(i int) string {
return c[i].FuncSignature
}
func (c Calls) GetArgs(i int) []interface{} {
return c[i].Args
}
func (c Calls) GetCallData(i int) []byte {
return c[i].CallData
}
func (c Calls) GetReturnTypes(i int) []string {
return c[i].ReturnTypes
}
func (c Calls) GetValue(i int) *big.Int {
return c[i].Value
}
func (c Calls) Len() int {
return len(c)
}
func (c Calls) ToArray(withValue bool, isMultiCall3Type bool) ([]any, *big.Int, error) {
var result []any
summed := big.NewInt(0)
for i := 0; i < c.Len(); i++ {
var args []any
args = append(args, c.GetTarget(i))
callData := c.GetCallData(i)
if callData == nil {
var err error
if c.GetArgs(i) != nil || len(c.GetArgs(i)) > 0 {
callData, err = abi.EncodeWithSignature(c.GetFuncSignature(i), c.GetArgs(i)...)
} else {
callData, err = abi.EncodeWithSignature(c.GetFuncSignature(i))
}
if err != nil {
return nil, nil, err
}
}
args = append(args, callData)
if withValue {
value := big.NewInt(0)
if c.GetValue(i) != nil {
value.Add(value, c.GetValue(i))
}
summed.Add(summed, value)
args = append(args, value)
}
result = append(result, args)
}
return result, summed, nil
}
func (c CallsWithFailure) GetTarget(i int) *common.Address {
return &c[i].Target
}
func (c CallsWithFailure) GetFuncSignature(i int) string {
return c[i].FuncSignature
}
func (c CallsWithFailure) GetArgs(i int) []interface{} {
return c[i].Args
}
func (c CallsWithFailure) GetCallData(i int) []byte {
return c[i].CallData
}
func (c CallsWithFailure) GetReturnTypes(i int) []string {
return c[i].ReturnTypes
}
func (c CallsWithFailure) GetValue(i int) *big.Int {
return c[i].Value
}
func (c CallsWithFailure) GetRequireSuccess(i int) bool {
return c[i].RequireSuccess
}
func (c CallsWithFailure) Len() int {
return len(c)
}
func (c CallsWithFailure) ToArray(withValue bool, isMultiCall3Type bool) ([]any, *big.Int, error) {
// Omnes: (address,bytes,uint256,bool)
// MultiCall3: (address,bool,bytes) or (address,bool,uint256,bytes)
var result []any
summed := big.NewInt(0)
for i := 0; i < c.Len(); i++ {
var args []any
callData := c.GetCallData(i)
if callData == nil {
var err error
if c.GetArgs(i) != nil || len(c.GetArgs(i)) > 0 {
callData, err = abi.EncodeWithSignature(c.GetFuncSignature(i), c.GetArgs(i)...)
} else {
callData, err = abi.EncodeWithSignature(c.GetFuncSignature(i))
}
if err != nil {
return nil, nil, err
}
}
if isMultiCall3Type {
args = append(args, c.GetTarget(i))
args = append(args, c.GetRequireSuccess(i))
if withValue {
value := big.NewInt(0)
if c.GetValue(i) != nil {
value.Add(value, c.GetValue(i))
}
summed.Add(summed, value)
args = append(args, value)
}
args = append(args, callData)
} else {
args = append(args, c.GetTarget(i))
args = append(args, callData)
if withValue {
value := big.NewInt(0)
if c.GetValue(i) != nil {
value.Add(value, c.GetValue(i))
}
summed.Add(summed, value)
args = append(args, value)
}
args = append(args, c.GetRequireSuccess(i))
}
result = append(result, args)
}
return result, summed, nil
}
// StateOverride matches the shape geth expects for eth_call overrides
// StateDiff: patch specific storage slots, leave rest untouched
// State: replace entire storage, unset slots become zero
// Balance: override ETH balance
// Nonce: override account nonce
// Code: replace contract bytecode entirely
type OverrideAccount struct {
StateDiff map[common.Hash]common.Hash `json:"stateDiff,omitempty"`
State map[common.Hash]common.Hash `json:"state,omitempty"`
Balance *hexutil.Big `json:"balance,omitempty"`
Nonce *hexutil.Uint64 `json:"nonce,omitempty"`
Code hexutil.Bytes `json:"code,omitempty"`
}
func (o *OverrideAccount) Add(other OverrideAccount) {
o.StateDiff = mergeStorageMaps(o.StateDiff, other.StateDiff)
o.State = mergeStorageMaps(o.State, other.State)
if other.Balance != nil {
if o.Balance != nil {
summed := new(big.Int).Add(o.Balance.ToInt(), other.Balance.ToInt())
b := hexutil.Big(*summed)
o.Balance = &b
} else {
b := hexutil.Big(*new(big.Int).Set(other.Balance.ToInt()))
o.Balance = &b
}
}
if other.Nonce != nil {
nonce := *other.Nonce
o.Nonce = &nonce
}
if other.Code != nil {
o.Code = append(hexutil.Bytes{}, other.Code...)
}
}
type StateOverride map[common.Address]OverrideAccount
func (s *StateOverride) AddAccount(address common.Address, override OverrideAccount) {
current, ok := (*s)[address]
if ok {
current.Add(override)
(*s)[address] = current // required: Add mutates a copy
return
}
(*s)[address] = override
}
func (s *StateOverride) Add(other StateOverride) {
if *s == nil {
*s = make(StateOverride)
}
for address, override := range other {
s.AddAccount(address, override)
}
}
type Overrides struct {
From *common.Address
StateOverrides StateOverride
BlockNumber *big.Int
}
// CallMsg-equivalent as a raw map that handles JSON-marshaled RPC data
type CallArgs struct {
From common.Address `json:"from,omitempty"`
To *common.Address `json:"to,omitempty"`
Data hexutil.Bytes `json:"data,omitempty"`
Value *hexutil.Big `json:"value,omitempty"`
}
func (c *CallArgs) ToEthereumCallMsg() *ethereum.CallMsg {
return ðereum.CallMsg{
From: c.From,
To: c.To,
Data: c.Data,
Value: c.Value.ToInt(),
}
}