Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get transaction count method to eth client #314

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading