viem-goviem-go

estimateFeesPerGas

Returns an estimate for the fees per gas for a transaction to be likely included in the next block

Returns an estimate for the fees per gas (in wei) for a transaction to be likely included in the next block.

  • For EIP-1559 chains, returns maxFeePerGas & maxPriorityFeePerGas.
  • For legacy chains, returns gasPrice.
  • Applies a base fee multiplier (default 1.2) to provide a safety buffer.

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 construct a public client and estimate fees for the next block.

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() }()
fees, err := public.EstimateFeesPerGas(ctx, publicClient, public.EstimateFeesPerGasParameters{})
if err != nil {
log.Fatal(err)
}
log.Printf("Max fee per gas: %s", fees.MaxFeePerGas.String())
log.Printf("Max priority fee per gas: %s", fees.MaxPriorityFeePerGas.String())

Returns

*EstimateFeesPerGasReturnType

An object containing:

  • Type FeeValuesType - The type of fee values ("eip1559" or "legacy")
  • MaxFeePerGas *big.Int - The max fee per gas (EIP-1559)
  • MaxPriorityFeePerGas *big.Int - The max priority fee per gas (EIP-1559)
  • GasPrice *big.Int - The gas price (legacy)

Parameters

Configuration options accepted by this action.

Type

  • Type: FeeValuesType
  • Default: FeeValuesTypeEIP1559
  • Optional

The type of fee values to return.

fees, err := public.EstimateFeesPerGas(ctx, publicClient, public.EstimateFeesPerGasParameters{
Type: public.FeeValuesTypeLegacy,
})

BaseFeeMultiplier

  • Type: *float64
  • Default: 1.2 (20% buffer)
  • Optional

The multiplier applied to the base fee per gas (or gas price for legacy chains) when computing fees.

multiplier := 1.5
fees, err := public.EstimateFeesPerGas(ctx, publicClient, public.EstimateFeesPerGasParameters{
BaseFeeMultiplier: &multiplier,
})

JSON-RPC Method

Underlying JSON-RPC methods used by this action.

Uses eth_getBlockByNumber, eth_maxPriorityFeePerGas, and eth_gasPrice to compute fees.