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:
Fetching the latest block:
Prints the current hashrate that is to be mined to add a block:
Checking if nodes are sycing properly or dead:
Displaying block number:
Input account key to get balance in Wei unit:
myBalance = web3.eth.getBalance(“YOUR_ACCOUNT”)
Current gas price in Wei unit:
For any queries, official documentation can be consulted.
One thought on “Inspecting Ethereum’s Blocks: The Web3py Way”