forked from streamingfast/eth-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolidity.go
More file actions
475 lines (366 loc) · 12 KB
/
Copy pathsolidity.go
File metadata and controls
475 lines (366 loc) · 12 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
package eth
import (
"fmt"
"regexp"
"strconv"
"strings"
)
//go:generate go-enum -f=$GOFILE --noprefix --prefix Type --names
// ENUM(
//
// Boolean
// Address
// SignedInteger
// UnsignedInteger
// SignedFixedPoint
// UsignedFixedPoint
// FixedSizeBytes
// Bytes
// String
// FixedSizeArray
// Array
// Struct
//
// Mappings
// )
type TypeKind int
type SolidityType interface {
Name() string
Kind() TypeKind
IsDynamic() bool
}
var arrayRegex = regexp.MustCompile(`^(.+)\[]$`)
var integerRegex = regexp.MustCompile(`^(u)?int([0-9]+)$`)
var fixedBytesRegex = regexp.MustCompile(`^bytes([0-9]+)$`)
var fixedPointRegex = regexp.MustCompile(`^(u)?fixed([0-9]+)x([0-9]+)$`)
var fixedArrayRegex = regexp.MustCompile(`^(.+)\[([0-9]+)\]$`)
func ParseType(raw string) (SolidityType, error) {
in := strings.ToLower(raw)
switch in {
case "bool":
return BooleanType{}, nil
case "address":
return AddressType{}, nil
case "int":
return SignedIntegerType{BitsSize: 256, ByteSize: 32}, nil
case "uint":
return UnsignedIntegerType{BitsSize: 256, ByteSize: 32}, nil
case "bytes":
return BytesType{}, nil
case "string":
return StringType{}, nil
case "fixed":
return SignedFixedPointType{BitsSize: 128, ByteSize: 16, Decimals: 18}, nil
case "ufixed":
return UnsignedFixedPointType{BitsSize: 128, ByteSize: 16, Decimals: 18}, nil
case "tuple":
return StructType{}, nil
}
if matches := integerRegex.FindAllStringSubmatch(in, 1); len(matches) == 1 {
return parseIntegerType(raw, matches[0])
}
if matches := fixedBytesRegex.FindAllStringSubmatch(in, 1); len(matches) == 1 {
return parseFixedBytes(raw, matches[0])
}
if matches := fixedPointRegex.FindAllStringSubmatch(in, 1); len(matches) == 1 {
return parseFixedPointType(raw, matches[0])
}
if matches := fixedArrayRegex.FindAllStringSubmatch(in, 1); len(matches) == 1 {
return parseFixedArray(raw, matches[0])
}
if matches := arrayRegex.FindAllStringSubmatch(in, 1); len(matches) == 1 {
return parseArray(raw, matches[0])
}
return nil, fmt.Errorf("type %q unknown", raw)
}
func parseFixedBytes(raw string, matchGroups []string) (SolidityType, error) {
byteSizeRaw := matchGroups[1]
byteSizeRawU64, err := strconv.ParseUint(byteSizeRaw, 10, strconv.IntSize)
if err != nil {
panic(fmt.Errorf("impossible (regex validated) invalid fixed bytes type %q: byte size not a number: %w", raw, err))
}
byteSize := uint(byteSizeRawU64)
if byteSize < 1 {
return nil, fmt.Errorf("invalid fixed bytes type %q: bits size %d is lower than 1", raw, byteSize)
}
if byteSize > 32 {
return nil, fmt.Errorf("invalid fixed bytes type %q: bits size %d is bigger than 32", raw, byteSize)
}
return FixedSizeBytesType{ByteSize: byteSize}, nil
}
func parseIntegerType(raw string, matchGroups []string) (SolidityType, error) {
bitsSizeRaw := matchGroups[2]
bitsSizeU64, err := strconv.ParseUint(bitsSizeRaw, 10, strconv.IntSize)
if err != nil {
panic(fmt.Errorf("impossible (regex validated) invalid integer type %q: bits size not a number: %w", raw, err))
}
bitsSize := uint(bitsSizeU64)
if bitsSize < 8 {
return nil, fmt.Errorf("invalid integer type %q: bits size %d is lower than 8", raw, bitsSize)
}
if bitsSize > 256 {
return nil, fmt.Errorf("invalid integer type %q: bits size %d is bigger than 256", raw, bitsSize)
}
if bitsSize%8 != 0 {
return nil, fmt.Errorf("invalid integer type %q: bits size %d must be divisble by 8", raw, bitsSize)
}
byteSize := bitsSize / 8
if matchGroups[1] == "u" {
return UnsignedIntegerType{BitsSize: bitsSize, ByteSize: byteSize}, nil
}
return SignedIntegerType{BitsSize: bitsSize, ByteSize: byteSize}, nil
}
func parseFixedPointType(raw string, matchGroups []string) (SolidityType, error) {
// Bits Size
bitsSizeRaw := matchGroups[2]
bitsSizeU64, err := strconv.ParseUint(bitsSizeRaw, 10, strconv.IntSize)
if err != nil {
panic(fmt.Errorf("impossible (regex validated) invalid integer type %q: bits size not a number: %w", raw, err))
}
bitsSize := uint(bitsSizeU64)
if bitsSize < 8 {
return nil, fmt.Errorf("invalid fixed point type %q: bits size %d is lower than 8", raw, bitsSize)
}
if bitsSize > 256 {
return nil, fmt.Errorf("invalid fixed point type %q: bits size %d is bigger than 256", raw, bitsSize)
}
if bitsSize%8 != 0 {
return nil, fmt.Errorf("invalid fixed point type %q: bits size %d must be divisble by 8", raw, bitsSize)
}
byteSize := bitsSize / 8
// Decimals
decimalsRaw := matchGroups[3]
decimalsU64, err := strconv.ParseUint(decimalsRaw, 10, strconv.IntSize)
if err != nil {
panic(fmt.Errorf("impossible (regex validated) invalid fixed point type %q: decimals not a number: %w", raw, err))
}
decimals := uint(decimalsU64)
if decimals > 80 {
return nil, fmt.Errorf("invalid fixed point type %q: decimals %d is bigger than 80", raw, decimals)
}
if matchGroups[1] == "u" {
return UnsignedFixedPointType{BitsSize: bitsSize, ByteSize: byteSize, Decimals: decimals}, nil
}
return SignedFixedPointType{BitsSize: bitsSize, ByteSize: byteSize, Decimals: decimals}, nil
}
func parseArray(raw string, matchGroups []string) (SolidityType, error) {
// TODO: Handle recursion limit
elementTypeRaw := matchGroups[1]
elementType, err := ParseType(elementTypeRaw)
if err != nil {
return nil, fmt.Errorf("invalid array type %q: element type invalid: %w", raw, err)
}
return ArrayType{ElementType: elementType}, nil
}
func parseFixedArray(raw string, matchGroups []string) (SolidityType, error) {
// TODO: Handle recursion limit
elementTypeRaw := matchGroups[1]
elementType, err := ParseType(elementTypeRaw)
if err != nil {
return nil, fmt.Errorf("invalid fixed array type %q: element type invalid: %w", raw, err)
}
lenghtRaw := matchGroups[2]
lenghtRawU64, err := strconv.ParseUint(lenghtRaw, 10, strconv.IntSize)
if err != nil {
panic(fmt.Errorf("impossible (regex validated) invalid fixed array type %q: length not a number: %w", raw, err))
}
return FixedSizeArrayType{ElementType: elementType, Length: uint(lenghtRawU64)}, nil
}
var _ SolidityType = BooleanType{}
var _ SolidityType = AddressType{}
var _ SolidityType = SignedIntegerType{}
var _ SolidityType = UnsignedIntegerType{}
var _ SolidityType = SignedFixedPointType{}
var _ SolidityType = UnsignedFixedPointType{}
var _ SolidityType = FixedSizeBytesType{}
var _ SolidityType = BytesType{}
var _ SolidityType = StringType{}
var _ SolidityType = FixedSizeArrayType{}
var _ SolidityType = ArrayType{}
var _ SolidityType = StructType{}
type BooleanType struct {
}
func (t BooleanType) Name() string {
return t.Kind().String()
}
func (a BooleanType) Kind() TypeKind {
return TypeBoolean
}
func (t BooleanType) IsDynamic() bool {
return false
}
type AddressType struct {
}
func (t AddressType) Name() string {
return t.Kind().String()
}
func (a AddressType) Kind() TypeKind {
return TypeAddress
}
func (t AddressType) IsDynamic() bool {
return false
}
type SignedIntegerType struct {
// BitsSize is the number of bites taken by this type, BitsSize / 8 = ByteSize, range of values is between 8 and 256 (inclusive) by step of 8
BitsSize uint
// ByteSize is the number of bytes taken by this type, ByteSize * 8 = BitsSize, range of values is between 1 and 32 (inclusive)
ByteSize uint
}
func (t SignedIntegerType) Name() string {
return fmt.Sprintf("int%d", t.BitsSize)
}
func (t SignedIntegerType) Kind() TypeKind {
return TypeSignedInteger
}
func (t SignedIntegerType) IsDynamic() bool {
return false
}
type UnsignedIntegerType struct {
// BitsSize is the number of bites taken by this type, BitsSize / 8 = ByteSize, range of values is between 8 and 256 (inclusive) by step of 8
BitsSize uint
// ByteSize is the number of bytes taken by this type, ByteSize * 8 = BitsSize, range of values is between 1 and 32 (inclusive)
ByteSize uint
}
func (t UnsignedIntegerType) Name() string {
return fmt.Sprintf("uint%d", t.BitsSize)
}
func (t UnsignedIntegerType) Kind() TypeKind {
return TypeUnsignedInteger
}
func (t UnsignedIntegerType) IsDynamic() bool {
return false
}
type SignedFixedPointType struct {
// BitsSize is the number of bites taken by this type, BitsSize / 8 = ByteSize, range of values is between 8 and 256 (inclusive) by step of 8
BitsSize uint
// ByteSize is the number of bytes taken by this type, ByteSize * 8 = BitsSize, range of values is between 1 and 32 (inclusive)
ByteSize uint
// Decimals represents the precision in decimals of this fixed type and will be between 0 and 80 (inclusive)
Decimals uint
}
func (t SignedFixedPointType) Name() string {
return fmt.Sprintf("fixed%d", t.BitsSize)
}
func (t SignedFixedPointType) Kind() TypeKind {
return TypeSignedFixedPoint
}
func (t SignedFixedPointType) IsDynamic() bool {
return false
}
type UnsignedFixedPointType struct {
// BitsSize is the number of bites taken by this type, BitsSize / 8 = ByteSize, range of values is between 8 and 256 (inclusive) by step of 8
BitsSize uint
// ByteSize is the number of bytes taken by this type, ByteSize * 8 = BitsSize, range of values is between 1 and 32 (inclusive)
ByteSize uint
// Decimals represents the precision in decimals of this fixed type and will be between 0 and 80 (inclusive)
Decimals uint
}
func (t UnsignedFixedPointType) Name() string {
return t.Kind().String()
}
func (t UnsignedFixedPointType) Kind() TypeKind {
return TypeUsignedFixedPoint
}
func (t UnsignedFixedPointType) IsDynamic() bool {
return false
}
type FixedSizeBytesType struct {
// ByteSize is the number of bytes taken by this type, range of values is between 1 and 32 (inclusive)
ByteSize uint
}
func (t FixedSizeBytesType) Name() string {
return t.Kind().String()
}
func (t FixedSizeBytesType) Kind() TypeKind {
return TypeFixedSizeBytes
}
func (t FixedSizeBytesType) IsDynamic() bool {
return false
}
type BytesType struct {
}
func (t BytesType) Name() string {
return t.Kind().String()
}
func (t BytesType) Kind() TypeKind {
return TypeBytes
}
func (t BytesType) IsDynamic() bool {
return true
}
type StringType struct {
}
func (t StringType) Name() string {
return t.Kind().String()
}
func (t StringType) Kind() TypeKind {
return TypeString
}
func (t StringType) IsDynamic() bool {
return true
}
type FixedSizeArrayType struct {
// ElementType represents the underlying stored element
ElementType SolidityType
// Length is the number of fixed element this array contains
Length uint
}
func (t FixedSizeArrayType) Name() string {
return fmt.Sprintf("%s[%d]", t.ElementType, t.Length)
}
func (t FixedSizeArrayType) Kind() TypeKind {
return TypeFixedSizeArray
}
func (t FixedSizeArrayType) IsDynamic() bool {
return t.ElementType.IsDynamic()
}
type ArrayType struct {
// ElementType represents the underlying stored element
ElementType SolidityType
}
func (t ArrayType) Name() string {
return fmt.Sprintf("%s[]", t.ElementType)
}
func (t ArrayType) Kind() TypeKind {
return TypeArray
}
func (t ArrayType) IsDynamic() bool {
return true
}
type StructType struct {
}
func (t StructType) Name() string {
return t.Kind().String()
}
func (t StructType) Kind() TypeKind {
return TypeStruct
}
func (t StructType) IsDynamic() bool {
return false
}
type StructComponent struct {
InternalType string
Name string
TypeName string
Type SolidityType
// Components holds nested struct components when TypeName is "tuple" or "tuple[]"
Components []*StructComponent
}
func (c *StructComponent) String() string {
return fmt.Sprintf("%s %s (%s)", c.TypeName, c.Name, c.InternalType)
}
// Signature returns the canonical type signature for this struct component,
// recursively expanding nested tuples. For example, a tuple containing
// (address, (uint256, string), uint64) returns "(address,(uint256,string),uint64)".
func (c *StructComponent) Signature() string {
typeName := c.TypeName
if strings.HasPrefix(typeName, "tuple") {
componentSigs := make([]string, len(c.Components))
for i, component := range c.Components {
componentSigs[i] = component.Signature()
}
// Replace "tuple" with "(sig1,sig2,...)" preserving any array suffix like [] or [N]
typeName = strings.Replace(typeName, "tuple", "("+strings.Join(componentSigs, ",")+")", 1)
}
return typeName
}