Skip to content
Docs
/
Hooks
/
useFeeData

useFeeData

Hook for fetching network fee information.

import { useFeeData } from 'wagmi'

Usage

import { useFeeData } from 'wagmi'

function App() {
  const { data, isError, isLoading } = useFeeData()

  if (isLoading) return <div>Fetching fee data…</div>
  if (isError) return <div>Error fetching fee data</div>
  return <div>Fee data: {JSON.stringify(data?.formatted)}</div>
}

Return Value

{
  data?: {
    gasPrice: BigNumber
    maxFeePerGas: BigNumber
    maxPriorityFeePerGas: BigNumber
    formatted: {
      gasPrice: string
      maxFeePerGas: string
      maxPriorityFeePerGas: string
    }
  }
  error?: Error
  isIdle: boolean
  isLoading: boolean
  isFetching: boolean
  isSuccess: boolean
  isError: boolean
  isFetched: boolean
  isRefetching: boolean
  refetch: (options: {
    throwOnError: boolean
    cancelRefetch: boolean
  }) => Promise<{
    gasPrice: BigNumber
    maxFeePerGas: BigNumber
    maxPriorityFeePerGas: BigNumber
    formatted: {
      gasPrice: string
      maxFeePerGas: string
      maxPriorityFeePerGas: string
    }
  }>
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

chainId (optional)

Force a specific chain id for the request. The wagmi Client's ethers provider must be set up as a chain-aware function for this to work correctly.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    chainId: 1,
  })
}

formatUnits (optional)

Formats fee data using ethers units. Defaults to wei.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    formatUnits: 'gwei',
  })
}

watch (optional)

Watches and refreshes data for new blocks.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    watch: true,
  })
}

cacheTime (optional)

Time (in ms) which the data should remain in the cache. Defaults to 0.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    cacheTime: 2_000,
  })
}

enabled (optional)

Set this to false to disable this query from automatically running. Defaults to true.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    enabled: false,
  })
}

staleTime (optional)

Time (in ms) after data is considered stale. If set to Infinity the data will never be considered stale. Defaults to 0.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    staleTime: 2_000,
  })
}

suspense (optional)

Set this to true to enable suspense mode.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    suspense: true,
  })
}

onSuccess (optional)

Function to invoke when fetching new data is successful.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    onSuccess(data) {
      console.log('Success', data)
    },
  })
}

onError (optional)

Function to invoke when an error is thrown while fetching new data.

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    onError(error) {
      console.log('Error', error)
    },
  })
}

onSettled (optional)

Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).

import { useFeeData } from 'wagmi'

function App() {
  const feeData = useFeeData({
    onSettled(data, error) {
      console.log('Settled', { data, error })
    },
  })
}