eth_getTransactionCount implementation (#91)
* Implemented eth_getTransactionCount endpoint * Linting fixes
This commit is contained in:
parent
0e942527da
commit
1e48e2b115
@ -102,8 +102,16 @@ func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
|
||||||
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) hexutil.Uint64 {
|
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) (hexutil.Uint64, error) {
|
||||||
return 0
|
ctx := e.cliCtx.WithHeight(blockNum.Int64())
|
||||||
|
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address), nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var out types.QueryResNonce
|
||||||
|
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
|
||||||
|
return hexutil.Uint64(out.Nonce), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
|
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package evm
|
package evm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/big"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/ethermint/version"
|
"github.com/cosmos/ethermint/version"
|
||||||
@ -8,7 +10,6 @@ import (
|
|||||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Supported endpoints
|
// Supported endpoints
|
||||||
@ -18,6 +19,7 @@ const (
|
|||||||
QueryBlockNumber = "blockNumber"
|
QueryBlockNumber = "blockNumber"
|
||||||
QueryStorage = "storage"
|
QueryStorage = "storage"
|
||||||
QueryCode = "code"
|
QueryCode = "code"
|
||||||
|
QueryNonce = "nonce"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewQuerier is the module level router for state queries
|
// NewQuerier is the module level router for state queries
|
||||||
@ -34,6 +36,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
|
|||||||
return queryStorage(ctx, path, keeper)
|
return queryStorage(ctx, path, keeper)
|
||||||
case QueryCode:
|
case QueryCode:
|
||||||
return queryCode(ctx, path, keeper)
|
return queryCode(ctx, path, keeper)
|
||||||
|
case QueryNonce:
|
||||||
|
return queryNonce(ctx, path, keeper)
|
||||||
default:
|
default:
|
||||||
return nil, sdk.ErrUnknownRequest("unknown query endpoint")
|
return nil, sdk.ErrUnknownRequest("unknown query endpoint")
|
||||||
}
|
}
|
||||||
@ -103,3 +107,15 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error
|
|||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
|
||||||
|
addr := ethcmn.BytesToAddress([]byte(path[1]))
|
||||||
|
nonce := keeper.GetNonce(ctx, addr)
|
||||||
|
nRes := types.QueryResNonce{Nonce: nonce}
|
||||||
|
res, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
|
||||||
|
if err != nil {
|
||||||
|
panic("could not marshal result to JSON")
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// QueryResProtocolVersion is response type for protocol version query
|
||||||
type QueryResProtocolVersion struct {
|
type QueryResProtocolVersion struct {
|
||||||
Version string `json:"result"`
|
Version string `json:"result"`
|
||||||
}
|
}
|
||||||
@ -13,6 +15,7 @@ func (q QueryResProtocolVersion) String() string {
|
|||||||
return q.Version
|
return q.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryResBalance is response type for balance query
|
||||||
type QueryResBalance struct {
|
type QueryResBalance struct {
|
||||||
Balance *hexutil.Big `json:"result"`
|
Balance *hexutil.Big `json:"result"`
|
||||||
}
|
}
|
||||||
@ -21,6 +24,7 @@ func (q QueryResBalance) String() string {
|
|||||||
return q.Balance.String()
|
return q.Balance.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryResBlockNumber is response type for block number query
|
||||||
type QueryResBlockNumber struct {
|
type QueryResBlockNumber struct {
|
||||||
Number *big.Int `json:"result"`
|
Number *big.Int `json:"result"`
|
||||||
}
|
}
|
||||||
@ -29,6 +33,7 @@ func (q QueryResBlockNumber) String() string {
|
|||||||
return q.Number.String()
|
return q.Number.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryResStorage is response type for storage query
|
||||||
type QueryResStorage struct {
|
type QueryResStorage struct {
|
||||||
Value []byte `json:"value"`
|
Value []byte `json:"value"`
|
||||||
}
|
}
|
||||||
@ -37,6 +42,7 @@ func (q QueryResStorage) String() string {
|
|||||||
return string(q.Value)
|
return string(q.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryResCode is response type for code query
|
||||||
type QueryResCode struct {
|
type QueryResCode struct {
|
||||||
Code []byte
|
Code []byte
|
||||||
}
|
}
|
||||||
@ -44,3 +50,12 @@ type QueryResCode struct {
|
|||||||
func (q QueryResCode) String() string {
|
func (q QueryResCode) String() string {
|
||||||
return string(q.Code)
|
return string(q.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryResNonce is response type for Nonce query
|
||||||
|
type QueryResNonce struct {
|
||||||
|
Nonce uint64 `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q QueryResNonce) String() string {
|
||||||
|
return string(q.Nonce)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user