> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Estimate Gas \[transaction.estimateGas]

Estimates the gas necessary to complete a transaction without submitting it to the network (`eth_estimateGas`).

## Usage

This example estimates the gas necessary to complete a transaction without submitting it to the network (`eth_estimateGas`).

:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'

const gas = await client.transaction.estimateGas({
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
// @log: 21000n
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())
```
:::

### Standalone Action

Call `Actions.transaction.estimateGas` directly by passing the Client as the first argument.

:::code-group
```ts twoslash [example.ts]
import { Actions } from 'viem'
import { client } from './viem.config'

const gas = await Actions.transaction.estimateGas(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
// @log: 21000n
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
})
```
:::

## Recipes

### Estimate with State Overrides

Pass `stateOverride` to estimate a transaction against temporary account state.

```ts twoslash
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())

const account = '0xd2135CfB216b74109775236E36d4b433F1DF507B'
const gas = await client.transaction.estimateGas({
  account,
  stateOverride: { // [!code focus]
    [account]: { // [!code focus]
      balance: 1000000000000000000n, // [!code focus]
    }, // [!code focus]
  }, // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### Estimate a Blob Transaction

Provide blobs and a [KZG context](/docs/utilities/kzg/from) to derive the versioned hashes before estimating gas.

```ts twoslash
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

declare const kzg: Kzg.Kzg

const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())

const gas = await client.transaction.estimateGas({
  blobs: ['0x1234'], // [!code focus]
  kzg, // [!code focus]
  maxFeePerBlobGas: 1_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  type: 'eip4844', // [!code focus]
})
```

## Return Value

`bigint`

The estimated gas (in units) required to execute the transaction.

## Parameters

### accessList

* **Type:** `AccessList`

The EIP-2930 access list to attach to the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  accessList: [ // [!code focus]
    { // [!code focus]
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
      storageKeys: [ // [!code focus]
        '0x0000000000000000000000000000000000000000000000000000000000000001', // [!code focus]
      ], // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### account

* **Type:** `Account | Address`
* **Default:** `client.account`

The Account object or address that supplies the transaction's `msg.sender`. This option takes precedence over [`from`](#from).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### authorizationList

* **Type:** `AuthorizationList`

The EIP-7702 (signed) authorization list to attach to the transaction. When `to` is omitted, it is inferred from the first authorization.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  authorizationList: [ // [!code focus]
    { // [!code focus]
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
      chainId: 1, // [!code focus]
      nonce: 0n, // [!code focus]
      r: '0x...', // [!code focus]
      s: '0x...', // [!code focus]
      yParity: 0, // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
})
```

### blobs

* **Type:** `readonly Hex[]`

The blobs to attach to the transaction (EIP-4844). Provide [`kzg`](#kzg) to derive blob versioned hashes locally.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

declare const kzg: Kzg.Kzg
const blobData = '0x1234' as const

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blobs: [blobData], // [!code focus]
  kzg, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### blobVersionedHashes

* **Type:** `readonly Hex[]`

The blob versioned hashes (EIP-4844). When omitted, they are derived from [`blobs`](#blobs) and [`kzg`](#kzg).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blobVersionedHashes: ['0x...'], // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### blockHash

* **Type:** `Hex`

Estimates gas against the state at a given block hash (EIP-1898).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blockHash: '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### blockNumber

* **Type:** `bigint`

Estimates gas against the state at a given block number.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blockNumber: 42069n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### blockTag

* **Type:** `'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`
* **Default:** `client.blockTag ?? 'latest'`

Estimates gas against the state at a given block tag.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blockTag: 'pending', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### chainId

* **Type:** `number | Hex`

The chain ID to include in the transaction request.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  chainId: 1, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### data

* **Type:** `Hex`

The calldata to send with the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  data: '0x06fdde03', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### from

* **Type:** `Address`

The raw sender address. Viem uses this field only when neither [`account`](#account) nor a Client Account is available.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  from: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### gas

* **Type:** `number | bigint | Hex`

The gas limit to use for the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  gas: 1_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### gasPrice

* **Type:** `number | bigint | Hex`

The legacy gas price (in wei) to use. Mutually exclusive with `maxFeePerGas`/`maxPriorityFeePerGas`.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  gasPrice: 20_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### kzg

* **Type:** `Kzg`

KZG context used to derive [`blobVersionedHashes`](#blobversionedhashes) from [`blobs`](#blobs).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

declare const kzg: Kzg.Kzg
const blobData = '0x1234' as const

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blobs: [blobData],
  kzg, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxFeePerBlobGas

* **Type:** `number | bigint | Hex`

The max fee per blob gas (in wei) the sender is willing to pay (EIP-4844).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  maxFeePerBlobGas: 1_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxFeePerGas

* **Type:** `number | bigint | Hex`

The total fee per gas (in wei) the sender is willing to pay (EIP-1559).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  maxFeePerGas: 20_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxPriorityFeePerGas

* **Type:** `number | bigint | Hex`

The max priority fee per gas (in wei) to pay to the block producer (EIP-1559).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  maxPriorityFeePerGas: 1_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### nonce

* **Type:** `number | bigint | Hex`

The nonce to use for the transaction's sender.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  nonce: 69, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### requestOptions

* **Type:** `{ dedupe?: boolean; signal?: AbortSignal; ... }`

Per-request transport options (for example, an `AbortSignal` to cancel the request).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const controller = new AbortController()
const gas = await Actions.transaction.estimateGas(client, {
  requestOptions: { signal: controller.signal }, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### requireCanonical

* **Type:** `boolean`
* **Default:** `false`

Whether to error if the [`blockHash`](#blockhash) is not in the canonical chain (EIP-1898).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  blockHash: '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d',
  requireCanonical: true, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### stateOverride

* **Type:** `StateOverrides`

Overrides account state (balance, nonce, code, storage) for the duration of the estimation.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  account: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
  stateOverride: { // [!code focus]
    '0xd2135CfB216b74109775236E36d4b433F1DF507B': { // [!code focus]
      balance: 1000000000000000000n, // [!code focus]
    }, // [!code focus]
  }, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  value: 1n,
})
```

### to

* **Type:** `Address | null`

The transaction recipient. Pass `null` for a contract creation request.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus]
  value: 1n,
})
```

### type

* **Type:** `string`

Forces the transaction request to a specific type (for example, `'eip1559'`, `'eip2930'`, `'legacy'`).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  type: 'eip1559', // [!code focus]
})
```

### value

* **Type:** `number | bigint | Hex`

The value (in wei) sent with the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const gas = await Actions.transaction.estimateGas(client, {
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n, // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `RpcError.ExecutionError` | The gas estimation failed (for example, the transaction reverted or fee invariants were violated). |
| `BlockParameter.RequireCanonicalError` | `requireCanonical` was provided without a `blockHash`. |
