-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
abigen.go
468 lines (424 loc) · 12.2 KB
/
abigen.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package gethwrappers
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
gethParams "github.com/ethereum/go-ethereum/params"
"golang.org/x/tools/go/ast/astutil"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
)
const headerComment = `// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
`
// AbigenArgs is the arguments to the abigen executable. E.g., Bin is the -bin
// arg.
type AbigenArgs struct {
Bin, ABI, Out, Type, Pkg string
}
// Abigen calls Abigen with the given arguments
//
// It might seem like a shame, to shell out to another golang program like
// this, but the abigen executable is the stable public interface to the
// geth contract-wrapper machinery.
//
// Check whether native abigen is installed, and has correct version
func Abigen(a AbigenArgs) {
var versionResponse bytes.Buffer
abigenExecutablePath := filepath.Join(GetProjectRoot(), "tools/bin/abigen")
abigenVersionCheck := exec.Command(abigenExecutablePath, "--version")
abigenVersionCheck.Stdout = &versionResponse
if err := abigenVersionCheck.Run(); err != nil {
Exit("no native abigen; you must install it (`make abigen` in the "+
"chainlink root dir)", err)
}
version := string(regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`).Find(
versionResponse.Bytes()))
if version != gethParams.Version {
Exit(fmt.Sprintf("wrong version (%s) of abigen; install the correct one "+
"(%s) with `make abigen` in the chainlink root dir", version,
gethParams.Version),
nil)
}
args := []string{
"-abi", a.ABI,
"-out", a.Out,
"-type", a.Type,
"-pkg", a.Pkg,
}
if a.Bin != "-" {
args = append(args, "-bin", a.Bin)
}
buildCommand := exec.Command(abigenExecutablePath, args...)
var buildResponse bytes.Buffer
buildCommand.Stderr = &buildResponse
if err := buildCommand.Run(); err != nil {
Exit("failure while building "+a.Pkg+" wrapper, stderr: "+buildResponse.String(), err)
}
ImproveAbigenOutput(a.Out, a.ABI)
}
func ImproveAbigenOutput(path string, abiPath string) {
abiBytes, err := os.ReadFile(abiPath)
if err != nil {
Exit("Error while improving abigen output", err)
}
abi, err := abi.JSON(strings.NewReader(string(abiBytes)))
if err != nil {
Exit("Error while improving abigen output", err)
}
bs, err := os.ReadFile(path)
if err != nil {
Exit("Error while improving abigen output", err)
}
fset, fileNode := parseFile(bs)
logNames := getLogNames(fileNode)
if len(logNames) > 0 {
astutil.AddImport(fset, fileNode, "fmt")
astutil.AddImport(fset, fileNode, "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated")
}
contractName := getContractName(fileNode)
fileNode = addContractStructFields(contractName, fileNode)
fileNode = replaceAnonymousStructs(contractName, fileNode)
bs = generateCode(fset, fileNode)
bs = writeAdditionalMethods(contractName, logNames, abi, bs)
err = os.WriteFile(path, bs, 0600)
if err != nil {
Exit("Error while writing improved abigen source", err)
}
fset, fileNode = parseFile(bs)
fileNode = writeInterface(contractName, fileNode)
bs = generateCode(fset, fileNode)
bs = addHeader(bs)
err = os.WriteFile(path, bs, 0600)
if err != nil {
Exit("Error while writing improved abigen source", err)
}
}
func parseFile(bs []byte) (*token.FileSet, *ast.File) {
fset := token.NewFileSet()
fileNode, err := parser.ParseFile(fset, "", string(bs), parser.AllErrors)
if err != nil {
Exit("Error while improving abigen output", err)
}
return fset, fileNode
}
func generateCode(fset *token.FileSet, fileNode *ast.File) []byte {
var buf bytes.Buffer
err := format.Node(&buf, fset, fileNode)
if err != nil {
Exit("Error while writing improved abigen source", err)
}
return buf.Bytes()
}
func getContractName(fileNode *ast.File) string {
// Search for the ABI const e.g. VRFCoordinatorV2ABI = "0x..."
var contractName string
astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
x, is := cursor.Node().(*ast.ValueSpec)
if !is {
return true
}
if len(x.Names) > 0 {
for _, n := range x.Names {
if len(n.Name) < 3 {
return true
}
if n.Name[len(n.Name)-3:] != "ABI" {
return true
}
contractName = n.Name[:len(n.Name)-3]
}
}
return false
}, nil)
return contractName
}
// Add the `.address` and `.abi` fields to the contract struct.
func addContractStructFields(contractName string, fileNode *ast.File) *ast.File {
fileNode = addContractStructFieldsToStruct(contractName, fileNode)
fileNode = addContractStructFieldsToConstructor(contractName, fileNode)
fileNode = addContractStructFieldsToDeployMethod(contractName, fileNode)
return fileNode
}
// Add the fields to the contract struct.
func addContractStructFieldsToStruct(contractName string, fileNode *ast.File) *ast.File {
return astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
x, is := cursor.Node().(*ast.StructType)
if !is {
return true
}
theType, is := cursor.Parent().(*ast.TypeSpec)
if !is {
return false
} else if theType.Name.Name != contractName {
return false
}
addrField := &ast.Field{
Names: []*ast.Ident{ast.NewIdent("address")},
Type: &ast.SelectorExpr{
X: ast.NewIdent("common"),
Sel: ast.NewIdent("Address"),
},
}
abiField := &ast.Field{
Names: []*ast.Ident{ast.NewIdent("abi")},
Type: &ast.SelectorExpr{
X: ast.NewIdent("abi"),
Sel: ast.NewIdent("ABI"),
},
}
x.Fields.List = append([]*ast.Field{addrField, abiField}, x.Fields.List...)
return false
}, nil).(*ast.File)
}
// Add the fields to the return value of the constructor.
func addContractStructFieldsToConstructor(contractName string, fileNode *ast.File) *ast.File {
return astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
x, is := cursor.Node().(*ast.FuncDecl)
if !is {
return true
} else if x.Name.Name != "New"+contractName {
return false
}
for _, stmt := range x.Body.List {
returnStmt, is := stmt.(*ast.ReturnStmt)
if !is {
continue
}
lit, is := returnStmt.Results[0].(*ast.UnaryExpr).X.(*ast.CompositeLit)
if !is {
continue
}
addressExpr := &ast.KeyValueExpr{
Key: ast.NewIdent("address"),
Value: ast.NewIdent("address"),
}
abiExpr := &ast.KeyValueExpr{
Key: ast.NewIdent("abi"),
Value: ast.NewIdent("abi"),
}
lit.Elts = append([]ast.Expr{addressExpr, abiExpr}, lit.Elts...)
}
parseABIStmt := &ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("abi"), ast.NewIdent("err")},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("abi"),
Sel: ast.NewIdent("JSON"),
},
Args: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("strings"),
Sel: ast.NewIdent("NewReader"),
},
Args: []ast.Expr{ast.NewIdent(contractName + "ABI")},
},
},
},
},
}
checkParseABIErrStmt := &ast.IfStmt{
Cond: &ast.BinaryExpr{
X: ast.NewIdent("err"),
Op: token.NEQ,
Y: ast.NewIdent("nil"),
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{ast.NewIdent("nil"), ast.NewIdent("err")},
},
},
},
}
x.Body.List = append([]ast.Stmt{parseABIStmt, checkParseABIErrStmt}, x.Body.List...)
return false
}, nil).(*ast.File)
}
// Add the fields to the returned struct in the 'Deploy<contractName>' method.
func addContractStructFieldsToDeployMethod(contractName string, fileNode *ast.File) *ast.File {
return astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
x, is := cursor.Node().(*ast.FuncDecl)
if !is {
return true
} else if x.Name.Name != "Deploy"+contractName {
return false
}
for _, stmt := range x.Body.List {
returnStmt, is := stmt.(*ast.ReturnStmt)
if !is {
continue
}
if len(returnStmt.Results) < 3 {
continue
}
rs, is := returnStmt.Results[2].(*ast.UnaryExpr)
if !is {
return true
}
lit, is := rs.X.(*ast.CompositeLit)
if !is {
continue
}
addressExpr := &ast.KeyValueExpr{
Key: ast.NewIdent("address"),
Value: ast.NewIdent("address"),
}
abiExpr := &ast.KeyValueExpr{
Key: ast.NewIdent("abi"),
Value: ast.NewIdent("*parsed"),
}
lit.Elts = append([]ast.Expr{addressExpr, abiExpr}, lit.Elts...)
}
return false
}, nil).(*ast.File)
}
func getLogNames(fileNode *ast.File) []string {
var logNames []string
astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
x, is := cursor.Node().(*ast.FuncDecl)
if !is {
return true
} else if !strings.HasPrefix(x.Name.Name, "Parse") {
return false
}
logNames = append(logNames, x.Name.Name[len("Parse"):])
return false
}, nil)
return logNames
}
func replaceAnonymousStructs(contractName string, fileNode *ast.File) *ast.File {
done := map[string]bool{}
return astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
// Replace all anonymous structs with named structs
x, is := cursor.Node().(*ast.FuncDecl)
if !is {
return true
} else if len(x.Type.Results.List) == 0 {
return false
}
theStruct, is := x.Type.Results.List[0].Type.(*ast.StructType)
if !is {
return false
}
methodName := x.Name.Name
x.Type.Results.List[0].Type = ast.NewIdent(methodName)
x.Body = astutil.Apply(x.Body, func(cursor *astutil.Cursor) bool {
if _, is := cursor.Node().(*ast.StructType); !is {
return true
}
if call, is := cursor.Parent().(*ast.CallExpr); is {
for i, arg := range call.Args {
if arg == cursor.Node() {
call.Args[i] = ast.NewIdent(methodName)
break
}
}
}
return true
}, nil).(*ast.BlockStmt)
if done[contractName+methodName] {
return true
}
// Add the named structs to the bottom of the file
fileNode.Decls = append(fileNode.Decls, &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: ast.NewIdent(methodName),
Type: theStruct,
},
},
})
done[contractName+methodName] = true
return false
}, nil).(*ast.File)
}
func writeAdditionalMethods(contractName string, logNames []string, abi abi.ABI, bs []byte) []byte {
// Write the ParseLog method
if len(logNames) > 0 {
var logSwitchBody string
for _, logName := range logNames {
logSwitchBody += fmt.Sprintf(`case _%v.abi.Events["%v"].ID:
return _%v.Parse%v(log)
`, contractName, logName, contractName, logName)
}
bs = append(bs, []byte(fmt.Sprintf(`
func (_%v *%v) ParseLog(log types.Log) (generated.AbigenLog, error) {
switch log.Topics[0] {
%v
default:
return nil, fmt.Errorf("abigen wrapper received unknown log topic: %%v", log.Topics[0])
}
}
`, contractName, contractName, logSwitchBody))...)
}
// Write the Topic method
for _, logName := range logNames {
bs = append(bs, []byte(fmt.Sprintf(`
func (%v%v) Topic() common.Hash {
return common.HexToHash("%v")
}
`, contractName, logName, abi.Events[logName].ID.Hex()))...)
}
// Write the Address method to the bottom of the file
bs = append(bs, []byte(fmt.Sprintf(`
func (_%v *%v) Address() common.Address {
return _%v.address
}
`, contractName, contractName, contractName))...)
return bs
}
func writeInterface(contractName string, fileNode *ast.File) *ast.File {
// Generate an interface for the contract
var methods []*ast.Field
ast.Inspect(fileNode, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
if x.Recv == nil {
return true
}
star, is := x.Recv.List[0].Type.(*ast.StarExpr)
if !is {
return false
}
typeName := star.X.(*ast.Ident).String()
if typeName != contractName && typeName != contractName+"Caller" && typeName != contractName+"Transactor" && typeName != contractName+"Filterer" {
return true
}
methods = append(methods, &ast.Field{
Names: []*ast.Ident{x.Name},
Type: x.Type,
})
}
return true
})
fileNode.Decls = append(fileNode.Decls, &ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: ast.NewIdent(contractName + "Interface"),
Type: &ast.InterfaceType{
Methods: &ast.FieldList{
List: methods,
},
},
},
},
})
return fileNode
}
func addHeader(code []byte) []byte {
return utils.ConcatBytes([]byte(headerComment), code)
}