From 123a6566c7b5fd16b0819e03bfbb674fcce0327f Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 28 Dec 2023 12:14:14 +0800 Subject: [PATCH] fix get storage/ ErrorObject.Error / code gen --- abi/abi.go | 13 +++++-------- abi/type.go | 10 +++++++--- abigen/generator.go | 2 +- jsonrpc/codec/codec.go | 16 +++++++++------- jsonrpc/eth.go | 4 ++++ utils/utils.go | 7 +++++-- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/abi/abi.go b/abi/abi.go index df1c787..7887eec 100644 --- a/abi/abi.go +++ b/abi/abi.go @@ -156,7 +156,7 @@ type Error struct { Inputs *Type } -//Copy is lightly copy inside inputs, do not modify inner pointer objects. +// Copy is lightly copy inside inputs, do not modify inner pointer objects. func (e *Error) Copy() *Error { return &Error{ Name: e.Name, @@ -195,7 +195,7 @@ type Method struct { Outputs *Type } -//Copy is lightly copy inside inputs, do not modify inner pointer objects. +// Copy is lightly copy inside inputs, do not modify inner pointer objects. func (m *Method) Copy() *Method { return &Method{ m.Name, @@ -295,7 +295,7 @@ func (e *Event) Sig() string { return buildSignature(e.Name, e.Inputs) } -//Copy is lightly copy inside inputs, do not modify inner pointer objects. +// Copy is lightly copy inside inputs, do not modify inner pointer objects. func (e *Event) Copy() *Event { return &Event{ Name: e.Name, @@ -429,15 +429,12 @@ func (a *argument) UnmarshalJSON(data []byte) error { } t, err := NewTypeFromArgument(arg) - utils.Ensure(err) + utils.Ensure(err, string(data)) if t.kind == KindTuple { if arg.InternalType == "" { t.tupleName = "" } else { - tuples := strings.Split(arg.InternalType, ".") - tuplename := tuples[len(tuples)-1] - tuplename = strings.Title(tuplename) - t.tupleName = tuplename + t.tupleName = strings.TrimSpace(internalTypeToArg(arg.InternalType)) } } diff --git a/abi/type.go b/abi/type.go index b503fc4..093954b 100644 --- a/abi/type.go +++ b/abi/type.go @@ -112,7 +112,7 @@ func (t *Type) RawName() string { return t.tupleName } -//just light copy +// just light copy func (t *Type) Copy() *Type { a := Type{ kind: t.kind, @@ -194,7 +194,7 @@ func (t *Type) isDynamicType() bool { } func internalTypeToArg(internalType string) string { - internalName := internalType + internalName := strings.TrimPrefix(internalType, "struct ") internalNames := strings.Split(internalName, ".") internalName = internalNames[len(internalNames)-1] internalName = strings.Title(internalName) @@ -243,7 +243,11 @@ func NewTypeFromArgument(arg *ArgumentStr) (*Type, error) { if err != nil { return nil, err } - return NewType(str) + ty, err := NewType(str) + if err != nil { + return nil, fmt.Errorf("create type error: %s, %v", str, err) + } + return ty, nil } // NewType parses a type in string format diff --git a/abigen/generator.go b/abigen/generator.go index 23db095..cb51b3d 100644 --- a/abigen/generator.go +++ b/abigen/generator.go @@ -83,7 +83,7 @@ func genCodeToBytes(name string, funcMap template.FuncMap, temp string, input ma } b, err := format.Source(buffer.Bytes()) if err != nil { - return nil, err + return nil, fmt.Errorf("format code error: %s, %v", string(buffer.Bytes()), err) } return b, nil } diff --git a/jsonrpc/codec/codec.go b/jsonrpc/codec/codec.go index d15b99b..602e700 100644 --- a/jsonrpc/codec/codec.go +++ b/jsonrpc/codec/codec.go @@ -25,10 +25,10 @@ type Response struct { // ErrorObject is a jsonrpc error type ErrorObject struct { - Code int `json:"code"` - Message string `json:"message"` - Data interface{} `json:"data,omitempty"` - DecodedMessage string `json:"decoded_message,omitempty"` + Code int `json:"code"` + Message string `json:"message"` + Data string `json:"data,omitempty"` + DecodedMessage string `json:"decoded_message,omitempty"` } // Subscription is a jsonrpc subscription @@ -39,9 +39,11 @@ type Subscription struct { // Error implements error interface func (e *ErrorObject) Error() string { - info, err := registry.ErrInstance().ParseError(hexutil.MustDecode(e.Data.(map[string]interface{})["data"].(string))) - if err == nil { - e.DecodedMessage = info + if len(e.Data) != 0 { + info, err := registry.ErrInstance().ParseError(hexutil.MustDecode(e.Data)) + if err == nil { + e.DecodedMessage = info + } } data, err := json.Marshal(e) if err != nil { diff --git a/jsonrpc/eth.go b/jsonrpc/eth.go index ad0e9a3..d546743 100644 --- a/jsonrpc/eth.go +++ b/jsonrpc/eth.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math/big" + "strings" "github.com/laizy/web3" "github.com/laizy/web3/utils/common/hexutil" @@ -193,6 +194,9 @@ func (ec *Eth) GetStorage(account web3.Address, key web3.Hash, blockNumber web3. if err := ec.c.Call("eth_getStorageAt", &out, account, slot, blockNumber.String()); err != nil { return web3.Hash{}, err } + if len(strings.TrimPrefix(out, "0x")) == 0 { + return web3.Hash{}, nil + } return web3.HexToHash(out), nil } diff --git a/utils/utils.go b/utils/utils.go index 24edfe8..e2faf17 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,9 +8,12 @@ import ( "strings" ) -func Ensure(err error) { +func Ensure(err error, msg ...interface{}) { if err != nil { - panic(err) + if len(msg) == 0 { + msg = []interface{}{""} + } + panic(fmt.Errorf("%v %v", err, msg[0])) } }