想知道是否有人知道如何通过 web3.py 发送最大数量的以太币或我的全部以太币余额?这是我一直在使用的代码块。在“值”中,我只能发送特定数量的以太币,并且不知道如何让它找出它可以发送的最大以太币量。
balance_sender = web3.from_wei(web3.eth.get_balance(sender_address), 'ether')
balance_recipient = web3.from_wei(web3.eth.get_balance(recipient_address), 'ether')
print(f'The balance of {sender_address} is: {balance_sender} ETH')
print(f'The balance of {recipient_address} is: {balance_recipient} ETH')
# Get and determine gas parameters
latest_block = web3.eth.get_block("latest")
base_fee_per_gas = latest_block.baseFeePerGas # Base fee in the latest block (in wei)
max_priority_fee_per_gas = web3.to_wei(1, 'gwei') # Priority fee to include the transaction in the block
max_fee_per_gas = (5 * base_fee_per_gas) + max_priority_fee_per_gas # Maximum amount you’re willing to pay
# Define the transaction parameters
transaction_params = {
'from': sender_address,
'to': recipient_address,
'value': web3.to_wei(0.002, 'ether'), # Transaction value (0.1 Ether in this example)
'nonce': web3.eth.get_transaction_count(sender_address),
'gas': 21000, # Gas limit for the transaction
'maxFeePerGas': max_fee_per_gas, # Maximum amount you’re willing to pay
'maxPriorityFeePerGas': max_priority_fee_per_gas, # Priority fee to include the transaction in the block
'chainId': 11155111 # ChainId of Sepolia Testnet
}
# Sign the transaction
transaction = web3.eth.account.sign_transaction(transaction_params, private_key)
# Send the transaction
transaction_hash = web3.eth.send_raw_transaction(transaction.rawTransaction)
# Wait for the transaction to be mined
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
我尝试创建一个从balance_sender中减去gas的函数,但它只是返回了
类型错误:交易具有无效字段:{'value':Decimal('0.011652999998614')}'
尝试:
value
: int(web3.to_wei(0.002, '以太'))