-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathethabi.go
More file actions
109 lines (83 loc) · 2.68 KB
/
ethabi.go
File metadata and controls
109 lines (83 loc) · 2.68 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
package ethabi
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/jurshsmith/ethabi-go/utils"
)
func ParseABI(humanReadableAbi *string) (abi.ABI, error) {
if strings.HasPrefix(*humanReadableAbi, string(Event)) == true {
eventAbi := New(humanReadableAbi, Event)
eventAbiJson, _ := json.Marshal(&eventAbi)
eventAbiJsonString := fmt.Sprintf("[%s]", string(eventAbiJson))
abiReader := strings.NewReader(eventAbiJsonString)
return abi.JSON(abiReader)
}
return abi.ABI{}, errors.New("HumanReadableABI is either invalid or unsupported")
}
func GetABIName(humanReadableAbi *string) string {
abiTokens := strings.Split(*humanReadableAbi, "(")
abiTokens = strings.Split(abiTokens[0], " ")
return abiTokens[1]
}
type Abi struct {
Type string `json:"type"`
Name string `json:"name"`
Inputs []AbiInput `json:"inputs"`
Anonymous bool `json:"anonymous"`
}
type AbiType string
const (
Event = AbiType("event")
)
func New(humanReadableAbi *string, Type AbiType) Abi {
name := GetABIName(humanReadableAbi)
inputs := NewAbiInputs(humanReadableAbi)
return Abi{Type: string(Type), Name: name, Inputs: inputs, Anonymous: false}
}
type AbiInput struct {
Type string `json:"type"`
InternalType string `json:"internalType"`
Name string `json:"name"`
Indexed bool `json:"indexed"`
}
func NewAbiInputs(humanReadableAbi *string) []AbiInput {
inputTokens := getInputTokens(humanReadableAbi)
return utils.MapOverSlice(inputTokens, NewAbiInput)
}
func getInputTokens(humanReadableAbi *string) []string {
abi := removeInputTokensBeforePart(humanReadableAbi)
abi = removeInputTokensAfterPart(&abi)
return strings.Split(abi, ",")
}
func removeInputTokensBeforePart(humanReadableAbi *string) string {
abiTokens := strings.Split(*humanReadableAbi, "(")
return abiTokens[1]
}
func removeInputTokensAfterPart(humanReadableAbi *string) string {
abiTokens := strings.Split(*humanReadableAbi, ")")
return abiTokens[0]
}
func NewAbiInput(inputToken *string) AbiInput {
sanitizedInputToken := strings.TrimSpace(*inputToken)
inputTokenParts := strings.Split(sanitizedInputToken, " ")
theType := inputTokenParts[0]
theType = maybeNormalizeType(&theType)
name, indexed := getNameAndIndexStatus(&inputTokenParts)
return AbiInput{Type: theType, InternalType: theType, Name: name, Indexed: indexed}
}
func maybeNormalizeType(theType *string) string {
if *theType == "uint" {
return "uint256"
}
return *theType
}
func getNameAndIndexStatus(inputTokenParts *[]string) (string, bool) {
if len(*inputTokenParts) == 3 {
return (*inputTokenParts)[2], true
} else {
return (*inputTokenParts)[1], false
}
}