Send transaction with web3 using python

Introduction to web3.py and sending transaction on testnet

web3.py

Aniket Savji

@ankasaw99
1977 1 3

Introduction 

Like web3.js we can use web3.py to communicate with blockchain. This smartbook will be an introduction to web3.py. We will simply transafer some test ethers using web3.py that way we will get to know about web3.ps's working as it is little different that web3.js. You can refer this smartbook which did same interaction with blockchain but with implementation of web3.js library.

Configuration

1. Getting rpc url 

RPC url is required for our program to interact with a blockchain network. You can check different methods to connect through providers in this smartbook.

For this example provder we will be using is Infura via http. All we have to do is create an account and generate a project which will provide us with rpc url for created project's api key.

2. Getting test tokens

You can get test tokens from ropsten faucet. Enter your address and click on submit.

3. Web3 Installation

pip is the most used pacakage management system for python so we will be using it to install our web3 library using following command

> pip install web3

Transfer Program

1. Initialise Variables

Let's create a web3 instace with http provider.

from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/<API>'))
Private_Key  =  '<Sender Private Key>'
from_address  =  '<Sender Public Address>'
to_address  =  '<Receiver Public Address>'

2. Transaction Object

Transaction object which we will be creating require following parameters

  • nonce  :  It is a random number required to process transactions to avoid repeatation of transaction 
  • to  :  Receiver's address
  • value  :  Amount to be sent in wei (1 ether = 10^18 wei)
  • gas  :  Gas amount in number
  • gasPrice  :  Gas fees in gwei

We will be obtaining nonce with the help of getTransactionCount method. 

nonce = web3.eth.getTransactionCount(from_address)
gasPrice = web3.toWei('50', 'gwei')
value = web3.toWei(0.1, 'ether')

tx = {
    'nonce': nonce,
    'to': to_address,
    'value': value,
    'gas': 2000000,
    'gasPrice': gasPrice
}

Note that : the transaction object that we created here is a python dictionary.

2. Sign Transaction

This function will return a transaction that’s been signed by the node’s private key (i.e we have private key and a rpc node), but not yet submitted. 

Syntax  :  web3.eth.account.sign_transaction( Tx Object, Private_Key ) 

Parameters  :

  • Tx Object  -  The transaction data/object to sign.
  • Private_Key  -  Private key of a address to sign a transaction with.

Returns  :  Signed transaction object is returned.

Implementation  :

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

2. Send Signed Transaction

This function will send a signed and serialized transaction and will returns the transaction hash as a HexBytes object.

Syntax  :  web3.eth.send_raw_transaction(signed_tx.rawTransaction) 

Parameters  :

  • Raw_Transaction  -  Raw transaction which we got from sign transaction method.

Returns  :  Transaction hash as a HexBytes object.

Implementation  :

tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)

Here we will get transaction hash in the hex byte format we need to convert it into hex format.

This is how we can send our transaction on the ethereum network using web3.py 

 

Complete Code 

from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://ropsten.infura.io/v3/<API>'))
Private_Key  =  '<Sender Private Key>'
from_address  =  '<Sender Public Address>'
to_address  =  '<Receiver Public Address>'

nonce = web3.eth.getTransactionCount(from_address)
gasPrice = web3.toWei('50', 'gwei')
value = web3.toWei(0.1, 'ether')

tx = {
    'nonce': nonce,
    'to': to_address,
    'value': value,
    'gas': 2000000,
    'gasPrice': gasPrice
}

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

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

#Print transaction hash in hex
print(web3.toHex(tx_hash))

More SmartBooks like this