Web3.py: How to Send EIP-1559 Transactions Using Python

If you prefer using Python instead of JavaScript for building web3 applications, Web3.py is your go-to library. Sending an EIP-1559 transaction using Web3.py is pretty straight forward. Below you will find the steps required to do so, along with the needed dependencies for running this script.

Steps :athletic_shoe::athletic_shoe:

  1. If you don’t have pip installed, you’ll have to install it first. (here’s how, depending on your OS: MacOS, Ubuntu, Windows)

  2. Install Web3.py, by running the following command in a terminal:

    pip install web3

  3. Install python-dotenv, which will allow us to use a .env file in order to not have our private information (project ID, private key, etc.) directly in our script’s code:

    pip install python-dotenv

  4. Create the .env file. If you’re on MacOS or Ubuntu, this can be achieved by running touch .env in a terminal. If on Windows, you can simply open a notepad file and then modify the extension to ‘.env’.

  5. Now let’s populate our .env file with our Infura project ID (found in Project settings in your Infura account), the network we are willing to perform the transaction on, and the private key of the account which will perform the transaction - keep in mind that anyone who gains access to this key will also gain access to your account, so make sure to keep it safe.
    To open the .env file on MacOS or Ubuntu, you can simply run open .env in the terminal. Then fill in the file, like this, using your details (my project ID & private key are random characters, which will not work):

    ETHEREUM_NETWORK = “goerli”
    INFURA_PROJECT_ID = “ca9073a7b024c700364a615e4e5950897”
    SIGNER_PRIVATE_KEY = “563d0fd1377367a8fc5609950ca2322b2e71b1669ac68d132cab0e”

  6. After completing the .env file, we are finally ready for building our script. You can create a .py file by running touch your_file_name.py on MacOS & Ubuntu, or simply create the file as usual on Windows. The code is below:

import os
from dotenv import load_dotenv
from web3 import Web3


infura_url = ('https://%s.infura.io/v3/%s' %(os.getenv('ETHEREUM_NETWORK'), os.getenv('INFURA_PROJECT_ID')))
private_key = os.getenv('SIGNER_PRIVATE_KEY')

from_account = '0x66D0A322E94A8B3be15d1e1E88c34a0E91C28d97'
to_account = '0x895c21B0416188CfB3eA5c8F48b0497B2110BA06'

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

Make sure to replace the from_account and to_account with the public addresses of the accounts you want to be part of the transaction. To run the script simply execute the following command in the terminal:

      python3 your_file_name.py

If everything works as expected, you should see your transaction’s hash in the terminal after a few seconds! :tada::tada: