Skip to content

Commit

Permalink
generation of custom error unpacking builds (not tested yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwasinger committed Dec 12, 2024
1 parent 6df0f05 commit e31de66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions accounts/abi/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions accounts/abi/bind/source2.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
9 changes: 9 additions & 0 deletions accounts/abi/bind/v2/internal/solc_errors/bindings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e31de66

Please sign in to comment.