Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add python examples #10

Merged
merged 9 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

![Era Logo](https://github.com/matter-labs/era-contracts/raw/main/eraLogo.svg)

Examples on how to interact with zkSync Era network using SDKs for different languages.
Examples on how to interact with zkSync Era network using different SDKs.

- [Go examples](go)
- [JavaScript examples](js)
- [Go](go)
- [JavaScript](js)
- [Python](python)

## 🤝 Contributing

Expand Down
2 changes: 1 addition & 1 deletion go/17_deploy_create_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
var (
PrivateKey = os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider = "https://sepolia.era.zksync.dev"
TokenAddress = "0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150" // Crown token which can be minted for free
TokenAddress = "0xA70dF8446A6AeA0017D60e97e816e141aa28759b" // Crown token which can be minted for free
)

// Connect to zkSync network
Expand Down
2 changes: 1 addition & 1 deletion go/18_deploy_create2_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
var (
PrivateKey = os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider = "https://sepolia.era.zksync.dev"
TokenAddress = "0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150" // Crown token which can be minted for free
TokenAddress = "0xA70dF8446A6AeA0017D60e97e816e141aa28759b" // Crown token which can be minted for free
)

// Connect to zkSync network
Expand Down
2 changes: 1 addition & 1 deletion go/19_use_paymaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
var (
PrivateKey = os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider = "https://sepolia.era.zksync.dev"
TokenAddress = common.HexToAddress("0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150") // Crown token which can be minted for free
TokenAddress = common.HexToAddress("0xA70dF8446A6AeA0017D60e97e816e141aa28759b") // Crown token which can be minted for free
PaymasterAddress = common.HexToAddress("0x57F48f0d845E0ed7C9Bf066cEbFF64FbeBE6AFEF") // Paymaster for Crown token
)

Expand Down
2 changes: 1 addition & 1 deletion js/src/17_deploy_create_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const provider = Provider.getDefaultProvider(types.Network.Sepolia);
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const wallet = new Wallet(PRIVATE_KEY, provider);

const tokenAddress = "0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150";
const tokenAddress = "0xA70dF8446A6AeA0017D60e97e816e141aa28759b";

async function main() {
const conf = require("../../solidity/custom_paymaster/paymaster/build/Paymaster.json");
Expand Down
2 changes: 1 addition & 1 deletion js/src/18_deploy_create2_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const provider = Provider.getDefaultProvider(types.Network.Sepolia);
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const wallet = new Wallet(PRIVATE_KEY, provider);

const tokenAddress = "0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150";
const tokenAddress = "0xA70dF8446A6AeA0017D60e97e816e141aa28759b";

async function main() {
const conf = require("../../solidity/custom_paymaster/paymaster/build/Paymaster.json");
Expand Down
2 changes: 1 addition & 1 deletion js/src/19_use_paymaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ethProvider = ethers.getDefaultProvider("sepolia");
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider);

const tokenAddress = "0x765F5AF819D818a8e8ee6ff63D8d0e8056DBE150"; // Crown token which can be minted for free
const tokenAddress = "0xA70dF8446A6AeA0017D60e97e816e141aa28759b"; // Crown token which can be minted for free
const paymasterAddress = "0x57F48f0d845E0ed7C9Bf066cEbFF64FbeBE6AFEF"; // Paymaster for Crown token
const token = new ethers.Interface(
require("../../solidity/custom_paymaster/token/build/Token.json").abi,
Expand Down
160 changes: 160 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
51 changes: 51 additions & 0 deletions python/01_deposit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os

from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexStr
from web3 import Web3

from zksync2.account.wallet import Wallet
from zksync2.core.types import Token, DepositTransaction
from zksync2.manage_contracts.utils import zksync_abi_default
from zksync2.module.module_builder import ZkSyncBuilder

if __name__ == "__main__":
# Get the private key from OS environment variables
PRIVATE_KEY = bytes.fromhex(os.environ.get("PRIVATE_KEY"))

# Set a provider
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
ETH_PROVIDER = "https://rpc.ankr.com/eth_sepolia"

# Connect to zkSync network
zk_web3 = ZkSyncBuilder.build(ZKSYNC_PROVIDER)

# Connect to Ethereum network
eth_web3 = Web3(Web3.HTTPProvider(ETH_PROVIDER))

# Get account object by providing from private key
account: LocalAccount = Account.from_key(PRIVATE_KEY)

# Create Ethereum provider
wallet = Wallet(zk_web3, eth_web3, account)

#ZkSync contract
zksync_contract = eth_web3.eth.contract(Web3.to_checksum_address(zk_web3.zksync.main_contract_address),
abi=zksync_abi_default())

amount = 0.1
l2_balance_before = wallet.get_balance()

tx_hash = wallet.deposit(DepositTransaction(token=Token.create_eth().l1_address,
amount=Web3.to_wei(amount, "ether"),
to=wallet.address))

l1_tx_receipt = eth_web3.eth.wait_for_transaction_receipt(tx_hash)
l2_hash = zk_web3.zksync.get_l2_hash_from_priority_op(l1_tx_receipt, zksync_contract)
l2_tx_receipt = zk_web3.zksync.wait_for_transaction_receipt(transaction_hash=l2_hash,
timeout=360,
poll_latency=10)

print(f"L1 transaction: {l1_tx_receipt}")
print(f"L2 transaction: {l2_tx_receipt}")
47 changes: 47 additions & 0 deletions python/02_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os

from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexStr
from web3 import Web3

from zksync2.account.wallet import Wallet
from zksync2.core.types import TransferTransaction
from zksync2.module.module_builder import ZkSyncBuilder

if __name__ == "__main__":

# Get the private key from OS environment variables
PRIVATE_KEY = bytes.fromhex(os.environ.get("PRIVATE_KEY"))

# Set a provider
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
ETH_PROVIDER = "https://rpc.ankr.com/eth_sepolia"

# Connect to zkSync network
zk_web3 = ZkSyncBuilder.build(ZKSYNC_PROVIDER)

# Connect to Ethereum network
eth_web3 = Web3(Web3.HTTPProvider(ETH_PROVIDER))

# Get account object by providing from private key
account: LocalAccount = Account.from_key(PRIVATE_KEY)

# Create Ethereum provider
wallet = Wallet(zk_web3, eth_web3, account)

# Show balance before ETH transfer
print(f"Balance before transfer : {wallet.get_l1_balance()} ETH")

# Perform the ETH transfer
tx_hash = wallet.transfer(TransferTransaction(
to=HexStr("0x81E9D85b65E9CC8618D85A1110e4b1DF63fA30d9"),
amount=Web3.to_wei(0.001, "ether"),

))
zk_web3.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)

# Show balance after ETH transfer
print(f"Balance after transfer : {wallet.get_balance()} ETH")
Loading
Loading