-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract.go
571 lines (535 loc) · 15.4 KB
/
contract.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package abi
import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/defiweb/go-sigparser"
)
// Contract provides a high-level API for interacting with a contract. It can
// be created from a JSON ABI definition using the ParseJSON function or from
// a list of signatures using the ParseSignatures function.
type Contract struct {
Constructor *Constructor
Methods map[string]*Method
MethodsBySignature map[string]*Method
Events map[string]*Event
Errors map[string]*Error
Types map[string]Type // Types defined in the ABI (structs, enums and user-defined Value Types)
}
// IsError returns true if the given error data, returned by a contract call,
// corresponds to a revert, panic, or custom error.
func (c *Contract) IsError(data []byte) bool {
if IsRevert(data) || IsPanic(data) {
return true
}
for _, err := range c.Errors {
if err.Is(data) {
return true
}
}
return false
}
// ToError returns an error if the given error data, returned by a contract
// call, corresponds to a revert, panic, or a custom error. It returns nil if
// the data cannot be recognized as an error.
func (c *Contract) ToError(data []byte) error {
if IsRevert(data) {
return RevertError{Reason: DecodeRevert(data)}
}
if IsPanic(data) {
return PanicError{Code: DecodePanic(data)}
}
for _, err := range c.Errors {
if err.Is(data) {
return CustomError{Type: err, Data: data}
}
}
return nil
}
// HandleError converts an error returned by a contract call to a RevertError,
// PanicError, or CustomError if applicable. If not, it returns the original
// error.
func (c *Contract) HandleError(err error) error {
if err == nil {
return nil
}
var dataErr interface{ RPCErrorData() any }
if !errors.As(err, &dataErr) {
return err
}
data, ok := dataErr.RPCErrorData().([]byte)
if !ok {
return err
}
if err := c.ToError(data); err != nil {
return err
}
return err
}
// RegisterTypes registers types defined in the contract to the given ABI
// instance. This enables the use of types defined in the contract in all
// Parse* methods.
//
// If the type name already exists, it will be overwritten.
func (c *Contract) RegisterTypes(a *ABI) {
for n, t := range c.Types {
a.Types[n] = t
}
}
// LoadJSON loads the ABI from the given JSON file and returns a Contract
// instance.
func LoadJSON(path string) (*Contract, error) {
return Default.LoadJSON(path)
}
// MustLoadJSON is like LoadJSON but panics on error.
func MustLoadJSON(path string) *Contract {
return Default.MustLoadJSON(path)
}
// ParseJSON parses the given ABI JSON and returns a Contract instance.
func ParseJSON(data []byte) (*Contract, error) {
return Default.ParseJSON(data)
}
// MustParseJSON is like ParseJSON but panics on error.
func MustParseJSON(data []byte) *Contract {
return Default.MustParseJSON(data)
}
// ParseSignatures parses list of signatures and returns a Contract instance.
// Signatures must be prefixed with the kind, e.g. "function" or "event".
//
// It accepts signatures in the same format as ParseConstructor, ParseMethod,
// ParseEvent, and ParseError functions.
func ParseSignatures(signatures ...string) (*Contract, error) {
return Default.ParseSignatures(signatures...)
}
// MustParseSignatures is like ParseSignatures but panics on error.
func MustParseSignatures(signatures ...string) *Contract {
return Default.MustParseSignatures(signatures...)
}
// LoadJSON loads the ABI from the given JSON file and returns a Contract
// instance.
func (a *ABI) LoadJSON(path string) (*Contract, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return a.ParseJSON(data)
}
// MustLoadJSON is like LoadJSON but panics on error.
func (a *ABI) MustLoadJSON(path string) *Contract {
c, err := a.LoadJSON(path)
if err != nil {
panic(err)
}
return c
}
// ParseJSON parses the given ABI JSON and returns a Contract instance.
func (a *ABI) ParseJSON(data []byte) (*Contract, error) {
var fields []jsonField
if err := json.Unmarshal(data, &fields); err != nil {
return nil, err
}
c := &Contract{
Methods: make(map[string]*Method),
MethodsBySignature: make(map[string]*Method),
Events: make(map[string]*Event),
Errors: make(map[string]*Error),
Types: make(map[string]Type),
}
for _, f := range fields {
inputs, err := f.Inputs.toTypes(a)
if err != nil {
return nil, err
}
outputs, err := f.Outputs.toTypes(a)
if err != nil {
return nil, err
}
for _, k := range []int{kindType, kindStruct, kindEnum} {
for n, t := range inputs.internalTypes(k) {
c.Types[n] = t
}
for n, t := range outputs.internalTypes(k) {
c.Types[n] = t
}
}
switch f.Type {
case "constructor":
c.Constructor = a.NewConstructor(inputs.toTupleType())
case "function", "":
method := a.NewMethod(
f.Name,
inputs.toTupleType(),
outputs.toTupleType(),
StateMutabilityFromString(f.StateMutability),
)
c.Methods[f.Name] = method
c.MethodsBySignature[method.Signature()] = method
case "event":
c.Events[f.Name] = a.NewEvent(f.Name, inputs.toEventTupleType(), f.Anonymous)
case "error":
c.Errors[f.Name] = a.NewError(f.Name, inputs.toTupleType())
case "fallback":
case "receive":
default:
return nil, fmt.Errorf("unknown type: %s", f.Type)
}
}
return c, nil
}
// MustParseJSON is like ParseJSON but panics on error.
func (a *ABI) MustParseJSON(data []byte) *Contract {
c, err := a.ParseJSON(data)
if err != nil {
panic(err)
}
return c
}
// ParseSignatures parses list of signatures and returns a Contract instance.
// Signatures must be prefixed with the kind, e.g. "constructor" or "event".
// For functions, the "function" prefix can be omitted.
//
// In case of duplicate function, event or error names, a counter will be
// appended to the name starting from 2.
func (a *ABI) ParseSignatures(signatures ...string) (*Contract, error) {
c := &Contract{
Methods: make(map[string]*Method),
MethodsBySignature: make(map[string]*Method),
Events: make(map[string]*Event),
Errors: make(map[string]*Error),
Types: make(map[string]Type),
}
extraTypes := map[string]Type{}
for _, s := range signatures {
switch sigparser.Kind(s) {
case sigparser.StructDefinitionInput:
typ, err := sigparser.ParseStruct(s)
if err != nil {
return nil, err
}
if typ.Name == "" {
return nil, errors.New("struct must have a name")
}
alias, err := newTypeFromSig(a, extraTypes, typ)
if err != nil {
return nil, err
}
alias = NewAliasType(typ.Name, alias)
c.Types[typ.Name] = alias
extraTypes[typ.Name] = alias
case sigparser.TupleInput, sigparser.TypeInput, sigparser.ArrayInput:
typ, err := sigparser.ParseParameter(s)
if err != nil {
return nil, err
}
if typ.Name == "" {
return nil, errors.New("type must have a name")
}
alias, err := newTypeFromSig(a, extraTypes, typ)
if err != nil {
return nil, err
}
alias = NewAliasType(typ.Name, alias)
c.Types[typ.Name] = alias
extraTypes[typ.Name] = alias
case sigparser.ConstructorSignatureInput:
sig, err := sigparser.ParseSignatureAs(sigparser.ConstructorKind, s)
if err != nil {
return nil, err
}
constructor, err := newConstructorFromSig(a, extraTypes, sig)
if err != nil {
return nil, err
}
c.Constructor = constructor
case sigparser.FunctionSignatureInput:
sig, err := sigparser.ParseSignatureAs(sigparser.FunctionKind, s)
if err != nil {
return nil, err
}
method, err := newMethodFromSig(a, extraTypes, sig)
if err != nil {
return nil, err
}
appendWithCounter(c.Methods, method.Name(), method)
c.MethodsBySignature[method.Signature()] = method
case sigparser.EventSignatureInput:
sig, err := sigparser.ParseSignatureAs(sigparser.EventKind, s)
if err != nil {
return nil, err
}
event, err := newEventFromSig(a, extraTypes, sig)
if err != nil {
return nil, err
}
appendWithCounter(c.Events, event.Name(), event)
case sigparser.ErrorSignatureInput:
sig, err := sigparser.ParseSignatureAs(sigparser.ErrorKind, s)
if err != nil {
return nil, err
}
errsig, err := newErrorFromSig(a, extraTypes, sig)
if err != nil {
return nil, err
}
appendWithCounter(c.Errors, errsig.Name(), errsig)
default:
return nil, fmt.Errorf("invalid signature: %s", s)
}
}
return c, nil
}
// MustParseSignatures is like ParseSignatures but panics on error.
func (a *ABI) MustParseSignatures(signatures ...string) *Contract {
c, err := a.ParseSignatures(signatures...)
if err != nil {
panic(err)
}
return c
}
type jsonField struct {
Type string `json:"type"`
Name string `json:"name"`
Constant bool `json:"constant"`
Anonymous bool `json:"anonymous"`
StateMutability string `json:"stateMutability"`
Inputs jsonParameters `json:"inputs"`
Outputs jsonParameters `json:"outputs"`
}
type jsonParameters []jsonParameter
func (a jsonParameters) toTypes(abi *ABI) (types jsonABITypes, err error) {
types = make(jsonABITypes, len(a))
for i, p := range a {
var typ jsonABIType
if typ, err = p.toType(abi); err != nil {
return
}
types[i] = typ
}
return
}
type jsonParameter struct {
Name string `json:"name"`
Type string `json:"type"`
InternalType string `json:"internalType"`
Indexed bool `json:"indexed"`
Components jsonParameters `json:"components"`
}
// toType converts a JSON parameter to a parsed type.
//
// It also returns a map of internal types defined in the parameter.
func (a jsonParameter) toType(abi *ABI) (typ jsonABIType, err error) {
baseTyp, arrays, err := parseArrays(a.Type)
if err != nil {
return
}
var (
kind int
intName string
intNamespace string
)
// If the internal type is different from type, it means that the type
// is a custom type defined in the contract code. We can extract the name
// and use it as an alias. As a result, when printing signatures, the
// name from the contract code will be used instead of the type.
if len(a.InternalType) > 0 && a.Type != a.InternalType {
kind, intName, intNamespace = parseInternalType(a.InternalType)
}
typ.name = a.Name
typ.indexed = a.Indexed
typ.internalTypes = make(map[string]jsonABIType)
switch {
case len(arrays) > 0:
elemType := jsonParameter{
Name: a.Name,
Type: baseTyp,
InternalType: baseTyp,
Components: a.Components,
}
if typ, err = elemType.toType(abi); err != nil {
return
}
if len(intName) > 0 {
typ.typ = NewAliasType(intName, typ.typ)
}
typ.elemTyp = typ.typ
for i := len(arrays) - 1; i >= 0; i-- {
if arrays[i] == -1 {
typ.typ = NewArrayType(typ.typ)
} else {
typ.typ = NewFixedArrayType(typ.typ, arrays[i])
}
}
case len(a.Components) > 0:
tuple := make([]TupleTypeElem, len(a.Components))
for i, jsonComp := range a.Components {
var compTyp jsonABIType
compTyp, err = jsonComp.toType(abi)
if err != nil {
return
}
tuple[i].Type = compTyp.typ
tuple[i].Name = compTyp.name
for intName, intTyp := range typ.internalTypes {
typ.internalTypes[intName] = intTyp
}
}
typ.typ = NewTupleType(tuple...)
if len(intName) > 0 {
typ.typ = NewAliasType(intName, typ.typ)
}
default:
typ.typ = abi.Types[baseTyp]
if typ.typ == nil {
return jsonABIType{}, fmt.Errorf("abi: unknown type %q", a.Type)
}
if len(intName) > 0 {
typ.typ = NewAliasType(intName, typ.typ)
}
}
if typ.elemTyp == nil {
typ.elemTyp = typ.typ
}
if kind != kindNone {
typ.internalTypes[a.InternalType] = jsonABIType{
typ: typ.elemTyp,
elemTyp: typ.elemTyp,
name: intName,
namespace: intNamespace,
kind: kind,
}
}
return
}
// jsonABIType represents a ABI type extracted from a JSON ABI.
type jsonABIType struct {
typ Type // Type of the parameter.
elemTyp Type // In case of arrays, the type of the elements. Otherwise, the same as typ.
name string // Name of the parameter.
namespace string // Namespace of the parameter.
indexed bool // Whether the parameter is indexed.
kind int // Kind of the internal parameter, e.g. (enum, struct, type).
internalTypes map[string]jsonABIType // All internal types defined in the parameter.
}
type jsonABITypes []jsonABIType
func (a jsonABITypes) internalTypes(kind int) map[string]Type {
internalTypes := make(map[string]Type)
for _, param := range a {
for _, v := range param.internalTypes {
if v.kind != kind {
continue
}
internalTypes[v.name] = v.elemTyp
}
}
return internalTypes
}
// toTupleType converts parameters to a TupleType type.
func (a jsonABITypes) toTupleType() *TupleType {
tupleElements := make([]TupleTypeElem, len(a))
for i, param := range a {
tupleElements[i] = TupleTypeElem{
Name: param.name,
Type: param.typ,
}
}
return NewTupleType(tupleElements...)
}
// toEventTupleType converts parameters to a EventTupleType type.
func (a jsonABITypes) toEventTupleType() *EventTupleType {
tupleElements := make([]EventTupleElem, len(a))
for i, param := range a {
tupleElements[i] = EventTupleElem{
Name: param.name,
Indexed: param.indexed,
Type: param.typ,
}
}
return NewEventTupleType(tupleElements...)
}
// parseArray parses type name and returns the name and array dimensions.
// For example, "uint256[][3]" will return "uint256" and [-1, 3].
// For unbounded arrays, the dimension is -1.
func parseArrays(typ string) (baseTyp string, arrays []int, err error) {
openBracket := strings.Index(typ, "[")
if openBracket == -1 {
baseTyp = typ
return
}
baseTyp = typ[:openBracket]
for {
closeBracket := openBracket
for closeBracket < len(typ) && typ[closeBracket] != ']' {
closeBracket++
}
if openBracket >= closeBracket {
return "", nil, fmt.Errorf("abi: invalid type %q", typ)
}
n := typ[openBracket+1 : closeBracket]
if len(n) == 0 {
arrays = append(arrays, -1)
} else {
i, err := strconv.Atoi(n)
if err != nil {
return "", nil, err
}
if i <= 0 {
return "", nil, fmt.Errorf("abi: invalid array size %d", i)
}
arrays = append(arrays, i)
}
if closeBracket+1 == len(typ) {
break
}
openBracket = closeBracket + 1
}
return
}
const (
kindNone = iota
kindEnum
kindStruct
kindType
)
func parseInternalType(typ string) (int, string, string) {
if len(typ) == 0 {
return kindNone, "", ""
}
var (
kind int
prefixLen int
)
switch {
case strings.Index(typ, "struct ") == 0:
kind, prefixLen = kindStruct, 7
case strings.Index(typ, "enum ") == 0:
kind, prefixLen = kindEnum, 5
case !strings.Contains(typ, " "):
kind, prefixLen = kindType, 0
default:
return kindNone, "", ""
}
intName := typ[prefixLen:]
intNamespace := ""
if bracket := strings.Index(intName, "["); bracket != -1 {
intName = intName[:bracket]
}
if parts := strings.SplitN(intName, ".", 2); len(parts) == 2 {
intName, intNamespace = parts[1], parts[0]
}
return kind, intName, intNamespace
}
// appendWithCounter appends a value to a map. If the key already exists, it
// will be suffixed with a number.
func appendWithCounter[T any](m map[string]T, key string, value T) {
nextKey := key
for i := 0; ; i++ {
if _, ok := m[nextKey]; !ok {
m[nextKey] = value
return
}
nextKey = key + strconv.Itoa(i+2)
}
}