Links

Use Python

Send a regular transaction from one account to another with Python.

Prerequisites

  • An Ethereum account containing some Goerli test ETH.
Use MetaMask or similar to create an Ethereum account for testing.

Steps

1. Create a project directory

Create a new directory:
mkdir infura
cd into the directory:
cd infura

2. install the dependencies

pip install web3
pip install python-dotenv

3. Create .env file

Add your private key to the .env file.
SIGNER_PRIVATE_KEY = <PRIVATE-KEY>
Find out how to access the private key of your Ethereum account.
Never disclose your private key.
A malicious actor who has access to your private key can steal your assets.

4. Create eip1559_tx.py file

Replace from_account and to_account with the relevant details.
import os
from dotenv import load_dotenv
from web3 import Web3
load_dotenv()
infura_url = "https://goerli.infura.io/v3/<API-KEY>"
private_key = os.getenv('PRIVATE_KEY')
from_account = '0xeF8692168D0C165488ae3F0105Fe096f5E81820b'
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': 3
}
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)))
If using a different Ethereum network, update the URL in the script.

4. Execute the transaction

Run the script:
python eip1559_tx.py
Example output:
Transaction hash: 0x30c0ef29111ca7aecc78a99149649b5076d104afa7ed2f603ff2d2ec1aa27a8c
You can search for the transaction on a block explorer like Goerli Etherscan
Last modified 6mo ago