full support for errors with working test
This commit is contained in:
parent
9552b30263
commit
736d587990
|
@ -156,10 +156,10 @@ var (
|
||||||
{{$i := 0}}
|
{{$i := 0}}
|
||||||
{{range $k, $v := .Errors}}
|
{{range $k, $v := .Errors}}
|
||||||
{{ if eq $i 0 }}
|
{{ if eq $i 0 }}
|
||||||
if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
|
if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err == nil {
|
||||||
return val
|
return val
|
||||||
{{ else }}
|
{{ else }}
|
||||||
} else if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err != nil {
|
} else if val, err := _{{$contract.Type}}.Unpack{{.Normalized.Name}}Error(raw); err == nil {
|
||||||
return val
|
return val
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
{{$i = add $i 1}}
|
{{$i = add $i 1}}
|
||||||
|
|
|
@ -67,10 +67,10 @@ func (_C *C) PackFoo() ([]byte, error) {
|
||||||
|
|
||||||
func (_C *C) UnpackError(raw []byte) any {
|
func (_C *C) UnpackError(raw []byte) any {
|
||||||
|
|
||||||
if val, err := _C.UnpackBadThingError(raw); err != nil {
|
if val, err := _C.UnpackBadThingError(raw); err == nil {
|
||||||
return val
|
return val
|
||||||
|
|
||||||
} else if val, err := _C.UnpackBadThing2Error(raw); err != nil {
|
} else if val, err := _C.UnpackBadThing2Error(raw); err == nil {
|
||||||
return val
|
return val
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"contracts":{"contract.sol:C":{"abi":[{"inputs":[{"internalType":"uint256","name":"arg1","type":"uint256"},{"internalType":"uint256","name":"arg2","type":"uint256"},{"internalType":"uint256","name":"arg3","type":"uint256"},{"internalType":"bool","name":"arg4","type":"bool"}],"name":"BadThing","type":"error"},{"inputs":[{"internalType":"uint256","name":"arg1","type":"uint256"},{"internalType":"uint256","name":"arg2","type":"uint256"},{"internalType":"uint256","name":"arg3","type":"uint256"},{"internalType":"uint256","name":"arg4","type":"uint256"}],"name":"BadThing2","type":"error"},{"inputs":[],"name":"Bar","outputs":[],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"Foo","outputs":[],"stateMutability":"pure","type":"function"}],"bin":"6080604052348015600e575f80fd5b506101c58061001c5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063b0a378b014610038578063bfb4ebcf14610042575b5f80fd5b61004061004c565b005b61004a610092565b005b5f6001600260036040517fd233a24f00000000000000000000000000000000000000000000000000000000815260040161008994939291906100ef565b60405180910390fd5b5f600160025f6040517fbb6a82f10000000000000000000000000000000000000000000000000000000081526004016100ce949392919061014c565b60405180910390fd5b5f819050919050565b6100e9816100d7565b82525050565b5f6080820190506101025f8301876100e0565b61010f60208301866100e0565b61011c60408301856100e0565b61012960608301846100e0565b95945050505050565b5f8115159050919050565b61014681610132565b82525050565b5f60808201905061015f5f8301876100e0565b61016c60208301866100e0565b61017960408301856100e0565b610186606083018461013d565b9594505050505056fea26469706673582212203f89da086f6d7e52e75f82a20ebbf7337f166a6dbae309180c8bb95e1a157e6e64736f6c634300081a0033"}},"version":"0.8.26+commit.8a97fa7a.Darwin.appleclang"}
|
{"contracts":{"contract.sol:C":{"abi":[{"inputs":[{"internalType":"uint256","name":"arg1","type":"uint256"},{"internalType":"uint256","name":"arg2","type":"uint256"},{"internalType":"uint256","name":"arg3","type":"uint256"},{"internalType":"bool","name":"arg4","type":"bool"}],"name":"BadThing","type":"error"},{"inputs":[{"internalType":"uint256","name":"arg1","type":"uint256"},{"internalType":"uint256","name":"arg2","type":"uint256"},{"internalType":"uint256","name":"arg3","type":"uint256"},{"internalType":"uint256","name":"arg4","type":"uint256"}],"name":"BadThing2","type":"error"},{"inputs":[],"name":"Bar","outputs":[],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"Foo","outputs":[],"stateMutability":"pure","type":"function"}],"bin":"6080604052348015600e575f80fd5b506101c58061001c5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063b0a378b014610038578063bfb4ebcf14610042575b5f80fd5b61004061004c565b005b61004a610092565b005b5f6001600260036040517fd233a24f00000000000000000000000000000000000000000000000000000000815260040161008994939291906100ef565b60405180910390fd5b5f600160025f6040517fbb6a82f10000000000000000000000000000000000000000000000000000000081526004016100ce949392919061014c565b60405180910390fd5b5f819050919050565b6100e9816100d7565b82525050565b5f6080820190506101025f8301876100e0565b61010f60208301866100e0565b61011c60408301856100e0565b61012960608301846100e0565b95945050505050565b5f8115159050919050565b61014681610132565b82525050565b5f60808201905061015f5f8301876100e0565b61016c60208301866100e0565b61017960408301856100e0565b610186606083018461013d565b9594505050505056fea264697066735822122043974fbdd5c75b36bb8fe9dd68c112de4d094a0d8626d74e03edd5e48f18118164736f6c634300081a0033"},"contract.sol:C2":{"abi":[{"inputs":[{"internalType":"uint256","name":"arg1","type":"uint256"},{"internalType":"uint256","name":"arg2","type":"uint256"},{"internalType":"uint256","name":"arg3","type":"uint256"},{"internalType":"bool","name":"arg4","type":"bool"}],"name":"BadThing","type":"error"},{"inputs":[],"name":"Foo","outputs":[],"stateMutability":"pure","type":"function"}],"bin":"6080604052348015600e575f80fd5b506101148061001c5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c8063bfb4ebcf14602a575b5f80fd5b60306032565b005b5f600160025f6040517fbb6a82f1000000000000000000000000000000000000000000000000000000008152600401606c949392919060a3565b60405180910390fd5b5f819050919050565b6085816075565b82525050565b5f8115159050919050565b609d81608b565b82525050565b5f60808201905060b45f830187607e565b60bf6020830186607e565b60ca6040830185607e565b60d560608301846096565b9594505050505056fea264697066735822122073ad1e2383066bba44481ee5aaadd9a60a3e08e602e13ebf1c67c51ef47d191564736f6c634300081a0033"}},"version":"0.8.26+commit.8a97fa7a.Darwin.appleclang"}
|
||||||
|
|
|
@ -22,3 +22,15 @@ contract C {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// purpose of this is to test that generation of metadata for contract that emits one error produces valid Go code
|
||||||
|
contract C2 {
|
||||||
|
function Foo() public pure {
|
||||||
|
revert BadThing({
|
||||||
|
arg1: uint256(0),
|
||||||
|
arg2: uint256(1),
|
||||||
|
arg3: uint256(2),
|
||||||
|
arg4: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -373,3 +373,15 @@ func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []
|
||||||
}
|
}
|
||||||
return unpack(packedOutput)
|
return unpack(packedOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func UnpackError(metadata *bind.MetaData, raw []byte) (any, error) {
|
||||||
|
fmt.Printf("raw is %x\n", raw[0:4])
|
||||||
|
errType := metadata.Errors[fmt.Sprintf("%x", raw[0:4])]
|
||||||
|
abi, _ := metadata.GetAbi() // TODO: check error here?
|
||||||
|
res := reflect.New(errType).Interface()
|
||||||
|
fmt.Printf("err type name is %s\n", errType.Name())
|
||||||
|
err := abi.UnpackIntoInterface(&res, errType.Name(), raw)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
||||||
|
"github.com/ethereum/go-ethereum/ethclient"
|
||||||
"github.com/ethereum/go-ethereum/ethclient/simulated"
|
"github.com/ethereum/go-ethereum/ethclient/simulated"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
@ -396,18 +397,32 @@ func TestErrors(t *testing.T) {
|
||||||
t.Fatalf("WaitDeployed failed %v", err)
|
t.Fatalf("WaitDeployed failed %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctrct, _ := solc_errors.NewC()
|
||||||
|
|
||||||
var packedInput []byte
|
var packedInput []byte
|
||||||
opts := &bind.CallOpts{
|
opts := &bind.CallOpts{
|
||||||
From: res.Addrs[solc_errors.CMetaData.Pattern],
|
From: res.Addrs[solc_errors.CMetaData.Pattern],
|
||||||
}
|
}
|
||||||
|
packedInput, _ = ctrct.PackFoo()
|
||||||
|
|
||||||
instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend}
|
instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend}
|
||||||
_, err := Call[struct{}](&instance, opts, packedInput, func(packed []byte) (*struct{}, error) { return nil, nil })
|
_, err = Call[struct{}](&instance, opts, packedInput, func(packed []byte) (*struct{}, error) { return nil, nil })
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected call to fail")
|
t.Fatalf("expected call to fail")
|
||||||
}
|
}
|
||||||
|
raw, hasRevertErrorData := ethclient.RevertErrorData(err)
|
||||||
|
if !hasRevertErrorData {
|
||||||
|
t.Fatalf("expected call error to contain revert error data.")
|
||||||
|
}
|
||||||
|
unpackedErr := ctrct.UnpackError(raw)
|
||||||
|
if unpackedErr == nil {
|
||||||
|
t.Fatalf("expected to unpack error")
|
||||||
|
}
|
||||||
|
// TODO: check anything about the error?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this test will pass if the code is changed but not compiled to a combined-abi.json.
|
||||||
|
// Not really possible to test this without including solc in the path when running CI.
|
||||||
func TestBindingGeneration(t *testing.T) {
|
func TestBindingGeneration(t *testing.T) {
|
||||||
matches, _ := filepath.Glob("internal/*")
|
matches, _ := filepath.Glob("internal/*")
|
||||||
var dirs []string
|
var dirs []string
|
||||||
|
|
|
@ -38,6 +38,8 @@ type Error struct {
|
||||||
// ID returns the canonical representation of the error's signature used by the
|
// ID returns the canonical representation of the error's signature used by the
|
||||||
// abi definition to identify event names and types.
|
// abi definition to identify event names and types.
|
||||||
ID common.Hash
|
ID common.Hash
|
||||||
|
|
||||||
|
Selector string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewError(name string, inputs Arguments) Error {
|
func NewError(name string, inputs Arguments) Error {
|
||||||
|
@ -74,6 +76,7 @@ func NewError(name string, inputs Arguments) Error {
|
||||||
str: str,
|
str: str,
|
||||||
Sig: sig,
|
Sig: sig,
|
||||||
ID: id,
|
ID: id,
|
||||||
|
Selector: fmt.Sprintf("%x", id[0:4]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue