-
Notifications
You must be signed in to change notification settings - Fork 5
/
type.go
622 lines (533 loc) · 15.1 KB
/
type.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package abi
import (
"fmt"
"strings"
)
// Type is a representation of a type like uint256 or address. The type can be
// used to create a new value of that type, but it cannot store a value.
type Type interface {
// CanonicalType returns the canonical name of the type. In case of a
// tuple, the canonical name is the canonical name of the tuple's
// elements, separated by commas and enclosed in parentheses. Arrays
// are represented by the canonical name of the element type followed
// by square brackets with the array size.
CanonicalType() string
// String returns the user-friendly name of the type.
String() string
// Value creates a new zero value for the type.
Value() Value
}
// ParseType parses a type signature and returns a new Type.
//
// A type can be either an elementary type like uint256 or a tuple type. Tuple
// types are denoted by parentheses, with the optional keyword "tuple" before
// the parentheses. Parameter names are optional.
//
// The generated types can be used to create new values, which can then be used
// to encode or decode ABI data.
//
// Custom types may be added to the ABI.Types, this will allow the parser to
// handle them.
//
// The following examples are valid type signatures:
//
// uint256
// (uint256 a,bytes32 b)
// tuple(uint256 a, bytes32 b)[]
//
// This function is equivalent to calling Parser.ParseType with the default
// configuration.
func ParseType(signature string) (Type, error) {
return Default.ParseType(signature)
}
// ParseStruct parses a struct definition and returns a new Type.
//
// It is similar to ParseType, but accepts a struct definition instead of a
// type signature.
//
// For example, the following two calls are equivalent:
//
// ParseType("(uint256 a, bytes32 b)")
// ParseStruct("struct { uint256 a; bytes32 b; }")
func ParseStruct(definition string) (Type, error) {
return Default.ParseStruct(definition)
}
// MustParseType is like ParseType but panics on error.
func MustParseType(signature string) Type {
t, err := ParseType(signature)
if err != nil {
panic(err)
}
return t
}
// MustParseStruct is like ParseStruct but panics on error.
func MustParseStruct(definition string) Type {
t, err := ParseStruct(definition)
if err != nil {
panic(err)
}
return t
}
// ParseType parses a type signature and returns a new Type.
//
// See ParseType for more information.
func (a *ABI) ParseType(signature string) (Type, error) {
return parseType(a, signature)
}
// ParseStruct parses a struct definition and returns a new Type.
//
// See ParseStruct for more information.
func (a *ABI) ParseStruct(definition string) (Type, error) {
return parseStruct(a, definition)
}
// AliasType wraps another type and gives it a different type name. The canonical
// type name is the same as the wrapped type.
type AliasType struct {
alias string
typ Type
}
// NewAliasType creates a new alias type.
func NewAliasType(alias string, typ Type) *AliasType {
return &AliasType{alias: alias, typ: typ}
}
// CanonicalType implements the Type interface.
func (a *AliasType) CanonicalType() string {
return a.typ.CanonicalType()
}
// String implements the Type interface.
func (a *AliasType) String() string {
return a.alias
}
// Value implements the Type interface.
func (a *AliasType) Value() Value {
return a.typ.Value()
}
// TupleType represents a tuple type.
type TupleType struct {
elems []TupleTypeElem
}
// TupleTypeElem is an element of a tuple.
type TupleTypeElem struct {
// Name of the tuple element. It is used when mapping values from and to
// maps and structures. If the name is empty, when creating a new value
// the name will be set to argN, where N is the index of the element.
Name string
// Type is the type of the element.
Type Type
}
// NewTupleType creates a new tuple type with the given elements.
func NewTupleType(elems ...TupleTypeElem) *TupleType {
return &TupleType{elems: elems}
}
// Size returns the number of elements in the tuple.
func (t *TupleType) Size() int {
return len(t.elems)
}
// Elements returns the tuple elements.
func (t *TupleType) Elements() []TupleTypeElem {
cpy := make([]TupleTypeElem, len(t.elems))
copy(cpy, t.elems)
return cpy
}
// CanonicalType implements the Type interface.
func (t *TupleType) CanonicalType() string {
var buf strings.Builder
buf.WriteString("(")
for i, elem := range t.elems {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString(elem.Type.CanonicalType())
}
buf.WriteString(")")
return buf.String()
}
// String implements the Type interface.
func (t *TupleType) String() string {
var buf strings.Builder
buf.WriteString("(")
for i, elem := range t.elems {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(elem.Type.String())
if len(elem.Name) > 0 {
buf.WriteString(" ")
buf.WriteString(elem.Name)
}
}
buf.WriteString(")")
return buf.String()
}
// Value implements the Type interface.
func (t *TupleType) Value() Value {
v := make(TupleValue, len(t.elems))
for i, elem := range t.elems {
v[i] = TupleValueElem{
Name: elem.Name,
Value: elem.Type.Value(),
}
if len(elem.Name) == 0 {
v[i].Name = fmt.Sprintf("arg%d", i)
}
}
return &v
}
// EventTupleType represents a tuple type for event inputs. It works just like
// TupleType, but elements can be marked as indexed. When creating a new value,
// the indexed elements will be created first, followed by the non-indexed
// elements.
type EventTupleType struct {
elems []EventTupleElem
indexed int
}
// EventTupleElem is an element of an event tuple.
type EventTupleElem struct {
// Name of the tuple element. It is used when mapping values from and to
// maps and structures. If the name is empty, when creating a new value,
// the name will be set to topicN or dataN, where N is the index of the
// topic or data element. Topics are counted from 1 because the first topic
// is the event signature.
Name string
// Indexed indicates whether the element is indexed.
Indexed bool
// Type is the type of the element.
Type Type
}
// NewEventTupleType creates a new tuple type with the given elements.
func NewEventTupleType(elems ...EventTupleElem) *EventTupleType {
indexed := 0
for _, elem := range elems {
if elem.Indexed {
indexed++
}
}
return &EventTupleType{elems: elems, indexed: indexed}
}
// Size returns the number of elements in the tuple.
func (t *EventTupleType) Size() int {
return len(t.elems)
}
// IndexedSize returns the number of indexed elements in the tuple.
func (t *EventTupleType) IndexedSize() int {
return t.indexed
}
// DataSize returns the number of non-indexed elements in the tuple.
func (t *EventTupleType) DataSize() int {
return len(t.elems) - t.indexed
}
// Elements returns the tuple elements.
func (t *EventTupleType) Elements() []EventTupleElem {
cpy := make([]EventTupleElem, len(t.elems))
copy(cpy, t.elems)
return cpy
}
// TopicsTuple returns the tuple of indexed arguments.
func (t *EventTupleType) TopicsTuple() *TupleType {
topics := make([]TupleTypeElem, 0, t.indexed)
for _, elem := range t.elems {
if !elem.Indexed {
continue
}
name := elem.Name
if len(name) == 0 {
name = fmt.Sprintf("topic%d", len(topics))
}
topics = append(topics, TupleTypeElem{
Name: name,
Type: elem.Type,
})
}
return &TupleType{elems: topics}
}
// DataTuple returns the tuple of non-indexed arguments.
func (t *EventTupleType) DataTuple() *TupleType {
data := make([]TupleTypeElem, 0, len(t.elems)-t.indexed)
for _, elem := range t.elems {
if elem.Indexed {
continue
}
name := elem.Name
if len(name) == 0 {
name = fmt.Sprintf("data%d", len(data))
}
data = append(data, TupleTypeElem{
Name: name,
Type: elem.Type,
})
}
return &TupleType{elems: data}
}
// CanonicalType implements the Type interface.
func (t *EventTupleType) CanonicalType() string {
var buf strings.Builder
buf.WriteString("(")
for i, elem := range t.elems {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString(elem.Type.CanonicalType())
}
buf.WriteString(")")
return buf.String()
}
// String implements the Type interface.
func (t *EventTupleType) String() string {
var buf strings.Builder
buf.WriteString("(")
for i, elem := range t.elems {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(elem.Type.String())
if elem.Indexed {
buf.WriteString(" indexed")
}
if len(elem.Name) > 0 {
buf.WriteString(" ")
buf.WriteString(elem.Name)
}
}
buf.WriteString(")")
return buf.String()
}
// Value implements the Type interface.
func (t *EventTupleType) Value() Value {
v := make(TupleValue, len(t.elems))
// Fills tuple in such a way that indexed fields are first.
dataIdx, topicIdx := 0, 0
for _, elem := range t.elems {
idx := 0
if elem.Indexed {
idx = topicIdx
topicIdx++
} else {
idx = dataIdx + t.indexed
dataIdx++
}
v[idx] = TupleValueElem{
Name: elem.Name,
Value: elem.Type.Value(),
}
if len(elem.Name) == 0 {
if elem.Indexed {
v[idx].Name = fmt.Sprintf("topic%d", topicIdx)
} else {
v[idx].Name = fmt.Sprintf("data%d", dataIdx-1)
}
}
}
return &v
}
// ArrayType represents an unbounded array type.
type ArrayType struct {
typ Type
}
// NewArrayType creates a dynamic array type with the given element type.
func NewArrayType(typ Type) *ArrayType {
return &ArrayType{typ: typ}
}
// ElementType returns the type of the array elements.
func (a *ArrayType) ElementType() Type {
return a.typ
}
// CanonicalType implements the Type interface.
func (a *ArrayType) CanonicalType() string {
return a.typ.CanonicalType() + "[]"
}
// String implements the Type interface.
func (a *ArrayType) String() string {
return a.typ.String() + "[]"
}
// Value implements the Type interface.
func (a *ArrayType) Value() Value {
return &ArrayValue{Type: a.typ}
}
// FixedArrayType represents a fixed-size array type.
type FixedArrayType struct {
typ Type
size int
}
// NewFixedArrayType creates a new fixed array type with the given element type
// and size.
func NewFixedArrayType(typ Type, size int) *FixedArrayType {
if size <= 0 {
panic(fmt.Errorf("abi: invalid array size %d", size))
}
return &FixedArrayType{typ: typ, size: size}
}
// Size returns the size of the array.
func (f *FixedArrayType) Size() int {
return f.size
}
// ElementType returns the type of the array elements.
func (f *FixedArrayType) ElementType() Type {
return f.typ
}
// CanonicalType implements the Type interface.
func (f *FixedArrayType) CanonicalType() string {
return f.typ.CanonicalType() + fmt.Sprintf("[%d]", f.size)
}
// String implements the Type interface.
func (f *FixedArrayType) String() string {
return f.typ.String() + fmt.Sprintf("[%d]", f.size)
}
// Value implements the Type interface.
func (f *FixedArrayType) Value() Value {
elems := make([]Value, f.size)
for i := range elems {
elems[i] = f.typ.Value()
}
return (*FixedArrayValue)(&elems)
}
// BytesType represents a bytes type.
type BytesType struct{}
// NewBytesType creates a new "bytes" type.
func NewBytesType() *BytesType {
return &BytesType{}
}
// CanonicalType implements the Type interface.
func (b *BytesType) CanonicalType() string {
return "bytes"
}
// String implements the Type interface.
func (b *BytesType) String() string {
return "bytes"
}
// Value implements the Type interface.
func (b *BytesType) Value() Value {
return &BytesValue{}
}
// StringType represents a string type.
type StringType struct{}
// NewStringType creates a new "string" type.
func NewStringType() *StringType {
return &StringType{}
}
// Type implements the Type interface.
func (s *StringType) String() string {
return "string"
}
// CanonicalType implements the Type interface.
func (s *StringType) CanonicalType() string {
return "string"
}
// Value implements the Type interface.
func (s *StringType) Value() Value {
return new(StringValue)
}
// FixedBytesType represents a fixed-size bytes type.
type FixedBytesType struct{ size int }
// NewFixedBytesType creates a new fixed-size bytes type with the given size.
// The size must be between 1 and 32.
func NewFixedBytesType(size int) *FixedBytesType {
if size < 0 || size > 32 {
panic(fmt.Sprintf("abi: invalid fixed bytes size %d", size))
}
return &FixedBytesType{size: size}
}
// Size returns the size of the bytes type.
func (f *FixedBytesType) Size() int {
return f.size
}
// CanonicalType implements the Type interface.
func (f *FixedBytesType) CanonicalType() string {
return fmt.Sprintf("bytes%d", f.size)
}
// String implements the Type interface.
func (f *FixedBytesType) String() string {
return fmt.Sprintf("bytes%d", f.size)
}
// Value implements the Type interface.
func (f *FixedBytesType) Value() Value {
b := make(FixedBytesValue, f.size)
return &b
}
// UintType represents an unsigned integer type.
type UintType struct{ size int }
// NewUintType creates a new "uint" type with the given size. The size must be
// between 8 and 256 and a multiple of 8.
func NewUintType(size int) *UintType {
if size < 0 || size > 256 || size%8 != 0 {
panic(fmt.Errorf("abi: invalid uint size %d", size))
}
return &UintType{size: size}
}
// Size returns the size of the uint type.
func (u *UintType) Size() int {
return u.size
}
// CanonicalType implements the Type interface.
func (u *UintType) CanonicalType() string {
return fmt.Sprintf("uint%d", u.size)
}
// String implements the Type interface.
func (u *UintType) String() string {
return fmt.Sprintf("uint%d", u.size)
}
// Value implements the Type interface.
func (u *UintType) Value() Value {
return &UintValue{Size: u.size}
}
// IntType represents a signed integer type.
type IntType struct{ size int }
// NewIntType creates a new "int" type with the given size. The size must be
// between 8 and 256 and a multiple of 8.
func NewIntType(size int) *IntType {
if size < 0 || size > 256 || size%8 != 0 {
panic(fmt.Errorf("abi: invalid int size %d", size))
}
return &IntType{size: size}
}
// Size returns the size of the int type.
func (i *IntType) Size() int {
return i.size
}
// Type implements the Type interface.
func (i *IntType) String() string {
return fmt.Sprintf("int%d", i.size)
}
// CanonicalType implements the Type interface.
func (i *IntType) CanonicalType() string {
return fmt.Sprintf("int%d", i.size)
}
// Value implements the Type interface.
func (i *IntType) Value() Value {
return &IntValue{Size: i.size}
}
// BoolType represents a boolean type.
type BoolType struct{}
// NewBoolType creates a new "bool" type.
func NewBoolType() *BoolType {
return &BoolType{}
}
// CanonicalType implements the Type interface.
func (b *BoolType) CanonicalType() string {
return "bool"
}
// String implements the Type interface.
func (b *BoolType) String() string {
return "bool"
}
// Value implements the Type interface.
func (b *BoolType) Value() Value {
return new(BoolValue)
}
// AddressType represents an address type.
type AddressType struct{}
// NewAddressType creates a new "address" type.
func NewAddressType() *AddressType {
return &AddressType{}
}
// CanonicalType implements the Type interface.
func (a *AddressType) CanonicalType() string {
return "address"
}
// String implements the Type interface.
func (a *AddressType) String() string {
return "address"
}
// Value implements the Type interface.
func (a *AddressType) Value() Value {
return new(AddressValue)
}