Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 1e48e2b

Browse files
authored
eth_getTransactionCount implementation (#91)
* Implemented eth_getTransactionCount endpoint * Linting fixes
1 parent 0e94252 commit 1e48e2b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

rpc/eth_api.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,16 @@ func (e *PublicEthAPI) GetStorageAt(address common.Address, key string, blockNum
102102
}
103103

104104
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
105-
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) hexutil.Uint64 {
106-
return 0
105+
func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) (hexutil.Uint64, error) {
106+
ctx := e.cliCtx.WithHeight(blockNum.Int64())
107+
res, _, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/nonce/%s", types.ModuleName, address), nil)
108+
if err != nil {
109+
return 0, err
110+
}
111+
112+
var out types.QueryResNonce
113+
e.cliCtx.Codec.MustUnmarshalJSON(res, &out)
114+
return hexutil.Uint64(out.Nonce), nil
107115
}
108116

109117
// GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash.

x/evm/querier.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package evm
22

33
import (
4+
"math/big"
5+
46
"github.com/cosmos/cosmos-sdk/codec"
57
sdk "github.com/cosmos/cosmos-sdk/types"
68
"github.com/cosmos/ethermint/version"
79
"github.com/cosmos/ethermint/x/evm/types"
810
ethcmn "github.com/ethereum/go-ethereum/common"
911
"github.com/ethereum/go-ethereum/common/hexutil"
1012
abci "github.com/tendermint/tendermint/abci/types"
11-
"math/big"
1213
)
1314

1415
// Supported endpoints
@@ -18,6 +19,7 @@ const (
1819
QueryBlockNumber = "blockNumber"
1920
QueryStorage = "storage"
2021
QueryCode = "code"
22+
QueryNonce = "nonce"
2123
)
2224

2325
// NewQuerier is the module level router for state queries
@@ -34,6 +36,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
3436
return queryStorage(ctx, path, keeper)
3537
case QueryCode:
3638
return queryCode(ctx, path, keeper)
39+
case QueryNonce:
40+
return queryNonce(ctx, path, keeper)
3741
default:
3842
return nil, sdk.ErrUnknownRequest("unknown query endpoint")
3943
}
@@ -103,3 +107,15 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error
103107

104108
return res, nil
105109
}
110+
111+
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
112+
addr := ethcmn.BytesToAddress([]byte(path[1]))
113+
nonce := keeper.GetNonce(ctx, addr)
114+
nRes := types.QueryResNonce{Nonce: nonce}
115+
res, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
116+
if err != nil {
117+
panic("could not marshal result to JSON")
118+
}
119+
120+
return res, nil
121+
}

x/evm/types/querier.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package types
22

33
import (
4-
"github.com/ethereum/go-ethereum/common/hexutil"
54
"math/big"
5+
6+
"github.com/ethereum/go-ethereum/common/hexutil"
67
)
78

9+
// QueryResProtocolVersion is response type for protocol version query
810
type QueryResProtocolVersion struct {
911
Version string `json:"result"`
1012
}
@@ -13,6 +15,7 @@ func (q QueryResProtocolVersion) String() string {
1315
return q.Version
1416
}
1517

18+
// QueryResBalance is response type for balance query
1619
type QueryResBalance struct {
1720
Balance *hexutil.Big `json:"result"`
1821
}
@@ -21,6 +24,7 @@ func (q QueryResBalance) String() string {
2124
return q.Balance.String()
2225
}
2326

27+
// QueryResBlockNumber is response type for block number query
2428
type QueryResBlockNumber struct {
2529
Number *big.Int `json:"result"`
2630
}
@@ -29,6 +33,7 @@ func (q QueryResBlockNumber) String() string {
2933
return q.Number.String()
3034
}
3135

36+
// QueryResStorage is response type for storage query
3237
type QueryResStorage struct {
3338
Value []byte `json:"value"`
3439
}
@@ -37,10 +42,20 @@ func (q QueryResStorage) String() string {
3742
return string(q.Value)
3843
}
3944

45+
// QueryResCode is response type for code query
4046
type QueryResCode struct {
4147
Code []byte
4248
}
4349

4450
func (q QueryResCode) String() string {
4551
return string(q.Code)
4652
}
53+
54+
// QueryResNonce is response type for Nonce query
55+
type QueryResNonce struct {
56+
Nonce uint64 `json:"result"`
57+
}
58+
59+
func (q QueryResNonce) String() string {
60+
return string(q.Nonce)
61+
}

0 commit comments

Comments
 (0)