# Eth.estimateGas

Returns the gas a transaction would need, found by running it against the state of a block
without 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.estimateGas({ from: FROM, to: TO, value: 10n ** 18n });

const gas = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// 21000n
```

A transfer of ether between two accounts costs `21000` gas. The examples below leave the
transport out.

### A contract call

Put the calldata in `data`, as hex. [`Call.encode`](https://ethect.dev/reference/call/encode/) builds it from
a call of a contract.

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

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const RECIPIENT = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const gas = Effect.gen(function* () {
  // transfer(address,uint256): the selector, then one 32-byte word per argument
  const { data } = yield* Call.encode(Erc20.at(TOKEN).transfer(RECIPIENT, 10n ** 18n));
  return yield* Eth.estimateGas({ from: FROM, to: TOKEN, data });
});
```

### Setting the gas of a transaction

Estimate, then send the same request with the result as its `gas`.

```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 sent = Effect.gen(function* () {
  const request = { from: FROM, to: TO, value: 10n ** 18n };
  const gas = yield* Eth.estimateGas(request);
  return yield* Eth.sendTransaction({ ...request, gas });
});
```

A node or wallet estimates the gas itself when a request has no `gas`, so this step is for when
you want the number: to show a cost, or to add a margin.

## Returns

[`Effect<bigint, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The gas the transaction needs, in units of gas.

## Parameters

### request

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

The transaction to estimate. Every field is optional, and an estimate uses these:

| Field | Type | Description |
| --- | --- | --- |
| `from` | `Address` | the account that would send it |
| `to` | `Address` | the recipient or the contract, left out to create a contract |
| `value` | `bigint` | wei sent with it |
| `data` | `Hex` | the calldata, or the creation code of a contract |
| `gas` | `bigint` | a ceiling for the estimate |

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

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

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

### block (optional)

- **Type:** [`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)

The block whose state the estimate runs on: a number, a tag, or an object that names the block
by hash.

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

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

const gas = Eth.estimateGas({ from: FROM, to: TO, value: 1n }, "pending");
```

### stateOverride (optional)

- **Type:** [`StateOverride.StateOverride`](https://ethect.dev/rpc/types/state-override/)

State the estimate pretends is on chain, per account.

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

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

const gas = Eth.estimateGas(
  { from: FROM, to: TO, value: 10n ** 18n },
  "latest",
  { [FROM]: { balance: 10n ** 19n } },
);
// 21000n
```

The account holds nothing on a local anvil. With the override the estimate succeeds, and
without it the node rejects the request.

### blockOverride (optional)

- **Type:** [`BlockOverride.BlockOverride`](https://ethect.dev/rpc/types/block-override/)

Header fields the estimate pretends its block has.

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

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

const gas = Eth.estimateGas(
  { from: FROM, to: TO, value: 1n },
  "latest",
  {},
  { time: 2_000_000_000n },
);
```

## Behavior

- **An estimate for one block.** The state can change before the transaction is mined, and the
  gas it uses with it.
- **A revert fails the estimate.** The error is the one [`Eth.call`](https://ethect.dev/rpc/eth/call/) gives:
  an `RpcError` whose reason is `ExecutionReverted`, with the revert data.
- **`gas` is a ceiling.** A request that needs more than its `gas` fails instead of returning a
  higher number.
- **No default block.** Left out, the param is not sent and the node chooses the block:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **Params are positional.** To pass a `blockOverride` without a state override, pass `{}` as
  `stateOverride`.

## 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 transaction would revert: `reason.data` is the revert data |
| `RpcError` | `TransactionRejected` | `from` does not hold the `value` |
| `RpcError` | `TransactionRejected` | the transaction needs more gas than the `gas` given |
| `RpcError` | `InvalidParams` | the block number is above the chain's head |
| `RpcError` | `ResourceNotFound` | no block has the given `blockHash` |
| `CodecError` | `on: "params"` | a quantity is negative: nothing is sent |

## JSON-RPC method

`eth_estimateGas`
