diff --git a/accounts/abi/bind/bindv2.go b/accounts/abi/bind/bindv2.go index 2ed8ad1a90..4f42d1b668 100644 --- a/accounts/abi/bind/bindv2.go +++ b/accounts/abi/bind/bindv2.go @@ -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 { diff --git a/accounts/abi/bind/source2.go.tpl b/accounts/abi/bind/source2.go.tpl index c51a8a6d0f..809e655396 100644 --- a/accounts/abi/bind/source2.go.tpl +++ b/accounts/abi/bind/source2.go.tpl @@ -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}} diff --git a/accounts/abi/bind/v2/internal/contracts/db/bindings.go b/accounts/abi/bind/v2/internal/contracts/db/bindings.go index fe9d5680cb..a5bc3329a7 100644 --- a/accounts/abi/bind/v2/internal/contracts/db/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/db/bindings.go @@ -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 diff --git a/accounts/abi/bind/v2/internal/contracts/events/bindings.go b/accounts/abi/bind/v2/internal/contracts/events/bindings.go index 5598dfe5d3..d689caff3d 100644 --- a/accounts/abi/bind/v2/internal/contracts/events/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/events/bindings.go @@ -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 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 12910afcc6..540d9548eb 100644 --- a/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/nested_libraries/bindings.go @@ -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