-
Notifications
You must be signed in to change notification settings - Fork 5
/
07_bridge_withdraw.py
121 lines (97 loc) · 4.08 KB
/
07_bridge_withdraw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
from dotenv import load_dotenv
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexStr
from eth_utils import remove_0x_prefix
from hexbytes import HexBytes
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.types import TxReceipt
from zksync2.core.types import Token
from zksync2.module.module_builder import ZkSyncBuilder
from zksync2.provider.eth_provider import EthereumProvider
from zksync2.transaction.transaction_builders import TxWithdraw
from utils.read_wallets import ReadWallets
def withdraw(
zksync_provider: Web3, account: LocalAccount, amount: float
) -> HexBytes:
"""Withdraw from Layer 2 to Layer 1 on zkSync network
:param zksync_provider:
Instance of ZkSync provider
:param account:
From which ETH account the withdrawal will be made
:param amount:
How much would the withdrawal will contain
:return:
Hash of withdraw transaction on L2 network
"""
# Create withdrawal transaction
withdrawal = TxWithdraw(
web3=zksync_provider,
token=Token.create_eth(),
amount=Web3.to_wei(amount, "ether"),
gas_limit=0, # unknown
account=account,
)
# ZkSync transaction gas estimation
estimated_gas = zksync_provider.zksync.eth_estimate_gas(withdrawal.tx)
# Estimate gas transaction
tx = withdrawal.estimated_gas(estimated_gas)
# Sign the transaction
signed = account.sign_transaction(tx)
# Broadcast the transaction to the network
return zksync_provider.zksync.send_raw_transaction(signed.rawTransaction)
def finalize_withdraw(
zksync_provider: Web3, ethereum_provider: EthereumProvider, withdraw_tx_hash: HexBytes
) -> TxReceipt:
"""
Execute finalize withdraw transaction on L1 network
:type zksync_provider:
Instance of ZkSync provider
:param ethereum_provider
Instance of EthereumProvider
:param withdraw_tx_hash
Hash of withdraw transaction on L2 network
:return:
TxReceipt of finalize withdraw transaction on L1 network
"""
zks_receipt = zksync_provider.zksync.wait_finalized(withdraw_tx_hash)
# Check if withdraw transaction was successful
if not zks_receipt["status"]:
raise RuntimeError("Withdraw transaction on L2 network failed")
# Execute finalize withdraw
tx_receipt = ethereum_provider.finalize_withdrawal(zks_receipt["transactionHash"])
# Check if finalize withdraw transaction was successful
if not tx_receipt["status"]:
raise RuntimeError("Finalize withdraw transaction L1 network failed")
return tx_receipt
if __name__ == "__main__":
# Get the private key from OS environment variables
load_dotenv()
zksync_url = os.getenv('ZKSYNC_URL')
eth_url = os.getenv('ETH_URL')
if zksync_url is None:
print("Err: ZKSYNC_URL not set")
if eth_url is None:
print("Err: ETH_URL not set")
wallets = ReadWallets()
accounts = wallets.get_accounts()
# Connect to zkSync network
zk_web3 = ZkSyncBuilder.build(zksync_url)
# Connect to Ethereum network
eth_web3 = Web3(Web3.HTTPProvider(eth_url))
eth_web3.middleware_onion.inject(geth_poa_middleware, layer=0)
for wallet in accounts:
# Get account object by providing from private key
account: LocalAccount = Account.from_key(bytes.fromhex(remove_0x_prefix(HexStr(wallet['privateKey']))))
# Create Ethereum provider
eth_provider = EthereumProvider(zk_web3, eth_web3, account)
amount = 0.01
# Perform the withdrawal
withdraw_tx_hash = withdraw(zk_web3, account, amount)
print(f"Withdraw transaction hash: {withdraw_tx_hash.hex()}")
print("Wait for withdraw transaction to be finalized on L2 network (11-24 hours)")
print("Read more about withdrawal delay: https://era.zksync.io/docs/dev/troubleshooting/withdrawal-delay.html")
print("When withdraw transaction is finalized, execute 10_finalize_withdrawal.py script "
"with WITHDRAW_TX_HASH environment variable set")