Package: zksync2
🚀 zksync2 Python SDK 🚀In order to provide easy access to all the features of zkSync Era, thezksync2Python SDK was created,
which is made in a way that has an interface very similar to those ofweb3.py. In
fact,web3.pyis a peer dependency of our library and most of the objects exported byzksync2inherit from the correspondingweb3.pyobjects 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 replacingweb3.pywithzksync2most client apps will work out of
box.🔗 For a detailed walkthrough, refer to theofficial documentation.📌 OverviewTo 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:Providerprovides 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.Walletwraps 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.🛠 PrerequisitesToolRequiredpython3.8, 3.9, 3.10package managerpip📥 Installation & Setuppip install zksync2📝 ExamplesThe complete examples with various use cases are availablehere.Connect to the zkSync Era network:fromzksync2.module.module_builderimportZkSyncBuilder...web3=ZkSyncBuilder.build("ZKSYNC_NET_URL")AccountAccount encapsulate private key and, frequently based on it, the unique user identifier in the network.This unique identifier also mean by wallet address.Account constructionZkSync2 Python SDK account is compatible witheth_accountpackage
In most cases user has its private key and gets account instance by using it.frometh_accountimportAccountfrometh_account.signers.localimportLocalAccount...account:LocalAccount=Account.from_key("PRIVATE_KEY")The base property that is used directly of account is:Account.addressSignerSigner 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 validationSigner constructionzkSync2 already has implementation of signer. For constructing the instance it needs only account and chain_idExample:fromzksync2.signer.eth_signerimportPrivateKeyEthSignerfrometh_accountimportAccountfromzksync2.module.module_builderimportZkSyncBuilderaccount=Account.from_key("PRIVATE_KEY")zksync_web3=ZkSyncBuilder.build("ZKSYNC_NETWORK_URL")...chain_id=zksync_web3.zksync.chain_idsigner=PrivateKeyEthSigner(account,chain_id)Create a walletPRIVATE_KEY=HexStr("<PRIATE_KEY>")account:LocalAccount=Account.from_key(env_key.key)wallet=Wallet(zk_sync,eth_web3,account)Check account balanceseth_balance=wallet.getBalance()# balance on zkSync Era networketh_balance_l1=wallet.getBalanceL1()# balance on goerli networkTransfer fundsTransfer funds among accounts on L2 network.receiver=account.create().addresstransfer=wallet.transfer(TransferTransaction(to=Web3.to_checksum_address(receiver),token_address=ADDRESS_DEFAULT,amount=Web3.to_wei(0.1,"ether")))Deposit fundsTransfer funds from L1 to L2 network.transfer=wallet.deposit(DepositTransaction(token_address=ADDRESS_DEFAULT,amount=Web3.to_wei(0.1,"ether")))Withdraw fundsTransfer funds from L2 to L1 network.transfer=wallet.deposit(WithdrawTransaction(token_address=ADDRESS_DEFAULT,amount=Web3.to_wei(0.1,"ether")))🤖Running TestsIn order to run test you need to run local-setup on your machine. For running tests, use:make waitmake prepare-testsmake run-tests🤝 ContributingWe welcome contributions from the community! If you're interested in contributing to thezksync2Python SDK,
please take a look at ourCONTRIBUTING.mdfor guidelines and details on the process.Thank you for makingzksync2Python SDK better! 🙌