V6

Getting Started

Explore zksync-ethers SDK version 6

This document provides an overview of the zksync-ethers SDK for interacting with ZKsync Era.

Key concepts

  • Provider: Provides a connection to the ZKsync Era blockchain, enabling querying of blockchain state (e.g., accounts, blocks, transactions) and sending transactions.
  • Wallet: Manages operations involving an account, typically using a private key for signing actions.

Installation

Install the zksync-ethers library:

npm install zksync-ethers ethers

Initialization

To initialize the provider to connect to the ZKsync Sepolia network:

import { Provider, types } from "zksync-ethers";

const provider = Provider.getDefaultProvider(types.Network.Sepolia);

Network information

To fetch details about the connected network and the current block number:

(async () => {
  const network = await provider.getNetwork();
  const blockNumber = await provider.getBlockNumber();
  console.log(`Network: ${JSON.stringify(network)}`);
  console.log(`Block number: ${blockNumber}`);
})();

Transaction details

To fetch transaction details:

(async () => {
  const transactionHash = "0xaa065e5a57e1f8470a6f258e2b6eee87c547eab066b8620ce7f3fd51405665e1";
  const result = await provider.getTransactionDetails(transactionHash);
  console.log(`Transaction Details: ${JSON.stringify(result)}`);
})();

Error handling

Wrap the main logic in a try-catch block to handle potential errors:

(async () => {
  try {
    // Your code here
  } catch (error) {
    console.error(`Error fetching transaction details from ZKsync: ${error}`);
  }
})();

Example code

Here is an example script demonstrating the usage of the SDK:

import { Provider, types } from "zksync-ethers";

async function main() {
  const provider = Provider.getDefaultProvider(types.Network.Sepolia);

  const network = await provider.getNetwork();
  const blockNumber = await provider.getBlockNumber();
  console.log(`Network: ${JSON.stringify(network)}`);
  console.log(`Block number: ${blockNumber}`);

  const transactionHash = "0xaa065e5a57e1f8470a6f258e2b6eee87c547eab066b8620ce7f3fd51405665e1";

  try {
    const result = await provider.getTransactionDetails(transactionHash);
    if (result) {
      console.log(`Transaction Details from ZKsync: ${JSON.stringify(result)}`);
      console.log(`ethCommitTxHash: ${result.ethCommitTxHash}`);
      console.log(`ethExecuteTxHash: ${result.ethExecuteTxHash}`);
      console.log(`ethProveTxHash: ${result.ethProveTxHash}`);
    } else {
      console.log(`Transaction with hash ${transactionHash} not found in ZKsync.`);
    }
  } catch (error) {
    console.error(`Error fetching transaction details from ZKsync: ${error}`);
  }
}

main()
  .then(() => console.log("Script executed successfully"))
  .catch((error) => console.error(`Error executing script: ${error}`));

Run the script

Use the following command to run the script:

npx ts-node <file-name>

Output

Example output when the script runs successfully:

Network: {"name":"unknown","chainId":"300"}
Block number: 2725282
Transaction Details from ZKsync: {"ethCommitTxHash":"0xfe921b3af6bf14d35d6c550f1a337f20a46997a36c24bae37c1b2d129ee3b4d6","ethExecuteTxHash":"0x08e42763d6ba1052d117174acbb708d9e015ae9246574cf9d9b06c001b31e750","ethProveTxHash":"0x49ab3b1a7cc72911492afe39f21d5f557abcb2769aa63841f658c719a7ec5ba2","fee":"0x1252b3c112d2e","gasPerPubdata":"0xc350","initiatorAddress":"0xb71ce978bf48e3e4669a7a0acb89850023fc3279","isL1Originated":false,"receivedAt":"2024-06-03T09:16:41.519Z","status":"verified"}
ethCommitTxHash: 0xfe921b3af6bf14d35d6c550f1a337f20a46997a36c24bae37c1b2d129ee3b4d6
ethExecuteTxHash: 0x08e42763d6ba1052d117174acbb708d9e015ae9246574cf9d9b06c001b31e750
ethProveTxHash: 0x49ab3b1a7cc72911492afe39f21d5f557abcb2769aa63841f658c719a7ec5ba2
Script executed successfully

Made with ❤️ by the ZKsync Community