Skip to content

Commit

Permalink
feat: added support for the new version API of web3py
Browse files Browse the repository at this point in the history
  • Loading branch information
ares0x committed Jun 25, 2023
1 parent 7da0cea commit 706aa5a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions ArbitrumBridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def get_w3_by_network(network='mainnet'):

# bridge eth from rinkeby to arbitrum testnet
def bridge_arbitrum_eth(w3, from_address, private_key, contract_address, amount_in_ether, chainId):
from_address = Web3.toChecksumAddress(from_address)
contract_address = Web3.toChecksumAddress(contract_address)
from_address = Web3.to_checksum_address(from_address)
contract_address = Web3.to_checksum_address(contract_address)

ABI = '[{"inputs":[{"internalType":"uint256","name":"maxSubmissionCost","type":"uint256"}],"name":"depositEth","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"}]'

amount_in_wei = w3.toWei(amount_in_ether, 'ether')
maxSubmissionCost = int(amount_in_wei * 0.01) # 定义参数值
nonce = w3.eth.getTransactionCount(from_address)
nonce = w3.eth.get_transaction_count(from_address)

params = {
'chainId': chainId,
Expand All @@ -43,7 +43,7 @@ def bridge_arbitrum_eth(w3, from_address, private_key, contract_address, amount_
try:
tx = func.buildTransaction(params)
signed_tx = w3.eth.account.sign_transaction(tx, private_key=private_key)
txn = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
txn = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
return {'status': 'succeed', 'txn_hash': w3.toHex(txn), 'task': 'Bridge ETH'}
except Exception as e:
return {'status': 'failed', 'error': e, 'task': 'Bridge ETH'}
Expand Down
4 changes: 2 additions & 2 deletions Tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def main():
w3 = get_w3_by_network(network='mainnet')

# 检查接入状态
print(w3.isConnected())
print(w3.is_connected())

# 当前区块高度
print(w3.eth.block_number)
Expand All @@ -29,7 +29,7 @@ def main():
vb = '0x220866b1a2219f40e72f5c628b65d54268ca3a9d'

# 地址格式转换
address = Web3.toChecksumAddress(vb)
address = Web3.to_checksum_address(vb)

# 查询地址 ETH余额
balance = w3.eth.get_balance(address) / 1e18
Expand Down
16 changes: 8 additions & 8 deletions transferETH.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ def get_w3_by_network(network='mainnet'):


def transfer_eth(w3,from_address,private_key,target_address,amount,gas_price=5,gas_limit=21000,chainId=4):
from_address = Web3.toChecksumAddress(from_address)
target_address = Web3.toChecksumAddress(target_address)
nonce = w3.eth.getTransactionCount(from_address) # 获取 nonce 值
from_address = Web3.to_checksum_address(from_address)
target_address = Web3.to_checksum_address(target_address)
nonce = w3.eth.get_transaction_count(from_address) # 获取 nonce 值
params = {
'from': from_address,
'nonce': nonce,
'to': target_address,
'value': w3.toWei(amount, 'ether'),
'value': w3.to_wei(amount, 'ether'),
'gas': gas_limit,
# 'gasPrice': w3.toWei(gas_price, 'gwei'),
'maxFeePerGas': w3.toWei(gas_price, 'gwei'),
'maxPriorityFeePerGas': w3.toWei(gas_price, 'gwei'),
'maxFeePerGas': w3.to_wei(gas_price, 'gwei'),
'maxPriorityFeePerGas': w3.to_wei(gas_price, 'gwei'),
'chainId': chainId,

}
try:
signed_tx = w3.eth.account.signTransaction(params, private_key=private_key)
txn = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
signed_tx = w3.eth.account.sign_transaction(params, private_key=private_key)
txn = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
return {'status': 'succeed', 'txn_hash': w3.toHex(txn), 'task': 'Transfer ETH'}
except Exception as e:
return {'status': 'failed', 'error': e, 'task': 'Transfer ETH'}
Expand Down
6 changes: 3 additions & 3 deletions zkSyncBridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def get_w3_by_network(network='goerli'):

# bridge eth from goerli to zkSync 2.0 testnet
def bridge_zkSync_eth(w3, from_address, private_key, contract_address, amount_in_ether, chainId):
from_address = Web3.toChecksumAddress(from_address)
contract_address = Web3.toChecksumAddress(contract_address)
from_address = Web3.to_checksum_address(from_address)
contract_address = Web3.to_checksum_address(contract_address)

# Deposit ETH ABI
ABI = '[{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"address","name":"_zkSyncAddress","type":"address"},{"internalType":"enum Operations.QueueType","name":"_queueType","type":"uint8"},{"internalType":"enum Operations.OpTree","name":"_opTree","type":"uint8"}],"name":"depositETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"emergencyFreezeDiamond","outputs":[],"stateMutability":"nonpayable","type":"function"}]'

amount_in_wei = w3.toWei(amount_in_ether, 'ether')
nonce = w3.eth.getTransactionCount(from_address)
nonce = w3.eth.get_transaction_count(from_address)

# goerli链:无须设置 gas, gas price , chainId, 会自动计算并配置为 EIP 1559 类型
tx_params = {
Expand Down

0 comments on commit 706aa5a

Please sign in to comment.