Détail du package

zksync-web3

zksync-sdk245.7kMITobsolète0.17.1

This package has been deprecated in favor of zksync-ethers@5.0.0

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

zksync, web3, ethereum, layer2

readme

🚀 zksync-web3 JavaScript SDK 🚀

In order to provide easy access to all the features of zkSync Era, the zksync-web3 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-web3 ( 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 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-web3 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-web3
yarn add ethers@5 # ethers is a peer dependency of zksync-web3

📝 Examples

Connect to the zkSync Era network:

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

const provider = new Provider("https://testnet.era.zksync.dev"); // zkSync Era testnet (L2)
const ethProvider = ethers.getDefaultProvider("goerli"); // goerli 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 goerli 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.utils.parseEther("1.0"),
});

Deposit funds

Transfer funds from L1 to L2 network.

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

Withdraw funds

Transfer funds from L2 to L1 network.

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

🤝 Contributing

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

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

changelog

Changelog

0.17.1 (2023-12-06)

Bug Fixes

  • return normalized bytecode in normalizeBytecode function (e74798f)

0.17.0 (2023-12-06)

Features

  • add Provider.getBytecodeByHash and Provider.getRawBlockTransactions (125a092)

0.16.0 (2023-11-01)

Features

  • add Provider.estimateFee method (e4466d3)
  • provide account and contract deployment using CREATE2 opcode (97a1fc2)
  • add AdapterL2.getDeploymentNonce (9ad8265)
  • add INonceHolder ABI and typechain (7433697)

Bug Fixes

  • resolved wrong types in types.Fee (8f1bf71)

0.15.5 (2023-10-02)

Bug Fixes

  • handle errors from wETHL1 and wETHL2 bridges (a3cb054))

Features

  • make input optional in create2Address (8745cfd)

0.15.4 (2023-07-25)

Bug Fixes

  • allow null for txIndexInL1Batch in formatter

0.15.3 (2023-07-25)

Bug Fixes

  • Fix getting receipt for transactions rejected in state keeper
  • make new fields optional in SDK

0.15.2 (2023-07-06)

Features

  • Integrate WETH bridge into server & SDK
  • add tx_index_in_l1_batch field to L2ToL1Log
  • add gas_per_pubdata to zks_getTransactionDetails

0.15.1 (2023-04-24)

Bug Fixes

  • add coefficient to gas limit + method for full fee estimation

0.15.0 (2023-04-20)

⚠ BREAKING CHANGES

  • Implement WETH bridge, support custom bridge in sdk, bootloader gas calculation fix

Features

  • Implement WETH bridge, support custom bridge in sdk, bootloader gas calculation fix

Bug Fixes

  • Fix getSignInput when gas parameters are 0

0.14.4 (2023-04-13)

Features

  • add getL1BatchDetails method to js SDK
  • extend BlockDetails type to include l1BatchNumber