-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path03_transfer_erc20_token.py
More file actions
93 lines (67 loc) · 2.93 KB
/
03_transfer_erc20_token.py
File metadata and controls
93 lines (67 loc) · 2.93 KB
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
import os
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexAddress, HexStr
from web3 import Web3
from zksync2.account.wallet import Wallet
from zksync2.core.types import EthBlockParams, TransferTransaction
from zksync2.module.module_builder import ZkSyncBuilder
def transfer_erc20(
token_contract,
account: LocalAccount,
address: HexAddress,
amount: float) -> HexStr:
"""
Transfer ERC20 token to a desired address on zkSync network
:param token_contract:
Instance of ERC20 contract
:param account:
From which account the transfer will be made
:param address:
Desired ETH address that you want to transfer to.
:param amount:
Desired ETH amount that you want to transfer.
:return:
The transaction hash of the transfer transaction.
"""
tx = token_contract.functions.transfer(address, amount).build_transaction({
"nonce": zk_web3.zksync.get_transaction_count(account.address, EthBlockParams.LATEST.value),
"from": account.address,
"maxPriorityFeePerGas": 1_000_000,
"maxFeePerGas": zk_web3.zksync.gas_price,
})
signed = account.sign_transaction(tx)
# Send transaction to zkSync network
tx_hash = zk_web3.zksync.send_raw_transaction(signed.rawTransaction)
print(f"Tx: {tx_hash.hex()}")
tx_receipt = zk_web3.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)
print(f"Tx status: {tx_receipt['status']}")
return tx_hash
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)
# Get l2 token address from l1
l1_token_address = "0x70a0F165d6f8054d0d0CF8dFd4DD2005f0AF6B55"
l2_token_address = zk_web3.zksync.l2_token_address(l1_token_address)
print(wallet.get_balance(token_address=l2_token_address))
tx_hash = wallet.transfer(TransferTransaction(to=Web3.to_checksum_address(wallet.address),
token_address=Web3.to_checksum_address(l2_token_address),
amount=1))
tx_receipt = zk_web3.zksync.wait_for_transaction_receipt(
tx_hash, timeout=240, poll_latency=0.5
)
print(wallet.get_balance(token_address=l2_token_address))
print(f"Tx receipt : {tx_receipt}")