Skip to content

Commit

Permalink
Add missing fields to generated Deploy methods (#11061)
Browse files Browse the repository at this point in the history
* Add missing fields to generated Deploy methods

* Regenerate wrappers

* Abigen test

* Error handling

* Test Address method returning the proper address

* Update generated wrappers

* Refactor method and check type assertion

* Format
  • Loading branch information
jarnaud authored Oct 26, 2023
1 parent b31284b commit 09f0afd
Show file tree
Hide file tree
Showing 127 changed files with 207 additions and 133 deletions.
61 changes: 53 additions & 8 deletions core/gethwrappers/abigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,17 @@ func getContractName(fileNode *ast.File) string {
return contractName
}

// Add the `.address` and `.abi` fields to the contract struct.
func addContractStructFields(contractName string, fileNode *ast.File) *ast.File {
// Add the `.address` and `.abi` fields to the contract struct
fileNode = astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
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
Expand All @@ -188,14 +196,14 @@ func addContractStructFields(contractName string, fileNode *ast.File) *ast.File
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
fileNode = astutil.Apply(fileNode, func(cursor *astutil.Cursor) bool {
// 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
Expand Down Expand Up @@ -260,11 +268,48 @@ func addContractStructFields(contractName string, fileNode *ast.File) *ast.File
}

x.Body.List = append([]ast.Stmt{parseABIStmt, checkParseABIErrStmt}, x.Body.List...)

return false
}, nil).(*ast.File)
}

return fileNode
// 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 {
Expand Down
29 changes: 29 additions & 0 deletions core/gethwrappers/abigen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gethwrappers

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/log_emitter"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
)

// Test that the generated Deploy method fill all the required fields and returns the correct address.
// We perform this test using the generated LogEmitter wrapper.
func TestGeneratedDeployMethodAddressField(t *testing.T) {
owner := testutils.MustNewSimTransactor(t)
ec := backends.NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), map[common.Address]core.GenesisAccount{
owner.From: {
Balance: big.NewInt(0).Mul(big.NewInt(10), big.NewInt(1e18)),
},
}, 10e6)
emitterAddr, _, emitter, err := log_emitter.DeployLogEmitter(owner, ec)
require.NoError(t, err)
require.Equal(t, emitterAddr, emitter.Address())
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/gethwrappers/functions/generated/ocr2dr/ocr2dr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09f0afd

Please sign in to comment.