# More lookups

The other ways to find a transaction: by its position in a block, by its sender and nonce, in
its signed and encoded form, and all those in the pending pool.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getTransactionByBlockNumberAndIndex`](#gettransactionbyblocknumberandindex) | `eth_getTransactionByBlockNumberAndIndex` | `Transaction \| null` | A transaction by its position in a block, by number or tag. |
| [`getTransactionByBlockHashAndIndex`](#gettransactionbyblockhashandindex) | `eth_getTransactionByBlockHashAndIndex` | `Transaction \| null` | A transaction by its position in a block, by block hash. |
| [`getTransactionBySenderAndNonce`](#gettransactionbysenderandnonce) | `eth_getTransactionBySenderAndNonce` | `Transaction \| null` | The transaction an account sent with a given nonce. |
| [`getRawTransactionByHash`](#getrawtransactionbyhash) | `eth_getRawTransactionByHash` | `Hex \| null` | A transaction in its signed, encoded form, by hash. |
| [`getRawTransactionByBlockNumberAndIndex`](#getrawtransactionbyblocknumberandindex) | `eth_getRawTransactionByBlockNumberAndIndex` | `Hex \| null` | The same, by position in a block, by number or tag. |
| [`getRawTransactionByBlockHashAndIndex`](#getrawtransactionbyblockhashandindex) | `eth_getRawTransactionByBlockHashAndIndex` | `Hex \| null` | The same, by position in a block, by block hash. |
| [`pendingTransactions`](#pendingtransactions) | `eth_pendingTransactions` | `ReadonlyArray<Transaction>` | The transactions in the node's pending pool. |

Each returns an [`Effect`](https://ethect.dev/rpc/method/#calling-a-method) of the type in the table, which
fails with a `Method.Error` and needs a `Transport`. Every lookup of one transaction returns
`null` when the node finds nothing. To look a transaction up by its hash, use
[`Eth.getTransactionByHash`](https://ethect.dev/rpc/eth/get-transaction-by-hash/).

## getTransactionByBlockNumberAndIndex

Returns the transaction at a position in a block, by the number or tag of the block.

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

const first = Eth.getTransactionByBlockNumberAndIndex("latest", 0).pipe(
  Effect.map((transaction) => transaction?.hash),
);
```

- **JSON-RPC:** `eth_getTransactionByBlockNumberAndIndex`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)), then `index`
  (`number`), counted from `0`.
- **Returns:** [`Transaction.Transaction`](https://ethect.dev/rpc/types/transaction/), or `null` when the
  node does not have the block or the block has no transaction at that position.

## getTransactionByBlockHashAndIndex

Returns the transaction at a position in a block, by the hash of the block.

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

const first = Eth.getTransactionByBlockHashAndIndex(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
  0,
);
```

- **JSON-RPC:** `eth_getTransactionByBlockHashAndIndex`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)), then `index` (`number`),
  counted from `0`.
- **Returns:** [`Transaction.Transaction`](https://ethect.dev/rpc/types/transaction/), or `null` when no
  block has the hash or the block has no transaction at that position.

## getTransactionBySenderAndNonce

Returns the transaction an account sent with a given nonce.

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

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

const firstSent = Effect.gen(function* () {
  const transaction = yield* Eth.getTransactionBySenderAndNonce(ADDRESS, 0);
  return transaction?.hash;
});
// "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3"
```

- **JSON-RPC:** `eth_getTransactionBySenderAndNonce`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then `nonce`
  (`number`).
- **Returns:** [`Transaction.Transaction`](https://ethect.dev/rpc/types/transaction/), or `null` when the
  account sent nothing with that nonce. anvil finds a transaction that is still pending too.
- Not served by every client.

## getRawTransactionByHash

Returns a transaction in its signed, encoded form: the bytes that
[`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/) takes.

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

const raw = Eth.getRawTransactionByHash(
  "0xa23b52004fe574000765fbe20a311b2785a318c9dbe6b0bf6a4c1f8936f57ff3",
);
// "0x02f8…": the first byte, 0x02, is the type of an EIP-1559 transaction
```

- **JSON-RPC:** `eth_getRawTransactionByHash`
- **Parameters:** `hash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)).
- **Returns:** [`Hex`](https://ethect.dev/reference/primitives/hex/), or `null` when the node does not know the
  hash. A pending transaction has a raw form too.

## getRawTransactionByBlockNumberAndIndex

Returns the signed, encoded form of the transaction at a position in a block, by the number or
tag of the block.

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

const raw = Eth.getRawTransactionByBlockNumberAndIndex("latest", 0);
```

- **JSON-RPC:** `eth_getRawTransactionByBlockNumberAndIndex`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)), then `index`
  (`number`), counted from `0`.
- **Returns:** [`Hex`](https://ethect.dev/reference/primitives/hex/), or `null` when the block has no transaction
  at that position.

## getRawTransactionByBlockHashAndIndex

Returns the signed, encoded form of the transaction at a position in a block, by the hash of
the block.

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

const raw = Eth.getRawTransactionByBlockHashAndIndex(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
  0,
);
```

- **JSON-RPC:** `eth_getRawTransactionByBlockHashAndIndex`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)), then `index` (`number`),
  counted from `0`.
- **Returns:** [`Hex`](https://ethect.dev/reference/primitives/hex/), or `null` when no block has the hash or the
  block has no transaction at that position.

## pendingTransactions

Returns the transactions waiting in the node's pending pool.

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

const waiting = Eth.pendingTransactions().pipe(
  Effect.map((transactions) =>
    transactions.map(({ from, nonce, hash }) => ({ from, nonce, hash })),
  ),
);
```

- **JSON-RPC:** `eth_pendingTransactions`
- **Parameters:** none.
- **Returns:** `ReadonlyArray<Transaction.Transaction>`, empty when the pool is. Each one has
  `blockHash`, `blockNumber` and `transactionIndex` set to `null`.
- Not served by every client. To be told about each new one, use
  [`Eth.subscribe.newPendingTransactions`](https://ethect.dev/rpc/eth/subscribe/new-pending-transactions/).

## Behavior

- **The same transaction, whatever the lookup.** The three lookups that return a `Transaction`
  give the object [`Eth.getTransactionByHash`](https://ethect.dev/rpc/eth/get-transaction-by-hash/) gives.
- **Positions and nonces are `number`.** An `index` and a `nonce` are not `bigint`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).
- **The raw form is not decoded.** The hex is returned as the node sent it, and nothing public
  in ethect parses a signed transaction.
- **A development node mines at once.** On anvil the pool is empty unless automine is off:
  see [`Anvil.setAutomine`](https://ethect.dev/rpc/anvil/mining/#setautomine).
