Skip to content

Commit

Permalink
Merge pull request #1 from kaonone/feat/eth_getTransactionCount_imple…
Browse files Browse the repository at this point in the history
…mented

add eth_getTransactionCount to getaddressnounce mapped
  • Loading branch information
askiiRobotics authored Nov 28, 2024
2 parents 878cfe7 + 388a482 commit 37714f2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pkg/eth/rpc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,14 @@ type (
Address string
Tag string
}
GetTransactionCountResponse string
)

func (r *GetTransactionCountRequest) UnmarshalJSON(data []byte) error {
tmp := []interface{}{&r.Address, &r.Tag}
return json.Unmarshal(data, &tmp)
}

// ========== getstorage ============= //
type (
GetStorageRequest struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/kaon/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
MethodSendRawTx = "sendrawtransaction"
MethodGetStakingInfo = "getstakinginfo"
MethodGetAddressBalance = "getaddressbalance"
MethodGetAddressNounce = "getaddressnounce"
MethodGetAddressUTXOs = "getaddressutxos"
MethodCreateWallet = "createwallet"
MethodLoadWallet = "loadwallet"
Expand Down
17 changes: 11 additions & 6 deletions pkg/kaon/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,17 @@ func (m *Method) GetGasPrice(ctx context.Context) (result *big.Int, err error) {
return result, nil
}

// TODO: fixme, remove hardcode
func (m *Method) GetTransactionCount(ctx context.Context, address string, status string) (*big.Int, error) {
// eventually might work this out to see if there's any transactions pending for an address in the mempool
// for now just always return 1
m.GetDebugLogger().Log("Message", "GetTransactionCount is hardcoded to one")
return big.NewInt(0x1), nil
func (m *Method) GetTransactionCount(ctx context.Context, req *GetTransactionCountRequest) (resp *GetTransactionCountResponse, err error) {
if err := m.RequestWithContext(ctx, MethodGetAddressNounce, req, &resp); err != nil {
if m.IsDebugEnabled() {
m.GetDebugLogger().Log("function", "GetTransactionCount", "error", err)
}
return nil, err
}
if m.IsDebugEnabled() {
m.GetDebugLogger().Log("function", "GetTransactionCount", "request", marshalToString(req), "msg", "Successfully got getaddressnounce response")
}
return
}

func (m *Method) GetBlockHash(ctx context.Context, b *big.Int) (resp GetBlockHashResponse, err error) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/kaon/rpc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,32 @@ func (request *GetStorageRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(params)
}

// =======getaddressnounce ============= //
type (
/*
Arguments:
1. "address", (string) The Kaon address
2. "blockNumber" (int | "latest", optional, default=null) The block number to start looking for logs. ()
Result: n (int) The integer of the number of transactions sent from an address
*/
GetTransactionCountRequest struct {
Address string
BlockNumber interface{} `json:"blockNumber"`
}

GetTransactionCountResponse struct {
*big.Int
}
)

func (request *GetTransactionCountRequest) MarshalJSON() ([]byte, error) {
params := []interface{}{request.Address}
if request.BlockNumber != nil {
params = append(params, request.BlockNumber)
}
return json.Marshal(params)
}

// ======== getaddressbalance ========= //
type (

Expand Down
31 changes: 26 additions & 5 deletions pkg/transformer/eth_getTransactionCount.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package transformer

import (
"math/big"
"context"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/kaonone/eth-rpc-gate/pkg/utils"
"github.com/labstack/echo"
)

Expand All @@ -19,15 +20,35 @@ func (p *ProxyETHTxCount) Method() string {
}

func (p *ProxyETHTxCount) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, *eth.JSONRPCError) {
kaonresp, err := p.Kaon.GetTransactionCount(c.Request().Context(), "", "")
var req eth.GetTransactionCountRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}

kaonAddress := utils.RemoveHexPrefix(req.Address)

return p.request(
c.Request().Context(),
&kaon.GetTransactionCountRequest{
Address: kaonAddress,
BlockNumber: req.Tag,
},
)
}

func (p *ProxyETHTxCount) request(ctx context.Context, ethreq *kaon.GetTransactionCountRequest) (*eth.GetTransactionCountResponse, *eth.JSONRPCError) {
kaonresp, err := p.Kaon.GetTransactionCount(ctx, ethreq)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
}

// kaon res -> eth res
return p.response(kaonresp), nil
return p.ToResponse(kaonresp), nil
}

func (p *ProxyETHTxCount) response(kaonresp *big.Int) string {
return hexutil.EncodeBig(kaonresp)
func (p *ProxyETHTxCount) ToResponse(kaonresp *kaon.GetTransactionCountResponse) *eth.GetTransactionCountResponse {
hexVal := hexutil.EncodeBig(kaonresp.Int)
ethresp := eth.GetTransactionCountResponse(hexVal)
return &ethresp
}

0 comments on commit 37714f2

Please sign in to comment.