diff --git a/eth/bind.go b/eth/bind.go index 769a6c741c59..6e044716d6b3 100644 --- a/eth/bind.go +++ b/eth/bind.go @@ -104,7 +104,7 @@ func toBlockNumber(num *big.Int) rpc.BlockNumber { // PendingAccountNonce implements bind.ContractTransactor retrieving the current // pending nonce associated with an account. func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (nonce uint64, err error) { - out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber) + out, err := b.txapi.NextNonce(ctx, account) if out != nil { nonce = uint64(*out) } diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 87a912901af5..75db592b2c0d 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -400,7 +400,7 @@ func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([] // This is the nonce that should be used for the next transaction. func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { var result hexutil.Uint64 - err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending") + err := ec.c.CallContext(ctx, &result, "eth_nextNonce", account) return uint64(result), err } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 55bd5aa1baa6..575528ec2503 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -971,6 +971,17 @@ func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, addr return (*hexutil.Uint64)(&nonce), state.Error() } +// NextNonce returns the next nonce for the given address taking into account any pending transactions the address has +func (s *PublicTransactionPoolAPI) NextNonce(ctx context.Context, address common.Address) (*hexutil.Uint64, error) { + // Ask transaction pool for the nonce which includes pending transactions + nonce, err := s.b.GetPoolNonce(ctx, address) + if err != nil { + return nil, err + } + + return (*hexutil.Uint64)(&nonce), nil +} + // GetTransactionByHash returns the transaction for the given hash func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, hash common.Hash) *RPCTransaction { // Try to return an already finalized transaction