# The txpool_ methods (Txpool)

The `txpool_` namespace: what waits in the node's transaction pool, as counts, as transactions
in full or as one line of text each.

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Anvil, Eth, Txpool } from "@ethect/core/rpc";

const program = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  // keep the transaction in the pool: no block per transaction
  yield* Anvil.setAutomine(false);
  yield* Eth.sendTransaction({ from, to, value: 1n });
  const waiting = yield* Txpool.status();
  yield* Anvil.mine();
  const after = yield* Txpool.status();
  return { waiting, after };
});

const result = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { waiting: { pending: 1, queued: 0 }, after: { pending: 0, queued: 0 } }
```

The namespace comes from geth, and anvil serves it. It is most useful on a local test node,
where a test decides when blocks are mined: a test gets a fresh node from
[`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/), see [Testing](https://ethect.dev/guides/testing/).

A method takes its JSON-RPC params in order and returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). The name is the
wire name without its prefix: `txpool_status` is `Txpool.status`.

## Methods

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`status`](#status) | `txpool_status` | `TransactionPool.Status` | How many transactions are pending and how many are queued. |
| [`content`](#content) | `txpool_content` | `TransactionPool` | Every transaction in the pool, by sender then by nonce. |
| [`contentFrom`](#contentfrom) | `txpool_contentFrom` | `TransactionPool.From` | The transactions of one sender in the pool, by nonce. |
| [`inspect`](#inspect) | `txpool_inspect` | `TransactionPool.Inspect` | Every transaction in the pool as a line of text. |

The pool has two parts. A `pending` transaction can be mined now. A `queued` one waits for an
earlier nonce of its sender. The four result types are on the
[`TransactionPool`](https://ethect.dev/rpc/types/transaction-pool/) page.

## status

Returns how many transactions are pending and how many are queued.

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

const waiting = Txpool.status().pipe(
  Effect.map(({ pending, queued }) => pending + queued),
);
```

- **JSON-RPC:** `txpool_status`
- **Parameters:** none.
- **Returns:** `TransactionPool.Status`: `pending` and `queued`, each a `number`.

## content

Returns every transaction in the pool, by sender then by nonce.

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

const queuedHash = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setAutomine(false);
  // nonce 5 with nothing before it: the transaction waits in the queue
  const hash = yield* Eth.sendTransaction({ from, to, value: 1n, nonce: 5 });
  const pool = yield* Txpool.content();
  return pool.queued[from!]?.["5"]?.hash === hash; // true
});
```

- **JSON-RPC:** `txpool_content`
- **Parameters:** none.
- **Returns:** [`TransactionPool`](https://ethect.dev/rpc/types/transaction-pool/): `pending` and `queued`,
  each a record of senders to a record of nonces to a
  [`Transaction`](https://ethect.dev/rpc/types/transaction/).
- A sender key is a lowercase address, as [`Eth.accounts`](https://ethect.dev/rpc/eth/accounts/) returns
  it. A nonce key is the nonce in decimal, as a string.

## contentFrom

Returns the transactions of one sender in the pool, by nonce.

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

const nonces = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setAutomine(false);
  yield* Eth.sendTransaction({ from, to, value: 1n });
  const mine = yield* Txpool.contentFrom(from!); // anvil always has a first account
  return Object.keys(mine.pending); // ["0"]
});
```

- **JSON-RPC:** `txpool_contentFrom`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), the sender.
- **Returns:** `TransactionPool.From`: `pending` and `queued`, each a record of nonces to a
  [`Transaction`](https://ethect.dev/rpc/types/transaction/). Both are empty for a sender with nothing in
  the pool.

## inspect

Returns every transaction in the pool as one line of text, by sender then by nonce.

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

const line = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.setAutomine(false);
  yield* Eth.sendTransaction({ from, to, value: 1n });
  const pool = yield* Txpool.inspect();
  return pool.pending[from!]?.["0"];
  // "0x70997970c51812dc3a010c7d01b50e0d17dc79c8: 1 wei + 21000 gas × 2000000000 wei"
});
```

- **JSON-RPC:** `txpool_inspect`
- **Parameters:** none.
- **Returns:** `TransactionPool.Inspect`: the shape of [`content`](#content), with a `string`
  in place of each transaction. The line reads `<to>: <value> wei + <gas> gas × <gas price> wei`.

## Behavior

- **A pending transaction has no block.** Its `blockHash`, `blockNumber` and
  `transactionIndex` are `null`.
- **Empty unless the node waits.** A node that mines a block per transaction empties its pool
  at once. Turn that off with
  [`Anvil.setAutomine(false)`](https://ethect.dev/rpc/anvil/mining/#setautomine) to see a transaction
  wait.
- **Mining takes the pending part.** After a block is mined the queued transactions are still
  there, until the nonces before them arrive.
- **Removing transactions** is done with the [`Anvil` pool methods](https://ethect.dev/rpc/anvil/pool/).
