infura ethereum sending transaction python is sending a way too high gas fee every time how do I fix this

69 Views Asked by At

I am trying to send a test transaction. When I set a static transaction fee of 21000 wei it doesn't go through, I get a transaction hash but the transaction won't get mined. When I use the same concept as on the infura docs for sending it will give me a transaction fee how way too much:

Error sending transaction:

 {'code': -32000, 'message': 'insufficient funds for gas * price + value: balance 0, tx cost 15250000000000000, overshot 15250000000000000'}

my code is this.

def send_transaction(private_key, address, infura_api_key='key'):
w3 = Web3(Web3.HTTPProvider(f'https://mainnet.infura.io/v3/{infura_api_key}'))
from_address = get_address_from_private_key(private_key)

current_nonce = get_nonce_from_file(private_key)
new_nonce = current_nonce + 1

# Lower gas price and higher gas limit
gas_price = w3.eth.gas_price
f = int(w3.from_wei(gas_price, 'gwei')) + 2
print(f)
print(gas_price)
gas_limit = 30000  # Adjust this value as needed

tx = {
    'type': '0x2',
    'to': address,
    'nonce': new_nonce,
    'value': w3.to_wei(0.01, 'ether'),
    'maxFeePerGas': w3.to_wei('250', 'gwei'),
    'maxPriorityFeePerGas': w3.to_wei('3', 'gwei'),
    'chainId': 11155111,
}
gas = w3.eth.estimate_gas(tx)
tx['gas'] = gas

signed_transaction = w3.eth.account.sign_transaction(tx, private_key)

try:
    tx_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
    print(f"Transaction sent. Transaction Hash: {tx_hash.rawTransaction}")
    update_nonce_in_file(private_key, new_nonce)
except Exception as e:
    print(f"Error sending transaction: {e}")

I also tried using chain id 1 but this gives me the same result

0

There are 0 best solutions below