Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

fix nil pointer exception when parameters are nil #180

Merged
merged 2 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ethereum/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (e *PublicAPI) Sign(address common.Address, data hexutil.Bytes) (hexutil.By

// SendTransaction sends an Ethereum transaction.
func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, error) {
e.logger.Debugln("eth_sendTransaction", "args", args)
e.logger.Debugln("eth_sendTransaction", "args", args.String())

// Look up the wallet containing the requested signer
_, err := e.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes()))
Expand Down Expand Up @@ -493,7 +493,7 @@ func (e *PublicAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error)

// Call performs a raw contract call.
func (e *PublicAPI) Call(args rpctypes.CallArgs, blockNr rpctypes.BlockNumber, _ *rpctypes.StateOverride) (hexutil.Bytes, error) {
e.logger.Debugln("eth_call", "args", args, "block number", blockNr)
e.logger.Debugln("eth_call", "args", args.String(), "block number", blockNr)
simRes, err := e.doCall(args, blockNr, big.NewInt(ethermint.DefaultRPCGasLimit))
if err != nil {
return []byte{}, err
Expand Down
58 changes: 52 additions & 6 deletions ethereum/rpc/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -68,6 +70,20 @@ type SendTxArgs struct {
ChainID *hexutil.Big `json:"chainId,omitempty"`
}

// String return the struct in a string format
func (args *SendTxArgs) String() string {
// Todo: There is currently a bug with hexutil.Big when the value its nil, printing would trigger an exception
return fmt.Sprintf("SendTxArgs{From:%v, To:%v, Gas:%v,"+
" Nonce:%v, Data:%v, Input:%v, AccessList:%v}",
args.From,
args.To,
args.Gas,
args.Nonce,
args.Data,
args.Input,
args.AccessList)
}

// ToTransaction converts the arguments to an ethereum transaction.
// This assumes that setTxDefaults has been called.
func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx {
Expand All @@ -79,16 +95,34 @@ func (args *SendTxArgs) ToTransaction() *evmtypes.MsgEthereumTx {
}

data := &evmtypes.TxData{
To: args.To.Hex(),
ChainID: args.ChainID.ToInt().Bytes(),
Nonce: uint64(*args.Nonce),
GasLimit: uint64(*args.Gas),
GasPrice: args.GasPrice.ToInt().Bytes(),
Amount: args.Value.ToInt().Bytes(),
Input: input,
Accesses: evmtypes.NewAccessList(args.AccessList),
}

if args.ChainID != nil {
data.ChainID = args.ChainID.ToInt().Bytes()
}

if args.Nonce != nil {
data.Nonce = uint64(*args.Nonce)
}

if args.Gas != nil {
data.GasLimit = uint64(*args.Gas)
}

if args.GasPrice != nil {
data.GasPrice = args.GasPrice.ToInt().Bytes()
}

if args.Value != nil {
data.Amount = args.Value.ToInt().Bytes()
}

if args.To != nil {
data.To = args.To.Hex()
}

return &evmtypes.MsgEthereumTx{
Data: data,
From: args.From.String(),
Expand All @@ -106,6 +140,18 @@ type CallArgs struct {
AccessList *ethtypes.AccessList `json:"accessList"`
}

// String return the struct in a string format
func (args *CallArgs) String() string {
// Todo: There is currently a bug with hexutil.Big when the value its nil, printing would trigger an exception
return fmt.Sprintf("SendTxArgs{From:%v, To:%v, Gas:%v,"+
" Data:%v, AccessList:%v}",
args.From,
args.To,
args.Gas,
args.Data,
args.AccessList)
}

// StateOverride is the collection of overridden accounts.
type StateOverride map[common.Address]OverrideAccount

Expand Down