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
Name | Description |
---|---|
token | The address of the deposited L1 ERC20 token. |
amount | The amount of the token to be deposited. |
address | The address that receives the deposited tokens on L2. |
options | The options for transaction fee and gas. |
returns | The 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
Name | Description |
---|---|
depositHash | The L2 transaction hash of the failed deposit finalization. |
ep | The Ethereum provider which interacts with L1 network. |
returns | The 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
Name | Description |
---|---|
to | The address that receives the withdrawn tokens on L1. |
amount | The amount of the token to be deposited. |
token | The address of the L2 ERC20 token. |
nonce | The sender's nonce. |
returns | The transaction hash of the withdrawal. |
FinalizeWithdraw
func (w *Wallet) FinalizeWithdraw(withdrawalHash common.Hash, index int) (common.Hash, error)
Inputs and outputs
Name | Description |
---|---|
withdrawalHash | The L2 transaction hash of the withdrawal. |
index | The position in the L2 Merkle-tree logs of the l2Log that was sent with the message. |
returns | The 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)