“SignedTransaction”对象没有属性“rawTransaction”

问题描述 投票:0回答:1

`

from web3 import Web3
import os
import time
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Define RPC URLs for different networks
RPC_URLS = {
    "ethereum": os.getenv("ETHEREUM_RPC_URL", f"https://eth-mainnet.alchemyapi.io/v2/{os.getenv('ALCHEMY_API_KEY')}"),
    "polygon": os.getenv("POLYGON_RPC_URL", "https://polygon-rpc.com"),
    "base": os.getenv("BASE_RPC_URL", "https://mainnet.base.org")
}

# Wallet details
SOURCE_PRIVATE_KEY = os.getenv("SOURCE_PRIVATE_KEY")
SOURCE_ADDRESS = Web3.to_checksum_address(os.getenv("SOURCE_ADDRESS"))
DESTINATION_ADDRESS = Web3.to_checksum_address(os.getenv("DESTINATION_ADDRESS"))

# Forwarding function for all networks
def forward_eth(network, rpc_url):
    """Send ETH from the source wallet to the destination on a specific network."""
    try:
        # Connect to the Web3 provider for the current network
        web3 = Web3(Web3.HTTPProvider(rpc_url))

        # Get the balance of the source wallet in wei
        balance = web3.eth.get_balance(SOURCE_ADDRESS)

        # Debugging: Print balance for the current network
        print(f"{network.capitalize()} Network:")
        print(f"Wallet Balance (Wei): {balance}")
        print(f"Wallet Balance (Ether): {web3.from_wei(balance, 'ether')}")
        
        # Estimate gas
        gas_price = web3.eth.gas_price
        gas_limit = 21000  # Standard gas limit for ETH transfer
        total_gas_cost = gas_price * gas_limit
        
        # Debugging: Print gas calculation
        print(f"Gas Price (Wei): {gas_price}")
        print(f"Gas Limit: {gas_limit}")
        print(f"Total Gas Cost (Wei): {total_gas_cost}")
        
        # Calculate the amount to send, excluding gas fees
        amount_to_send = balance - total_gas_cost

        # If you want to send all ETH, including gas fees, use this:
        # amount_to_send = balance

        # Prepare transaction if the balance is enough
        if amount_to_send > 0:
            nonce = web3.eth.get_transaction_count(SOURCE_ADDRESS)
            tx = {
                'nonce': nonce,
                'to': DESTINATION_ADDRESS,
                'value': amount_to_send,
                'gas': gas_limit,
                'gasPrice': gas_price
            }

            # Sign the transaction
            signed_tx = web3.eth.account.sign_transaction(tx, private_key=SOURCE_PRIVATE_KEY)
            
            # Send the transaction
            tx_hash = web3.eth.send_raw_transaction(signed_tx["rawTransaction"])  # Correct access in v6+
            print(f"Transaction sent! Hash: {web3.toHex(tx_hash)}\n")
        else:
            print(f"Insufficient balance to cover gas fees on {network}.\n")

    except Exception as e:
        print(f"Error with {network}: {e}\n")

# Main function to check all networks and forward ETH
def forward_eth_across_all_networks():
    print("Starting ETH forwarding bot across all networks...\n")
    
    # List of networks to check
    networks = ["ethereum", "polygon", "base"]
    for network in networks:
        # Get the RPC URL for the current network
        rpc_url = RPC_URLS.get(network)

        # Call the forwarding function for the current network
        forward_eth(network, rpc_url)

def main():
    while True:
        forward_eth_across_all_networks()
        time.sleep(30)  # Check every 30 seconds for all networks

if __name__ == "__main__":
    main()

` 尝试制作一个 eth 转发机器人,但不断遇到此问题,并且不知道如何解决。 如果我做错了,在这里发布第一篇文章,只是想解决一个恼人的问题。这段代码的目的是成为一个机器人,每当 eth 发送到我的替代钱包时,该机器人就会自动将其发送到我的主钱包,它会计算汽油费以及其他费用 这就是它返回的内容,并不是真正的错误,而是某个地方出了问题

python bots cryptocurrency web3py
1个回答
0
投票

您使用的 web3.py 版本是什么?看起来您正在使用 >= v6.0.0,其中已弃用的camelCase方法已被删除,取而代之的是snake_case方法,而您的代码已为编写

© www.soinside.com 2019 - 2024. All rights reserved.