forked from streamingfast/eth-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
548 lines (434 loc) · 13 KB
/
Copy pathtypes.go
File metadata and controls
548 lines (434 loc) · 13 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
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
// Copyright 2021 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eth
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/holiman/uint256"
)
type Uint8 uint8
func (b *Uint8) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 8)
if err != nil {
return err
}
*b = Uint8(value)
return nil
}
type Uint16 uint16
func (b *Uint16) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 16)
if err != nil {
return err
}
*b = Uint16(value)
return nil
}
type Uint32 uint32
func (b *Uint32) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 32)
if err != nil {
return err
}
*b = Uint32(value)
return nil
}
type Uint64 uint64
func (b *Uint64) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 64)
if err != nil {
return err
}
*b = Uint64(value)
return nil
}
type Uint256 uint256.Int
func (b *Uint256) UnmarshalText(text []byte) error {
// we want to remove "0x0" (and any other zeroes) then we add back the leading "0x"
noZero := bytes.TrimLeft(text, "0x")
if len(noZero) == 0 {
text = []byte{'0', 'x', '0'}
} else {
text = append([]byte{'0', 'x'}, noZero...)
}
return (*uint256.Int)(b).UnmarshalText(text)
}
func (b *Uint256) MarshalText() ([]byte, error) {
return []byte((*uint256.Int)(b).Hex()), nil
}
func (b *Uint256) MarshalJSONRPC() ([]byte, error) {
return []byte(`"` + (*uint256.Int)(b).Hex() + `"`), nil
}
// FixedUint64 is a fixed size uint64, marshalled as a fixed 8 bytes big endian
type FixedUint64 uint64
func (b *FixedUint64) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 64)
if err != nil {
return err
}
*b = FixedUint64(value)
return nil
}
func (n *FixedUint64) MarshalJSONRPC() ([]byte, error) {
if n == nil {
return []byte(`"0x0000000000000000"`), nil
}
v := make([]byte, 8)
binary.BigEndian.PutUint64(v, uint64(*n))
return Hex(v).MarshalJSONRPC()
}
// Timestamp represents a timestamp value on the Ethereum chain always in UTC
// time zone. Recorded in unix seconds
type Timestamp time.Time
func (t Timestamp) MarshalJSONRPC() ([]byte, error) {
seconds := Uint64(time.Time(t).Unix())
v := make([]byte, 0, 16)
v = strconv.AppendUint(v, uint64(seconds), 16)
return []byte(`"0x` + string(v) + `"`), nil
}
func (t Timestamp) MarshalText() ([]byte, error) {
return []byte((time.Time)(t).Format(time.RFC3339)), nil
}
func (t *Timestamp) UnmarshalText(text []byte) error {
// Shall we deal with an actual date time string format also here?
value, err := parseUint(string(text), 64)
if err != nil {
return err
}
*t = Timestamp(time.Unix(int64(value), 0).UTC())
return nil
}
func parseUint(text string, bitSize int) (uint64, error) {
if len(text) == 0 {
return 0, nil
}
// If it's a hexadecimal string, let's parse it as-is
if strings.HasPrefix(text, "0x") || strings.HasPrefix(text, "0X") {
text = text[2:]
if text == "" {
return 0, nil
}
value, err := strconv.ParseUint(text, 16, bitSize)
if err != nil {
return 0, fmt.Errorf("invalid hex uint%d number: %w", bitSize, err)
}
return value, nil
}
// Otherwise, we assume it's a decimal number
value, err := strconv.ParseUint(text, 10, bitSize)
if err != nil {
return 0, fmt.Errorf("invalid uint%d number: %w", bitSize, err)
}
return value, nil
}
type Int int
func (b *Int) UnmarshalText(text []byte) error {
value, err := parseInt(string(text), 8)
if err != nil {
return err
}
*b = Int(value)
return nil
}
type Int8 int8
func (b *Int8) UnmarshalText(text []byte) error {
value, err := parseInt(string(text), 8)
if err != nil {
return err
}
*b = Int8(value)
return nil
}
type Int16 int16
func (b *Int16) UnmarshalText(text []byte) error {
value, err := parseInt(string(text), 16)
if err != nil {
return err
}
*b = Int16(value)
return nil
}
type Int32 int32
func (b *Int32) UnmarshalText(text []byte) error {
value, err := parseInt(string(text), 32)
if err != nil {
return err
}
*b = Int32(value)
return nil
}
type Int64 int64
func (b *Int64) UnmarshalText(text []byte) error {
value, err := parseInt(string(text), 64)
if err != nil {
return err
}
*b = Int64(value)
return nil
}
func parseInt(text string, bitSize int) (int64, error) {
if len(text) == 0 {
return 0, nil
}
// If it's a hexadecimal string, let's parse it as-is
if strings.HasPrefix(text, "0x") || strings.HasPrefix(text, "0X") {
text = text[2:]
if text == "" {
return 0, nil
}
value, err := strconv.ParseInt(text, 16, bitSize)
if err != nil {
return 0, fmt.Errorf("invalid hex int%d number: %w", bitSize, err)
}
return value, nil
}
// Otherwise, we assume it's a decimal number
value, err := strconv.ParseInt(text, 10, bitSize)
if err != nil {
return 0, fmt.Errorf("invalid int%d number: %w", bitSize, err)
}
return value, nil
}
type Bytes []byte
func MustNewBytes(input string) Bytes {
return Bytes(mustNewByteSlice("bytes", input))
}
func NewBytes(input string) (Bytes, error) {
out, err := newByteSlice("bytes", input)
if err != nil {
return nil, err
}
return Bytes(out), nil
}
func (h Bytes) String() string { return byteSlice(h).String() }
func (h Bytes) Pretty() string { return byteSlice(h).Pretty() }
func (h Bytes) Bytes() []byte { return h[:] }
func (h Bytes) MarshalText() ([]byte, error) { return byteSlice(h).MarshalText() }
func (h Bytes) ID() uint64 { return byteSlice(h).ID() }
func (h Bytes) MarshalJSON() ([]byte, error) { return byteSlice(h).MarshalJSON() }
func (h Bytes) MarshalJSONRPC() ([]byte, error) { return byteSlice(h).MarshalJSONRPC() }
func (h *Bytes) UnmarshalJSON(data []byte) error { return (*byteSlice)(h).UnmarshalJSON(data) }
func (h *Bytes) UnmarshalText(data []byte) error { return (*byteSlice)(h).UnmarshalText(data) }
type Hex []byte
func MustNewHex(input string) Hex {
return Hex(mustNewByteSlice("hex", input))
}
func NewHex(input string) (Hex, error) {
out, err := newByteSlice("hex", input)
if err != nil {
return nil, err
}
return Hex(out), nil
}
func (h Hex) String() string { return byteSlice(h).String() }
func (h Hex) Pretty() string { return byteSlice(h).Pretty() }
func (h Hex) Bytes() []byte { return h[:] }
func (h Hex) MarshalText() ([]byte, error) { return byteSlice(h).MarshalText() }
func (h Hex) ID() uint64 { return byteSlice(h).ID() }
func (h Hex) MarshalJSON() ([]byte, error) { return byteSlice(h).MarshalJSON() }
func (h Hex) MarshalJSONRPC() ([]byte, error) { return byteSlice(h).MarshalJSONRPC() }
func (h *Hex) UnmarshalJSON(data []byte) error { return (*byteSlice)(h).UnmarshalJSON(data) }
func (h *Hex) UnmarshalText(data []byte) error { return (*byteSlice)(h).UnmarshalText(data) }
type Hash []byte
func MustNewHash(input string) Hash {
return Hash(mustNewByteSlice("hash", input))
}
func NewHash(input string) (Hash, error) {
out, err := newByteSlice("hash", input)
if err != nil {
return nil, err
}
return Hash(out), nil
}
func (h Hash) String() string { return byteSlice(h).String() }
func (h Hash) Pretty() string { return byteSlice(h).Pretty() }
func (h Hash) Bytes() []byte { return h[:] }
func (h Hash) MarshalText() ([]byte, error) { return byteSlice(h).MarshalText() }
func (h Hash) ID() uint64 { return byteSlice(h).ID() }
func (h Hash) MarshalJSON() ([]byte, error) { return byteSlice(h).MarshalJSON() }
func (h Hash) MarshalJSONRPC() ([]byte, error) { return byteSlice(h).MarshalJSONRPC() }
func (h *Hash) UnmarshalJSON(data []byte) error { return (*byteSlice)(h).UnmarshalJSON(data) }
func (h *Hash) UnmarshalText(data []byte) error { return (*byteSlice)(h).UnmarshalText(data) }
type Address []byte
// MustNewAddress is exactly like [NewAddress] but it panics if an error occurrs.
//
// See [NewAdress] for detaiks.
func MustNewAddress(input string) Address {
out, err := NewAddress(input)
if err != nil {
panic(err)
}
return out
}
// MustNewAddressLoose is exactly like [NewAddressLoose] but it panics if an error occurrs.
//
// See [NewAddressLoose] for detaiks.
func MustNewAddressLoose(input string) Address {
out, err := NewAddressLoose(input)
if err != nil {
panic(err)
}
return out
}
// NewAddress creates an address where the input **must** contain exactly 20 bytes.
//
// If the actual input is not an hexadecimal string, an error is returned or the decoded
// lenght is not exactly 20 bytes, an error is returned.
func NewAddress(input string) (Address, error) {
out, err := newByteSlice("address", input)
if err != nil {
return nil, err
}
if len(out) != 20 {
return nil, fmt.Errorf("invalid length in bytes, wanted 20 bytes once decoded but decoding gave us %d bytes", len(out))
}
return Address(out), nil
}
// NewAddressLoose creates an address where the input can contain more than 20 bytes (truncated) or
// less than 20 bytes (left as-is).
//
// If the actual input is not an hexadecimal string, an error is returned.
func NewAddressLoose(input string) (Address, error) {
out, err := newByteSlice("address", input)
if err != nil {
return nil, err
}
byteCount := len(out)
if byteCount > 20 {
out = out[byteCount-20:]
}
return Address(out), nil
}
func (a Address) String() string { return byteSlice(a).String() }
func (a Address) Pretty() string { return byteSlice(a).Pretty() }
func (a Address) Bytes() []byte { return a[:] }
func (a Address) MarshalText() ([]byte, error) { return byteSlice(a).MarshalText() }
func (a Address) ID() uint64 { return byteSlice(a).ID() }
func (a Address) MarshalJSON() ([]byte, error) { return byteSlice(a).MarshalJSON() }
func (a Address) MarshalJSONRPC() ([]byte, error) { return byteSlice(a).MarshalJSONRPC() }
func (a *Address) UnmarshalJSON(data []byte) error { return (*byteSlice)(a).UnmarshalJSON(data) }
func (a *Address) UnmarshalText(data []byte) error { return (*byteSlice)(a).UnmarshalText(data) }
type byteSlice []byte
func mustNewByteSlice(tag string, input string) byteSlice {
out, err := newByteSlice(tag, input)
if err != nil {
panic(err)
}
return out
}
func newByteSlice(tag string, input string) (out byteSlice, err error) {
bytes, err := hex.DecodeString(SanitizeHex(input))
if err != nil {
return out, fmt.Errorf("invalid %s %q: %w", tag, input, err)
}
return bytes, nil
}
func (b byteSlice) String() string {
return hex.EncodeToString(b)
}
func (b byteSlice) Pretty() string {
return "0x" + hex.EncodeToString(b)
}
func (b byteSlice) Bytes() []byte {
return b
}
func (b byteSlice) MarshalText() ([]byte, error) {
return []byte(b.String()), nil
}
func (b byteSlice) ID() uint64 {
return binary.LittleEndian.Uint64(b)
}
func (b byteSlice) MarshalJSON() ([]byte, error) {
return []byte(`"` + hex.EncodeToString([]byte(b)) + `"`), nil
}
func (b byteSlice) MarshalJSONRPC() ([]byte, error) {
return []byte(`"` + b.Pretty() + `"`), nil
}
func (b *byteSlice) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
return b.UnmarshalText([]byte(s))
}
func (b *byteSlice) UnmarshalText(text []byte) error {
s := strings.TrimPrefix(string(text), "0x")
if len(s)%2 != 0 {
s = "0" + s
}
var err error
if *b, err = hex.DecodeString(s); err != nil {
return err
}
return nil
}
type Topic [32]byte
func (f Topic) MarshalJSONRPC() ([]byte, error) {
return []byte(`"0x` + hex.EncodeToString(f[:]) + `"`), nil
}
func LogTopic(in interface{}) *Topic {
switch v := in.(type) {
case string:
return padToTopic(MustNewHash(v))
case []byte:
return padToTopic(v)
case Hash:
return padToTopic(v)
case Hex:
return padToTopic(v)
case Address:
return padToTopic(v)
case nil:
return nil
default:
valueOf := reflect.ValueOf(v)
if valueOf.Kind() == reflect.Ptr && valueOf.IsNil() {
return nil
}
panic(fmt.Errorf("don't know how to turn %T into a LogTopic", in))
}
}
func padToTopic(in []byte) (out *Topic) {
startOffset := 32 - len(in)
if startOffset < 0 {
startOffset = 0
}
var topic Topic
copy(topic[startOffset:], in)
return &topic
}
//go:generate go-enum -f=$GOFILE --noprefix --prefix TxType --lower --names
// ENUM(
//
// Legacy
// AccessList
// DynamicFee
//
// )
type TransactionType uint8
func (b *TransactionType) UnmarshalText(text []byte) error {
value, err := parseUint(string(text), 8)
if err != nil {
return err
}
*b = TransactionType(value)
return nil
}