clawchain/wagmi/site/react/api/hooks/useEstimateGas.md

8.1 KiB

title, description
title description
useEstimateGas Hook for estimating the gas necessary to complete a transaction without submitting it to the network.

useEstimateGas

Hook for estimating the gas necessary to complete a transaction without submitting it to the network.

Import

import { useEstimateGas } from 'wagmi'

Usage

::: code-group

import { useEstimateGas } from 'wagmi'

function App() {
  const result = useEstimateGas()
}

<<< @/snippets/react/config.ts[config.ts] :::

Parameters

import { type UseEstimateGasParameters } from 'wagmi'

accessList

AccessList | undefined

The access list.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    accessList: [{ // [!code focus]
      address: '0x1', // [!code focus]
      storageKeys: ['0x1'], // [!code focus]
    }], // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

account

Address | Account | undefined

Account to use when estimating gas.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

chainId

config['chains'][number]['id'] | undefined

Chain ID to target when estimating gas.

::: code-group

import { useEstimateGas } from 'wagmi'
import { mainnet } from '@wagmi/core/chains'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    chainId: mainnet.id, // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

connector

Connector | undefined

Connector to estimate with. If no account is provided, will use default account from connector.

::: code-group

import { getConnections, estimateGas } from '@wagmi/core'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const connections = getConnections(config)
  const result = useEstimateGas({
    connector: connections[0]?.connector, // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

data

`0x${string}` | undefined

A contract hashed method call with encoded function data.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

gas

bigint | undefined

Gas provided for transaction execution.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther, parseGwei } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    gas: parseGwei('20'), // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::


gasPrice

bigint | undefined

The price in wei to pay per gas. Only applies to Legacy Transactions.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther, parseGwei } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    gasPrice: parseGwei('20'), // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

maxFeePerGas

bigint | undefined

Total fee per gas in wei, inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther, parseGwei } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    maxFeePerGas: parseGwei('20'), // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

maxPriorityFeePerGas

bigint | undefined

Max priority fee per gas in wei. Only applies to EIP-1559 Transactions.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther, parseGwei } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    maxFeePerGas: parseGwei('20'),
    maxPriorityFeePerGas: parseGwei('2'), // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::


nonce

number

Unique number identifying this transaction.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    nonce: 123, // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

scopeKey

string | undefined

Scopes the cache to a given context. Hooks that have identical context will share the same cache.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    scopeKey: 'foo', // [!code focus]
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

to

Address | undefined

The transaction recipient or contract address.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

type

'legacy' | 'eip1559' | 'eip2930' | undefined

Optional transaction request type to narrow parameters.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    type: 'eip1559', // [!code focus]
    value: parseEther('0.01'),
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

value

bigint | undefined

Value in wei sent with this transaction.

::: code-group

import { useEstimateGas } from 'wagmi'
import { parseEther } from 'viem'
import { config } from './config'

function App() {
  const result = useEstimateGas({
    to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', 
    value: parseEther('0.01'), // [!code focus]
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

Return Type

import { type UseEstimateGasReturnType } from 'wagmi'

Action