Gas Fee Estimation

Purpose

Gas Fee Estimation addresses the challenge of accurately calculating transaction gas fees on Ethereum and EVM-compatible blockchains. Given the dynamic nature of blockchain transaction costs—driven by network demand and protocol changes—this component provides reliable fee estimates at various percentiles by analyzing recent block and transaction fee data. This enables clients to select appropriate gas fees for timely and cost-effective transaction inclusion, improving user experience and reducing failed transactions due to underpayment.

Functionality

The estimation logic centers on collecting and processing gas price data from recent blocks:

Core Methods & Data Flow

This data-driven, percentile-based approach allows nuanced fee recommendations that adapt dynamically to network conditions.

Relationship to Parent Topic and Other Subtopics

Within the broader **EVM Gas Oracle** module, Gas Fee Estimation provides the foundational calculation logic that feeds into other components such as:

Gas Fee Estimation is distinct in focusing on statistical analysis of historical block data rather than solely integrating or exposing fees. It ensures that the gas fee values used throughout the system are statistically sound, responsive to network changes, and customized per blockchain’s capabilities and quirks.


Diagram

flowchart TD
  Start[Start Oracle] --> FetchBlocks[Fetch Latest N Blocks]
  FetchBlocks --> ExtractFees[Extract Tx Gas Prices & Priority Fees]
  ExtractFees --> UpdateCache[Update Fee Cache per Block]
  UpdateCache --> SyncState[Sync & Prune Cache]
  SyncState --> Estimate[Estimate Fees at Requested Percentiles]
  Estimate --> ReturnFees[Return Fee Estimates]
  ReturnFees --> AwaitNewBlocks[Wait for New Blocks]
  AwaitNewBlocks -->|New Block via WebSocket| EnqueueBlock[Enqueue New Block]
  EnqueueBlock --> ProcessBlock[Process Next Block]
  ProcessBlock --> UpdateCache
  ProcessBlock --> SyncState
  SyncState --> Estimate

This flowchart highlights the continuous cycle of fetching, caching, syncing, and estimating gas fees using recent blockchain data, incorporating new blocks via WebSocket events.


Code Snippet Illustration

The key method that calculates average fees at a percentile across recent blocks:

private averageAtPercentile(blockFees: Array<BlockFees>, percentile: number) {
  const valueAtPercentile = (fees: Array<number>) => {
    if (!fees.length) return 0
    const rank = Math.ceil((percentile / 100) * fees.length)
    return fees[rank - 1]
  }

  // Filter out outliers using thresholds
  const gasPriceThreshold = getFeeThreshold(blockFees.flatMap(f => f.gasPrices).filter(p => p > 1))
  const maxPriorityFeeThreshold = getFeeThreshold(blockFees.flatMap(f => f.maxPriorityFees).filter(p => p > 1))

  const sum = blockFees.reduce((sum, fees) => {
    sum.gasPrice += valueAtPercentile(fees.gasPrices.filter(p => p <= gasPriceThreshold))
    sum.maxPriorityFee += valueAtPercentile(fees.maxPriorityFees.filter(p => p <= maxPriorityFeeThreshold))
    return sum
  }, { gasPrice: 0, maxPriorityFee: 0 })

  return {
    gasPrice: Math.ceil(sum.gasPrice / blockFees.length),
    maxPriorityFee: Math.ceil(sum.maxPriorityFee / blockFees.length),
  }
}

This snippet demonstrates how the oracle smooths out gas price data by removing outliers and averaging percentile values across multiple blocks to provide robust fee estimates.


Gas Fee Estimation thus serves as the statistical engine powering accurate, adaptive gas fee recommendations essential for efficient transaction processing on EVM-based blockchains.