# TransactionRequest

A transaction as a caller describes it, for the methods that run, estimate, fill, sign or send
one. Every field is optional: the node or wallet fills in what is missing.

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

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Effect.gen(function* () {
  const request: TransactionRequest.TransactionRequest = { from: FROM, to: TO, value: 1n };
  const gas = yield* Eth.estimateGas(request);
  return yield* Eth.sendTransaction({ ...request, gas });
});
```

A request is a plain object. The same one goes to several methods.

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `TransactionRequest.schema` | `TransactionRequest.TransactionRequest` | a transaction request | the methods below |

| Method | What it does with the request |
| --- | --- |
| [`Eth.call`](https://ethect.dev/rpc/eth/call/) | runs it without sending it |
| [`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/) | returns the gas it needs |
| [`Eth.createAccessList`](https://ethect.dev/rpc/eth/create-access-list/) | returns the access list it would use |
| [`Eth.simulateV1`](https://ethect.dev/rpc/eth/simulate-v1/) | runs it as one of the `calls` of a simulated block |
| [`Eth.fillTransaction`](https://ethect.dev/rpc/eth/fill-transaction/) | fills in what it leaves out |
| [`Eth.signTransaction`](https://ethect.dev/rpc/eth/sign-transaction/) | signs it |
| [`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/), [`Eth.sendTransactionSync`](https://ethect.dev/rpc/eth/send-transaction-sync/) | signs it and sends it |
| [`Wallet.walletSendTransaction`](https://ethect.dev/rpc/wallet/wallet-send-transaction/) | the same, under the name some wallets use |
| [`Debug.traceCall`](https://ethect.dev/rpc/debug/trace-call/) | runs it and traces it |
| [`Anvil.sendUnsignedTransaction`](https://ethect.dev/rpc/anvil/accounts/#sendunsignedtransaction) | sends it from any account of a test node, without a signature |
| [`Anvil.reorg`](https://ethect.dev/rpc/anvil/reorg/) | puts it in one of the blocks that replace the latest ones |

## Fields

Every field is optional.

| Field | Type | Notes |
| --- | --- | --- |
| `type` | `Transaction.Type \| Hex` | `"legacy"`, `"eip2930"`, `"eip1559"`, `"eip4844"`, `"eip7702"`, or the raw type byte of another type |
| `from` | `Address` | the sender |
| `to` | `Address \| null` | the recipient or the contract, left out or `null` to create a contract |
| `nonce` | `number` | |
| `gas` | `bigint` | the gas limit |
| `value` | `bigint` | in wei |
| `data` | `Hex` | the calldata, or the creation code of a contract |
| `input` | `Hex` | the same, under the name a node returns it by: see [below](#a-transaction-read-from-a-node) |
| `gasPrice` | `bigint` | in wei, for a legacy or EIP-2930 transaction |
| `maxFeePerGas` | `bigint` | in wei, since EIP-1559 |
| `maxPriorityFeePerGas` | `bigint` | in wei, since EIP-1559 |
| `maxFeePerBlobGas` | `bigint` | in wei, for an EIP-4844 transaction |
| `chainId` | `number` | |
| `accessList` | [`AccessList`](https://ethect.dev/rpc/types/access-list/) | since EIP-2930 |
| `blobVersionedHashes` | `ReadonlyArray<Hash>` | for an EIP-4844 transaction |
| `blobs` | `ReadonlyArray<Hex>` | for an EIP-4844 transaction |
| `commitments` | `ReadonlyArray<Hex>` | for an EIP-4844 transaction |
| `proofs` | `ReadonlyArray<Hex>` | for an EIP-4844 transaction |
| `authorizationList` | `ReadonlyArray<`[`Authorization`](https://ethect.dev/rpc/types/authorization/)`>` | for an EIP-7702 transaction |

## On the wire

The schema encodes the request: quantities become hex, the type becomes its byte, and
addresses go out in their EIP-55 form.

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

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const result = Eth.call({
  type: "eip1559",
  from: FROM,
  to: TO,
  value: 10n ** 18n,
  nonce: 7,
  maxFeePerGas: 30n * 10n ** 9n,
});
```

```json
{
  "type": "0x2",
  "from": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
  "to": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
  "nonce": "0x7",
  "value": "0xde0b6b3a7640000",
  "maxFeePerGas": "0x6fc23ac00"
}
```

## Behavior

- **Write the calldata as `data`.** Nodes read `data` or `input`, and wallets only know
  `data`. Each is sent under the name you used.
- **`input` is for a transaction you read.** A [`Transaction`](https://ethect.dev/rpc/types/transaction/)
  from a node has its calldata as `input`, and it is sent when you pass one as a request.
- **Do not set both to different values.** anvil 1.9 refuses a request whose `data` and
  `input` differ, with an `RpcError` of reason `InvalidParams`.
- **Other unknown keys are left out.** A variable with more keys than a request has compiles,
  and only the fields above are sent: the `hash` and `blockNumber` of a transaction are not.
- **Leave out what you do not set.** A field you do not give is not sent, and the node or
  wallet chooses: the nonce of `from`, an estimate of the gas, the fees.
- **No `to` creates a contract.** `data` is then the creation code.
- **Nothing is built for you here.** A request goes out as you wrote it. `data` is calldata
  you bring: [`Call.encode`](https://ethect.dev/reference/call/encode/) builds it from a call. `blobs`,
  `commitments`, `proofs` and the signatures of `authorizationList` are yours too, and nothing
  in the library builds them. To have the fields filled, signed and sent, use
  [`Tx.send`](https://ethect.dev/reference/tx/send/).
- **Numbers.** Wei and gas are `bigint`. `nonce` and `chainId` are `number`. See
  [values on the wire](https://ethect.dev/rpc/guides/values/).

### A transaction read from a node

A transaction is accepted where a request is: its fields are a request's, with the calldata as
`input`.

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

/** Runs a mined transaction again, on the latest state. */
const replay = (transaction: Transaction.Eip1559) => {
  // The node filled in `gasPrice`: anvil refuses it next to a fee cap.
  const { gasPrice, ...request } = transaction;
  return Eth.call(request, "latest");
};
```

A legacy transaction goes back as it is. A mined EIP-1559 one carries the `gasPrice` the node
added, and anvil 1.9 refuses a request with both a `gasPrice` and a `maxFeePerGas`.
