Design a site like this with WordPress.com
Get started

Inspecting Ethereum’s Blocks: The Web3py Way

Ethereum is a public blockchain used worldwide. It has rich features to implement “Smart Contracts”. A smart contract is basically a program that contains the business-logic and transaction related conditions that has to be deployed on a specific peer.

Web3py: It is a Python library that eases the interaction between the client and the Ethereum blockchain.
Infura: This is a node provider that lets us use remote nodes without prior setup.

The following program connects to the Ethereum blockchain and inspects block-specific details:

Importing Web3 library:

from web3 import Web3

Place your infura API key:

Infura_Connection = “https://mainnet.infura.io/v3/INFURA_KEY”

Producing the connection to the blockchain:

web3 = Web3(Web3.HTTPProvider(Infura_Connection))

Checking protocol version:

print(web3.eth.protocolVersion)

Fetching the latest block:

print(web3.eth.getBlock(‘latest’))

Displaying coinbase:

print(web3.eth.coinbase)

Prints the current hashrate that is to be mined to add a block:

print(web3.eth.hashrate)

Checking if nodes are sycing properly or dead:

print(web3.eth.syncing)

fetching ChainID:

print(web3.eth.chainId)

Displaying block number:

print(web3.eth.blockNumber)

Input account key to get balance in Wei unit:

myBalance = web3.eth.getBalance(“YOUR_ACCOUNT”)
print(myBalance)

Current gas price in Wei unit:

print(web3.eth.gasPrice)

For any queries, official documentation can be consulted.

Advertisement

Published by Farial Mahmod Tishan

Life-long learner. Developing Flutter apps on Parrot Linux OS .

One thought on “Inspecting Ethereum’s Blocks: The Web3py Way

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: