[Python] 'SignedTransaction' object has no attribute 'rawTransaction'

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    `

    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()


    ` trying to make an eth forwarding bot and keep getting this issue and have no idea on how to fix. First post on here mb if i did it wrong, just trying to fix an annoying problem. The point of this code is to be a bot that, whenever eth is sent to my alt wallet, this bot automatically sends it to my main wallet, it calculates for gas fees and what not too this is what it returns, not really an error but something that went wrong somewhere

    Continue reading...

Compartilhe esta Página