diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index c22eb4ae8432..4f65971b7033 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -87,9 +87,11 @@ const tmplSourceGo = ` package {{.Package}} import ( + "errors" + "fmt" "math/big" + "reflect" "strings" - "errors" ethereum "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" @@ -378,6 +380,52 @@ var ( } {{end}} + {{$metaType := .Type}} + {{range .Transacts}} + {{if ne (len .Normalized.Inputs) 0}} + + // {{.Normalized.Name}}Params is an auto generated read-only Go binding of transcaction calldata params + type {{.Normalized.Name}}Params struct { + {{range $i, $_ := .Normalized.Inputs}} Param_{{.Name}} {{bindtype .Type $structs}} + {{end}} + } + + // Parse {{.Normalized.Name}} method from calldata of a transaction + // + // Solidity: {{.Original.String}} + func Parse{{.Normalized.Name}}(calldata []byte) (*{{.Normalized.Name}}Params, error) { + if len(calldata) <= 4 { + return nil, fmt.Errorf("invalid calldata input") + } + + _abi, err := {{$metaType}}MetaData.GetAbi() + if err != nil { + return nil, fmt.Errorf("failed to get abi of registry metadata: %w", err) + } + + out, err := _abi.Methods["{{.Original.Name}}"].Inputs.Unpack(calldata[4:]) + if err != nil { + return nil, fmt.Errorf("failed to unpack {{.Original.Name}} params data: %w", err) + } + + var paramsResult = new({{.Normalized.Name}}Params) + value := reflect.ValueOf(paramsResult).Elem() + + if value.NumField() != len(out) { + return nil, fmt.Errorf("failed to match calldata with param field number") + } + + {{range $i, $t := .Normalized.Inputs}} + out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}} + + return &{{.Normalized.Name}}Params{ + {{range $i, $_ := .Normalized.Inputs}} Param_{{.Name}} : out{{$i}},{{end}} + }, nil + } + + {{end}} + {{end}} + {{if .Fallback}} // Fallback is a paid mutator transaction binding the contract fallback function. //