├── .gitignore ├── README.md ├── private.json.example ├── requirements.txt └── zksync.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.json 2 | __pycache__ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zksync 2 | `pip3 install -r requirements` 3 | -------------------------------------------------------------------------------- /private.json.example: -------------------------------------------------------------------------------- 1 | { 2 | "private": [ 3 | "", 4 | "" 5 | ] 6 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | schedule 2 | zksync2 -------------------------------------------------------------------------------- /zksync.py: -------------------------------------------------------------------------------- 1 | from web3 import Web3 2 | from zksync2.module.module_builder import ZkSyncBuilder 3 | from web3 import Web3 4 | from web3.middleware import geth_poa_middleware 5 | from eth_account import Account 6 | from eth_account.signers.local import LocalAccount 7 | from zksync2.manage_contracts.gas_provider import StaticGasProvider 8 | from zksync2.manage_contracts.l2_bridge import L2BridgeEncoder 9 | from zksync2.core.types import Token 10 | from zksync2.provider.eth_provider import EthereumProvider 11 | from zksync2.core.types import EthBlockParams 12 | from zksync2.signer.eth_signer import PrivateKeyEthSigner 13 | from zksync2.transaction.transaction712 import Transaction712 14 | from zksync2.core.types import ZkBlockParams, BridgeAddresses 15 | from eth_typing import HexStr 16 | from zksync2.module.request_types import create_function_call_transaction 17 | from decimal import Decimal 18 | import schedule 19 | import time 20 | import json 21 | from random import choice 22 | from datetime import datetime 23 | 24 | URL_TO_ETH_NETWORK = "https://rpc.ankr.com/eth_goerli" 25 | ZKSYNC_NETWORK_URL = "https://zksync2-testnet.zksync.dev" 26 | 27 | private = json.load(open('private.json')) 28 | print("总共", len(private["private"]), "钱包") 29 | #account: LocalAccount = Account.from_key(private["private"]) 30 | 31 | 32 | def deposit(): 33 | try: 34 | print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "随机执行存款") 35 | account: LocalAccount = Account.from_key(choice(private["private"])) 36 | eth_web3 = Web3(Web3.HTTPProvider(URL_TO_ETH_NETWORK)) 37 | zkSync_web3 = ZkSyncBuilder.build(ZKSYNC_NETWORK_URL) 38 | #geth_poa_middleware is used to connect to geth --dev. 39 | eth_web3.middleware_onion.inject(geth_poa_middleware, layer=0) 40 | 41 | #calculate gas fees 42 | gas_provider = StaticGasProvider(Web3.toWei(1, "gwei"), 555000) 43 | 44 | #Create the ethereum provider for interacting with ethereum node, initialize zkSync signer and deposit funds. 45 | eth_provider = EthereumProvider.build_ethereum_provider(zksync=zkSync_web3, 46 | eth=eth_web3, 47 | account=account, 48 | gas_provider=gas_provider) 49 | tx_receipt = eth_provider.deposit(Token.create_eth(), 50 | eth_web3.toWei("0.0001", "ether"), 51 | account.address) 52 | # Show the output of the transaction details. 53 | #print(f"tx status: {tx_receipt}") 54 | print(f"tx status: {tx_receipt['status']}") 55 | except Exception as e: 56 | print(e, account.address) 57 | pass 58 | 59 | def get_account_balance(): 60 | account: LocalAccount = Account.from_key(choice(private["private"])) 61 | eth_web3 = Web3(Web3.HTTPProvider(URL_TO_ETH_NETWORK)) 62 | zkSync_web3 = ZkSyncBuilder.build(ZKSYNC_NETWORK_URL) 63 | zk_balance = zkSync_web3.zksync.get_balance(account.address, EthBlockParams.LATEST.value) 64 | print(f"zkSync balance: {zk_balance}") 65 | 66 | def transfer_to_self(): 67 | try: 68 | print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "随机执行发送") 69 | account: LocalAccount = Account.from_key(choice(private["private"])) 70 | eth_web3 = Web3(Web3.HTTPProvider(URL_TO_ETH_NETWORK)) 71 | zkSync_web3 = ZkSyncBuilder.build(ZKSYNC_NETWORK_URL) 72 | chain_id = zkSync_web3.zksync.chain_id 73 | signer = PrivateKeyEthSigner(account, chain_id) 74 | 75 | nonce = zkSync_web3.zksync.get_transaction_count(account.address, ZkBlockParams.COMMITTED.value) 76 | tx = create_function_call_transaction(from_=account.address, 77 | to=account.address, 78 | ergs_price=0, 79 | ergs_limit=0, 80 | data=HexStr("0x")) 81 | estimate_gas = int(zkSync_web3.zksync.eth_estimate_gas(tx) * 1.3) 82 | 83 | gas_price = zkSync_web3.zksync.gas_price 84 | print(f"Fee for transaction is: {estimate_gas * gas_price}") 85 | 86 | tx_712 = Transaction712(chain_id=chain_id, 87 | nonce=nonce, 88 | gas_limit=estimate_gas, 89 | to=tx["to"], 90 | value=Web3.toWei(0.001, 'ether'), 91 | data=tx["data"], 92 | maxPriorityFeePerGas=100000000, 93 | maxFeePerGas=gas_price, 94 | from_=account.address, 95 | meta=tx["eip712Meta"]) 96 | 97 | singed_message = signer.sign_typed_data(tx_712.to_eip712_struct()) 98 | msg = tx_712.encode(singed_message) 99 | tx_hash = zkSync_web3.zksync.send_raw_transaction(msg) 100 | tx_receipt = zkSync_web3.zksync.wait_for_transaction_receipt(tx_hash, timeout=240, poll_latency=0.5) 101 | print(f"tx status: {tx_receipt['status']}") 102 | except Exception as e: 103 | print(e, account.address) 104 | pass 105 | 106 | def withdraw(): 107 | try: 108 | print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "随机执行提款") 109 | account: LocalAccount = Account.from_key(choice(private["private"])) 110 | eth_web3 = Web3(Web3.HTTPProvider(URL_TO_ETH_NETWORK)) 111 | zkSync_web3 = ZkSyncBuilder.build(ZKSYNC_NETWORK_URL) 112 | chain_id = zkSync_web3.zksync.chain_id 113 | signer = PrivateKeyEthSigner(account, chain_id) 114 | ETH_TOKEN = Token.create_eth() 115 | 116 | nonce = zkSync_web3.zksync.get_transaction_count(account.address, ZkBlockParams.COMMITTED.value) 117 | bridges: BridgeAddresses = zkSync_web3.zksync.zks_get_bridge_contracts() 118 | 119 | l2_func_encoder = L2BridgeEncoder(zkSync_web3) 120 | call_data = l2_func_encoder.encode_function(fn_name="withdraw", args=[ 121 | account.address, 122 | ETH_TOKEN.l2_address, 123 | ETH_TOKEN.to_int(Decimal("0.001")) 124 | ]) 125 | 126 | tx = create_function_call_transaction(from_=account.address, 127 | to=bridges.l2_eth_default_bridge, 128 | ergs_limit=0, 129 | ergs_price=0, 130 | data=HexStr(call_data)) 131 | estimate_gas = int(zkSync_web3.zksync.eth_estimate_gas(tx) * 1.3) 132 | gas_price = zkSync_web3.zksync.gas_price 133 | 134 | print(f"Fee for transaction is: {estimate_gas * gas_price}") 135 | 136 | tx_712 = Transaction712(chain_id=chain_id, 137 | nonce=nonce, 138 | gas_limit=estimate_gas, 139 | to=tx["to"], 140 | value=tx["value"], 141 | data=tx["data"], 142 | maxPriorityFeePerGas=100000000, 143 | maxFeePerGas=gas_price, 144 | from_=account.address, 145 | meta=tx["eip712Meta"]) 146 | 147 | singed_message = signer.sign_typed_data(tx_712.to_eip712_struct()) 148 | msg = tx_712.encode(singed_message) 149 | tx_hash = zkSync_web3.zksync.send_raw_transaction(msg) 150 | tx_receipt = zkSync_web3.zksync.wait_for_transaction_receipt(tx_hash, timeout=240, poll_latency=0.5) 151 | print(f"tx status: {tx_receipt['status']}") 152 | except Exception as e: 153 | print(e, account.address) 154 | pass 155 | 156 | transfer_to_self() 157 | deposit() 158 | withdraw() 159 | #schedule.every(10).seconds.do(lambda: transfer_to_self()) 160 | 161 | schedule.every(10).minutes.do(lambda: transfer_to_self()) 162 | schedule.every(10).minutes.do(lambda: deposit()) 163 | schedule.every(10).minutes.do(lambda: withdraw()) 164 | while True: 165 | schedule.run_pending() 166 | time.sleep(1) 167 | 168 | --------------------------------------------------------------------------------