# Eth.feeHistory

Returns the base fees, the gas usage and the priority fees paid over a range of blocks that ends
at a block you name.

## Usage

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

const history = await Eth.feeHistory(2, "latest", [50]).pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// {
//   oldestBlock: 2n,
//   baseFeePerGas: [875175000n, 765931281n, 670323909n],
//   gasUsedRatio: [0.0007, 0.0007],
//   reward: [[1000000000n], [1000000000n]],
//   baseFeePerBlobGas: [1n, 1n, 1n],
//   blobGasUsedRatio: [0, 0],
// }
```

The call asks for the last two blocks and the median priority fee of each. The result above is
from a local anvil at block 3, after three transfers.

The examples below leave the transport out.

### Suggesting fees

The last entry of `baseFeePerGas` is the base fee of the next block, and `reward` holds what
transactions paid on top of it.

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

const fees = Effect.gen(function* () {
  const history = yield* Eth.feeHistory(10, "latest", [50]);
  const nextBaseFee = history.baseFeePerGas.at(-1) ?? 0n;
  const tips = (history.reward ?? []).map(([median]) => median ?? 0n);
  const tip = tips.reduce((a, b) => (a > b ? a : b), 0n);
  // room for the base fee to double before the transaction is priced out
  return { maxPriorityFeePerGas: tip, maxFeePerGas: nextBaseFee * 2n + tip };
});
```

This takes the highest median of the last ten blocks as the priority fee. It is one policy
among many: the numbers are yours to choose.

### Without priority fees

Pass an empty array when you only want base fees and gas usage.

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

const baseFees = Eth.feeHistory(20, "latest", []).pipe(
  Effect.map((history) => history.baseFeePerGas),
);
```

## Returns

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

A [`FeeHistory`](https://ethect.dev/rpc/types/fee-history/): the number of the oldest block of the range,
and one array per figure.

## Parameters

### blockCount

- **Type:** `number`

How many blocks to return, counting back from `newestBlock`.

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

const history = Eth.feeHistory(100, "latest", []);
```

### newestBlock

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

The last block of the range: a number or a tag. A block hash is not accepted here.

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

const history = Eth.feeHistory(100, 19_000_000n, []);
```

### rewardPercentiles

- **Type:** `ReadonlyArray<number>`

The percentiles of each block's priority fees to return, between 0 and 100, in increasing
order. `[25, 50, 75]` gives three figures per block: a low, a median and a high one.

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

const history = Eth.feeHistory(100, "latest", [25, 50, 75]);
```

The param is required: pass `[]` for none.

## Behavior

- **One more base fee than blocks.** `baseFeePerGas` and `baseFeePerBlobGas` have
  `blockCount + 1` entries. The last one is for the block after `newestBlock`, and it is what
  [`Eth.baseFee`](https://ethect.dev/rpc/eth/fees/#basefee) returns.
- **A shorter chain gives a shorter range.** When `blockCount` reaches before the first block,
  the node returns the blocks it has. Read the range from `oldestBlock` and the length of the
  arrays.
- **Ratios are JSON numbers.** `gasUsedRatio` and `blobGasUsedRatio` are `number`s between 0 and
  1, and a percentile is a `number` too. Fees are `bigint`, in wei.
- **`reward` can be absent.** The field is optional in the type. With `[]`, anvil answers with
  an empty array.
- **No block, empty arrays.** With a `blockCount` of `0`, `baseFeePerGas` and `gasUsedRatio`
  are `[]`: anvil 1.9 leaves them out of its answer, and a node that sends `null` reads the
  same.

## 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` | `InvalidParams` | `newestBlock` is above the chain's head |
| `RpcError` | `InvalidParams` | the percentiles are not increasing, or one is above 100 |
| `CodecError` | `on: "params"` | `blockCount` is negative or not an integer: nothing is sent |

## JSON-RPC method

`eth_feeHistory`
