Skip to main content

zkSync2 python client sdk

Project description

🚀 zksync2-python Python SDK 🚀

Era Logo

In order to provide easy access to all the features of zkSync Era, the zksync2-python Python SDK was created, which is made in a way that has an interface very similar to those of web3py. In fact, web3py is a peer dependency of our library and most of the objects exported by zksync2-python inherit from the corresponding web3py 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 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 web3py with zksync2-python 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 Python SDK, please take a look at our CONTRIBUTING.md for guidelines and details on the process.

Thank you for making zksync2-python JavaScript SDK better! 🙌

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zksync2-1.0.0.tar.gz (97.9 kB view details)

Uploaded Source

Built Distribution

zksync2-1.0.0-py3-none-any.whl (52.3 kB view details)

Uploaded Python 3

File details

Details for the file zksync2-1.0.0.tar.gz.

File metadata

  • Download URL: zksync2-1.0.0.tar.gz
  • Upload date:
  • Size: 97.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for zksync2-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1b022bbe4ca5085caef2596c0d61cb8dd0cb0555e30e59e4b1d8a175dabd7d12
MD5 a6b7309d3a0fbc239db2078dfeb2fdbf
BLAKE2b-256 0b76fdd0bb3c7b5dce67d2ed2db2032847fdc256a7631ced5491829ce407a049

See more details on using hashes here.

File details

Details for the file zksync2-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: zksync2-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 52.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for zksync2-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5e8d2007eb6e67efac697e9c474cad2ab36bd924f998eff3068f76e41f489d0
MD5 8425eb9f651b99c509fc38f968ef7105
BLAKE2b-256 9ba9f495a21f572f8e18a2df8312119ae4c78955eb44bee8bccd04176c710b7b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page