forked from streamingfast/eth-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
414 lines (327 loc) · 9.62 KB
/
types.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
// 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 (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
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
}
// Timestamp represents a timestamp value on the Ethereum chain always in UTC
// time zone.
type Timestamp time.Time
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 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) }
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) }
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) }
type Address []byte
func MustNewAddress(input string) Address {
out, err := NewAddress(input)
if err != nil {
panic(err)
}
return out
}
func NewAddress(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) }
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
}
s = strings.TrimPrefix(s, "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
}