-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
66 lines (55 loc) · 1.24 KB
/
decoder.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
package tbsdk
import (
"fmt"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
)
type Decoder interface {
Decode(interface{}) error
}
type Int64ChainId struct {
Value int64
}
func (i *Int64ChainId) Decode(rawResult interface{}) error {
if rawResult == nil {
return fmt.Errorf("rawResult not found")
}
hexVal, ok := rawResult.(string)
if !ok {
return fmt.Errorf("invalid type, expected string for rawResult")
}
result, err := strconv.ParseInt(strings.TrimPrefix(hexVal, "0x"), 16, 64)
if err != nil {
return err
}
i.Value = result
return nil
}
type AddressArrayEntryPoints struct {
Value []common.Address
}
func (i *AddressArrayEntryPoints) Decode(rawResult interface{}) error {
if rawResult == nil {
return fmt.Errorf("rawResult not found")
}
resSlice, ok := rawResult.([]interface{})
if !ok {
return fmt.Errorf("invalid type, expected []interface{} for rawResult")
}
for _, v := range resSlice {
strValue, ok := v.(string)
if !ok {
return fmt.Errorf("invalid type, expected string for strValue")
}
i.Value = append(i.Value, common.HexToAddress(strValue))
}
return nil
}
func DecodeResult[T Decoder](r interface{}, dest T) error {
err := dest.Decode(r)
if err != nil {
return err
}
return nil
}