Skip to content

Add eth_feeHistory based fee estimation - #270

Merged
alisinabh merged 4 commits into
mainfrom
fee-history-estimation
Jul 30, 2026
Merged

Add eth_feeHistory based fee estimation#270
alisinabh merged 4 commits into
mainfrom
fee-history-estimation

Conversation

@alisinabh

Copy link
Copy Markdown
Member
  • New Ethers.fee_history/4 (+ bang variant) wrapping eth_feeHistory with all quantities decoded to integers; also usable in Ethers.batch/2 via {:fee_history, [block_count, newest_block, reward_percentiles]}
  • New Ethers.estimate_fees/1 (+ bang variant): estimates EIP-1559 fees from the median of recent blocks' priority fees sampled at a percentile selected by :speed (:slow/:standard/:fast or a raw percentile), with max_fee_per_gas = 2 * next_base_fee + max_priority_fee_per_gas
  • Transaction auto-fill now prices missing max_fee_per_gas / max_priority_fee_per_gas through estimate_fees instead of a margin over eth_gasPrice, falling back to the legacy estimation on RPC clients without eth_feeHistory support (the new adapter callback is optional)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces eth_feeHistory-based fee estimation to improve EIP-1559 transaction pricing, exposing a decoded Ethers.fee_history/4 API, a higher-level Ethers.estimate_fees/1 helper, and updating transaction auto-fill to prefer these estimates with a legacy fallback when unsupported.

Changes:

  • Add Ethers.fee_history/4 (+ fee_history!/4) and support batching via {:fee_history, [...]}.
  • Add Ethers.estimate_fees/1 (+ estimate_fees!/1) to derive EIP-1559 fee caps from recent blocks’ priority fee samples and next base fee.
  • Update EIP-1559 transaction auto-fill to use estimate_fees/1 when supported, falling back to legacy gas price heuristics otherwise, and add test coverage.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/ethers/fee_estimation_test.exs Adds unit/integration tests for fee history decoding, fee estimation math, batching, and transaction auto-fill fallback behavior.
lib/ethers/transaction.ex Switches EIP-1559 fee auto-fill to prefer estimate_fees/1 when available and retain legacy fill actions as fallback.
lib/ethers/rpc_client/adapter.ex Adds optional eth_fee_history/4 callback to keep custom adapters backward compatible.
lib/ethers.ex Implements fee_history/4, estimate_fees/1, decoding/post-processing, and percentile/median helpers.
CHANGELOG.md Documents the new APIs and changed transaction auto-fill fee behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/ethers.ex
Comment thread lib/ethers.ex Outdated
Comment thread lib/ethers.ex
alisinabh and others added 4 commits July 29, 2026 23:09
- New Ethers.fee_history/4 (+ bang variant) wrapping eth_feeHistory with all
  quantities decoded to integers; also usable in Ethers.batch/2 via
  {:fee_history, [block_count, newest_block, reward_percentiles]}
- New Ethers.estimate_fees/1 (+ bang variant): estimates EIP-1559 fees from the
  median of recent blocks' priority fees sampled at a percentile selected by
  :speed (:slow/:standard/:fast or a raw percentile), with
  max_fee_per_gas = 2 * next_base_fee + max_priority_fee_per_gas
- Transaction auto-fill now prices missing max_fee_per_gas /
  max_priority_fee_per_gas through estimate_fees instead of a margin over
  eth_gasPrice, falling back to the legacy estimation on RPC clients without
  eth_feeHistory support (the new adapter callback is optional)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pKYp3j1ew76CY7HA2nBTF
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Quantities must be integers - hex encoded strings are rejected instead of
being passed through, in line with the library convention of a single
accepted representation per input:

- block_count: positive integer only
- newest_block: non-negative integer or one of the named block tags
  (latest/earliest/pending/safe/finalized)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pKYp3j1ew76CY7HA2nBTF
Ethers.fee_history/4 called the optional eth_fee_history/4 adapter callback
directly, raising UndefinedFunctionError on RPC client adapters that do not
implement it. Guard with function_exported?/3 and return an error tuple
instead, keeping the {:ok, _} | {:error, _} contract.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pKYp3j1ew76CY7HA2nBTF
@alisinabh
alisinabh force-pushed the fee-history-estimation branch from 47a455c to 638f0a6 Compare July 30, 2026 03:09
@alisinabh
alisinabh merged commit f2e8d02 into main Jul 30, 2026
5 checks passed
@alisinabh
alisinabh deleted the fee-history-estimation branch July 30, 2026 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants