# FeeHistory

What Eth.feeHistory returns. It holds the base fees, the gas usage and the priority fees of a range of blocks, one array per figure.

What [`Eth.feeHistory`](https://ethect.dev/rpc/eth/fee-history/) returns: the base fees, the gas usage and
the priority fees of a range of blocks, one array per figure.

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

const busiest = Effect.gen(function* () {
  const history = yield* Eth.feeHistory(10, "latest", []);
  // history: FeeHistory.FeeHistory
  const ratio = Math.max(...history.gasUsedRatio);
  return history.oldestBlock + BigInt(history.gasUsedRatio.indexOf(ratio));
});
```

The arrays are aligned: the entry at index `i` is about the block `oldestBlock + i`.

## Schemas

| Schema | Type | Describes | Returned by |
| --- | --- | --- | --- |
| `FeeHistory.schema` | `FeeHistory.FeeHistory` | fees over a range of blocks | [`Eth.feeHistory`](https://ethect.dev/rpc/eth/fee-history/) |

## Fields

| Field | Type | Notes |
| --- | --- | --- |
| `oldestBlock` | `bigint` | the number of the first block of the range |
| `baseFeePerGas` | `ReadonlyArray<bigint>` | in wei, one per block, plus one for the block after the range |
| `gasUsedRatio` | `ReadonlyArray<number>` | gas used over gas limit, between 0 and 1, one per block |
| `reward` | `ReadonlyArray<ReadonlyArray<bigint>>` (optional) | in wei, per block, the priority fee at each percentile asked for |
| `baseFeePerBlobGas` | `ReadonlyArray<bigint>` (optional) | in wei, one per block, plus one for the block after the range, since Cancun |
| `blobGasUsedRatio` | `ReadonlyArray<number>` (optional) | blob gas used over the limit, one per block, since Cancun |

## Behavior

- **One more base fee than blocks.** For a range of `n` blocks, `baseFeePerGas` and
  `baseFeePerBlobGas` have `n + 1` entries and the other arrays have `n`. The last base fee is
  that of the next block.
- **`reward` follows the percentiles.** `reward[i][j]` is the priority fee at the `j`-th
  percentile asked for, in block `oldestBlock + i`. Asked for none, anvil answers with an empty
  array.
- **Ratios are `number`s.** A ratio is a JSON number on the wire, not a hex quantity. Every fee
  is a `bigint`.
- **Optional means absent.** `reward` and the two blob fields are left out of the object when
  the node does not send them or sends `null`. Test with `history.reward !== undefined`.
- **`baseFeePerGas` and `gasUsedRatio` are always arrays.** A node asked about no block leaves
  them out or sends `null`: both read as `[]`.
