How to Get zkSync Token Balances — An Easy Guide for Layer 2 Balance Management

NOWNodes
5 min readMar 26, 2024
How to Get zkSync Token Balances

If you’re an active web3 developer familiar with using Ethereum L2 solutions, keeping all your Layer 2 token balances organized might be pretty challenging. However, using zkSync Era is a perfect way to solve this problem. How? Keep reading to get the answer!

For Web3 developers navigating the evolving landscape of Layer 2 solutions, zkSync stands out for its promise of scalability and lower transaction costs. Therefore, understanding how to interact with zkSync to check balances is crucial. This post guides you through various methods to check zkSync balances, emphasizing the most efficient and accurate approaches.

zkSync Check Balance Methods

Several methods are available for checking zkSync network token balances, each catering to different needs:

  • zkSync CLI Wallet Commands:
  • The command-line interface offers a straightforward way to interact with zkSync. Using the wallet balance command, you can quickly view the ETH balance of a specified address on zkSync L2.
  • zkSync JSON-RPC API Method:
  • For a more integrated solution, the zkSync JSON-RPC API provides the zks_getAllAccountBalances method. It returns all token balances for a given account, ideal for applications needing comprehensive account data.
  • Ethereum JSON-RPC API Method:
  • eth_getBalance Borrowed from Ethereum’s JSON-RPC API, this method can also be used within zkSync to check an account’s ETH balance. It’s a familiar choice for those already versed in Ethereum’s API.

zkSync CLI Wallet Commands

To begin with, the zkSync CLI (Command Line Interface) is a powerful tool that facilitates direct interaction with the zkSync network from the command line. It is designed for developers who prefer a straightforward, scriptable interface for performing various operations, including checking balances.

How it works: To use the CLI for checking balances, you first need to install the zkSync CLI package. You can execute commands directly in your terminal to interact with zkSync. The command for checking a wallet’s balance is structured as follows:

npx zksync-cli wallet balance --address <your-address>

Replace <your-address> with the actual zkSync address you wish to query.

The command returns the balance of the specified address in a human-readable format, making it convenient for quick checks or integration into scripts and automation tools.

Use case: This method is ideal for developers who need to automate balance checks or incorporate them into deployment scripts, backend services, or other automated workflows.

Access a zkSync Node via NOWNodes to Check L2 Tokens Balances

Here’s one thing you need to know before using next two methods involving JSON-RPC API. To make requests to the zkSync node to get account balances you need to connect to it using an API key that you can get easily by signing up at nownodes.io and selecting one of our subscription plans (including a free one).

NOWNodes is a Blockchain-as-a-Service solution that provides access to 100+ blockchains, including the zkSync mainnet network. NOWNodes offers a convenient gateway to access zkSync node data without the overhead of running your own node. By connecting to NOWNodes, developers can easily query L2 token balances, enhancing efficiency and reducing development time.

With the help of NOWNodes, you can easily connect a zkSync full node to not only check your token balances but also to get access to all the great functionality of the zkSync network. Here’s a simple guide to get you started, using our API keys and comprehensive documentation:

  1. Create an account on the NOWNodes website and get your API key.
  2. If you select a free plan make sure to add a zkSync node to the list of 5 nodes available.
  3. Visit our Postman and check out the documentation for zkSync (or any other Node you wish).
  4. Now you are all set to make requests to our nodes, just put your API key to your Postman.

zkSync JSON-RPC API Method

The zks_getAllAccountBalances RPC method is part of the zkSync JSON-RPC API, designed for programmatic access to zkSync’s Layer 2 functionalities. This API follows the standard JSON-RPC protocol, allowing developers to interact with zkSync using JSON objects over HTTP or WebSocket connections.

How it works: To use this method, you make a POST request to the zkSync JSON-RPC endpoint with a JSON object containing the method name (zks_getAllAccountBalances), the parameters (the address whose balances you want to check), and an ID to identify the request. An example request looks like this:

curl --request POST \
--url https://zksync.nownodes.io/ \
--header 'Content-Type: application/json' \
--header 'api-key: <your API key>' \
--data '{
"jsonrpc": "2.0",
"id": 1,
"method": "zks_getAllAccountBalances",
"params": [
"<your-address>"
]
}'

Ensure <your-address> is replaced with the target account. You can use our endpoint <zksync.nownodes.io> that’s already integrated into the request.

The response will include all the token balances associated with the specified address, allowing for a comprehensive overview of the account’s holdings on zkSync.

Use case: This method is suitable for applications that require detailed account information, such as wallets, dashboards, or financial tracking tools, where displaying a complete picture of an account’s assets is crucial.

Ethereum JSON-RPC API Method

As zkSync is an Ethereum L2 (Level 2) typically used method for Ethereum, eth_getBalance can also be employed in zkSync. It’s a testament to zkSync’s compatibility with Ethereum’s ecosystem, allowing developers to use familiar tools and methods.

How it works: Similarly to the zkSync JSON-RPC API, you make a POST request with a JSON object specifying the eth_getBalance method, the address to check, and the desired block number (often “latest”). Here’s a sample request:

curl --location 'https://zksync.nownodes.io/<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"<Your_address>",
"Latest"
],
"id": 1
}'

Don’t forget to replace placeholders as mentioned in the previous section.

The response will be the balance of the specified address in Wei (Ethereum’s smallest denomination).

Use case: This method is particularly useful for developers already familiar with Ethereum’s API, as it allows them to apply their existing knowledge and tools to zkSync. It’s ideal for simple balance checks, especially when only the ETH balance is required. The difference between the last two methods that involve JSON-RPC API is that zks_getAllAccountBalances provides more sophisticated data than eth_getBalance.

Conclusion

Nowadays, in the dynamic world of Web3 development, efficiency and accuracy are paramount. Leveraging services like NOWNodes for accessing zkSync node data presents a seamless way to retrieve L2 token balances. It eliminates the need for maintaining individual nodes, allowing developers to focus on building innovative and scalable applications. By integrating NOWNodes into your development workflow, you benefit from reliable, up-to-date zkSync data, ensuring your applications remain at the forefront of L2 technology.

In conclusion, each method described offers unique advantages, from the simplicity of CLI commands to comprehensive data access via JSON-RPC API. Depending on your project’s requirements and your familiarity with blockchain technologies, you can choose the most suitable approach to interact with zkSync. Whether you’re building user-facing applications or backend systems, understanding these methods enhances your ability to work effectively with the zkSync era, pushing the boundaries of what’s possible in the Layer 2 zkSync ecosystem.

We wish to know whether this guide was helpful. Please, leave your feedback in the comments in our Telegram Builders Community or just hit our DMs at X/Twitter.

Connect to the zkSync ecosystem #NOW!

--

--

NOWNodes

99+ Blockchain Networks | RPC Nodes | API & Explorers at nownodes.io