accounts/abi/bind: fix some ugliness in the generated binding code which deals with pointer types

This commit is contained in:
Jared Wasinger 2025-01-06 18:45:46 +07:00 committed by Felix Lange
parent 3d974b22e2
commit 8ea9929a30
5 changed files with 65 additions and 32 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"go/format"
"reflect"
"regexp"
"slices"
"strings"
@ -12,6 +13,20 @@ import (
"unicode"
)
// underlyingBindType returns the underlying Go type represented by the given type, panicking if it is not a pointer type.
func underlyingBindType(typ abi.Type) string {
goType := typ.GetType()
if goType.Kind() != reflect.Pointer {
panic("trying to retrieve underlying bind type of non-pointer type.")
}
return goType.Elem().String()
}
// isPointerType returns true if the
func isPointerType(typ abi.Type) bool {
return typ.GetType().Kind() == reflect.Pointer
}
type binder struct {
// contracts is the map of each individual contract requested binding
contracts map[string]*tmplContractV2
@ -275,6 +290,8 @@ func BindV2(types []string, abis []string, bytecodes []string, pkg string, libs
"add": func(val1, val2 int) int {
return val1 + val2
},
"ispointertype": isPointerType,
"underlyingbindtype": underlyingBindType,
}
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSourceV2))
if err := tmpl.Execute(buffer, data); err != nil {

View file

@ -93,15 +93,24 @@ var (
return *outstruct, err
}
{{range $i, $t := .Normalized.Outputs}}
outstruct.{{.Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
{{if ispointertype .Type}}
outstruct.{{.Name}} = abi.ConvertType(out[{{$i}}], new({{underlyingbindtype .Type }})).({{underlyingbindtype .Type }})
{{ else }}
outstruct.{{.Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
{{ end }}{{end}}
return *outstruct, err
{{else}}
if err != nil {
return {{range $i, $_ := .Normalized.Outputs}}*new({{bindtype .Type $structs}}), {{end}} err
return {{range $i, $_ := .Normalized.Outputs}}{{if ispointertype .Type}}new({{underlyingbindtype .Type }}), {{else}}*new({{bindtype .Type $structs}}), {{end}}{{end}} err
}
{{range $i, $t := .Normalized.Outputs}}
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}}
{{ if ispointertype .Type }}
out{{$i}} := abi.ConvertType(out[{{$i}}], new({{underlyingbindtype .Type}})).({{bindtype .Type $structs}})
{{ else }}
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
{{ end }}
{{end}}
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err
{{end}}

View file

@ -68,10 +68,10 @@ func (_DB *DB) UnpackGet(data []byte) (*big.Int, error) {
out, err := _DB.abi.Unpack("get", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -98,9 +98,11 @@ func (_DB *DB) UnpackGetNamedStatParams(data []byte) (GetNamedStatParamsOutput,
return *outstruct, err
}
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)
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
@ -127,9 +129,11 @@ func (_DB *DB) UnpackGetStatParams(data []byte) (GetStatParamsOutput, error) {
return *outstruct, err
}
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)
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
@ -166,10 +170,10 @@ func (_DB *DB) UnpackInsert(data []byte) (*big.Int, error) {
out, err := _DB.abi.Unpack("insert", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err

View file

@ -78,9 +78,12 @@ func (_C *C) UnpackDoSomethingWithManyArgs(data []byte) (DoSomethingWithManyArgs
return *outstruct, err
}
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)
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)
outstruct.Arg3 = *abi.ConvertType(out[3], new(bool)).(*bool)
return *outstruct, err

View file

@ -65,10 +65,10 @@ func (_C1 *C1) UnpackDo(data []byte) (*big.Int, error) {
out, err := _C1.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -116,10 +116,10 @@ func (_C2 *C2) UnpackDo(data []byte) (*big.Int, error) {
out, err := _C2.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -163,10 +163,10 @@ func (_L1 *L1) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L1.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -213,10 +213,10 @@ func (_L2 *L2) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L2.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -263,10 +263,10 @@ func (_L2b *L2b) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L2b.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -310,10 +310,10 @@ func (_L3 *L3) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L3.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -361,10 +361,10 @@ func (_L4 *L4) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L4.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
@ -411,10 +411,10 @@ func (_L4b *L4b) UnpackDo(data []byte) (*big.Int, error) {
out, err := _L4b.abi.Unpack("Do", data)
if err != nil {
return *new(*big.Int), err
return new(big.Int), err
}
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err