# TransactionPool

The node's transaction pool, in four shapes: its counts, its transactions in full by sender and
nonce, those of one sender, and one line of text per transaction.

```ts
import { Effect } from "effect";
import { Txpool } from "@ethect/core/rpc";

const hashes = Effect.gen(function* () {
  const pool = yield* Txpool.content();
  // pool: TransactionPool.TransactionPool
  return Object.values(pool.pending).flatMap((byNonce) =>
    Object.values(byNonce).map((transaction) => transaction.hash),
  );
});
```

Every shape has the same two parts. A `pending` transaction can be mined now. A `queued` one
waits for an earlier nonce of its sender.

## Schemas

| Schema | Type | Describes | Returned by |
| --- | --- | --- | --- |
| `TransactionPool.statusSchema` | `TransactionPool.Status` | how many transactions each part holds | [`Txpool.status`](https://ethect.dev/rpc/txpool/#status) |
| `TransactionPool.schema` | `TransactionPool.TransactionPool` | every transaction, by sender then by nonce | [`Txpool.content`](https://ethect.dev/rpc/txpool/#content) |
| `TransactionPool.fromSchema` | `TransactionPool.From` | the transactions of one sender, by nonce | [`Txpool.contentFrom`](https://ethect.dev/rpc/txpool/#contentfrom) |
| `TransactionPool.inspectSchema` | `TransactionPool.Inspect` | every transaction as a line of text, by sender then by nonce | [`Txpool.inspect`](https://ethect.dev/rpc/txpool/#inspect) |

Import a type to name it in your own signatures.

```ts
import type { TransactionPool } from "@ethect/core/rpc";

const isEmpty = (status: TransactionPool.Status): boolean =>
  status.pending === 0 && status.queued === 0;
```

## Fields

Each type is an object with the keys `pending` and `queued`. What the two hold depends on the
type.

| Type | `pending` and `queued` are each |
| --- | --- |
| `Status` | `number`: a count |
| `TransactionPool` | `Record<Address, Record<string, Transaction>>`: sender, then nonce, then the [`Transaction`](https://ethect.dev/rpc/types/transaction/) |
| `From` | `Record<string, Transaction>`: nonce, then the [`Transaction`](https://ethect.dev/rpc/types/transaction/) |
| `Inspect` | `Record<Address, Record<string, string>>`: sender, then nonce, then a line of text |

## On the wire

What anvil 1.9 answers to `txpool_inspect` with two transfers of one sender waiting, the second
with the nonce `5` and nothing before it:

```json
{
  "pending": {
    "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": {
      "0": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8: 1 wei + 21000 gas × 2000000000 wei"
    }
  },
  "queued": {
    "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": {
      "5": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8: 1 wei + 21000 gas × 2000000000 wei"
    }
  }
}
```

`txpool_status` answers `{"pending":"0x1","queued":"0x1"}` for the same pool.

## Behavior

- **Nonce keys are decimal strings.** The nonce `5` is the key `"5"`, not `"0x5"` and not a
  number. Read it with `byNonce["5"]`, or go through `Object.entries`.
- **Sender keys are lowercase addresses.** anvil sends them in checksum case, as above, and
  they are decoded to lowercase like every address. An
  [`Address.Address`](https://ethect.dev/reference/primitives/address/) indexes the record directly.
- **A sender with nothing is not a key.** An empty part is an empty object, and for
  [`Txpool.contentFrom`](https://ethect.dev/rpc/txpool/#contentfrom) both parts are.
- **Transactions are pending ones.** Their `blockHash`, `blockNumber` and `transactionIndex`
  are `null`.
- **The text of `Inspect`** reads `<to>: <value> wei + <gas> gas × <gas price> wei`. It is for
  a person to read: take figures from `TransactionPool`, not from the text.
- **Counts are decoded.** The node sends the counts of `Status` as hex quantities and they are
  decoded to `number`.
