Skip to content
On this page

estimateGas โ€‹

Estimates the gas necessary to complete a transaction without submitting it to the network.

Usage โ€‹

ts
import { account, publicClient } from './config'

const gasEstimate = await publicClient.estimateGas({ 
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})
ts
import { createPublicClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'

// JSON-RPC Account
export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
// Local Account
export const account = privateKeyToAccount(...)

export const publicClient = createPublicClient({
  chain: mainnet,
  transport: http()
})

Returns โ€‹

bigint

The gas estimate (in wei).

Parameters โ€‹

account (optional) โ€‹

  • Type: Account | Address

The Account to estimate gas from.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus)]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

data (optional) โ€‹

  • Type: 0x${string}

Contract code or a hashed method call with encoded args.

ts
const gasEstimate = await publicClient.estimateGas({
  data: '0x...', 
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

gasPrice (optional) โ€‹

  • Type: bigint

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

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  gasPrice: parseGwei('20'), 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1') 
})

maxFeePerGas (optional) โ€‹

  • Type: bigint

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

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  maxFeePerGas: parseGwei('20'),  
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

maxPriorityFeePerGas (optional) โ€‹

  • Type: bigint

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

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'), 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

to (optional) โ€‹

Transaction recipient.

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
  value: parseEther('1')
})

value (optional) โ€‹

  • Type: bigint

Value (in wei) sent with this transaction.

ts
const gasEstimate = await publicClient.estimateGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1') 
})

blockNumber (optional) โ€‹

  • Type: number

The block number to perform the gas estimate against.

ts
const gasEstimate = await publicClient.estimateGas({
  blockNumber: 15121123n, 
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1') 
})

blockTag (optional) โ€‹

  • Type: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'
  • Default: 'latest'

The block tag to perform the gas estimate against.

ts
const gasEstimate = await publicClient.estimateGas({
  blockTag: 'safe', 
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1') 
})

JSON-RPC Methods โ€‹

eth_estimateGas

Released under the MIT License.