There are ample blockchain explorers online to query Ethereum wallet balance. But have you ever thought of checking the ETH balance directly from your command-line in a couple of steps? Let's create a tool for that with only 6 lines of code! 😎

Requirements

  • Python Web3 Library
    Install Python 'Web3.py' Library by executing the following command:

    pip install web3
    
  • Ethereum Node API Endpoint
    Though there are many Ethereum node service providers available online, we'll use Pocket Network for our project as it is free.

    1. Signup for an account at Pokt.network and verify your email address to get access to the endpoints.

    2. Select Apps from the side menu and click Create.

    3. Enter desired App Name and click Launch Application. Create App

    4. Copy the Endpoint provided. (Ensure that Ethereum Mainnet is selected as the Endpoint network! Copy Endpoint

The web3 library will allow us to interact with the Ethereum blockchain through the API endpoint.

Let's Code!

  1. Import required modules

    from web3 import Web3, HTTPProvider
    
  2. Save the endpoint URL in a variable

    endpoint = 'YOUR_ENDPOINT_URL_HERE'
    
  3. Make a connection to Ethereum blockchain through the endpoint

    connection = Web3(HTTPProvider(endpoint))
    
  4. Get the wallet address input from the user

    address = input("Enter ETH wallet address: ")
    
  5. Fetch the latest ETH balance from the blockchain and convert it to ether denomination.

    balance = connection.fromWei(connection.eth.getBalance(address, 'latest'), 'ether')
    
  6. Display the balance

    print(f"Balance: {balance} ether")
    
https://gist.github.com/abraarahmed/e2e93e6a30ef254920d9b18cc23f45d6

Save the file; run it; enter ETH address and get your balance right in the terminal. That simple!

Result

So, what's your balance? 😁

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐