How to Query the Ethereum Blockchain with Python

Learn how to interact with Ethereum blockchain like retrieving blocks, check for validation, making transactions and more using Python.
  Guest Contributor · 11 min read · Updated sep 2022 · General Python Tutorials

Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting 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:

What is the Ethereum Blockchain?

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.

Steps to Follow Before Querying the Ethereum Blockchain

Step 1 - Install Web3

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

Step 2 - Choose How you Connect to the Ethereum Nodes

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.

  • Local Node - A local node is one that started in your local machine using providers like Geth or Parity. It allows you to be more secure when connecting to the nodes. However, you need additional work to set up the local node, which will take significant time and resources on your computer.

Steps to connect to the Ethereum nodes using a local node: 

    1. Download and install Geth.
    2. Once it is installed, type geth --help.
    3. Start Get and wait until it syncs the network.
  • Hosted Node - a hosted node is a node created and hosted by a third party over which you don't have any control. It is less secure than a local node because there can be malicious hosted nodes. However, no additional setup time or resources are required if you choose a hosted node. 

Steps to connect to the Ethereum nodes using a Hosted node:

  1. Create an account in Infura.
  2. Go to the infura.io dashboard and create a new project, providing a name. 

Once you have created a new project, you will see all the credentials and endpoints for the hosted Ethereum node.

Step 3 - Connect to the Ethereum Nodes

There are three ways of connecting to the Ethereum nodes:

  • HTTP
  • WebSockets
  • IPC

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

How to Query the Ethereum Blockchain

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.

Retrieving the Latest Block

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': [],
})
  • Use the following command to query the latest block:
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': []})

  • You can also print the block number as follows:
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.

Querying Individual Transactions

Now, let's check a specific translation using its transaction hash.

  • Following is how you can get information about a particular transaction:
transaction1 = w3.eth.get_transaction('0x0e3d45ec3e1d145842ce5bc56ad168e4a98508e0429da96c1ff89f11076da36d')

print(transaction1)

The output of the transaction looks like the below:

  • You can also use the block number to query the transaction as follows:
transaction2 = w3.eth.get_transaction_by_block(15410924, 0)
print(transaction2)
  • If you want to get the number of transactions in a particular block, here is how you can do it:
transactionCount = w3.eth.get_transaction_count('0x486976656f6e2065752d68656176792d657163')
print(transactionCount)

Output: 650

Checking if a Block Address is Correct

  • Ethereum blockchain has specific Ethereum address formats. You can use the following code to check if a given address is valid or specified in the standard address format:
isValid = w3.isAddress('0xed44e77fb3408cd5ad415d7467af6f6783218fb74c3824de1258f6d266bcc7b7')
print(isValid)
  • Also, you can check if an address is a valid EIP55 checksummed address using the following method:
isChecksumAddressValid = Web3.isChecksumAddress('0x486976656f6e2065752d68656176792d657163')
print(isChecksumAddressValid)

Checking the Balance of a Particular Block

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

Query the Storage Values of an Account

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': []
        })
    ]
})

Querying Uncle Blocks

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)

Sending Transactions

You can also use web3 to send transactions to Ethereum. There are two types of transactions: 

  • Balance transfer transactions where you send eth to another address without requiring data. 
  • Also, smart contract transactions from which can send some smart contract code. Following is a simple code that signs and sends a particular transaction. 

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'',
    )
)

How to Use Smart Contract Functions?

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()

Retrieving the Token Metadata

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

Finding the Account Balance 

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()

Conclusion

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. 

  1. How to get the latest block.
  2. How to query individual transitions.
  3. How to check the validity of an Ethereum block.
  4. How to query storage values of an account.
  5. What are uncle blocks, and how to query them.
  6. How to send transactions.
  7. Some basic things you can do with smart contract functions.

Finally, if you want someone to do this for you, check out our Webisoft.com.

Happy coding ♥

Want to code smarter? Our Python Code Assistant is waiting to help you. Try it now!

View Full Code Fix My Code
Sharing is caring!



Read Also



Comment panel

    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!






    🕒 Black Friday Limited-Time Offer!
    EBook Image

    Black Friday Week Special! Get our eBook Bundle at 52% off. Limited time only!

    $102.0 $49.0