From 4c524e6b1db9bca7efaf3a7612f24ed0141d6670 Mon Sep 17 00:00:00 2001 From: maskpp Date: Tue, 22 Apr 2025 18:47:33 +0800 Subject: [PATCH] add TryPack template method --- accounts/abi/abigen/source2.go.tpl | 16 ++- .../bind/v2/internal/contracts/db/bindings.go | 80 +++++++++-- .../v2/internal/contracts/events/bindings.go | 32 ++++- .../contracts/nested_libraries/bindings.go | 128 +++++++++++++++--- .../contracts/solc_errors/bindings.go | 48 ++++++- .../contracts/uint256arrayreturn/bindings.go | 16 ++- 6 files changed, 280 insertions(+), 40 deletions(-) diff --git a/accounts/abi/abigen/source2.go.tpl b/accounts/abi/abigen/source2.go.tpl index 7c02b1ce24..b61a2ff988 100644 --- a/accounts/abi/abigen/source2.go.tpl +++ b/accounts/abi/abigen/source2.go.tpl @@ -89,11 +89,23 @@ var ( {{ end }} {{range .Calls}} - // Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling + // Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling, will panic for any error. // 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, error) { + 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 + } + + // Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling, return error if it failed to pack. + // the contract method with ID 0x{{printf "%x" .Original.ID}}. + // + // Solidity: {{.Original.String}} + func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) TryPack{{.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}}) } diff --git a/accounts/abi/bind/v2/internal/contracts/db/bindings.go b/accounts/abi/bind/v2/internal/contracts/db/bindings.go index fd54e53304..b5ba69eaef 100644 --- a/accounts/abi/bind/v2/internal/contracts/db/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/db/bindings.go @@ -58,11 +58,23 @@ func (c *DB) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackGet is the Go binding used to pack the parameters required for calling +// PackGet is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x9507d39a. // // Solidity: function get(uint256 k) returns(uint256) -func (dB *DB) PackGet(k *big.Int) ([]byte, error) { +func (dB *DB) PackGet(k *big.Int) []byte { + enc, err := dB.abi.Pack("get", k) + if err != nil { + panic(err) + } + return enc +} + +// PackGet is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x9507d39a. +// +// Solidity: function get(uint256 k) returns(uint256) +func (dB *DB) TryPackGet(k *big.Int) ([]byte, error) { return dB.abi.Pack("get", k) } @@ -79,11 +91,23 @@ func (dB *DB) UnpackGet(data []byte) (*big.Int, error) { return out0, nil } -// PackGetNamedStatParams is the Go binding used to pack the parameters required for calling +// PackGetNamedStatParams is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xe369ba3b. // // Solidity: function getNamedStatParams() view returns(uint256 gets, uint256 inserts, uint256 mods) -func (dB *DB) PackGetNamedStatParams() ([]byte, error) { +func (dB *DB) PackGetNamedStatParams() []byte { + enc, err := dB.abi.Pack("getNamedStatParams") + if err != nil { + panic(err) + } + return enc +} + +// PackGetNamedStatParams is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xe369ba3b. +// +// Solidity: function getNamedStatParams() view returns(uint256 gets, uint256 inserts, uint256 mods) +func (dB *DB) TryPackGetNamedStatParams() ([]byte, error) { return dB.abi.Pack("getNamedStatParams") } @@ -111,11 +135,23 @@ func (dB *DB) UnpackGetNamedStatParams(data []byte) (*GetNamedStatParamsOutput, return outstruct, nil } -// PackGetStatParams is the Go binding used to pack the parameters required for calling +// PackGetStatParams is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x6fcb9c70. // // Solidity: function getStatParams() view returns(uint256, uint256, uint256) -func (dB *DB) PackGetStatParams() ([]byte, error) { +func (dB *DB) PackGetStatParams() []byte { + enc, err := dB.abi.Pack("getStatParams") + if err != nil { + panic(err) + } + return enc +} + +// PackGetStatParams is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x6fcb9c70. +// +// Solidity: function getStatParams() view returns(uint256, uint256, uint256) +func (dB *DB) TryPackGetStatParams() ([]byte, error) { return dB.abi.Pack("getStatParams") } @@ -143,11 +179,23 @@ func (dB *DB) UnpackGetStatParams(data []byte) (*GetStatParamsOutput, error) { return outstruct, nil } -// PackGetStatsStruct is the Go binding used to pack the parameters required for calling +// PackGetStatsStruct is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xee8161e0. // // Solidity: function getStatsStruct() view returns((uint256,uint256,uint256)) -func (dB *DB) PackGetStatsStruct() ([]byte, error) { +func (dB *DB) PackGetStatsStruct() []byte { + enc, err := dB.abi.Pack("getStatsStruct") + if err != nil { + panic(err) + } + return enc +} + +// PackGetStatsStruct is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xee8161e0. +// +// Solidity: function getStatsStruct() view returns((uint256,uint256,uint256)) +func (dB *DB) TryPackGetStatsStruct() ([]byte, error) { return dB.abi.Pack("getStatsStruct") } @@ -164,11 +212,23 @@ func (dB *DB) UnpackGetStatsStruct(data []byte) (DBStats, error) { return out0, nil } -// PackInsert is the Go binding used to pack the parameters required for calling +// PackInsert is the Go binding used to pack the parameters required for calling, will panic for any 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, error) { +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 +} + +// PackInsert is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x1d834a1b. +// +// Solidity: function insert(uint256 k, uint256 v) returns(uint256) +func (dB *DB) TryPackInsert(k *big.Int, v *big.Int) ([]byte, error) { return dB.abi.Pack("insert", k, v) } diff --git a/accounts/abi/bind/v2/internal/contracts/events/bindings.go b/accounts/abi/bind/v2/internal/contracts/events/bindings.go index a4bf69bcc6..f2918afb64 100644 --- a/accounts/abi/bind/v2/internal/contracts/events/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/events/bindings.go @@ -51,19 +51,43 @@ func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.Bo return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackEmitMulti is the Go binding used to pack the parameters required for calling +// PackEmitMulti is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xcb493749. // // Solidity: function EmitMulti() returns() -func (c *C) PackEmitMulti() ([]byte, error) { +func (c *C) PackEmitMulti() []byte { + enc, err := c.abi.Pack("EmitMulti") + if err != nil { + panic(err) + } + return enc +} + +// PackEmitMulti is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xcb493749. +// +// Solidity: function EmitMulti() returns() +func (c *C) TryPackEmitMulti() ([]byte, error) { return c.abi.Pack("EmitMulti") } -// PackEmitOne is the Go binding used to pack the parameters required for calling +// PackEmitOne is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xe8e49a71. // // Solidity: function EmitOne() returns() -func (c *C) PackEmitOne() ([]byte, error) { +func (c *C) PackEmitOne() []byte { + enc, err := c.abi.Pack("EmitOne") + if err != nil { + panic(err) + } + return enc +} + +// PackEmitOne is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xe8e49a71. +// +// Solidity: function EmitOne() returns() +func (c *C) TryPackEmitOne() ([]byte, error) { return c.abi.Pack("EmitOne") } 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 dc4751596f..2a3d21a37c 100644 --- a/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go @@ -67,11 +67,23 @@ func (c1 *C1) PackConstructor(v1 *big.Int, v2 *big.Int) []byte { return enc } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256 res) -func (c1 *C1) PackDo(val *big.Int) ([]byte, error) { +func (c1 *C1) PackDo(val *big.Int) []byte { + enc, err := c1.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256 res) +func (c1 *C1) TryPackDo(val *big.Int) ([]byte, error) { return c1.abi.Pack("Do", val) } @@ -131,11 +143,23 @@ func (c2 *C2) PackConstructor(v1 *big.Int, v2 *big.Int) []byte { return enc } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256 res) -func (c2 *C2) PackDo(val *big.Int) ([]byte, error) { +func (c2 *C2) PackDo(val *big.Int) []byte { + enc, err := c2.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256 res) +func (c2 *C2) TryPackDo(val *big.Int) ([]byte, error) { return c2.abi.Pack("Do", val) } @@ -179,11 +203,23 @@ func (c *L1) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l1 *L1) PackDo(val *big.Int) ([]byte, error) { +func (l1 *L1) PackDo(val *big.Int) []byte { + enc, err := l1.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l1 *L1) TryPackDo(val *big.Int) ([]byte, error) { return l1.abi.Pack("Do", val) } @@ -230,11 +266,23 @@ func (c *L2) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l2 *L2) PackDo(val *big.Int) ([]byte, error) { +func (l2 *L2) PackDo(val *big.Int) []byte { + enc, err := l2.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l2 *L2) TryPackDo(val *big.Int) ([]byte, error) { return l2.abi.Pack("Do", val) } @@ -281,11 +329,23 @@ func (c *L2b) Instance(backend bind.ContractBackend, addr common.Address) *bind. return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l2b *L2b) PackDo(val *big.Int) ([]byte, error) { +func (l2b *L2b) PackDo(val *big.Int) []byte { + enc, err := l2b.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l2b *L2b) TryPackDo(val *big.Int) ([]byte, error) { return l2b.abi.Pack("Do", val) } @@ -329,11 +389,23 @@ func (c *L3) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l3 *L3) PackDo(val *big.Int) ([]byte, error) { +func (l3 *L3) PackDo(val *big.Int) []byte { + enc, err := l3.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l3 *L3) TryPackDo(val *big.Int) ([]byte, error) { return l3.abi.Pack("Do", val) } @@ -381,11 +453,23 @@ func (c *L4) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l4 *L4) PackDo(val *big.Int) ([]byte, error) { +func (l4 *L4) PackDo(val *big.Int) []byte { + enc, err := l4.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l4 *L4) TryPackDo(val *big.Int) ([]byte, error) { return l4.abi.Pack("Do", val) } @@ -432,11 +516,23 @@ func (c *L4b) Instance(backend bind.ContractBackend, addr common.Address) *bind. return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackDo is the Go binding used to pack the parameters required for calling +// PackDo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0x2ad11272. // // Solidity: function Do(uint256 val) pure returns(uint256) -func (l4b *L4b) PackDo(val *big.Int) ([]byte, error) { +func (l4b *L4b) PackDo(val *big.Int) []byte { + enc, err := l4b.abi.Pack("Do", val) + if err != nil { + panic(err) + } + return enc +} + +// PackDo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0x2ad11272. +// +// Solidity: function Do(uint256 val) pure returns(uint256) +func (l4b *L4b) TryPackDo(val *big.Int) ([]byte, error) { return l4b.abi.Pack("Do", val) } 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 4f424577a3..3ea8489b82 100644 --- a/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/solc_errors/bindings.go @@ -51,19 +51,43 @@ func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.Bo return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackBar is the Go binding used to pack the parameters required for calling +// PackBar is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xb0a378b0. // // Solidity: function Bar() pure returns() -func (c *C) PackBar() ([]byte, error) { +func (c *C) PackBar() []byte { + enc, err := c.abi.Pack("Bar") + if err != nil { + panic(err) + } + return enc +} + +// PackBar is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xb0a378b0. +// +// Solidity: function Bar() pure returns() +func (c *C) TryPackBar() ([]byte, error) { return c.abi.Pack("Bar") } -// PackFoo is the Go binding used to pack the parameters required for calling +// PackFoo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xbfb4ebcf. // // Solidity: function Foo() pure returns() -func (c *C) PackFoo() ([]byte, error) { +func (c *C) PackFoo() []byte { + enc, err := c.abi.Pack("Foo") + if err != nil { + panic(err) + } + return enc +} + +// PackFoo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xbfb4ebcf. +// +// Solidity: function Foo() pure returns() +func (c *C) TryPackFoo() ([]byte, error) { return c.abi.Pack("Foo") } @@ -160,11 +184,23 @@ func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) *bind.B return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackFoo is the Go binding used to pack the parameters required for calling +// PackFoo is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xbfb4ebcf. // // Solidity: function Foo() pure returns() -func (c2 *C2) PackFoo() ([]byte, error) { +func (c2 *C2) PackFoo() []byte { + enc, err := c2.abi.Pack("Foo") + if err != nil { + panic(err) + } + return enc +} + +// PackFoo is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xbfb4ebcf. +// +// Solidity: function Foo() pure returns() +func (c2 *C2) TryPackFoo() ([]byte, error) { return c2.abi.Pack("Foo") } diff --git a/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go b/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go index dd09903d18..c336198c88 100644 --- a/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/uint256arrayreturn/bindings.go @@ -51,11 +51,23 @@ func (c *MyContract) Instance(backend bind.ContractBackend, addr common.Address) return bind.NewBoundContract(addr, c.abi, backend, backend, backend) } -// PackGetNums is the Go binding used to pack the parameters required for calling +// PackGetNums is the Go binding used to pack the parameters required for calling, will panic for any error. // the contract method with ID 0xbd6d1007. // // Solidity: function GetNums() pure returns(uint256[5]) -func (myContract *MyContract) PackGetNums() ([]byte, error) { +func (myContract *MyContract) PackGetNums() []byte { + enc, err := myContract.abi.Pack("GetNums") + if err != nil { + panic(err) + } + return enc +} + +// PackGetNums is the Go binding used to pack the parameters required for calling, return error if it failed to pack. +// the contract method with ID 0xbd6d1007. +// +// Solidity: function GetNums() pure returns(uint256[5]) +func (myContract *MyContract) TryPackGetNums() ([]byte, error) { return myContract.abi.Pack("GetNums") }