Skip to content

Commit e31de66

Browse files
committed
generation of custom error unpacking builds (not tested yet)
1 parent 6df0f05 commit e31de66

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

accounts/abi/bind/bind.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func isKeyWord(arg string) bool {
7070
return true
7171
}
7272

73+
func add(val1, val2 int) int {
74+
return val1 + val1
75+
}
76+
7377
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
7478
// to be used as is in client code, but rather as an intermediate struct which
7579
// 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
146150
"bindtopictype": bindTopicType,
147151
"capitalise": capitalise,
148152
"decapitalise": decapitalise,
153+
"add": add,
149154
}
150155
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSourceV2))
151156
if err := tmpl.Execute(buffer, data); err != nil {

accounts/abi/bind/source2.go.tpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ var (
151151
}
152152
{{end}}
153153

154+
{{ if .Errors }}
155+
func (_{{$contract.Type}} *{{$contract.Type}}) UnpackError(raw []byte) any {
156+
{{$i := 0}}
157+
{{range $k, $v := .Errors}}
158+
{{ if eq $i 0 }}
159+
if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
160+
return val
161+
{{ else }}
162+
} else if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
163+
return val
164+
{{ end -}}
165+
{{$i = add $i 1}}
166+
{{end -}}
167+
}
168+
return nil
169+
}
170+
{{ end -}}
171+
154172
{{range .Errors}}
155173
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} error raised by the {{$contract.Type}} contract.
156174
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}

accounts/abi/bind/v2/internal/solc_errors/bindings.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)