How I Use GetBlock API For Onchain Data Analysis
What Exactly Is Onchain Data Analysis?
Imagine you have a clearer microscope that lets you peek into the inner workings of blockchain networks, showing how transactions flow, how users behave, liquidity movement, market trends and how the entire ecosystem thrives. This is what onchain data analysis offers, with GetBlock API.
For example, I can look at how many transactions are happening on Ethereum or Solana, how much value is being moved, or whether certain wallets are acting suspiciously. This raw data is a goldmine for understanding what is really going on in a blockchain network (Onchain).
Why This Matters With GetBlock
As a DeFi trader (and researcher), I use onchain data to spot trends that indicate shifts in market sentiment and liquidity movement on any blockchain. Similarly, as developers who understand network behaviour, using GetBlock API provides easy access to blockchain data via APIs, eliminating the need to run your own node.
See how I combine onchain analysis with GetBlock API endpoint to fetch the number of transactions recorded on a blockchain network after a certain period of time. Before we dive in, let’s set up the
Setting up an account with GetBlock
Step 1
Visit getblock.io. You can choose to connect your MetaMask wallet, email, or Google to signup. Once your account is created, you will be welcomed with a screen something similar to this below.
Step 2
Before diving into the analysis, I’m gonna test the API to ensure it’s working, using Bitcoin as example here. Head to the GetBlock documentation and find the API Reference section, such as Aptos, Bitcoin, and Ethereum.
I am going to use the very first method for the testing purpose. I just copy the CURL request and test on CLI:
And it generates the following output:
So my target is to get transaction related info on Bitcoin. Here is another example. The endpoint/function I am going to is
GetBlock supports both REST and JSON-RPC. So, I choose to use JSON-RPC here.
So the next code is going to store the total transactions that happened in the latest block. The infomation will be stored in the database that will be later visualised by plotting a graph.
My basic script is going to fetch the number of transactions in the latest block for the Ethereum blockchain. This will be done in just two steps. The first step, we will be using
import datetime
import requests
import json
import sqlite3
import sqlite3
import datetime
from time import sleep
from dotenv import dotenv_values
def store(block_number, tx_count):
try:
current_date = datetime.datetime.now()
formatted_date = current_date.strftime("%Y-%m-%d %H:%M:%S")
connection = sqlite3.connect("tx.sqlite3")
cursor = connection.cursor()
cursor.execute("INSERT INTO tx_data(block_no,tx_count,date) VALUES (?,?,?)",
(block_number, tx_count, formatted_date))
connection.commit()
connection.close()
except Exception as ex:
print(ex)
if __name__ == '__main__':
key = dotenv_values(".env")
API_KEY = key['API_KEY']
API_END_POINT = 'https://eth.getblock.io/{}/mainnet/'.format(API_KEY)
headers = {'Content-Type': 'application/json'}
# Construct the JSON-RPC payload to get the latest block number
latest_block_number_payload = {
'jsonrpc': '2.0',
'method': 'eth_blockNumber',
'params': [],
'id': 1
}
while True:
# Send the JSON-RPC request to get the latest block number
latest_block_number_response = requests.post(API_END_POINT, headers=headers, json=latest_block_number_payload)
latest_block_number_result = latest_block_number_response.json()
latest_block_number_hex = latest_block_number_result['result']
latest_block_number = int(latest_block_number_hex, 16)
print('Latest Block# ', latest_block_number)
# Construct the JSON-RPC payload to get the transaction count for the latest block
transaction_count_payload = {
'jsonrpc': '2.0',
'method': 'eth_getBlockTransactionCountByNumber',
'params': [hex(latest_block_number)],
'id': 2
}
# Send the JSON-RPC request to get the transaction count for the latest block
transaction_count_response = requests.post(API_END_POINT, headers=headers, json=transaction_count_payload)
transaction_count_result = transaction_count_response.json()
transaction_count_hex = transaction_count_result['result']
transaction_count = int(transaction_count_hex, 16)
store(block_number=latest_block_number, tx_count=transaction_count)
print('Transaction Count: ', transaction_count)
sleep(60)
So the slick interface of the dashboard show which methods were called and how many times:
The API_KEY is picked from.env
file. And later, the relevant requests are made to the getBlock.io website. Once the info is available, it is stored in an SQLite DB. And the data produces the chart like the one below:
This visualization above is simple yet very important.
The graph shows the transaction count in the latest block over time, providing valuable insights into the dynamics and health of a blockchain network. Understanding the importance of this information is crucial for several reasons I will state below:
- Network Activity and Adoption: Monitoring transaction count in the latest block allows users to gauge the level of activity and adoption of the blockchain network. Higher transaction count indicates increased usage and demand, suggesting a thriving ecosystem and growing user base.
- Scalability Assessment: Analyzing the transaction count over time, shows the network scalability. A consistently increasing transaction count, accompanied with stable or decreasing confirmation times, indicates a network capable of handling growing demand, this suggests scalability.
- Performance and Efficiency: Fluctuations in the transaction count can show the performance and efficiency of the blockchain network. Spikes or sudden drops in transaction counts can indicate congestion, network limitations, or significant events impacting transaction activity, highlighting the need for scalability improvements or further analysis.
- Fee Dynamics and Prioritization: Transaction count does influence fee dynamics, as higher transaction volumes can lead to increased competition for block space, resulting in higher transaction fees. Users can use this information to make informed decisions about transaction prioritization and fee optimization.
To Round up; my conclusion
Onchain analysis besides Fundamental and Technical Analysis for Cryptocurrency gives some useful insights that could help you in any trade. GetBlock makes it easy and seamless to connect with various blockchain networks by using their standard APIs. So you don’t need to worry about setting up a node or other things.
.