Skip to main content

🚀 zksync2 Python 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 zksync2 Python SDK was created, which is made in a way that has an interface very similar to those of web3.py. In fact, web3.py is a peer dependency of our library and most of the objects exported by zksync2 inherit from the corresponding web3.py objects and override only the fields that need to be changed.

While most of the existing SDKs functionalities 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 web3.py with zksync2 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

Tool Required
python 3.8, 3.9, 3.10
package manager pip

📥 Installation & Setup

pip install zksync2

📝 Examples

The complete examples with various use cases are available here.

Connect to the ZKsync Era network:

from zksync2.module.module_builder import ZkSyncBuilder
...
web3 = ZkSyncBuilder.build("ZKSYNC_NET_URL")

Account

Account encapsulate private key and, frequently based on it, the unique user identifier in the network.
This unique identifier also mean by wallet address.

Account construction

ZkSync2 Python SDK account is compatible with eth_account package In most cases user has its private key and gets account instance by using it.

from eth_account import Account
from eth_account.signers.local import LocalAccount
...
account: LocalAccount = Account.from_key("PRIVATE_KEY")

The base property that is used directly of account is: Account.address

Signer

Signer is used to generate signature of provided transaction based on your account(your private key)
This signature is added to the final EIP712 transaction for its validation

Signer construction

zkSync2 already has implementation of signer. For constructing the instance it needs only account and chain_id

Example:

from zksync2.signer.eth_signer import PrivateKeyEthSigner
from eth_account import Account
from zksync2.module.module_builder import ZkSyncBuilder


account = Account.from_key("PRIVATE_KEY")
zksync_web3 = ZkSyncBuilder.build("ZKSYNC_NETWORK_URL")
...
chain_id = zksync_web3.zksync.chain_id
signer = PrivateKeyEthSigner(account, chain_id)

Create a wallet

PRIVATE_KEY = HexStr("<PRIATE_KEY>")
account: LocalAccount = Account.from_key(env_key.key)
wallet = Wallet(zk_sync, eth_web3, account)

Check account balances

eth_balance = wallet.getBalance() # balance on ZKsync Era network

eth_balance_l1 = wallet.getBalanceL1() # balance on goerli network

Transfer funds

Transfer funds among accounts on L2 network.

receiver = account.create().address

transfer = wallet.transfer(
    TransferTransaction(to=Web3.to_checksum_address(receiver),
                        token_address=ADDRESS_DEFAULT,
                        amount=Web3.to_wei(0.1, "ether")))

Deposit funds

Transfer funds from L1 to L2 network.

transfer = wallet.deposit(
    DepositTransaction(token_address=ADDRESS_DEFAULT,
                        amount=Web3.to_wei(0.1, "ether")))

Withdraw funds

Transfer funds from L2 to L1 network.

transfer = wallet.deposit(
    WithdrawTransaction(token_address=ADDRESS_DEFAULT,
                        amount=Web3.to_wei(0.1, "ether")))

🤖Running Tests

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

make wait
make prepare-tests
make run-tests

🤝 Contributing

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

Thank you for making zksync2 Python SDK better! 🙌

Metadata

Release files for zksync2 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zksync2 2.0.0
File Size Uploaded
zksync2-2.0.0.tar.gz 162.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zksync2 2.0.0
File Interpreter ABI Platform
zksync2-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 237.2 kB

Release files / zksync2-2.0.0.tar.gz

Download URL zksync2-2.0.0.tar.gz
Size 162.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fb464fea439639eea801d5a0a7f255690839ed1beae1923b87c40e99fa5fa152
BLAKE2b-256 checksum
How to use checksums
ff9f6dde1573a5d68f34617369ccd655804b0f2773f25097b2c360b6d333c965
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.18

Release files / zksync2-2.0.0-py3-none-any.whl

Download URL zksync2-2.0.0-py3-none-any.whl
Size 75.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77eefad07b56914ad61b753d8004dcb0e72f75a0f85045d519ae62bd3d36f438
BLAKE2b-256 checksum
How to use checksums
057a306c8a4e9581001014b6b837efa9894f66960736e2cbdda1a18c0d935a39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.18

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page