Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python.

To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on the Ether blockchain using Python. You can also pull data from current blocks on the block chain and view information like a block’s hash , gas used and nonce. Here is the documentation for Web3 , it is a pretty cool library so definitely check it out.

Once Web3 is installed you will need a Infura account. Infura is a Ethereum node that allow users to interact with Ether blockchain using a API. Blockchain applications need connections to peer-to-peer networks, the Infura network help users quickly connect to the blockchain.

After you create your Infura account you will need to create a project in Infura. The most import item needed is the Project ID. How it works is the Web3 library uses the Infura project’s URL, which is how the Python code can interact with the Ether blockchain.

Set Up Connection

Variable eth_node is where you set your project link. To test if you are connected use infuraConnection.isConnected() if True is returned then connected, False would mean you are not connected.

from web3 import Web3

# Link Project and connecting to Infura
eth_node = '   #INPUT YOUR PROJECT ID HERE# '

infuraConnection = Web3(Web3.HTTPProvider(eth_node)) 

print("Connection to API :", infuraConnection.isConnected()) #Testing connection 
Get Block

Here you can grab the most recent mined block from the chain

def get_block():
     block = infuraConnection.eth.get_block('latest')
     return print(block)


Output :

difficulty': 6127037748199915, 
'extraData': HexBytes('0x486976656f6e2065752d68656176792d322054756258'), 
'gasLimit': 14962851,
 'gasUsed': 14953158,
 'hash': HexBytes('0xcf9e653ef7449329006a762f46552ce1938c9fe64c2eb855283978c6dfb3fed9'), 
 'nonce': HexBytes('0x65ba54d4f3295ee1'), 
'number': 12718638,
 'parentHash': HexBytes('0x49df4d887658e1336aecc785d25caf236633bd20f67325323b1346a87f0679a6'),
 'receiptsRoot': HexBytes('0xd286b411db6d6e8f0d1d95dc91d47cf11650621cfa54413a05eb5dcded7238cb'), 
'sha3Uncles': HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),
 'size': 89246, 
'stateRoot': HexBytes('0x4ca51044501148e8e5275c9548133b7bc65d6f381252815e8777aa98d8a2db1d'),
 'timestamp': 1624831185,
 'totalDifficulty': 26819475510492419903587,
 'transactions': [HexBytes('0x32a45c359cdbb2020914e45f5f960d4d232bf7c3fe52bfc654f3719744ac4c81'), 
Get Block Number

The command eth.block_number is used to get the most recent block number

def get_block():
   block = infuraConnection.eth.block_number

Get block Hash
block = infuraConnection.eth.block_number  #Get recent Ether block number

if block != 0:
    block_info = infuraConnection.eth.get_block(block).get('hash').hex()  #Grab hash value from block 



Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the … Read more Android Studio Loading Animation Between Activities

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value … Read more Android Studio Tutorial SeekBar

Python chr()

The chr() function returns a character that represents the specified unicode. Syntax chr(i) The chr() method takes only one integer as argument. The range may vary from 0 to 1,1141,111(0x10FFFF in base 16). The chr() method returns a character whose unicode point is num, an integer. If an integer is passed that is outside the range then … Read more Python chr()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s