Skip to content

Commit

Permalink
Add get transaction count method to eth client
Browse files Browse the repository at this point in the history
  • Loading branch information
dkeysil committed Aug 26, 2024
1 parent 04a9823 commit 2046b14
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
44 changes: 36 additions & 8 deletions ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Client interface {
BlockNumber(ctx context.Context) (*big.Int, error)
TransactionReceipt(ctx context.Context, txHash string) (*domain.TransactionReceipt, error)
ChainID(ctx context.Context) (*big.Int, error)
GetTransactionCount(ctx context.Context, address string, blockNumber *big.Int) (*big.Int, error)
TraceBlock(ctx context.Context, number *big.Int) ([]domain.Trace, error)
DebugTraceCall(
ctx context.Context, req *domain.TraceCallTransaction,
Expand All @@ -75,14 +76,15 @@ type Client interface {
}

const (
blocksByNumber = "eth_getBlockByNumber"
blocksByHash = "eth_getBlockByHash"
blockNumber = "eth_blockNumber"
getLogs = "eth_getLogs"
transactionReceipt = "eth_getTransactionReceipt"
traceBlock = "trace_block"
debugTraceCall = "debug_traceCall"
chainId = "eth_chainId"
blocksByNumber = "eth_getBlockByNumber"
blocksByHash = "eth_getBlockByHash"
blockNumber = "eth_blockNumber"
getLogs = "eth_getLogs"
transactionReceipt = "eth_getTransactionReceipt"
traceBlock = "trace_block"
debugTraceCall = "debug_traceCall"
chainId = "eth_chainId"
getTransactionCount = "eth_getTransactionCount"
)

const defaultRetryInterval = time.Second * 15
Expand Down Expand Up @@ -384,6 +386,32 @@ func (e *streamEthClient) TransactionReceipt(ctx context.Context, txHash string)
return &result, err
}

// GetTransactionCount returns the transaction count for an address
func (e *streamEthClient) GetTransactionCount(ctx context.Context, address string, blockNumber *big.Int) (*big.Int, error) {
name := fmt.Sprintf("%s(%s, %s)", getTransactionCount, address, blockNumber)
log.Debugf(name)
var result string
err := withBackoff(ctx, name, func(ctx context.Context) error {
err := e.rpcClient.CallContext(ctx, &result, getTransactionCount, address, blockNumber)
if err != nil {
return err
}
if result == "" {
return ErrNotFound
}
return nil
}, RetryOptions{
MinBackoff: pointDur(e.retryInterval),
MaxElapsedTime: pointDur(12 * time.Hour),
MaxBackoff: pointDur(e.retryInterval),
}, nil, nil)
if err != nil {
return nil, err
}

return utils.HexToBigInt(result)
}

// SubscribeToHead subscribes to the blockchain head and returns a channel which provides
// the latest block headers. The channel is closed when subscription encounters an error
// or becomes inactive (e.g. due to a hanging connection).
Expand Down
15 changes: 15 additions & 0 deletions ethereum/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2046b14

Please sign in to comment.