From d330916f57d46f4e382745e00a35cd91025552ad Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 22 Jan 2025 13:44:33 +0100 Subject: [PATCH] accounts/abi/bind: update v2 template Two changes: New function doesn't return error anymore. It's annoying having to handle an error there, especially since we know the ABI must be valid (we parsed it in order to generate the contract). The new Instance method returns the ContractInstance for use with v2 library functions. --- accounts/abi/bind/source2.go.tpl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/accounts/abi/bind/source2.go.tpl b/accounts/abi/bind/source2.go.tpl index 40d40933ff..edd3201709 100644 --- a/accounts/abi/bind/source2.go.tpl +++ b/accounts/abi/bind/source2.go.tpl @@ -8,7 +8,7 @@ import ( "errors" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/accounts/abi/bind/v2" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) @@ -17,7 +17,6 @@ import ( var ( _ = errors.New _ = big.NewInt - _ = bind.Bind _ = common.Big1 _ = types.BloomLookup _ = abi.ConvertType @@ -55,14 +54,20 @@ var ( } // New{{.Type}} creates a new instance of {{.Type}}. - func New{{.Type}}() (*{{.Type}}, error) { + func New{{.Type}}() *{{.Type}} { parsed, err := {{.Type}}MetaData.GetAbi() if err != nil { - return nil, err + panic(errors.New("invalid ABI: " + err.Error())) } - return &{{.Type}}{abi: *parsed}, nil + return &{{.Type}}{abi: *parsed} } + // Instance creates a wrapper for a deployed contract instance at the given address. + // Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc. + func (c *{{.Type}}) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance { + return bind.NewContractInstance(backend, addr, c.abi) + } + {{ if .Constructor.Inputs }} func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) PackConstructor({{range .Constructor.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) []byte { res, _ := {{ decapitalise $contract.Type}}.abi.Pack("" {{range .Constructor.Inputs}}, {{.Name}}{{end}})