| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- # -*- coding: utf-8 -*-
- from web3 import Web3, HTTPProvider
- # 连接到以太坊节点
- w3 = Web3(HTTPProvider('http://127.0.0.1:8545'))
- # 检查是否连接成功
- if w3.isConnected():
- print("Connected to Ethereum node")
- else:
- print("Failed to connect to Ethereum node")
- exit()
- # 1. 获取最新区块号
- latest_block = w3.eth.block_number
- print(f"Latest block number: {latest_block}")
- # 2. 获取区块信息
- block = w3.eth.get_block(latest_block)
- print(f"Block info: {block}")
- # 3. 获取账户余额
- account = '0xYourAccountAddress' # 替换为你的账户地址
- balance = w3.eth.get_balance(account)
- print(f"Balance of {account}: {w3.fromWei(balance, 'ether')} ETH")
- # 4. 发送交易
- sender = '0xYourAccountAddress' # 替换为你的账户地址
- private_key = 'YourPrivateKey' # 替换为你的私钥
- receiver = '0xReceiverAddress' # 替换为接收方地址
- nonce = w3.eth.get_transaction_count(sender)
- tx = {
- 'nonce': nonce,
- 'to': receiver,
- 'value': w3.toWei(0.01, 'ether'),
- 'gas': 2000000,
- 'gasPrice': w3.toWei('50', 'gwei'),
- }
- signed_tx = w3.eth.account.sign_transaction(tx, private_key)
- tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
- print(f"Transaction hash: {w3.toHex(tx_hash)}")
- # 5. 与智能合约交互
- contract_address = '0xYourContractAddress' # 替换为合约地址
- contract_abi = [...] # 替换为合约的ABI
- contract = w3.eth.contract(address=contract_address, abi=contract_abi)
- # 调用合约的只读方法
- result = contract.functions.yourReadOnlyMethod().call()
- print(f"Result from contract: {result}")
- # 调用合约的写方法
- tx = contract.functions.yourWriteMethod(param1, param2).buildTransaction({
- 'nonce': w3.eth.get_transaction_count(sender),
- 'gas': 2000000,
- 'gasPrice': w3.toWei('50', 'gwei'),
- })
- signed_tx = w3.eth.account.sign_transaction(tx, private_key)
- tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
- print(f"Transaction hash: {w3.toHex(tx_hash)}")
- # 6. 监听事件
- event_filter = contract.events.YourEvent.createFilter(fromBlock='latest')
- print("Listening for events...")
- while True:
- for event in event_filter.get_new_entries():
- print(f"New event: {event}")
- # 7. 获取交易收据
- tx_receipt = w3.eth.get_transaction_receipt(tx_hash)
- print(f"Transaction receipt: {tx_receipt}")
- # 8. 获取交易详情
- tx_detail = w3.eth.get_transaction(tx_hash)
- print(f"Transaction detail: {tx_detail}")
- # 9. 获取日志
- logs = w3.eth.get_logs({
- 'fromBlock': 0,
- 'toBlock': 'latest',
- 'address': contract_address,
- })
- print(f"Logs: {logs}")
- # 10. 获取当前 Gas 价格
- gas_price = w3.eth.gas_price
- print(f"Current gas price: {w3.fromWei(gas_price, 'gwei')} gwei")
- # 11. 获取账户交易数量
- transaction_count = w3.eth.get_transaction_count(sender)
- print(f"Transaction count: {transaction_count}")
- # 12. 获取合约代码
- contract_code = w3.eth.get_code(contract_address)
- print(f"Contract code: {contract_code}")
- # 13. 获取账户存储
- storage = w3.eth.get_storage_at(contract_address, 0)
- print(f"Storage at position 0: {storage}")
- # 14. 获取账户 nonce
- nonce = w3.eth.get_transaction_count(sender)
- print(f"Account nonce: {nonce}")
- # 15. 获取区块 gas 限制
- gas_limit = w3.eth.get_block('latest')['gasLimit']
- print(f"Gas limit: {gas_limit}")
- # 16. 获取区块 gas 使用量
- gas_used = w3.eth.get_block('latest')['gasUsed']
- print(f"Gas used: {gas_used}")
- # 17. 获取区块 gas 费用
- gas_fee = gas_used * gas_price
- print(f"Gas fee: {w3.fromWei(gas_fee, 'ether')} ETH")
|