Working with Wallets on ZKsync using Web3.js

Working with Wallets on ZKsync using Web3.js

Before jumping into wallet interactions, make sure you have already set up your local ZKsync environment as described in the previous article on setting up the ZKsync environment. It’s crucial to have a functional environment to interact with Layer 2 and execute the wallet operations discussed here. If you haven’t done that yet, follow the steps in that article to set up the provider and local environment.

In this article, we dive deeper into working with wallets on ZKsync. After setting up your provider and querying balances, it’s essential to understand how to handle wallets for making deposits, withdrawals, transfers, and sending transactions.

Using Web3.js with the ZKsync plugin, you can interact with wallets on both Layer 1 (Ethereum) and Layer 2 (ZKsync). In this guide, we will walk through the following operations:

  1. Initializing a wallet.
  2. Querying balances on both L1 and L2.
  3. Depositing and withdrawing funds between L1 and L2.
  4. Transferring assets.
  5. Sending transactions and signing them.

Step 1: Setting Up the Wallet

We begin by initializing our Web3 provider and registering the ZKsync plugin to enable Layer 2 capabilities. Then, we use a private key to create a new Wallet instance that can interact with both L1 and L2.

In the local testing environment, rich accounts with Ethereum (ETH) are available in the test/local/rich-wallets.json file in the web3-plugin-zksync repository.

The wallet object is created using the private key of a rich account, which has funds available for testing. This wallet is capable of interacting with ZKsync’s L2 environment.

Step 2: Querying Balances

With the wallet set up, let’s start by querying balances on both Layer 1 and Layer 2. This includes querying the ETH balance and ERC-20 token balances like DAI.

Step 3: Depositing Funds from L1 to L2

Next, we’ll demonstrate how to deposit ETH and ERC-20 tokens from Ethereum Layer 1 into ZKsync Layer 2. This process is essential for moving assets into the Layer 2 ecosystem, where they can be used with lower fees and faster transactions.

In this example, we deposit 2 ETH from L1 to L2. The waitFinalize() function waits for the deposit to be confirmed on both layers.

Deposit ERC-20 Tokens

Similarly, we can deposit 50,000 units of the DAI token from Layer 1 to Layer 2. The approveERC20 option ensures that the ERC-20 token is approved for transfer before the deposit occurs.

Step 4: Withdrawing Funds from L2 to L1

To move assets back from ZKsync Layer 2 to Ethereum Layer 1, you can initiate a withdrawal. This operation is slower than a deposit due to the finality requirements on L1, but it’s a straightforward process.

Withdraw ETH

We’re withdrawing 2 ETH back from L2 to L1. The transaction hash is logged, and the process finalizes with waitFinalize().

Withdraw ERC-20 Tokens

Similarly, we withdraw 100 units of the DAI token from ZKsync Layer 2 to Ethereum Layer 1.

Step 5: Transferring Funds on Layer 2

Layer 2 allows for much faster and cheaper transactions. Here’s how you can transfer assets between accounts on ZKsync Layer 2.

We transfer 1 ETH from the wallet to a new, randomly generated recipient account. The transaction is fast and cost-effective on Layer 2.

Transfer ERC-20 Tokens

Similarly, we transfer 100 DAI tokens between Layer 2 accounts.

Step 6: Sending and Signing Transactions

Finally, we demonstrate how to create, sign, and send custom transactions using the wallet

Here, we prepare a transaction, sign it using the wallet’s private key, and send it to the network. The transaction hash is returned, and we can track the status using this hash.

Summary

In this article, we’ve covered a variety of wallet operations on ZKsync using the Web3.js plugin:

  1. Setting up the wallet: We initialized a wallet from a private key.
  2. Querying balances: We retrieved balances on both L1 and L2.
  3. Depositing and withdrawing: We deposited and withdrew ETH and ERC-20 tokens between L1 and L2.
  4. Transferring assets: We transferred ETH and DAI between accounts on Layer 2.
  5. Sending and signing transactions: We created, signed, and sent custom transactions.

Understanding these core wallet operations allows you to build robust applications on ZKsync. Stay tuned for more advanced examples in future articles!

In the next article, we will explore how to interact with smart accounts on ZKsync!

Here are the essential links to help you get started:

  • Web3.js Plugin for ZKsync: You can find the official plugin repository here.
  • Examples for Using the Plugin: I've also prepared a dedicated repository with all the code examples from this tutorial and more, which you can access here.
  • ZKsync Web3.js Documentation: For more detailed information and resources, check out the official documentation here.

Read more