-
Notifications
You must be signed in to change notification settings - Fork 5
/
contract.go
293 lines (276 loc) · 7.67 KB
/
contract.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
package abi
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/defiweb/go-sigparser"
)
// Contract provides a high-level API for interacting with a contract. It can
// be created from a JSON ABI definition using the ParseJSON function or from
// a list of signatures using the ParseSignatures function.
type Contract struct {
Constructor *Constructor
Methods map[string]*Method
MethodsBySignature map[string]*Method
Events map[string]*Event
Errors map[string]*Error
}
// LoadJSON loads the ABI from the given JSON file and returns a Contract instance.
func LoadJSON(path string) (*Contract, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return ParseJSON(data)
}
// ParseJSON parses the given ABI JSON and returns a Contract instance.
func ParseJSON(data []byte) (*Contract, error) {
return Default.ParseJSON(data)
}
// ParseSignatures parses list of signatures and returns a Contract instance.
// Signatures must be prefixed with the kind, e.g. "function" or "event".
//
// It accepts signatures in the same format as ParseConstructor, ParseMethod,
// ParseEvent, and ParseError functions.
func ParseSignatures(signatures ...string) (*Contract, error) {
return Default.ParseSignatures(signatures...)
}
// MustParseJSON is like ParseJSON but panics on error.
func MustParseJSON(data []byte) *Contract {
abi, err := ParseJSON(data)
if err != nil {
panic(err)
}
return abi
}
// MustParseSignatures is like ParseSignatures but panics on error.
func MustParseSignatures(signatures ...string) *Contract {
abi, err := ParseSignatures(signatures...)
if err != nil {
panic(err)
}
return abi
}
// ParseJSON parses the given ABI JSON and returns a Contract instance.
func (a *ABI) ParseJSON(data []byte) (*Contract, error) {
var fields []jsonField
if err := json.Unmarshal(data, &fields); err != nil {
return nil, err
}
abi := &Contract{
Methods: make(map[string]*Method),
MethodsBySignature: make(map[string]*Method),
Events: make(map[string]*Event),
Errors: make(map[string]*Error),
}
for _, f := range fields {
switch f.Type {
case "constructor":
inputs, err := f.Inputs.toTupleType(a)
if err != nil {
return nil, err
}
abi.Constructor = a.NewConstructor(inputs)
case "function", "":
inputs, err := f.Inputs.toTupleType(a)
if err != nil {
return nil, err
}
outputs, err := f.Outputs.toTupleType(a)
if err != nil {
return nil, err
}
method := a.NewMethod(f.Name, inputs, outputs)
abi.Methods[f.Name] = method
abi.MethodsBySignature[method.Signature()] = method
case "event":
inputs, err := f.Inputs.toEventTupleType(a)
if err != nil {
return nil, err
}
abi.Events[f.Name] = a.NewEvent(f.Name, inputs, f.Anonymous)
case "error":
inputs, err := f.Inputs.toTupleType(a)
if err != nil {
return nil, err
}
abi.Errors[f.Name] = a.NewError(f.Name, inputs)
case "fallback":
case "receive":
default:
return nil, fmt.Errorf("unknown type: %s", f.Type)
}
}
return abi, nil
}
// ParseSignatures parses list of signatures and returns a Contract instance.
// Signatures must be prefixed with the kind, e.g. "constructor" or "event".
// For functions, the "function" prefix can be omitted.
func (a *ABI) ParseSignatures(signatures ...string) (*Contract, error) {
abi := &Contract{
Methods: make(map[string]*Method),
MethodsBySignature: make(map[string]*Method),
Events: make(map[string]*Event),
Errors: make(map[string]*Error),
}
for _, s := range signatures {
sig, err := sigparser.ParseSignature(s)
if err != nil {
return nil, err
}
switch sig.Kind {
case sigparser.ConstructorKind:
constructor, err := newConstructorFromSig(a, sig)
if err != nil {
return nil, err
}
abi.Constructor = constructor
case sigparser.FunctionKind, sigparser.UnknownKind:
method, err := newMethodFromSig(a, sig)
if err != nil {
return nil, err
}
abi.Methods[method.Name()] = method
abi.MethodsBySignature[method.Signature()] = method
case sigparser.EventKind:
event, err := newEventFromSig(a, sig)
if err != nil {
return nil, err
}
abi.Events[event.Name()] = event
case sigparser.ErrorKind:
errsig, err := newErrorFromSig(a, sig)
if err != nil {
return nil, err
}
abi.Errors[errsig.Name()] = errsig
default:
return nil, fmt.Errorf("unknown kind: %s", sig.Kind)
}
}
return abi, nil
}
type jsonField struct {
Type string `json:"type"`
Name string `json:"name"`
Constant bool `json:"constant"`
Anonymous bool `json:"anonymous"`
StateMutability string `json:"stateMutability"`
Inputs jsonParameters `json:"inputs"`
Outputs jsonParameters `json:"outputs"`
}
type jsonParameters []jsonParameter
// toTupleType converts parameters to a TupleType type.
func (a jsonParameters) toTupleType(abi *ABI) (*TupleType, error) {
var elems []TupleTypeElem
for _, param := range a {
typ, err := param.toType(abi)
if err != nil {
return nil, err
}
elems = append(elems, TupleTypeElem{
Name: param.Name,
Type: typ,
})
}
return NewTupleType(elems...), nil
}
// toEventTupleType converts parameters to a EventTupleType type.
func (a jsonParameters) toEventTupleType(abi *ABI) (*EventTupleType, error) {
var elems []EventTupleElem
for _, param := range a {
typ, err := param.toType(abi)
if err != nil {
return nil, err
}
elems = append(elems, EventTupleElem{
Name: param.Name,
Indexed: param.Indexed,
Type: typ,
})
}
return NewEventTupleType(elems...), nil
}
type jsonParameter struct {
Name string `json:"name"`
Type string `json:"type"`
Indexed bool `json:"indexed"`
Components jsonParameters `json:"components"`
}
// toType converts a jsonParameter to a Type.
func (a jsonParameter) toType(abi *ABI) (typ Type, err error) {
name, arrays, err := parseArrays(a.Type)
if err != nil {
return nil, err
}
switch {
case len(arrays) > 0:
a.Type = name
if typ, err = a.toType(abi); err != nil {
return nil, err
}
for i := len(arrays) - 1; i >= 0; i-- {
if arrays[i] == -1 {
typ = NewArrayType(typ)
} else {
typ = NewFixedArrayType(typ, arrays[i])
}
}
return typ, nil
case len(a.Components) > 0:
tuple := make([]TupleTypeElem, len(a.Components))
for i, comp := range a.Components {
tuple[i].Name = comp.Name
tuple[i].Type, err = comp.toType(abi)
if err != nil {
return nil, err
}
}
return NewTupleType(tuple...), nil
default:
if typ = abi.Types[name]; typ != nil {
return typ, nil
}
return nil, fmt.Errorf("abi: unknown type %q", a.Type)
}
}
// parseArray parses type name and returns the name and array dimensions.
// For example, "uint256[][3]" will return "uint256" and [-1, 3].
// For unbounded arrays, the dimension is -1.
func parseArrays(typ string) (name string, arrays []int, err error) {
openBracket := strings.Index(typ, "[")
if openBracket == -1 {
name = typ
return
}
name = typ[:openBracket]
for {
closeBracket := openBracket
for closeBracket < len(typ) && typ[closeBracket] != ']' {
closeBracket++
}
if openBracket >= closeBracket {
return "", nil, fmt.Errorf("abi: invalid type %q", typ)
}
n := typ[openBracket+1 : closeBracket]
if len(n) == 0 {
arrays = append(arrays, -1)
} else {
i, err := strconv.Atoi(n)
if err != nil {
return "", nil, err
}
if i <= 0 {
return "", nil, fmt.Errorf("abi: invalid array size %d", i)
}
arrays = append(arrays, i)
}
if closeBracket+1 == len(typ) {
break
}
openBracket = closeBracket + 1
}
return
}