viem-goviem-go

getFeeHistory

Returns a collection of historical gas information

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

An example showing how to request recent fee history and inspect the returned statistics.

import (
"context"
"log"
"github.com/ChefBingbong/viem-go/actions/public"
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
ctx := context.Background()
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{
Chain: definitions.Mainnet,
Transport: transport.HTTP("https://eth.llamarpc.com"),
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = publicClient.Close() }()
history, err := public.GetFeeHistory(ctx, publicClient, public.GetFeeHistoryParameters{
BlockCount: 4,
RewardPercentiles: []float64{25, 75},
})
if err != nil {
log.Fatal(err)
}
log.Printf("Fee history: %d blocks", len(history.Reward))

Returns

formatters.FeeHistory

A formatted fee history containing:

  • BaseFeePerGas []*big.Int - Base fee per gas for each block
  • GasUsedRatio []float64 - Gas used ratio for each block
  • OldestBlock uint64 - Oldest block number in the range
  • Reward [][]*big.Int - Reward percentiles for each block

Parameters

Configuration options accepted by this action.

BlockCount

  • Type: uint64
  • Required

The number of blocks in the requested range. Between 1 and 1024 blocks can be requested in a single query.

history, err := public.GetFeeHistory(ctx, publicClient, public.GetFeeHistoryParameters{
BlockCount: 4,
RewardPercentiles: []float64{25, 75},
})

RewardPercentiles

  • Type: []float64
  • Required

A monotonically increasing list of percentile values to sample from each block's effective priority fees per gas in ascending order, weighted by gas used.

history, err := public.GetFeeHistory(ctx, publicClient, public.GetFeeHistoryParameters{
BlockCount: 4,
RewardPercentiles: []float64{25, 75},
})

BlockNumber

  • Type: *uint64
  • Optional

The highest block number of the requested range.

blockNum := uint64(12345)
history, err := public.GetFeeHistory(ctx, publicClient, public.GetFeeHistoryParameters{
BlockCount: 4,
RewardPercentiles: []float64{25, 75},
BlockNumber: &blockNum,
})

BlockTag

  • Type: BlockTag
  • Default: "latest"
  • Optional

The highest block tag of the requested range (e.g., "latest").

history, err := public.GetFeeHistory(ctx, publicClient, public.GetFeeHistoryParameters{
BlockCount: 4,
RewardPercentiles: []float64{25, 75},
BlockTag: public.BlockTagSafe,
})

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_feeHistory