Send an ETH transaction to the blockchain using Web3.py in Python

This tutorial will provide you with a brief overview of how to use Python and Web3.py to send a transaction to the Ethereum blockchain. The goal is to help you understand how to setup the transaction, sign it and send it to the Ethereum blockchain. All of these steps can be accomplished in Python using the Web3.py plugin.

Basic Layout of Python code

In order to send your transaction to the Ethereum network you will need to make sure Python and Web3.py are installed. If you have not installed Web3.py check here for instructions. Next you want to open a new file in your IDE to use the code below. This code has the following layout:

  • Import Web3 into your file
  • Connect to an end point so you can communicate to the block chain
  • Configure your wallet information that you will be transacting from
  • Build your transaction
  • Sign your transaction. In order to sign a transaction you need a private key
  • Send the transaction
  • Print the transaction Hash so you can confirm that the network received your message

Review Python Web3.py Code

Please read the comments below and analyze the code so you can determine how this process works. Since we are sending data to the the blockchain there is a gas fee. It is advised that you practice this process in the test environment so you do not risk any money while working out bugs in your process. Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Please be careful and never give out your private key and never put your private key in code in clear text for others to view. The code below is a sample and for educational purposes only.

from web3 import Web3

ganache_url = 'IMPORTYOURURL'
web3 = Web3(Web3.HTTPProvider(ganache_url))
account_1 = 'INPUTACCOUNT1'
private_key1 = 'INPUTPRIVATEKEY'
account_2 = 'INPUTACCOUNT2'

#get the nonce.  Prevents one from sending the transaction twice
nonce = web3.eth.getTransactionCount(account_1)

#build a transaction in a dictionary
tx = {
    'nonce': nonce,
    'to': account_2,
    'value': web3.toWei(1, 'ether'),
    'gas': 2000000,
    'gasPrice': web3.toWei('50', 'gwei')
}

#sign the transaction
signed_tx = web3.eth.account.sign_transaction(tx, private_key1)

#send transaction
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)

#get transaction hash
print(web3.toHex(tx_hash))

Now that you know the basic structure to send a transaction to the Ethereum blockchain you can use this sample to define a trade function in your code. Then you can pass this function the basic parameters (to, value, gas, gasprice) to submit a trade.

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Ethereum test network configuration and test ETH faucet information
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc.
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc.
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc.
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc.
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Nodes

Learn how to run a Geth node. Read getting started with Geth to run an Ethereum node.

Fix a transaction

How to fix a pending transaction stuck on Ethereum or EVM compatible chain

Next – Use Python to monitor Twitter accounts for crypto tweets

1 thought on “Send an ETH transaction to the blockchain using Web3.py in Python

Leave a Reply