Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add transaction type #46

Merged
merged 1 commit into from
Apr 26, 2022
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
57 changes: 27 additions & 30 deletions rpc/ethereum/namespaces/rpc/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *PublicAPI) Coinbase() (string, error) {
}

// GasPrice returns the current gas price based on Ethermint's gas price oracle.
func (e *PublicAPI) GasPrice() (*big.Int, error) {
func (e *PublicAPI) GasPrice() (*hexutil.Big, error) {
e.logger.Debug("rpc_gasPrice")
tipcap, err := e.backend.SuggestGasTipCap()
if err != nil {
Expand All @@ -115,18 +115,18 @@ func (e *PublicAPI) GasPrice() (*big.Int, error) {
tipcap.Add(tipcap, head.BaseFee)
}

return tipcap, nil
return (*hexutil.Big)(tipcap), nil
}

// MaxPriorityFeePerGas returns a suggestion for a gas tip cap for dynamic fee transactions.
func (e *PublicAPI) MaxPriorityFeePerGas() (*big.Int, error) {
func (e *PublicAPI) MaxPriorityFeePerGas() (*hexutil.Big, error) {
e.logger.Debug("rpc_maxPriorityFeePerGas")
tipcap, err := e.backend.SuggestGasTipCap()
if err != nil {
return nil, err
}

return tipcap, nil
return (*hexutil.Big)(tipcap), nil
}

// Accounts returns the list of accounts available to this node.
Expand All @@ -149,17 +149,13 @@ func (e *PublicAPI) Accounts() ([]sdk.AccAddress, error) {
}

// BlockNumber returns the current block number.
func (e *PublicAPI) BlockNumber() (uint64, error) {
func (e *PublicAPI) BlockNumber() (hexutil.Uint64, error) {
e.logger.Debug("rpc_blockNumber")
result, err := e.backend.BlockNumber()
if err != nil {
return 0, nil
}
return uint64(result), nil
return e.backend.BlockNumber()
}

// GetBalance returns the provided account's balance up to the provided block number.
func (e *PublicAPI) GetBalance(addr sdk.AccAddress, blockNrOrHash rpctypes.BlockNumberOrHash) (*big.Int, error) {
func (e *PublicAPI) GetBalance(addr sdk.AccAddress, blockNrOrHash rpctypes.BlockNumberOrHash) (*hexutil.Big, error) {
address := common.BytesToAddress(addr)
e.logger.Debug("rpc_getBalance", "address", address.String(), "block number or hash", blockNrOrHash)

Expand All @@ -182,17 +178,17 @@ func (e *PublicAPI) GetBalance(addr sdk.AccAddress, blockNrOrHash rpctypes.Block
return nil, errors.New("invalid balance")
}

return val.BigInt(), nil
return (*hexutil.Big)(val.BigInt()), nil
}

// GetStorageAt returns the contract storage at the given address, block number, and key.
func (e *PublicAPI) GetStorageAt(addr sdk.AccAddress, key string, blockNrOrHash rpctypes.BlockNumberOrHash) (string, error) {
func (e *PublicAPI) GetStorageAt(addr sdk.AccAddress, key string, blockNrOrHash rpctypes.BlockNumberOrHash) (hexutil.Bytes, error) {
address := common.BytesToAddress(addr)
e.logger.Debug("rpc_getStorageAt", "address", address.Hex(), "key", key, "block number or hash", blockNrOrHash)

blockNum, err := e.getBlockNumber(blockNrOrHash)
if err != nil {
return "", err
return nil, err
}

req := &evmtypes.QueryStorageRequest{
Expand All @@ -202,60 +198,61 @@ func (e *PublicAPI) GetStorageAt(addr sdk.AccAddress, key string, blockNrOrHash

res, err := e.queryClient.Storage(rpctypes.ContextWithHeight(blockNum.Int64()), req)
if err != nil {
return "", err
return nil, err
}

value := common.HexToHash(res.Value)
return value.String(), nil
return value.Bytes(), nil
}

// GetTransactionCount returns the number of transactions at the given address up to the given block number.
func (e *PublicAPI) GetTransactionCount(addr sdk.AccAddress, blockNrOrHash rpctypes.BlockNumberOrHash) (uint64, error) {
func (e *PublicAPI) GetTransactionCount(addr sdk.AccAddress, blockNrOrHash rpctypes.BlockNumberOrHash) (*hexutil.Uint64, error) {
address := common.BytesToAddress(addr)
e.logger.Debug("rpc_getTransactionCount", "address", address.Hex(), "block number or hash", blockNrOrHash)
blockNum, err := e.getBlockNumber(blockNrOrHash)
if err != nil {
return 0, err
return nil, err
}
result, err := e.backend.GetTransactionCount(address, blockNum)
return uint64(*result), err
return e.backend.GetTransactionCount(address, blockNum)
}

// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) uint {
func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint {
e.logger.Debug("rpc_getBlockTransactionCountByHash", "hash", hash.Hex())

resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
if err != nil {
e.logger.Debug("block not found", "hash", hash.Hex(), "error", err.Error())
return 0
return nil
}

if resBlock.Block == nil {
e.logger.Debug("block not found", "hash", hash.Hex())
return 0
return nil
}

ethMsgs := e.backend.GetEthereumMsgsFromTendermintBlock(resBlock)
return uint(len(ethMsgs))
n := hexutil.Uint(len(ethMsgs))
return &n
}

// GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number.
func (e *PublicAPI) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) uint {
func (e *PublicAPI) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint {
e.logger.Debug("rpc_getBlockTransactionCountByNumber", "height", blockNum.Int64())
resBlock, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
if err != nil {
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
return 0
return nil
}

if resBlock.Block == nil {
e.logger.Debug("block not found", "height", blockNum.Int64())
return 0
return nil
}

ethMsgs := e.backend.GetEthereumMsgsFromTendermintBlock(resBlock)
return uint(len(ethMsgs))
n := hexutil.Uint(len(ethMsgs))
return &n
}

// GetCode returns the contract code at the given address and block number.
Expand Down Expand Up @@ -290,8 +287,8 @@ func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*EthLog, error) {
}
results := make([]*EthLog, 0)
for _, log := range logs {
var result = EthLog{log}
results = append(results, &result)
// var result = EthLog{log}
results = append(results, &EthLog{log})
}
return results, err
}
Expand Down
13 changes: 7 additions & 6 deletions rpc/ethereum/namespaces/rpc/log_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -18,22 +19,22 @@ func (l EthLog) MarshalJSON() ([]byte, error) {
Address sdk.AccAddress `json:"address" gencodec:"required"`
Topics []common.Hash `json:"topics" gencodec:"required"`
Data hexutil.Bytes `json:"data" gencodec:"required"`
BlockNumber uint64 `json:"blockNumber"`
BlockNumber hexutil.Uint64 `json:"blockNumber"`
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
TxIndex uint `json:"transactionIndex"`
TxIndex hexutil.Uint `json:"transactionIndex"`
BlockHash common.Hash `json:"blockHash"`
Index uint `json:"logIndex"`
Index hexutil.Uint `json:"logIndex"`
Removed bool `json:"removed"`
}
var enc Log
enc.Address = sdk.AccAddress(l.Address[:])
enc.Topics = l.Topics
enc.Data = l.Data
enc.BlockNumber = l.BlockNumber
enc.BlockNumber = hexutil.Uint64(l.BlockNumber)
enc.TxHash = l.TxHash
enc.TxIndex = l.TxIndex
enc.TxIndex = hexutil.Uint(l.TxIndex)
enc.BlockHash = l.BlockHash
enc.Index = l.Index
enc.Index = hexutil.Uint(l.Index)
enc.Removed = l.Removed
return json.Marshal(&enc)
}
28 changes: 2 additions & 26 deletions rpc/ethereum/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ import (
// internal pkg on geth.

type TransactionArgs struct {
From sdk.AccAddress `json:"from"`
To sdk.AccAddress `json:"to"`
Gas uint64 `json:"gas"`
GasPrice uint64 `json:"gasPrice"`
MaxFeePerGas uint64 `json:"maxFeePerGas"`
MaxPriorityFeePerGas uint64 `json:"maxPriorityFeePerGas"`
Value uint64 `json:"value"`
Nonce uint64 `json:"nonce"`
From sdk.AccAddress `json:"from"`
To sdk.AccAddress `json:"to"`
evmtypes.TransactionArgs
}

Expand All @@ -36,24 +30,6 @@ func (trans TransactionArgs) New() evmtypes.TransactionArgs {
to := common.BytesToAddress(trans.To)
args.To = &to
}
if trans.Gas > 0 {
args.Gas = (*hexutil.Uint64)(&trans.Gas)
}
if trans.GasPrice > 0 {
args.GasPrice = (*hexutil.Big)(big.NewInt(int64(trans.GasPrice)))
}
if trans.MaxFeePerGas > 0 {
args.MaxFeePerGas = (*hexutil.Big)(big.NewInt(int64(trans.MaxFeePerGas)))
}
if trans.MaxPriorityFeePerGas > 0 {
args.MaxPriorityFeePerGas = (*hexutil.Big)(big.NewInt(int64(trans.MaxPriorityFeePerGas)))
}
if trans.Value > 0 {
args.Value = (*hexutil.Big)(big.NewInt(int64(trans.Value)))
}
if trans.Nonce > 0 {
args.Nonce = (*hexutil.Uint64)(&trans.Nonce)
}
return args
}

Expand Down