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.
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
Section titled “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.
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
Section titled “Without priority fees”Pass an empty array when you only want base fees and gas usage.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const baseFees = Eth.feeHistory(20, "latest", []).pipe( Effect.map((history) => history.baseFeePerGas),);Returns
Section titled “Returns”Effect<FeeHistory.FeeHistory, Method.Error, Transport>
A FeeHistory: the number of the oldest block of the range,
and one array per figure.
Parameters
Section titled “Parameters”blockCount
Section titled “blockCount”- Type:
number
How many blocks to return, counting back from newestBlock.
import { Eth } from "@ethect/core/rpc";
const history = Eth.feeHistory(100, "latest", []);newestBlock
Section titled “newestBlock”- Type:
BlockId.NumberOrTag
The last block of the range: a number or a tag. A block hash is not accepted here.
import { Eth } from "@ethect/core/rpc";
const history = Eth.feeHistory(100, 19_000_000n, []);rewardPercentiles
Section titled “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.
import { Eth } from "@ethect/core/rpc";
const history = Eth.feeHistory(100, "latest", [25, 50, 75]);The param is required: pass [] for none.
Behavior
Section titled “Behavior”- One more base fee than blocks.
baseFeePerGasandbaseFeePerBlobGashaveblockCount + 1entries. The last one is for the block afternewestBlock, and it is whatEth.baseFeereturns. - A shorter chain gives a shorter range. When
blockCountreaches before the first block, the node returns the blocks it has. Read the range fromoldestBlockand the length of the arrays. - Ratios are JSON numbers.
gasUsedRatioandblobGasUsedRatioarenumbers between 0 and 1, and a percentile is anumbertoo. Fees arebigint, in wei. rewardcan be absent. The field is optional in the type. With[], anvil answers with an empty array.- No block, empty arrays. With a
blockCountof0,baseFeePerGasandgasUsedRatioare[]: anvil 1.9 leaves them out of its answer, and a node that sendsnullreads the same.
Errors
Section titled “Errors”The three 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
Section titled “JSON-RPC method”eth_feeHistory