Skip to content

Commit

Permalink
fix get storage/ ErrorObject.Error / code gen (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Dec 28, 2023
1 parent dbf5f63 commit 646a553
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
13 changes: 5 additions & 8 deletions abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
}

Expand Down
10 changes: 7 additions & 3 deletions abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion abigen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions jsonrpc/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,8 +39,8 @@ type Subscription struct {

// Error implements error interface
func (e *ErrorObject) Error() string {
if data := e.Data.(map[string]interface{})["data"]; data != nil {
info, err := registry.ErrInstance().ParseError(hexutil.MustDecode(data.(string)))
if len(e.Data) != 0 {
info, err := registry.ErrInstance().ParseError(hexutil.MustDecode(e.Data))
if err == nil {
e.DecodedMessage = info
}
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/laizy/web3"
"github.com/laizy/web3/utils/common/hexutil"
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
}

Expand Down

0 comments on commit 646a553

Please sign in to comment.