From 2958387a847c71c1321dd4c630b1c98fc4f28214 Mon Sep 17 00:00:00 2001 From: maskpp Date: Tue, 22 Apr 2025 17:08:31 +0800 Subject: [PATCH] fix abigen v2 --- accounts/abi/abigen/source2.go.tpl | 18 ++---- .../bind/v2/internal/contracts/db/bindings.go | 54 +++++----------- .../v2/internal/contracts/events/bindings.go | 16 ++--- .../contracts/nested_libraries/bindings.go | 64 +++++-------------- .../contracts/solc_errors/bindings.go | 24 ++----- .../contracts/uint256arrayreturn/bindings.go | 8 +-- 6 files changed, 50 insertions(+), 134 deletions(-) diff --git a/accounts/abi/abigen/source2.go.tpl b/accounts/abi/abigen/source2.go.tpl index 79f90b5f4e..7c02b1ce24 100644 --- a/accounts/abi/abigen/source2.go.tpl +++ b/accounts/abi/abigen/source2.go.tpl @@ -93,12 +93,8 @@ var ( // the contract method with ID 0x{{printf "%x" .Original.ID}}. // // Solidity: {{.Original.String}} - func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Pack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) []byte { - enc, err := {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) - if err != nil { - panic(err) - } - return enc + func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Pack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) ([]byte, error) { + return {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) } {{/* Unpack method is needed only when there are return args */}} @@ -117,14 +113,14 @@ var ( // // Solidity: {{.Original.String}} func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}(data []byte) ( - {{- if .Structured}} {{.Normalized.Name}}Output,{{else}} + {{- if .Structured}} *{{.Normalized.Name}}Output,{{else}} {{- range .Normalized.Outputs}} {{bindtype .Type $structs}},{{- end }} {{- end }} error) { out, err := {{ decapitalise $contract.Type}}.abi.Unpack("{{.Original.Name}}", data) {{- if .Structured}} outstruct := new({{.Normalized.Name}}Output) if err != nil { - return *outstruct, err + return outstruct, nil } {{- range $i, $t := .Normalized.Outputs}} {{- if ispointertype .Type}} @@ -133,8 +129,7 @@ var ( outstruct.{{capitalise .Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}) {{- end }} {{- end }} - return *outstruct, err - {{else}} + return outstruct, nil{{else}} if err != nil { return {{range $i, $_ := .Normalized.Outputs}}{{if ispointertype .Type}}new({{underlyingbindtype .Type }}), {{else}}*new({{bindtype .Type $structs}}), {{end}}{{end}} err } @@ -145,8 +140,7 @@ var ( out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}) {{- end }} {{- end}} - return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} nil - {{- end}} + return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} nil{{- end}} } {{end}} {{end}} diff --git a/accounts/abi/bind/v2/internal/contracts/db/bindings.go b/accounts/abi/bind/v2/internal/contracts/db/bindings.go index a614c3c08a..fd54e53304 100644 --- a/accounts/abi/bind/v2/internal/contracts/db/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/db/bindings.go @@ -62,12 +62,8 @@ func (c *DB) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0x9507d39a. // // Solidity: function get(uint256 k) returns(uint256) -func (dB *DB) PackGet(k *big.Int) []byte { - enc, err := dB.abi.Pack("get", k) - if err != nil { - panic(err) - } - return enc +func (dB *DB) PackGet(k *big.Int) ([]byte, error) { + return dB.abi.Pack("get", k) } // UnpackGet is the Go binding that unpacks the parameters returned @@ -87,12 +83,8 @@ func (dB *DB) UnpackGet(data []byte) (*big.Int, error) { // the contract method with ID 0xe369ba3b. // // Solidity: function getNamedStatParams() view returns(uint256 gets, uint256 inserts, uint256 mods) -func (dB *DB) PackGetNamedStatParams() []byte { - enc, err := dB.abi.Pack("getNamedStatParams") - if err != nil { - panic(err) - } - return enc +func (dB *DB) PackGetNamedStatParams() ([]byte, error) { + return dB.abi.Pack("getNamedStatParams") } // GetNamedStatParamsOutput serves as a container for the return parameters of contract @@ -107,29 +99,24 @@ type GetNamedStatParamsOutput struct { // from invoking the contract method with ID 0xe369ba3b. // // Solidity: function getNamedStatParams() view returns(uint256 gets, uint256 inserts, uint256 mods) -func (dB *DB) UnpackGetNamedStatParams(data []byte) (GetNamedStatParamsOutput, error) { +func (dB *DB) UnpackGetNamedStatParams(data []byte) (*GetNamedStatParamsOutput, error) { out, err := dB.abi.Unpack("getNamedStatParams", data) outstruct := new(GetNamedStatParamsOutput) if err != nil { - return *outstruct, err + return outstruct, nil } outstruct.Gets = abi.ConvertType(out[0], new(big.Int)).(*big.Int) outstruct.Inserts = abi.ConvertType(out[1], new(big.Int)).(*big.Int) outstruct.Mods = abi.ConvertType(out[2], new(big.Int)).(*big.Int) - return *outstruct, err - + return outstruct, nil } // PackGetStatParams is the Go binding used to pack the parameters required for calling // the contract method with ID 0x6fcb9c70. // // Solidity: function getStatParams() view returns(uint256, uint256, uint256) -func (dB *DB) PackGetStatParams() []byte { - enc, err := dB.abi.Pack("getStatParams") - if err != nil { - panic(err) - } - return enc +func (dB *DB) PackGetStatParams() ([]byte, error) { + return dB.abi.Pack("getStatParams") } // GetStatParamsOutput serves as a container for the return parameters of contract @@ -144,29 +131,24 @@ type GetStatParamsOutput struct { // from invoking the contract method with ID 0x6fcb9c70. // // Solidity: function getStatParams() view returns(uint256, uint256, uint256) -func (dB *DB) UnpackGetStatParams(data []byte) (GetStatParamsOutput, error) { +func (dB *DB) UnpackGetStatParams(data []byte) (*GetStatParamsOutput, error) { out, err := dB.abi.Unpack("getStatParams", data) outstruct := new(GetStatParamsOutput) if err != nil { - return *outstruct, err + return outstruct, nil } outstruct.Arg0 = abi.ConvertType(out[0], new(big.Int)).(*big.Int) outstruct.Arg1 = abi.ConvertType(out[1], new(big.Int)).(*big.Int) outstruct.Arg2 = abi.ConvertType(out[2], new(big.Int)).(*big.Int) - return *outstruct, err - + return outstruct, nil } // PackGetStatsStruct is the Go binding used to pack the parameters required for calling // the contract method with ID 0xee8161e0. // // Solidity: function getStatsStruct() view returns((uint256,uint256,uint256)) -func (dB *DB) PackGetStatsStruct() []byte { - enc, err := dB.abi.Pack("getStatsStruct") - if err != nil { - panic(err) - } - return enc +func (dB *DB) PackGetStatsStruct() ([]byte, error) { + return dB.abi.Pack("getStatsStruct") } // UnpackGetStatsStruct is the Go binding that unpacks the parameters returned @@ -186,12 +168,8 @@ func (dB *DB) UnpackGetStatsStruct(data []byte) (DBStats, error) { // the contract method with ID 0x1d834a1b. // // Solidity: function insert(uint256 k, uint256 v) returns(uint256) -func (dB *DB) PackInsert(k *big.Int, v *big.Int) []byte { - enc, err := dB.abi.Pack("insert", k, v) - if err != nil { - panic(err) - } - return enc +func (dB *DB) PackInsert(k *big.Int, v *big.Int) ([]byte, error) { + return dB.abi.Pack("insert", k, v) } // UnpackInsert is the Go binding that unpacks the parameters returned diff --git a/accounts/abi/bind/v2/internal/contracts/events/bindings.go b/accounts/abi/bind/v2/internal/contracts/events/bindings.go index 580bffa23e..a4bf69bcc6 100644 --- a/accounts/abi/bind/v2/internal/contracts/events/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/events/bindings.go @@ -55,24 +55,16 @@ func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.Bo // the contract method with ID 0xcb493749. // // Solidity: function EmitMulti() returns() -func (c *C) PackEmitMulti() []byte { - enc, err := c.abi.Pack("EmitMulti") - if err != nil { - panic(err) - } - return enc +func (c *C) PackEmitMulti() ([]byte, error) { + return c.abi.Pack("EmitMulti") } // PackEmitOne is the Go binding used to pack the parameters required for calling // the contract method with ID 0xe8e49a71. // // Solidity: function EmitOne() returns() -func (c *C) PackEmitOne() []byte { - enc, err := c.abi.Pack("EmitOne") - if err != nil { - panic(err) - } - return enc +func (c *C) PackEmitOne() ([]byte, error) { + return c.abi.Pack("EmitOne") } // CBasic1 represents a basic1 event raised by the C contract. diff --git a/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go b/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go index ba73dd95c2..dc4751596f 100644 --- a/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go @@ -71,12 +71,8 @@ func (c1 *C1) PackConstructor(v1 *big.Int, v2 *big.Int) []byte { // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256 res) -func (c1 *C1) PackDo(val *big.Int) []byte { - enc, err := c1.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (c1 *C1) PackDo(val *big.Int) ([]byte, error) { + return c1.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -139,12 +135,8 @@ func (c2 *C2) PackConstructor(v1 *big.Int, v2 *big.Int) []byte { // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256 res) -func (c2 *C2) PackDo(val *big.Int) []byte { - enc, err := c2.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (c2 *C2) PackDo(val *big.Int) ([]byte, error) { + return c2.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -191,12 +183,8 @@ func (c *L1) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l1 *L1) PackDo(val *big.Int) []byte { - enc, err := l1.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l1 *L1) PackDo(val *big.Int) ([]byte, error) { + return l1.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -246,12 +234,8 @@ func (c *L2) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l2 *L2) PackDo(val *big.Int) []byte { - enc, err := l2.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l2 *L2) PackDo(val *big.Int) ([]byte, error) { + return l2.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -301,12 +285,8 @@ func (c *L2b) Instance(backend bind.ContractBackend, addr common.Address) *bind. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l2b *L2b) PackDo(val *big.Int) []byte { - enc, err := l2b.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l2b *L2b) PackDo(val *big.Int) ([]byte, error) { + return l2b.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -353,12 +333,8 @@ func (c *L3) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l3 *L3) PackDo(val *big.Int) []byte { - enc, err := l3.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l3 *L3) PackDo(val *big.Int) ([]byte, error) { + return l3.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -409,12 +385,8 @@ func (c *L4) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l4 *L4) PackDo(val *big.Int) []byte { - enc, err := l4.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l4 *L4) PackDo(val *big.Int) ([]byte, error) { + return l4.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned @@ -464,12 +436,8 @@ func (c *L4b) Instance(backend bind.ContractBackend, addr common.Address) *bind. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l4b *L4b) PackDo(val *big.Int) []byte { - enc, err := l4b.abi.Pack("Do", val) - if err != nil { - panic(err) - } - return enc +func (l4b *L4b) PackDo(val *big.Int) ([]byte, error) { + return l4b.abi.Pack("Do", val) } // UnpackDo is the Go binding that unpacks the parameters returned diff --git a/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go b/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go index 067fb2b0e1..4f424577a3 100644 --- a/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go @@ -55,24 +55,16 @@ func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.Bo // the contract method with ID 0xb0a378b0. // // Solidity: function Bar() pure returns() -func (c *C) PackBar() []byte { - enc, err := c.abi.Pack("Bar") - if err != nil { - panic(err) - } - return enc +func (c *C) PackBar() ([]byte, error) { + return c.abi.Pack("Bar") } // PackFoo is the Go binding used to pack the parameters required for calling // the contract method with ID 0xbfb4ebcf. // // Solidity: function Foo() pure returns() -func (c *C) PackFoo() []byte { - enc, err := c.abi.Pack("Foo") - if err != nil { - panic(err) - } - return enc +func (c *C) PackFoo() ([]byte, error) { + return c.abi.Pack("Foo") } // UnpackError attempts to decode the provided error data using user-defined @@ -172,12 +164,8 @@ func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) *bind.B // the contract method with ID 0xbfb4ebcf. // // Solidity: function Foo() pure returns() -func (c2 *C2) PackFoo() []byte { - enc, err := c2.abi.Pack("Foo") - if err != nil { - panic(err) - } - return enc +func (c2 *C2) PackFoo() ([]byte, error) { + return c2.abi.Pack("Foo") } // UnpackError attempts to decode the provided error data using user-defined diff --git a/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go b/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go index badcd20894..dd09903d18 100644 --- a/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go @@ -55,12 +55,8 @@ func (c *MyContract) Instance(backend bind.ContractBackend, addr common.Address) // the contract method with ID 0xbd6d1007. // // Solidity: function GetNums() pure returns(uint256[5]) -func (myContract *MyContract) PackGetNums() []byte { - enc, err := myContract.abi.Pack("GetNums") - if err != nil { - panic(err) - } - return enc +func (myContract *MyContract) PackGetNums() ([]byte, error) { + return myContract.abi.Pack("GetNums") } // UnpackGetNums is the Go binding that unpacks the parameters returned