forked from streamingfast/eth-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabi_contract.go
More file actions
337 lines (284 loc) · 9.36 KB
/
Copy pathabi_contract.go
File metadata and controls
337 lines (284 loc) · 9.36 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
// 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/json"
"fmt"
"io"
"os"
)
func ParseABI(abiFilePath string) (*ABI, error) {
file, err := os.Open(abiFilePath)
if err != nil {
return nil, fmt.Errorf("open abi file: %w", err)
}
defer file.Close()
return parseABIFromReader(file)
}
func ParseABIFromBytes(content []byte) (*ABI, error) {
return parseABIFromReader(bytes.NewBuffer(content))
}
func parseDeclarations(content []byte) ([]*declaration, error) {
// Find first non-whitespace character to determine format
content = bytes.TrimSpace(content)
if len(content) == 0 {
return nil, fmt.Errorf("empty abi content")
}
switch content[0] {
case '[':
// Direct array format: [...]
var declarations []*declaration
if err := json.Unmarshal(content, &declarations); err != nil {
return nil, fmt.Errorf("read abi array: %w", err)
}
return declarations, nil
case '{':
// Object format: { "abi": [...], ... }
var wrapper struct {
ABI []*declaration `json:"abi"`
}
if err := json.Unmarshal(content, &wrapper); err != nil {
return nil, fmt.Errorf("read abi object: %w", err)
}
if wrapper.ABI == nil {
return nil, fmt.Errorf("abi object missing 'abi' key")
}
return wrapper.ABI, nil
default:
return nil, fmt.Errorf("invalid abi format: expected '[' or '{', got %q", content[0])
}
}
func parseABIFromReader(reader io.Reader) (*ABI, error) {
content, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("read content: %w", err)
}
declarations, err := parseDeclarations(content)
if err != nil {
return nil, err
}
abi := &ABI{
LogEventsMap: map[string][]*LogEventDef{},
FunctionsMap: map[string][]*MethodDef{},
ConstructorsMap: map[string][]*ConstructorDef{},
LogEventsByNameMap: map[string][]*LogEventDef{},
FunctionsByNameMap: map[string][]*MethodDef{},
ConstructorsByNameMap: map[string][]*ConstructorDef{},
}
for _, decl := range declarations {
if decl.Type == DeclarationTypeFunction {
methodDef, err := decl.toFunctionDef()
if err != nil {
return nil, fmt.Errorf("invalid method %q: %w", decl.Name, err)
}
methodID := string(methodDef.MethodID())
abi.FunctionsMap[methodID] = append(abi.FunctionsMap[methodID], methodDef)
abi.FunctionsByNameMap[methodDef.Name] = append(abi.FunctionsByNameMap[methodDef.Name], methodDef)
}
if decl.Type == DeclarationTypeConstructor {
constructorDef, err := decl.toConstructorDef()
if err != nil {
return nil, fmt.Errorf("invalid constructor: %w", err)
}
// Constructors are indexed by their signature (parameter types)
signature := constructorDef.Signature()
abi.ConstructorsMap[signature] = append(abi.ConstructorsMap[signature], constructorDef)
// Also store under empty name for easy lookup (most contracts have one constructor)
abi.ConstructorsByNameMap[""] = append(abi.ConstructorsByNameMap[""], constructorDef)
}
if decl.Type == DeclarationTypeEvent {
logEventDef, err := decl.toLogEventDef()
if err != nil {
return nil, fmt.Errorf("invalid log event %q: %w", decl.Name, err)
}
logID := string(logEventDef.LogID())
abi.LogEventsMap[logID] = append(abi.LogEventsMap[logID], logEventDef)
abi.LogEventsByNameMap[logEventDef.Name] = append(abi.LogEventsByNameMap[logEventDef.Name], logEventDef)
}
}
return abi, nil
}
//go:generate go-enum -f=$GOFILE --lower --marshal --names
// ENUM(
//
// Function
// Constructor
// Receive
// Fallback
// Event
// Error
//
// )
type DeclarationType int
// declaration is a generic struct output for each ABI element of an Ethereum contact
// compiled through solidity. It's a fairly generic structure encompassing multiple
// elements like function, events, constructors and others.
//
// See https://docs.soliditylang.org/en/v0.8.11/abi-spec.html#json
type declaration struct {
// Common to functions and events
Name string `json:"name,omitempty"`
Type DeclarationType `json:"type"`
Inputs []*typeInfo `json:"inputs"`
// Functions only
Outputs []*typeInfo `json:"outputs,omitempty"`
StateMutability StateMutability `json:"stateMutability,omitempty"`
// Functions only but removed in `solc` >= 0.5, now in `StateMutability` directly
Payable bool `json:"payable,omitempty"`
Constant bool `json:"constant,omitempty"`
// Events only
Anonymous bool `json:"anonymous,omitempty"`
}
func (d *declaration) toFunctionDef() (*MethodDef, error) {
out := &MethodDef{}
out.Name = d.Name
out.StateMutability = d.StateMutability
// Those were removed in `solc` >= 0.5, there are most probably exclusive to each other
if d.Payable {
out.StateMutability = StateMutabilityNonPayable
}
if d.Constant {
out.StateMutability = StateMutabilityPure
}
if len(d.Inputs) > 0 {
out.Parameters = make([]*MethodParameter, len(d.Inputs))
for i, input := range d.Inputs {
parsedType, err := ParseType(input.Type)
if err != nil {
return nil, fmt.Errorf("invalid input parameter %q type: %w", input.Name, err)
}
structComponents, err := toStructComponents(input.Components)
if err != nil {
return nil, fmt.Errorf("invalid input %q struct component: %w", input.Name, err)
}
out.Parameters[i] = &MethodParameter{
Name: input.Name,
TypeName: input.Type,
Type: parsedType,
InternalType: input.InternalType,
Components: structComponents,
}
}
}
if len(d.Outputs) > 0 {
out.ReturnParameters = make([]*MethodParameter, len(d.Outputs))
for i, output := range d.Outputs {
parsedType, err := ParseType(output.Type)
if err != nil {
return nil, fmt.Errorf("invalid output parameter %q type: %w", output.Name, err)
}
structComponents, err := toStructComponents(output.Components)
if err != nil {
return nil, fmt.Errorf("invalid output %q struct component: %w", output.Name, err)
}
out.ReturnParameters[i] = &MethodParameter{
Name: output.Name,
TypeName: output.Type,
Type: parsedType,
InternalType: output.InternalType,
Components: structComponents,
}
}
}
return out, nil
}
func (d *declaration) toConstructorDef() (*ConstructorDef, error) {
out := &ConstructorDef{}
out.StateMutability = d.StateMutability
// Handle old solc versions
if d.Payable {
out.StateMutability = StateMutabilityPayable
}
if len(d.Inputs) > 0 {
out.Parameters = make([]*MethodParameter, len(d.Inputs))
for i, input := range d.Inputs {
parsedType, err := ParseType(input.Type)
if err != nil {
return nil, fmt.Errorf("invalid input parameter %q type: %w", input.Name, err)
}
structComponents, err := toStructComponents(input.Components)
if err != nil {
return nil, fmt.Errorf("invalid input %q struct component: %w", input.Name, err)
}
out.Parameters[i] = &MethodParameter{
Name: input.Name,
TypeName: input.Type,
Type: parsedType,
InternalType: input.InternalType,
Components: structComponents,
}
}
}
return out, nil
}
func toStructComponents(in []*structComponent) (out []*StructComponent, err error) {
if len(in) == 0 {
return
}
out = make([]*StructComponent, len(in))
for i, component := range in {
parsedType, err := ParseType(component.Type)
if err != nil {
return nil, fmt.Errorf("invalid component %q type: %w", component.Name, err)
}
// Recursively process nested components for tuple types
nestedComponents, err := toStructComponents(component.Components)
if err != nil {
return nil, fmt.Errorf("invalid nested component in %q: %w", component.Name, err)
}
out[i] = &StructComponent{
InternalType: component.InternalType,
Name: component.Name,
TypeName: component.Type,
Type: parsedType,
Components: nestedComponents,
}
}
return out, nil
}
func (d *declaration) toLogEventDef() (*LogEventDef, error) {
out := &LogEventDef{}
out.Name = d.Name
out.Parameters = make([]*LogParameter, len(d.Inputs))
for i, input := range d.Inputs {
parsedType, err := ParseType(input.Type)
if err != nil {
return nil, fmt.Errorf("invalid parameter %q type: %w", input.Name, err)
}
out.Parameters[i] = &LogParameter{
Name: input.Name,
TypeName: input.Type,
Type: parsedType,
Indexed: input.Indexed,
}
}
return out, nil
}
type typeInfo struct {
InternalType string `json:"internalType"`
Name string `json:"name"`
Type string `json:"type"`
Indexed bool `json:"indexed"`
Components []*structComponent `json:"components,omitempty"`
}
type structComponent struct {
InternalType string `json:"internalType"`
Name string `json:"name"`
Type string `json:"type"`
Components []*structComponent `json:"components"`
}
func (c *structComponent) String() string {
return fmt.Sprintf("%s %s (%s)", c.Type, c.Name, c.InternalType)
}