Skip to content
On this page

writeWithdrawETH

Initiates an L2 -> L1 ETH transfer by calling withdrawTo and the L2StandardBridge contract.

INFO

Beyond this transaction, completing a withdraw requires

  1. Calling writeProveWithdrawalTransaction after the state root including this transaction is written to L1
  2. Calling writeFinalizeWithdrawalTransaction after the fault challenge period has elapsed.

Read here for more details.

WARNING

From Viem writeContract, which this function uses internally.

The writeContract internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with simulateContract before you execute it.

In this case, you can use simulateWithdrawETH.

ts
import { WithdrawETHParameters } from 'op-viem'
import { account, opStackL2WalletClient } from './config'

const args: WithdrawETHParameters = {
  to: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
  amount: 100n,
  minGasLimit: 85000,
  extraData: '0x123',
}

const hash = await opStackL2WalletClient.writeWithdrawETH({
  args,
  account,
})
ts
import { walletL2OpStackActions } from 'op-viem'
import { base } from 'op-viem/chains'
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

export const opStackL2WalletClient = createWalletClient({
  chain: base,
  transport: custom(window.ethereum)
}).extend(walletL2OpStackActions)

// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)

Return Value

Hash

A Transaction Hash.

writeContract only returns a Transaction Hash. If you would like to retrieve the return data of a write function, you can use the simulateWithdrawETH action – this action does not execute a transaction, and does not require gas.

Parameters

args

  • to

    • Type: Address
    • The to address of the L1 transaction.
  • amount

    • Type: bigint
    • Value in wei to withdraw. This is the amount of ETH, specified in wei, that will leave the user's address on L2.
  • minGasLimit

    • Type: number
    • The minimum gas limit for the L1 transaction.
  • extraData (optional)

    • Type: Hex
    • Default: 0x
    • The data of the L1 transaction
ts
await opStackL2PublicClient.writeWithdrawETH({
  args: { 
    to: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
    amount: 100n,
    minGasLimit: 85000,
    extraData: '0x123',
  },
})

TIP

account, accessList, chain, dataSuffix, gasPrice, maxFeePerGas, maxPriorityFeePerGas, and nonce can all also be passed and behave as with any viem writeContract call. See their documentation for more details.