Welcome! Meet our Python Code Assistant, your new coding buddy. Why wait? Start exploring now!
Blockchain is an innovative technology that has become widely popular worldwide among cryptocurrency companies, developers, and tech companies. It is a database or an immutable ledger that runs on a distributed network of nodes.
The central part of blockchain technology is a block or unit of data storage that consists of digital transactions. When a block is filled with data, it connects to the previously filled block and forms a chain of blocks. When a new transaction is added to the blockchain, it will be reflected in every participant's ledger.
Blockchain is a very secure database that is difficult to crack. Therefore many cryptocurrency systems like Bitcoin are built on blockchain technology for secure transactions.
In this tutorial, we will learn how to query the Ethereum blockchain in Python. We will use the web3.py client to query and connect to Ethereum nodes and query it to get different types of information.
Table of contents:
Ethereum is a decentralized blockchain that runs code known as 'smart contracts, which are self-executing or programmed transactions that enable participants to transact securely without a central authority. It is the basis for all digital payment applications and money that has created a digital economy worldwide.
Most importantly, Ethereum is programmable. It means you can query and create and deploy applications on Etherium.
Web3 is based on Javascript Web3.js
, the most widely-used Python client library for interacting with the Ethereum blockchain. Before the Web3 installation, check if you have installed the latest version of Python on your machine. If you have already installed it on your Windows machine, you can find its version by typing the below in the command prompt:
$ python --version
Then you are good to install the Python web3 library for interfacing with the Ethereum blockchain. Use the following command to install web3:
$ pip install web3
There are two ways you can interact with the Ethereum blockchain. To query it to get information like balances and transactions, you need to connect to its node, much like a connection to a remote database. You can choose a local or a hosted node to query the Ethereum blockchain. This tutorial will use a hosted node to connect with the Ethereum blockchain.
Steps to connect to the Ethereum nodes using a local node:
geth --help
.Steps to connect to the Ethereum nodes using a Hosted node:
Once you have created a new project, you will see all the credentials and endpoints for the hosted Ethereum node.
There are three ways of connecting to the Ethereum nodes:
For this tutorial, we will use the HTTP provider.
Now that the basic Python Web3 and Ethereum hosted node-setups are ready, let's proceed to the next steps to connect with the node and query it. First of all, note down the URL provided by the infra app, which is in the following format:
https://<endpoint>.infura.io/v3/<API_KEY>
There are several different endpoints like mainnet
, ropsten
, Kovan
, rinkeby
, goli
, and sepolia
, and you can format your URL according to the provider type. Also, remember that you can set more security by adding extra security settings for your projects like API Key secret and JWT.
Now, create a folder in your machine, and inside the folder, create a test Python file called query_ethereum_blockchain.py
. Add the following code to it:
from web3 import Web3
# infura API key
API_KEY = "put your API key here"
# change endpoint to mainnet or ropsten or any other of your account
url = f"https://<endpoint>.infura.io/v3/{API_KEY}"
w3 = Web3(Web3.HTTPProvider(url))
res = w3.isConnected()
print(res)
From the beginning, you need to import the web3 library to use the functions to interact with the Ethereum blockchain.
In the url
variable, specify the endpoint URL you marked down earlier. You can also include this in a separate config file and import the URL defined in that config file to the code.
The Web3(Web3.HTTPProvider(url))
is the syntax that uses HTTP to connect to the blockchain. The program will print the following output upon successful connection to the node.
Output:
True
Now that you have connected to the Ethereum nodes, we will use the web3.py library's various functions or methods to query and get different information from the blockchain.
Let's first check the latest block and see what it contains. The latest block data usually contains the various information in an attribute dictionary in the following format:
AttributeDict({
'difficulty': 49824742724615,
'extraData': '0xe4b883e5bda9e7a59ee4bb99e9b1bc',
'gasLimit': 4712388,
'gasUsed': 21000,
'hash': '',
'logsBloom': '',
'miner': '',
'nonce': '',
'number': 2000000,
'parentHash': '',
'receiptRoot': '',
'sha3Uncles': '',
'size': 650,
'stateRoot': '',
'timestamp': 1470173578,
'totalDifficulty': 44010101827705409388,
'transactions': [''],
'transactionsRoot': '',
'uncles': [],
})
latest = w3.eth.get_block('latest')
print(latest)
The output will be long text containing different attributes and a transactions array that looks something like the one below:
AttributeDict({'baseFeePerGas': 18888809092, 'difficulty': 11728765655288682, 'extraData': HexBytes('0x486976656f6e2072752d68656176792d6f6664'), 'gasLimit': 30000000, 'gasUsed': 29552641, 'hash': HexBytes('0xa9155da2bbc945b8eb42f55fbbc9718de50e8cc932372645e36f3d7bb9d4da01'), 'logsBloom': ………………………………….. 'transactionsRoot': HexBytes('0x07b76aa5b8948e19e1f92b268e0545069a9924e8625605c1b4ade775ee99f465'), 'uncles': []})
print(latest['number'])
If you check the output of the latest block, it contains the parent hashes indicating the previous block chained to it. It outputs different data like baseFeePerGas
, extraData
, gasLimit
, etc. Also, if you execute the code several times, note that the latest node changes every time you run the code.
Now, let's check a specific translation using its transaction hash.
transaction1 = w3.eth.get_transaction('0x0e3d45ec3e1d145842ce5bc56ad168e4a98508e0429da96c1ff89f11076da36d')
print(transaction1)
The output of the transaction looks like the below:
transaction2 = w3.eth.get_transaction_by_block(15410924, 0)
print(transaction2)
transactionCount = w3.eth.get_transaction_count('0x486976656f6e2065752d68656176792d657163')
print(transactionCount)
Output: 650
isValid = w3.isAddress('0xed44e77fb3408cd5ad415d7467af6f6783218fb74c3824de1258f6d266bcc7b7')
print(isValid)
isChecksumAddressValid = Web3.isChecksumAddress('0x486976656f6e2065752d68656176792d657163')
print(isChecksumAddressValid)
You can use the eth.get_balance()
RPC Method to query the balance of a specific account at the block specified by the block_identifier
as follows:
balance = w3.eth.get_balance('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
print(balance)
Output:
1790191567590102228
If you want to query the account and storage values such as the balance of the account, hash code, Merkle-proof, storage proof, etc. You can do so by using the get_proof()
method defined as follows:
proof = w3.eth.get_proof('0x486976656f6e2065752d68656176792d657163', [0], 3391)
print(proof)
The result has a format like the below:
AttributeDict({
'address': '',
'accountProof': [],
'balance': 0,
'codeHash': '',
'nonce': 1,
'storageHash': '',
'storageProof': [
AttributeDict({
'key': '0x00',
'value': '',
'proof': []
})
]
})
Uncle block is an unmined block that is not part of the canonical chain. Uncle blocks are created when more than one miner simultaneously creates blocks. They are similar to orphaned blocks which you can track by querying them using the following method in web3:
w3.eth.get_uncle_by_block(15410924, 0)
This returns the uncle block for a specific block identifier and at a specific index. You can also use a hash instead of the block number for the identifier.
In addition, you can find the number of uncle counts in a given block.
w3.eth.get_uncle_count(15410924)
You can also use web3 to send transactions to Ethereum. There are two types of transactions:
In the following code, the transactions objects' to
parameter specifies where to send the transaction, and the from
value specifies from which block it sends the transaction. The nonce
value here is the number of transaction counts:
nonce = w3.eth.getTransactionCount('0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63', 'latest');
transaction = {
'to': '0x31B98D14007bDEe637298086988A0bBd31184523',
'from': '0x31B98D14007bDEe63EREEDFT34544646MOI22',
'value': 500,
'gas': 10000,
'maxFeePerGas': 1000000208,
'nonce': nonce,
};
w3.eth.send_transaction(transaction)
In addition, you can use the signed translation method to sign the transaction using the Ethereum nodes' private key. Following is an example of how you can do that:
nonce = w3.eth.getTransactionCount('0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63', 'latest');
signed = w3.eth.sign_transaction(
dict(
nonce=nonce,
maxFeePerGas=34300000,
maxPriorityFeePerGas=25000000,
gas=100000,
to='0xerecfBYWlB99D67aCDFa17EREFEerrtr73601',
value=1,
data=b'',
)
)
Web3 also has methods to interact with smart contracts in the Ethereum blockchain that are exposed publicly. Two pieces of information are required to interact with them: Abstract Binary Interfaces (ABIs) and the addresses of the smart contracts. You can find this information using the Etherscan block explorer.
First, you need to initialize a contract instance using the ABI and the address:
address = '0x706f6f6c696e2e636f6d21688947c8f76c4e92'
abi = '[{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"minter_",........'
contract = w3.eth.contract(address=address, abi=abi)
Next, you can use different public functions of smart contracts to query the blockchain:
totalSupply = contract.functions.totalSupply().call()
print(totalSupply)
Use the following commands to read the data and update the state:
contract.functions.stored value().call()
tx_hash = contract.functions.updateValue(<statevalue>).transact()
Using simple contract function methods, you can retrieve the token metadata like the number of tokens, contract name, decimals, and contract symbol:
print(contract.functions.name().call())
print(contract.functions.decimals().call())
print(contract.functions.symbol().call())
Output:
SHIBACHU
9
SHIBACHU
Another thing we can do using contract functions is to find the account balance using the balanceOf()
public function:
address = '0x5eaaf114aad1313e7440d2ff805ced993e566df'
balance = contract.functions.balanceOf(address).call()
We started this article by explaining blockchain technology and Ethereum blockchain. Next, we explained how to query the Ethereum blockchain using Pythons' web3.py library, which provides a comprehensive list of methods to query the Ethereum blockchain. We did the following interactions with the Ethereum blockchain using the web3.py
library.
Finally, if you want someone to do this for you, check out our Webisoft.com.
Happy coding ♥
Let our Code Converter simplify your multi-language projects. It's like having a coding translator at your fingertips. Don't miss out!
View Full Code Create Code for Me
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!