# Transaction

A transaction as a node returns it: signed, with its place in the chain. It is a union of six
shapes, one per transaction type, told apart by `type`.

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

const feeCap = (transaction: Transaction.Transaction): bigint | undefined =>
  transaction.type === "eip1559"
    ? transaction.maxFeePerGas // transaction: Transaction.Eip1559
    : undefined;
```

Comparing `type` with a name narrows the union: `maxFeePerGas` does not exist on a legacy
transaction, and TypeScript only lets you read it once the type is known.

## Schemas

| Schema | Type | Describes | Returned by |
| --- | --- | --- | --- |
| `Transaction.schema` | `Transaction.Transaction` | any transaction: the union of the six below | [`Eth.getTransactionByHash`](https://ethect.dev/rpc/eth/get-transaction-by-hash/), the [other lookups](https://ethect.dev/rpc/eth/transaction-lookups/), [`Eth.getBlockByNumber(id, true)`](https://ethect.dev/rpc/eth/get-block-by-number/) |
| `Transaction.legacySchema` | `Transaction.Legacy` | a transaction from before typed transactions | as a member of the union |
| `Transaction.eip2930Schema` | `Transaction.Eip2930` | a gas price and an access list | as a member of the union |
| `Transaction.eip1559Schema` | `Transaction.Eip1559` | a fee cap and a priority fee | as a member of the union |
| `Transaction.eip4844Schema` | `Transaction.Eip4844` | a transaction that carries blobs | as a member of the union |
| `Transaction.eip7702Schema` | `Transaction.Eip7702` | a transaction that sets code on the accounts that authorized it | as a member of the union |
| `Transaction.unknownSchema` | `Transaction.Unknown` | a transaction of a type ethect has no name for | as a member of the union |
| `Transaction.unsignedSchema` | `Transaction.Unsigned` | any of the five named types before it is signed | [`Eth.fillTransaction`](https://ethect.dev/rpc/eth/fill-transaction/), as `tx` |
| `Transaction.typeSchema` | `Transaction.Type` | the five type names | the two schemas below |
| `Transaction.unknownTypeSchema` | a `Hex` | the raw type byte of a type that has no name | `Transaction.unknownSchema` |
| `Transaction.anyTypeSchema` | `Transaction.Type` or a `Hex` | a type name, or a raw type byte | `type` of a [`Receipt`](https://ethect.dev/rpc/types/receipt/) and of a [`TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/) |

## Types

`Transaction.Type` names a type by the EIP that introduced it. The node sends the byte, and
you read and write the name.

| `type` | On the wire | Shape |
| --- | --- | --- |
| `"legacy"` | `"0x0"` | `Transaction.Legacy` |
| `"eip2930"` | `"0x1"` | `Transaction.Eip2930` |
| `"eip1559"` | `"0x2"` | `Transaction.Eip1559` |
| `"eip4844"` | `"0x3"` | `Transaction.Eip4844` |
| `"eip7702"` | `"0x4"` | `Transaction.Eip7702` |
| any other byte, such as `"0x7e"` | the same byte | `Transaction.Unknown` |

A byte is matched by value, so `"0x02"` is `"eip1559"` too. A transaction a node sends without
a `type` is read as `"legacy"`.

A `switch` on `type` reaches every shape. What is left in `default` is `Transaction.Unknown`.

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

const price = (transaction: Transaction.Transaction): string => {
  switch (transaction.type) {
    case "legacy":
    case "eip2930":
      return `gas price ${transaction.gasPrice}`;
    case "eip1559":
    case "eip4844":
    case "eip7702":
      return `fee cap ${transaction.maxFeePerGas}`;
    default:
      // transaction: Transaction.Unknown, and its type is the raw byte
      return `unknown type ${transaction.type}`;
  }
};
```

## Common fields

Every shape has these.

| Field | Type | Notes |
| --- | --- | --- |
| `type` | see [Types](#types) | |
| `hash` | `Hash` | |
| `from` | `Address` | the sender |
| `to` | `Address \| null` | `null` when the transaction creates a contract |
| `nonce` | `number` | |
| `gas` | `bigint` | the gas limit |
| `value` | `bigint` | in wei |
| `input` | `Hex` | the calldata, or the code that creates a contract |
| `blockHash` | `Hash \| null` | `null` while pending |
| `blockNumber` | `bigint \| null` | `null` while pending |
| `transactionIndex` | `number \| null` | the position in the block, `null` while pending |
| `blockTimestamp` | `bigint` (optional) | not sent by every node |
| `r` | `Hex` | the signature. Optional on `Transaction.Unknown`. |
| `s` | `Hex` | the signature. Optional on `Transaction.Unknown`. |
| `v` | `bigint` (optional) | the signature's recovery value |

## Fields by type

What each shape adds to the common fields.

| Field | Type | `legacy` | `eip2930` | `eip1559` | `eip4844` | `eip7702` |
| --- | --- | --- | --- | --- | --- | --- |
| `chainId` | `number` | optional | yes | yes | yes | yes |
| `gasPrice` | `bigint` | yes | yes | optional | optional | optional |
| `accessList` | [`AccessList`](https://ethect.dev/rpc/types/access-list/) | | yes | yes | yes | yes |
| `yParity` | `number` | | optional | optional | optional | optional |
| `maxFeePerGas` | `bigint` | | | yes | yes | yes |
| `maxPriorityFeePerGas` | `bigint` | | | yes | yes | yes |
| `maxFeePerBlobGas` | `bigint` | | | | yes | |
| `blobVersionedHashes` | `ReadonlyArray<Hash>` | | | | yes | |
| `authorizationList` | `ReadonlyArray<`[`Authorization`](https://ethect.dev/rpc/types/authorization/)`>` | | | | | yes |

A typed transaction a node sends without an `accessList`, as zkSync Era does for one that
has none, decodes with `accessList: []`.

An empty cell means the field does not exist on that shape. In `eip4844` and `eip7702`, `to`
is an `Address` and never `null`: neither type can create a contract.

`Transaction.Unknown` has the common fields only, and its `r` and `s` are optional: a chain's
own transaction type is not always signed.

## Unsigned transactions

`Transaction.Unsigned` is what [`Eth.fillTransaction`](https://ethect.dev/rpc/eth/fill-transaction/)
returns as `tx`: one of the five named types, without what signing and mining add.

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

const filled = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  if (from === undefined || to === undefined) return yield* Effect.fail("no account");
  const { tx } = yield* Eth.fillTransaction({ from, to, value: 1n });
  // tx: Transaction.Unsigned
  return { type: tx.type, nonce: tx.nonce, gas: tx.gas };
});
// { type: "eip1559", nonce: 0, gas: 21000n }
```

It has no `hash`, `from`, `blockHash`, `blockNumber`, `blockTimestamp`, `transactionIndex`,
`r`, `s`, `v` or `yParity`. The other fields are the ones of its type.

## Behavior

- **`null` means pending.** `blockHash`, `blockNumber` and `transactionIndex` are `null` until
  the transaction is mined. They are always present, so they are typed `| null`, not optional.
- **Optional means absent.** A field marked optional is left out of the object when the node
  omits it or sends `null`. Test it with `transaction.chainId !== undefined`.
- **A legacy transaction can have no `chainId`.** One signed without replay protection names no
  chain.
- **No `type` means `legacy`.** A node that sends a transaction without the field gets the
  legacy shape, with `type: "legacy"` in the result.
- **`gasPrice` on a fee-market transaction is filled in by the node.** It is the price paid per
  gas once the transaction is mined.
- **Unknown fields are dropped.** A chain that adds fields to its transactions still decodes.
  The extra fields are not in the result: [extend the schema](#a-type-ethect-does-not-know) to
  read them.
- **Numbers.** Wei and gas are `bigint`. A nonce, a chain id and a position are `number`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).

### A type ethect does not know

A transaction whose type byte has no name, such as the deposit transaction of a rollup,
decodes to `Transaction.Unknown`: the common fields, and `type` as the byte the node sent.

```ts
import { Effect, Schema } from "effect";
import { Transaction } from "@ethect/core/rpc";

// What a node could send for a transaction of type 0x7e.
const wire = {
  type: "0x7e",
  hash: `0x${"ab".repeat(32)}`,
  from: "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
  to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
  nonce: "0x0",
  gas: "0xf4240",
  value: "0x0",
  input: "0x",
  blockHash: `0x${"cd".repeat(32)}`,
  blockNumber: "0x1",
  transactionIndex: "0x0",
  r: "0x0",
  s: "0x0",
  sourceHash: `0x${"ef".repeat(32)}`,
};

const decoded = Schema.decodeUnknownEffect(Transaction.schema)(wire).pipe(
  Effect.map((transaction) => transaction.type),
);
// "0x7e": the common fields are decoded, and `sourceHash` is dropped
```

To read the fields that chain adds, give the type a schema of its own and put it first in a
union with `Transaction.schema`.

```ts
import { Schema } from "effect";
import { Hash } from "@ethect/core";
import { Method, Transaction } from "@ethect/core/rpc";

// A rollup whose deposit transactions carry the hash of what caused them.
const Deposit = Schema.Struct({
  ...Transaction.unknownSchema.fields,
  type: Schema.Literal("0x7e"),
  sourceHash: Hash.schema,
});

const getTransactionByHash = Method.make<[hash: unknown]>()(
  "eth_getTransactionByHash",
  [Hash.schema],
  Schema.NullOr(Schema.Union([Deposit, Transaction.schema])),
);
```

`getTransactionByHash(hash)` now returns the six standard shapes plus one whose `type` is
`"0x7e"` and that has a `sourceHash`. See [Custom methods](https://ethect.dev/rpc/guides/custom-methods/).
