From 9a59bdb2396397ead57d1e7112b3c83dc4df7831 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Thu, 12 Dec 2024 13:28:03 +0700 Subject: [PATCH] generation of custom error unpacking builds (not tested yet) --- accounts/abi/bind/bind.go | 5 +++++ accounts/abi/bind/source2.go.tpl | 18 ++++++++++++++++++ .../bind/v2/internal/solc_errors/bindings.go | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go index ea4ed55540..c25aff450f 100644 --- a/accounts/abi/bind/bind.go +++ b/accounts/abi/bind/bind.go @@ -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 { diff --git a/accounts/abi/bind/source2.go.tpl b/accounts/abi/bind/source2.go.tpl index 990988bbf1..62d17d2c17 100644 --- a/accounts/abi/bind/source2.go.tpl +++ b/accounts/abi/bind/source2.go.tpl @@ -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}} diff --git a/accounts/abi/bind/v2/internal/solc_errors/bindings.go b/accounts/abi/bind/v2/internal/solc_errors/bindings.go index 1af79d97b3..8067dd2e96 100644 --- a/accounts/abi/bind/v2/internal/solc_errors/bindings.go +++ b/accounts/abi/bind/v2/internal/solc_errors/bindings.go @@ -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