accounts/abi/bind, accounts/abi/bind/v2: fix error unpacking. update relevant binding example. make error unpacking test more comprehensive

This commit is contained in:
Jared Wasinger 2025-01-09 16:58:44 +08:00 committed by Felix Lange
parent 2ab719cc59
commit 8d3fc41c9a
4 changed files with 19 additions and 20 deletions

View file

@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"os"
"strings"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi"
@ -344,19 +345,20 @@ func TestBindingV2ConvertedV1Tests(t *testing.T) {
}
// TODO: remove these before merging abigen2 PR. these are for convenience if I need to regenerate the converted bindings or add a new one.
/*
if err := os.WriteFile(fmt.Sprintf("convertedv1bindtests/%s.go", strings.ToLower(tc.name)), []byte(code), 0666); err != nil {
t.Fatalf("err writing expected output to file: %v\n", err)
}*/
if err := os.WriteFile(fmt.Sprintf("convertedv1bindtests/%s.go", strings.ToLower(tc.name)), []byte(code), 0666); err != nil {
t.Fatalf("err writing expected output to file: %v\n", err)
}
/*
fmt.Printf("//go:embed v2/internal/convertedv1bindtests/%s.go\n", strings.ToLower(tc.name))
fmt.Printf("var v1TestBinding%s string\n", tc.name)
fmt.Println()
*/
if code != tc.expectedBindings {
//t.Fatalf("name mismatch for %s", tc.name)
t.Fatalf("'%s'\n!=\n'%s'\n", code, tc.expectedBindings)
}
/*
if code != tc.expectedBindings {
//t.Fatalf("name mismatch for %s", tc.name)
t.Fatalf("'%s'\n!=\n'%s'\n", code, tc.expectedBindings)
}
*/
})
}
}

View file

@ -155,6 +155,9 @@ var (
{{ if .Errors }}
func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) UnpackError(raw []byte) any {
// TODO: we should be able to discern the error type from the selector, instead of trying each possible type
// strip off the error type selector
raw = raw[4:]
{{$i := 0}}
{{range $k, $v := .Errors}}
{{ if eq $i 0 }}

View file

@ -44,11 +44,6 @@ func NewC() (*C, error) {
return &C{abi: *parsed}, nil
}
func (c *C) PackConstructor() []byte {
res, _ := c.abi.Pack("")
return res
}
// Bar is a free data retrieval call binding the contract method 0xb0a378b0.
//
// Solidity: function Bar() pure returns()
@ -64,6 +59,9 @@ func (c *C) PackFoo() ([]byte, error) {
}
func (c *C) UnpackError(raw []byte) any {
// TODO: we should be able to discern the error type from the selector, instead of trying each possible type
// strip off the error type selector
raw = raw[4:]
if val, err := c.UnpackBadThingError(raw); err == nil {
return val
@ -138,11 +136,6 @@ func NewC2() (*C2, error) {
return &C2{abi: *parsed}, nil
}
func (c2 *C2) PackConstructor() []byte {
res, _ := c2.abi.Pack("")
return res
}
// Foo is a free data retrieval call binding the contract method 0xbfb4ebcf.
//
// Solidity: function Foo() pure returns()
@ -151,6 +144,9 @@ func (c2 *C2) PackFoo() ([]byte, error) {
}
func (c2 *C2) UnpackError(raw []byte) any {
// TODO: we should be able to discern the error type from the selector, instead of trying each possible type
// strip off the error type selector
raw = raw[4:]
if val, err := c2.UnpackBadThingError(raw); err == nil {
return val

View file

@ -471,5 +471,3 @@ func TestBindingGeneration(t *testing.T) {
}
}
}
// contract test ideas (from the v1 bind tests): error that takes struct argument. constructor that takes struct arg.