# Eth.fillTransaction

Fills in what a transaction request leaves out, such as the nonce, the gas and the fees, and
returns the complete transaction without signing or sending it.

## Usage

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

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

const program = Eth.fillTransaction({ from: FROM, to: TO, value: 1n });

const { raw, tx } = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// tx: {
//   to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
//   nonce: 0,
//   gas: 21000n,
//   value: 1n,
//   input: "0x",
//   chainId: 31337,
//   accessList: [],
//   maxFeePerGas: 2000000000n,
//   maxPriorityFeePerGas: 1000000000n,
//   gasPrice: 2000000000n,
//   type: "eip1559",
// }
```

The request had three fields. The node added the nonce of `from`, an estimate of the gas, the
fees, the chain id and the type.

The examples below leave the transport out.

### What you set is kept

The node fills in the fields a request leaves out. It does not touch the others.

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

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

const filled = Effect.gen(function* () {
  const request = { from: FROM, to: TO, value: 1n, nonce: 99, gas: 50_000n };
  const { tx } = yield* Eth.fillTransaction(request);
  return [tx.nonce, tx.gas]; // [99, 50000n]
});
```

### A legacy transaction

A request with a `gasPrice` is filled as a legacy transaction.

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

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

const filled = Effect.gen(function* () {
  const request = { from: FROM, to: TO, value: 1n, gasPrice: 3n * 10n ** 9n };
  const { tx } = yield* Eth.fillTransaction(request);
  return tx.type; // "legacy"
});
```

### Pinning the nonce and the gas before signing

Fill once, then sign the request with the values the node chose.

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

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

const signed = Effect.gen(function* () {
  const request = { from: FROM, to: TO, value: 1n };
  const { tx } = yield* Eth.fillTransaction(request);
  return yield* Eth.signTransaction({
    ...request,
    nonce: tx.nonce,
    gas: tx.gas,
  });
});
```

## Returns

[`Effect<{ raw: Hex, tx: Transaction.Unsigned }, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

| Field | Type | Description |
| --- | --- | --- |
| `raw` | `Hex` | the filled transaction, encoded |
| `tx` | `Transaction.Unsigned` | the filled transaction, as an object |

`tx` is a [`Transaction`](https://ethect.dev/rpc/types/transaction/) without what signing and mining add:
no `hash`, no `from`, no signature, no place in a block.

## Parameters

### request

- **Type:** [`TransactionRequest.TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)

The transaction to fill. The fields are those of
[`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/#request).

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

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

const filled = Eth.fillTransaction({ from: FROM, to: TO, value: 1n });
```

## Behavior

- **Nothing is sent.** Filling twice gives the same nonce twice: the nonce is read, not
  reserved.
- **Nothing is signed.** anvil fills a request for an account it holds no key for.
- **`tx` is not a request as it is.** It has no `from`, and anvil 1.9 fills an EIP-1559 `tx`
  with a `gasPrice` that it then refuses next to the fee cap. Copy the fields you want into
  your request, as in the example above. Its calldata is `input`, which a request accepts.
- **The gas is an estimate.** The node runs the transaction to find it, so a request that
  reverts fails as [`Eth.estimateGas`](https://ethect.dev/rpc/eth/estimate-gas/) does. A request with a
  `gas` is not run.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `ExecutionReverted` | the request has no `gas` and the transaction would revert |

## JSON-RPC method

`eth_fillTransaction`
