Eth.getTransactionCount
Returns the number of transactions an account has sent, which is the nonce its next transaction must carry.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Eth.getTransactionCount(ADDRESS, "latest");
const nonce = await program.pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// 3The call works over every transport. The examples below leave the transport out.
Count the transactions still pending
Section titled “Count the transactions still pending”With "pending" the count includes the transactions of the account that wait in the node’s
pool. Use it to pick a nonce while earlier transactions are not mined.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const nonces = Effect.all({ // 3 mined: Eth.getTransactionCount(ADDRESS, "latest"), // 4, with one transaction pending next: Eth.getTransactionCount(ADDRESS, "pending"),});At a past block
Section titled “At a past block”Pass a block number to read the count the account had at that block.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const atGenesis = Eth.getTransactionCount(ADDRESS, 0n); // 0Returns
Section titled “Returns”Effect<number, Method.Error, Transport>
The count, as a number. An address that was never used has a count of 0.
Parameters
Section titled “Parameters”address
Section titled “address”- Type:
Address.Address
The account. A plain string is not accepted: build it with
Address.makeUnsafe, or with Address.make when it
comes from outside.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const nonce = Eth.getTransactionCount(ADDRESS, "latest");block (optional)
Section titled “block (optional)”- Type:
BlockId.BlockId
The block to read at: a number, a tag ("latest", "earliest", "pending", "safe",
"finalized"), or an object that names the block by hash.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const counts = Effect.all([ Eth.getTransactionCount(ADDRESS, "pending"), Eth.getTransactionCount(ADDRESS, 19_000_000n), Eth.getTransactionCount(ADDRESS, { blockHash: `0x${"ab".repeat(32)}` }),]);Behavior
Section titled “Behavior”- No default block. Left out, the param is not sent and the node chooses the block: see Naming a block.
- A
number, not abigint. The node answers"0x3"and you get3. See values on the wire. - A contract has a nonce too. It counts the contracts that contract created, and starts at
1.
Errors
Section titled “Errors”The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:
| Error | Reason | When |
|---|---|---|
RpcError |
InvalidParams |
the block number is above the chain’s head |
RpcError |
ResourceNotFound |
no block has the given blockHash |
CodecError |
on: "params" |
the block number is negative: nothing is sent |
JSON-RPC method
Section titled “JSON-RPC method”eth_getTransactionCount