Skip to content

finalizeWithdrawal

Finalizes a withdrawal that occurred on an L2. Used in the Withdrawal flow.

Internally performs a contract write to the finalizeWithdrawalTransaction function on the Optimism Portal contract.

Usage

example.ts
import { account, publicClientL2, walletClientL1 } from './config'
 
const receipt = await getTransactionReceipt(publicClientL2, {
  hash: '0xbbdd0957a82a057a76b5f093de251635ac4ddc6e2d0c4aa7fbf82d73e4e11039',
})
 
const [withdrawal] = getWithdrawals(receipt)
 
const hash = await walletClientL1.finalizeWithdrawal({ 
  account, 
  targetChain: publicClientL2.chain, 
  withdrawal, 
}) 

Account Hoisting

If you do not wish to pass an account to every finalizeWithdrawal, you can also hoist the Account on the Wallet Client (see config.ts).

Learn more.

example.ts
import { account, publicClientL2, walletClientL1 } from './config'
 
const receipt = await getTransactionReceipt(publicClientL2, {
  hash: '0xbbdd0957a82a057a76b5f093de251635ac4ddc6e2d0c4aa7fbf82d73e4e11039',
})
 
const [withdrawal] = getWithdrawals(receipt)
 
const hash = await walletClientL1.finalizeWithdrawal({ 
  account, 
  targetChain: publicClientL2.chain, 
  withdrawal, 
}) 

Returns

Hash

The finalize withdrawal Transaction hash.

Parameters

account

  • Type: Account | Address

The Account to send the transaction from.

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

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  withdrawal: { /* ... */ },
  targetChain: optimism,
})

chain (optional)

  • Type: Chain
  • Default: client.chain

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

import { mainnet } from 'viem/chains'
 
const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  chain: mainnet, 
  withdrawal: { /* ... */ },
  targetChain: optimism,
})

gas (optional)

  • Type: bigint

Gas limit for transaction execution on the L1.

null to skip gas estimation & defer calculation to signer.

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  gas: 420_000n,  
  withdrawal: { /* ... */ },
  targetChain: optimism,
})

maxFeePerGas (optional)

  • Type: bigint

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

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  maxFeePerGas: parseGwei('20'),  
  withdrawal: { /* ... */ },
  targetChain: optimism,
})

maxPriorityFeePerGas (optional)

  • Type: bigint

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

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  maxFeePerGas: parseGwei('20'), 
  maxPriorityFeePerGas: parseGwei('2'),  
  withdrawal: { /* ... */ },
  targetChain: optimism,
})

nonce (optional)

  • Type: number

Unique number identifying this transaction.

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  withdrawal: { /* ... */ },
  nonce: 69, 
  targetChain: optimism,
})

portalAddress (optional)

  • Type: Address
  • Default: targetChain.contracts.portal[chainId].address

The address of the Optimism Portal contract. Defaults to the Optimism Portal contract specified on the targetChain.

If a portalAddress is provided, the targetChain parameter becomes optional.

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  portalAddress: '0xbEb5Fc579115071764c7423A4f12eDde41f106Ed'
  targetChain: optimism,
  withdrawal: { /* ... */ },
})

targetChain

The L2 chain to execute the transaction on.

import { mainnet } from 'viem/chains'
 
const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  withdrawal: { /* ... */ },
  targetChain: optimism, 
})

withdrawal

  • Type: bigint

The withdrawal.

const hash = await client.finalizeWithdrawal({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  gas: 420_000n, 
  withdrawal: { /* ... */ }, 
  targetChain: optimism,
})