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 edccde9 commit e2f1f89
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 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)
GetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
SubscribeToHead(ctx context.Context) (domain.HeaderCh, error)
Expand All @@ -76,6 +77,7 @@ const getLogs = "eth_getLogs"
const transactionReceipt = "eth_getTransactionReceipt"
const traceBlock = "trace_block"
const chainId = "eth_chainId"
const getTransactionCount = "eth_getTransactionCount"

const defaultRetryInterval = time.Second * 15

Expand Down Expand Up @@ -343,6 +345,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 e2f1f89

Please sign in to comment.