Skip to content

estimateInitiateWithdrawalGas

Estimates gas required to initiate a withdrawal on an L2 to the L1.

Usage

example.ts
import { base } from 'viem/chains'
import { account, walletClientL2 } from './config'
 
const gas = await walletClientL2.estimateInitiateWithdrawalGas({
  account,
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    value: parseEther('1')
  },
})

Returns

bigint

Estimated gas required to initiate the withdrawal.

Parameters

account

  • Type: Account | Address

The Account to send the transaction from.

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

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    value: parseEther('1')
  },
  targetChain: base,
})

args.data (optional)

  • Type: Hex

Encoded contract method & arguments.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    data: '0x...', 
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1')
  },
})

args.gas

  • Type: bigint

Gas limit for transaction execution on the L1.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n, 
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    value: parseEther('1')
  },
})

args.to

  • Type: Address

L1 Transaction recipient.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',  
    value: parseEther('1')
  },
})

args.value (optional)

  • Type: bigint

Value in wei to withdrawal from the L2 to the L1. Debited from the caller's L2 balance.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1') 
  },
})

chain (optional)

  • Type: Chain
  • Default: client.chain

The L2 chain. If there is a mismatch between the wallet's current chain & this chain, an error will be thrown.

import { optimism } from 'viem/chains'
 
const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1')
  },
  chain: optimism, 
})

maxFeePerGas (optional)

  • Type: bigint

Total fee per gas (in wei), inclusive of maxPriorityFeePerGas.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1')
  },
  maxFeePerGas: parseGwei('20'),  
})

maxPriorityFeePerGas (optional)

  • Type: bigint

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

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1')
  },
  maxFeePerGas: parseGwei('20'), 
  maxPriorityFeePerGas: parseGwei('2'),  
})

nonce (optional)

  • Type: number

Unique number identifying this transaction.

const gas = await client.estimateInitiateWithdrawalGas({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  request: {
    gas: 21_000n,
    to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
    value: parseEther('1')
  },
  nonce: 69, 
})