Accounts: L1->L2 transactions


Accounts: L1->L2 transactions

This section explores the methods which allow the account classes to send transactions from L1 to L2.

If you want some background on how L1->L2 interaction works on zkSync, go through the introduction.

Full examples of actions below are available on the getting started page.

Deposit


func (p *DefaultEthProvider) Deposit(token *Token, amount *big.Int, address common.Address, options *GasOptions) (*types.Transaction, error)

Inputs and outputs

NameDescription
tokenThe address of the deposited L1 ERC20 token.
amountThe amount of the token to be deposited.
addressThe address that receives the deposited tokens on L2.
optionsThe options for transaction fee and gas.
returnsThe transaction hash of the deposit.

Claim failed deposit

The ClaimFailedDeposit method withdraws funds from an initiated deposit, which failed when finalizing on L2.
If the L2 deposit transaction fails, it sends an L1 transaction calling the ClaimFailedDeposit method of the L1 bridge, which results in returning L1 tokens back to the depositor, or throws an error.


 func (w *Wallet) ClaimFailedDeposit(depositHash common.Hash, ep EthProvider) (common.Hash, error)

Inputs and Outputs

NameDescription
depositHashThe L2 transaction hash of the failed deposit finalization.
epThe Ethereum provider which interacts with L1 network.
returnsThe transaction hash of claim failed deposit

Withdrawals

Withdrawal are executed in 2 steps:

  • Withdraw: Initiates withdrawal on L2.
  • FinalizeWithdraw: Finalized withdrawal on L1.

Withdraw

func (w *Wallet) Withdraw(to common.Address, amount *big.Int, token *Token, nonce *big.Int) (common.Hash, error)

Inputs and outputs

NameDescription
toThe address that receives the withdrawn tokens on L1.
amountThe amount of the token to be deposited.
tokenThe address of the L2 ERC20 token.
nonceThe sender's nonce.
returnsThe transaction hash of the withdrawal.

FinalizeWithdraw

func (w *Wallet) FinalizeWithdraw(withdrawalHash common.Hash, index int) (common.Hash, error)

Inputs and outputs

NameDescription
withdrawalHashThe L2 transaction hash of the withdrawal.
indexThe position in the L2 Merkle-tree logs of the l2Log that was sent with the message.
returnsThe L1 transaction hash of the finalized withdrawal.

Example

// Perform withdraw
wHash, err := w.Withdraw(
  w.GetAddress(),
  big.NewInt(1000000000),
  nil,
  nil,
)
if err != nil {
  panic(err)
}
fmt.Println("Withdraw transaction: ", wHash)

// Wait until transaction is finalized
_, err = w.GetProvider().WaitFinalized(context.Background(), wHash)
if err != nil {
  panic(err)
}

// Perform finalize withdraw
fwHash, err := w.FinalizeWithdraw(wHash, 0)
if err != nil {
  panic(err)
}
fmt.Println("Finalize withdraw transaction", fwHash)