Problems creating a simple Görli Testnet transaction with Infura

Hi,

I want to create a simple Ethereum Görli testnet transaction, very similar to this tutorial: Use Python - Infura Docs

For this, I copied the code from there, it looks like this (besides my API number):

import os
from dotenv import load_dotenv
from web3 import Web3

load_dotenv()

infura_url = "https://goerli.infura.io/v3/<my API number>"
private_key = os.getenv('PRIVATE_KEY')

from_account = '0xa43190EF71561A9229D8ddC5449d68FFE518b1D8'
to_account = '0x37d1215743ACf59B85f2DDE7Ab103f2BBC5a568F'

web3 = Web3(Web3.HTTPProvider(infura_url))
nonce = web3.eth.getTransactionCount(from_account)

tx = {
    'type': '0x2',
    'nonce': nonce,
    'from': from_account,
    'to': to_account,
    'value': web3.toWei(0.01, 'ether'),
    'maxFeePerGas': web3.toWei('250', 'gwei'),
    'maxPriorityFeePerGas': web3.toWei('3', 'gwei'),
    'chainId': 5
}

gas = web3.eth.estimateGas(tx)
tx['gas'] = gas
signed_tx = web3.eth.account.sign_transaction(tx, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Transaction hash: " + str(web3.toHex(tx_hash)))

When I run the code, it creates an error in the line gas = web3.eth.estimateGas(tx):

Exception has occurred: ValueError
{'code': -32602, 'message': 'invalid argument 0: json: cannot unmarshal non-string into Go struct field TransactionArgs.maxFeePerGas of type *hexutil.Big'}
  File "C:\Users\userA\Documents\GitHub\solidity_playground_1\eip1559_tx.py", line 27, in <module>
    gas = web3.eth.estimateGas(tx)

Can you please help me to resolve it? Thanks in advance!

Hey @feufu , welcome to the community.
I just ran the same code you shared and was able to send a tx on Goerli.
Can you check if the private key in your .env file has a ‘0x’ prefix and remove it? Then try to run the code again?
Also can you share the python version you are using and the version of the web3.py library?

Hi @radu, thank you for your answer. I just upgraded the web3.py library, now it works. It seems like I still had an old version installed

Hey @feufu . That’s great! Happy it works now.