Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

zksync-ethers

zksync-sdk120.2kMIT6.15.4TypeScript support: included

A Web3 library for interacting with the ZkSync Layer 2 scaling solution.

zksync, web3, ethereum, layer2

readme

🚀 zksync-ethers JavaScript SDK 🚀

License License: Apache 2.0 Contributor Covenant Contributions Welcome X (formerly Twitter) Follow Code Style: Google

ZKsync Era Logo

In order to provide easy access to all the features of ZKsync Era, the zksync-ethers JavaScript SDK was created, which is made in a way that has an interface very similar to those of ethers. In fact, ethers is a peer dependency of our library and most of the objects exported by zksync-ethers ( e.g. Wallet, Provider etc.) inherit from the corresponding ethers objects and override only the fields that need to be changed.

While most of the existing SDKs should work out of the box, deploying smart contracts or using unique ZKsync Era features, like account abstraction, requires providing additional fields to those that Ethereum transactions have by default.

The library is made in such a way that after replacing ethers with zksync-ethers most client apps will work out of box.

🔗 For a detailed walkthrough, refer to the official documentation.

📌 Overview

To begin, it is useful to have a basic understanding of the types of objects available and what they are responsible for, at a high level:

  • Provider provides connection to the ZKsync Era blockchain, which allows querying the blockchain state, such as account, block or transaction details, querying event logs or evaluating read-only code using call. Additionally, the client facilitates writing to the blockchain by sending transactions.
  • Wallet wraps all operations that interact with an account. An account generally has a private key, which can be used to sign a variety of types of payloads. It provides easy usage of the most common features.

🛠 Prerequisites

📥 Installation & Setup

yarn add zksync-ethers
yarn add ethers@6 # ethers is a peer dependency of zksync-ethers

📝 Examples

The complete examples with various use cases are available here.

Connect to the ZKsync Era network:

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

const provider = Provider.getDefaultProvider(types.Network.Sepolia); // ZKsync Era testnet (L2)
const ethProvider = ethers.getDefaultProvider("sepolia"); // Sepolia testnet (L1)

Get the latest block number

const blockNumber = await provider.getBlockNumber();

Get the latest block

const block = await provider.getBlock("latest");

Create a wallet

const PRIVATE_KEY = process.env.PRIVATE_KEY;
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider);

Check account balances

const ethBalance = await wallet.getBalance(); // balance on ZKsync Era network

const ethBalanceL1 = await wallet.getBalanceL1(); // balance on Sepolia network

Transfer funds

Transfer funds among accounts on L2 network.

const receiver = Wallet.createRandom();

const transfer = await wallet.transfer({
    to: receiver,
    token: utils.ETH_ADDRESS,
    amount: ethers.parseEther("1.0"),
});

Deposit funds

Transfer funds from L1 to L2 network.

const deposit = await wallet.deposit({
    token: utils.ETH_ADDRESS,
    amount: ethers.parseEther("1.0"),
});

Withdraw funds

Transfer funds from L2 to L1 network.

const withdrawal = await wallet.withdraw({
    token: utils.ETH_ADDRESS,
    amount: ethers.parseEther("1.0"),
});

🤖 Running tests

In order to run test you need to run local-setup on your machine. For running tests, use:

yarn test:wait # waits for local-setup to be ready
yarn test:prepare # prepares the environment (deploys token on both layers, etc.)
yarn test

For running test coverage, use:

yarn test:coverage

🤝 Contributing

We welcome contributions from the community! If you're interested in contributing to the zksync-ethers JavaScript SDK, please take a look at our CONTRIBUTING.md for guidelines and details on the process.

Thank you for making zksync-ethers JavaScript SDK better! 🙌

changelog

6.15.4 (2025-01-17)

Bug Fixes

  • populateTransaction estimate fee only if necessary (8c85348)

6.15.3 (2024-12-06)

Bug Fixes

  • allow null for totalDifficulty (4681d5b)

6.15.2 (2024-12-02)

Bug Fixes

  • link to docs in readme (f93470a)
  • use correct tag for populating nonce (283ba7b)

6.15.1 (2024-11-21)

Bug Fixes

  • create2 and create2Account deploy with Contract2Factory contract (9cae26c)

6.15.0 (2024-11-06)

Features

  • provider: consider 0.0.0.0 url as localhost (563773b)

6.14.3 (2024-11-05)

Bug Fixes

  • include custom signature in estimateGas calls (703690d)

6.14.2 (2024-11-01)

Bug Fixes

  • contract: fix waitForDeployment missing startBlock (8ce644f)

6.14.1 (2024-10-30)

Bug Fixes

  • contract: deploy not returning block nubmer (7167496)

6.14.0 (2024-10-11)

Features

  • adapter: add l1TokenAddress method (4243db6)

6.13.1 (2024-10-07)

Bug Fixes

  • provider: estimateDefaultBridgeDepositL2Gas use ETH_ADDRESS_IN_CONTRACTS for base token (4116460)

6.13.0 (2024-09-26)

Bug Fixes

  • tyoes: add missing field for FeeParams (456c73a)

Features

  • utilize estimated gasPerPubData (9f98f75)

6.13.0 (2024-09-26)

Bug Fixes

  • tyoes: add missing field for FeeParams (456c73a)

Features

  • utilize estimated gasPerPubData (9f98f75)

6.12.1 (2024-09-06)

Bug Fixes

  • provider: fix getRpcError issue with chai matchers (a66a876)

6.12.0 (2024-08-27)

Bug Fixes

  • signer: fix populateFeeData provider check (b89429b)

Features

  • getTransfer and getWithdraw return EIP712 type (f92d344)
  • provider: add estimateDefaultBridgeDepositL2Gas and estimateCustomBridgeDepositL2Gas (1ffd39a)
  • provider: add getL1ChainId (0f8d58f)
  • signer: add finalizeWithdrawalParams (30fb918)
  • signer: add VoidSigner for l2 operations (358f733)
  • types: add timeout parameter to transaction wait() (2e55d0b)
  • wallet: add getFinalizeWithdrawalParams (87f6081)

6.11.2 (2024-08-09)

Bug Fixes

  • signer: sendTransaction populate from (a9db811)

6.11.1 (2024-08-01)

Bug Fixes

  • provider: switch localhost to ip (dc284de)

6.11.0 (2024-07-24)

Features

  • prioratize EIP712 and utilize zks_estimateFee (4c58829)
  • prioratize EIP712 and utilize zks_estimateFee (527a74f)
  • provider: add support for l1_committed block tag (011db1f)

6.10.0 (2024-07-09)

Bug Fixes

  • make weth bridge addresses optional (ad3cff8)

Features

  • types: add l1_committed type into type BlockTag (89e7f69)

6.9.0 (2024-06-19)

Bug Fixes

  • adapter: fix support for custom bridge (deb01a4)
  • adapter: return IL2SharedBridge (bd67567)

Features

  • align types and RPC endpoints with version v24.7.0 of a node (f7ef615)
  • provider: add Provider.getConfirmedTokens() method (887dc00)
  • provider: add Provider.getFeeParams() method (f5b9381)
  • provider: add Provider.getProtocolVersion() method (864c9d0)
  • provider: add Provider.sendRawTransactionWithDetailedOutput() method (d41a75d)

BREAKING CHANGES

  • Types and RPC endpoints are aligned with version v24.7.0 of a node.

6.8.0 (2024-06-06)

Bug Fixes

  • adapter: fix correct recommendedL1GasLimit in getFullRequiredDepositFee (8e92981)
  • provider: getTransferTx and getWithdrawTx non-eth-based chain (fc19131)
  • types: change types in FullDepositFee from BigInt to bigint (8707879)
  • types: ensure Block.l1BatchTimestamp has accurate value (cbbb84e)
  • types: update RawBlockTransaction (183fa57)

Features

  • provide support for Bridgehub (acfeadd)
  • provider: getTransactionReceipt() returns null if transaction is not mined or found (17f131d)

BREAKING CHANGES

  • provider: Provider.getTransactionReceipt() and BrowserProvider.getTransactionReceipt() return null if transaction is not mined, discarded or not found.

6.7.1 (2024-05-09)

Bug Fixes

  • export typechain at the top level of the package (d125eea)
  • getPriorityOpResponse correctly assigns waitL1Commit to l2Response object (0cfedc9)
  • signer: make Signer compatible when created using BrowserProvider.getSigner() (e62c98d)

6.7.0 (2024-04-04)

Bug Fixes

  • make Block.sealFields optional (5825404)
  • move abi folder at top level (2759e5d)
  • provider: drop support for the Goerli network (a395dae)
  • signer: add support for zks RPC methods in Signer (c63faea)

Features

  • add SmartAccount in order to provide better support for AA (d31a9b1)

BREAKING CHANGES

  • provider: The function Provider.getDefaultProvider() no longer supports types.Network.Goerli.

6.6.0 (2024-03-18)

Bug Fixes

  • provider: create prefetched txs correctly in Block object (8ef06f2), closes #75
  • provider: disable caching for local networks (26d6f63)
  • utils: add padding in apply and undo alias in order to return 20 bytes long address (a5a83b6)

Features

  • extract all files from the src folder into the build folder (e5f2209)
  • provider: add support for era test node (7be6040)
  • provider: remove support for the ZKSYNC_WEB3_API_URL environment variable (f5b8529)

Reverts

  • adapters: make AdapterL1.getFullRequiredDepositFee work with overrides (0ed9389)

BREAKING CHANGES

  • provider: Remove support for the ZKSYNC_WEB3_API_URL environment variable from the Provider.getDefaultProvider() to make it compatible with browser integration.
  • Previously, the build folder contained the src folder along with all the js and d.ts files. This setup resulted in a poor developer experience, as developers were required to use the src prefix in their imports (e.g., 'zksync-ethers/src/types'). Now, all files from the src folder are extracted, eliminating the need to specify the src prefix in the path.
  • adapters: Remove support from AdapterL1.getFullRequiredDepositFee for considering overrides.from as the initiator of the operation. This functionality was previously used to calculate the full deposit fee for accounts whose private key is unknown. However, this feature is no longer necessary because L1VoidSigner.getFullRequiredDepositFee is specifically designed to handle such cases.

6.5.0 (2024-03-05)

Features

  • adapters: make AdapterL1.getFullRequiredDepositFee work with overrides (9feb1e1)
  • signer: add L2VoidSigner and L1VoidSigner (84008bb)

6.4.0 (2024-02-29)

Bug Fixes

  • adapter: make l2GasLimit optional parameter in requestExecute (a5fb96c)
  • wallet: populate transaction before signing (72a0585)

Features

  • utils: add toJSON function for converting object to JSON string (8ef0299)

6.3.0 (2024-02-08)

Features

  • provider,wallet: fix typo in paymaster paramter name (7f71a9d)

BREAKING CHANGES

  • provider,wallet: Rename function paramter paymasterParamas to paymasterParams in Provider.getTransferTx(), Provider.estimateGasTransfer(), Provider.getWithdrawTx(), Provider.estimateGasWithdraw(), Wallet.transfer() and Wallet.withdraw() methods.

6.2.0 (2024-02-07)

Features

  • provider: getDefaultProvider() connects to local network by default (ae2146d)
  • provider: add getProof method for fetching storage proofs (d364000)
  • wallet,provider: add paymaster support for transfer and withdrawal tx (5cab446)

6.1.0 (2024-01-24)

Bug Fixes

  • utils: pad zeroes to left instead to right in getHashedL2ToL1Msg() (41d0ef3)
  • utils: pad zeroes to left instead to right in getSignature() (c252039)

Features

  • remove Provider.getConfirmedTokens method (7d5e0fe)
  • signer: EIP712Signer.getSignInput() now returns zero values as defaults instead of null (710b08c)
  • signer: add EIP712Signer.getDomain() method (907fee8)
  • types: add FinalizeWithdrawalParams interface (3220dee)
  • utils: add EIP712_TYPES (e148367)
  • utils: remove deprecated IPaymasterFlow ABI (56f8094)

BREAKING CHANGES

  • utils: remove the deprecated utils.IPaymasterFlow in favor of utils.PAYMASTER_FLOW_ABI.
  • signer: EIP712Signer.getSignInput() now returns zero values instead of null as default values. In the case of a number, the zero value is 0. In the case of an ethers.BigNumberish, the zero value is 0n. In the case of a string, the zero value is 0x0.
  • This method will be removed from JSON RPC API.

Changelog

6.0.0 (2023-12-08)

Features

  • setup zksync-ethers project (05bdb16)