Skip to content

feature(abigen): support parse transaction input bytes readable go struct in abigen #27361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion accounts/abi/bind/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
//
Expand Down