# Eth.getTransactionCount

Returns the number of transactions an account has sent, which is the nonce its next
transaction must carry.

## Usage

```ts
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,
);
// 3
```

The call works over every [transport](https://ethect.dev/guides/transports/). The examples below leave the
transport out.

### 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.

```ts
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

Pass a block number to read the count the account had at that block.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const atGenesis = Eth.getTransactionCount(ADDRESS, 0n); // 0
```

## Returns

[`Effect<number, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The count, as a `number`. An address that was never used has a count of `0`.

## Parameters

### address

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The account. A plain string is not accepted: build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when it
comes from outside.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const nonce = Eth.getTransactionCount(ADDRESS, "latest");
```

### block (optional)

- **Type:** [`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)

The block to read at: a number, a tag (`"latest"`, `"earliest"`, `"pending"`, `"safe"`,
`"finalized"`), or an object that names the block by hash.

```ts
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

- **No default block.** Left out, the param is not sent and the node chooses the block:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **A `number`, not a `bigint`.** The node answers `"0x3"` and you get `3`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).
- **A contract has a nonce too.** It counts the contracts that contract created, and starts at
  `1`.

## Errors

The three [errors](https://ethect.dev/reference/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

`eth_getTransactionCount`
