In this guide, we’re going to demonstrate how to write and run the simplest script for tracking Ethereum wallet balance with GetBlock’s RPC API.
What is Token Ownership?
Token ownership is just a digital version of holding an asset. It is similar to holding stocks, currencies, or commodities like gold or silver. Each token is linked to an account (or wallet address), and the balance of tokens in that account reflects ownership.
When you acquire tokens, whether you are buying them on an exchange through trading, getting them as staking rewards, or someone giving you as a gift, they are transferred to the respective wallet addresses and you become their rightful owner. This ownership allows you to use these tokens in any possible way.
Why is Tracking Tokens Important?
Just like you keep an eye on other assets; whether they’re going up or down, tracking digital tokens is also very necessary. Tracking the tokens you own serves many purposes. First, it gives you a clear picture of your portfolio and its standing.
Second, it helps you to stay on top of your investments. Also, it can be useful while tracking “interesting” wallets of whales, developers, team members, and so on.
Accessing Ethereum blockchain using GetBlock API
You may be curious about how to retrieve blockchain data, verify wallet balances, and perform various tasks programmatically. Enter GetBlock, a Blockchain-as-a-Service (BaaS) platform that provides a fast and easy API connection to full nodes from 50+ leading blockchain platforms. It takes the headache out of tracking your token holdings.
Think of it as your personal blockchain data assistant that provides quick and reliable access to token information. It facilitates you to request on-chain information of a blockchain without setting up manually with JSON-RPC, REST, and WebSockets.
In this post, we are going to access the Ethereum blockchain and we will be fetching the ETH balance of a certain wallet. We will be using the API endpoint that retrieves the balance. I will be showing how you can use APIs in Python.
Creating an Access Token
To access GetBlock API you should first create an access token: go to GetBlock and sign up. Once you are signed up and verified, you will be welcomed by this dashboard:
To access APIs you would need to create an access token first. Initially, no access token is available.
Selecting the Blockchain Protocol and Network
If you click on the Start Now button, you will be asked to select the protocol, network, and API.
I selected Ethereum as the protocol, Mainnet as the network, and JSON-RPC as the API format. Once you click the Get button you will see an entry for Ethereum like the one below:
Retrieving the API Endpoint
Now click on Ethereum’s entry and you will see something like the below:
As you can see, it gives the main API URI along with the access token: https://go.getblock.io/f7f1e4a673301d0fc69a9848848
Please note that this access token is available for this very network only.
We have selected the node and the network.
Now all we need is an Ethereum wallet address so that we can find how many ETH coins it has. We are going to use the eth_getBalance method for this purpose.
On the page, you will see a curl example like the one below that you can run on the terminal
curl --location --request POST 'https://go.getblock.io/<ACCESS-TOKEN>/' --header 'Content-Type: application/json' --data-raw '{"jsonrpc": "2.0", "method": "eth_getBalance", "params": ["0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", "latest"], "id": "getblock.io"}'
You set JSON headers, the method parameter is set to eth_getBalance and the params contain the wallet address.
Checking Balance with cURL
From the EtherScan website, I just picked a random wallet (0xF977814e90dA44bFA03b6295A0616a897441aceC) which you can visit here.
As you can see the balance (at the time of writing the article) is 799,655.363544175804339796 ETH. Now, first, run the cURL request (real access token has been tempered) in the terminal:
curl --location --request POST 'https://go.getblock.io/f7f1e4a67330490ea69ab58737c7' \ --header 'Content-Type: application/json' \ --data-raw '{ "id": "1", "jsonrpc": "2.0", "method": "eth_getBalance", "params": [ "0xF977814e90dA44bFA03b6295A0616a897441aceC", "latest" ] }'
When I run this, I get the following response:
{"jsonrpc":"2.0","id":"1","result":"0xa955677463bb9136d654"}
The result
field contains the balance of the wallet in HEX format.
Automating Balance Checks with Python
Now convert this into a Python script:
import requests import json # Set the URL and headers url = "https://go.getblock.io/f7f1e4a67330490da69ab5bd9a9bf127" headers = { "Content-Type": "application/json" } # Set up the request payload payload = { "id": "1", "jsonrpc": "2.0", "method": "eth_getBalance", "params": [ "0xF977814e90dA44bFA03b6295A0616a897441aceC", "latest" ] } # Send the POST request response = requests.post(url, headers=headers, data=json.dumps(payload)) # Check if the request was successful if response.status_code == 200: # Convert the hex balance to decimal and then to Ether balance_wei = int(response.json()['result'], 16) balance_eth = balance_wei / 10**18 # Convert Wei to Ether print(f"The balance of the address is: {balance_eth} ETH") else: print(f"Error: {response.status_code}") print(response.text)
Assuming you have Python installed on your machine and you have saved the above code in the file named balance.py, when you run this code it prints:
The balance of the address is: 799655.3635441758 ETH.
The balance_eth value matches the balance shown on Etherscan, proving the accuracy of this API.
The balance_eth value matches the balance shown on Etherscan, proving the accuracy of this API.
As you see, in just a few lines of code you can track a portfolio. From here, the possibilities are endless; you can come up with a notification system that will alert you how much your portfolio goes up or down, a custom dashboard that caters to your needs, and many other things. GetBlock made it easier and smooth.
Closing thoughts
To wrap things up, keeping track of token balances and wallet holdings is essential for managing digital assets effectively. GetBlock makes it easy for developers to access Ethereum’s blockchain data without the hassle of setting up and maintaining nodes. By using methods like eth_getBalance, you can quickly get real-time information on wallet balances, which helps stay on top of your assets.
With GetBlock you can focus on building new features and applications. Whether you’re working on dApps, managing a portfolio, or building financial tools, GetBlock’s straightforward API access makes interacting with the blockchain smooth and efficient.
Starting from November 25, 2024, GetBlock is welcoming all its users to join the Black Friday promo event. Whether you’re a new or experienced user, GetBlock has something for you to offer. The last day to redeem this offer is Monday, December 2, 2024.