generation of custom error unpacking builds (not tested yet)

This commit is contained in:
Jared Wasinger 2024-12-12 13:28:03 +07:00 committed by Felix Lange
parent e7e1426d89
commit 9a59bdb239
3 changed files with 32 additions and 0 deletions

View File

@ -70,6 +70,10 @@ func isKeyWord(arg string) bool {
return true
}
func add(val1, val2 int) int {
return val1 + val1
}
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
// to be used as is in client code, but rather as an intermediate struct which
// enforces compile time type safety and naming convention as opposed to having to
@ -146,6 +150,7 @@ func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[strin
"bindtopictype": bindTopicType,
"capitalise": capitalise,
"decapitalise": decapitalise,
"add": add,
}
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSourceV2))
if err := tmpl.Execute(buffer, data); err != nil {

View File

@ -151,6 +151,24 @@ var (
}
{{end}}
{{ if .Errors }}
func (_{{$contract.Type}} *{{$contract.Type}}) UnpackError(raw []byte) any {
{{$i := 0}}
{{range $k, $v := .Errors}}
{{ if eq $i 0 }}
if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
return val
{{ else }}
} else if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
return val
{{ end -}}
{{$i = add $i 1}}
{{end -}}
}
return nil
}
{{ end -}}
{{range .Errors}}
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} error raised by the {{$contract.Type}} contract.
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}

View File

@ -58,6 +58,15 @@ func (_C *C) PackFoo() ([]byte, error) {
return _C.abi.Pack("Foo")
}
func (_C *C) UnpackError(raw []byte) any {
if val, err := _C.UnpackBadThingError(raw); err != nil {
return val
}
return nil
}
// CBadThing represents a BadThing error raised by the C contract.
type CBadThing struct {
Arg1 *big.Int