Getting Started

Getting started with zksync2-java

This guide is designed to help you quickly get up and running with the SDK, enabling you to interact with the ZKSync Era blockchain efficiently.

Understanding key components

Before delving into code examples, it's useful to familiarize yourself with the primary objects provided by the SDK and their responsibilities:

  • ZkSync: This class provides the connection to the ZKSync Era blockchain. It allows you to query blockchain states, such as account details, block or transaction information, and event logs. It also facilitates read-only interactions with the blockchain through calls and enables writing to the blockchain by sending transactions.
  • Wallet: This object encapsulates all operations that interact with a user account. It manages the account's private key, which is used to sign various types of payloads, and offers straightforward methods for executing common blockchain functions.
Ensure you have installed the zksync2-java SDK. If you still need to do so, please refer to the installation guide for detailed instructions.

Examples

Connect to the ZKsync Era network:

import io.zksync.protocol.ZkSync;
import org.web3j.protocol.http.HttpService;

public class Main {
    public static void main(String ...args) {
        ZkSync zksync = ZkSync.build(new HttpService("http://127.0.0.1:3050"));
    }
}

Get the latest block number:

BigInteger blockNumber = zksync.ethBlockNumber().send().getBlockNumber();
;
ZksBlock block = zksync.zksGetBlockByHash("0xb472c070c9e121ba42702f6c322b7b266e287a4d8b5fa426ed265b105430c397", true).send().getBlock();
ZkTransaction transaction = zksync.zksGetTransactionByHash("0x9af27afed9a4dd018c0625ea1368afb8ba08e4cfb69b3e76dfb8521c8a87ecfc").send().getResult();

Made with ❤️ by the ZKsync Community