Getting started with On-chain Data Analysis in Python using getblock.io
Tags: onchainanalysis, getblock, onchain, python
Published: 2023-06-21 07:26:28

---
title: Getting started with On-chain Data Analysis in Python using getblock.io
published: true
date: 2023-06-20 20:47:31 UTC
tags: onchainanalysis,getblock,onchain,python
canonical_url: https://itnext.io/getting-started-with-on-chain-data-analysis-in-python-using-getblock-io-eab3c8b06fad
---

![](https://cdn-images-1.medium.com/max/1024/0*5sssQ39_9QdxU1EV.png)

On-chain data analysis involves studying the information recorded on a blockchain to gain insights into transaction patterns, market trends, and network behavior. By examining the data stored on the blockchain, analysts can uncover valuable information about user behavior, market sentiment, and the overall health of a blockchain network. In this post, I am going to discuss some basics about on-chain data analysis for blockchain and then will be discussing how we can leverage the [getblock.io](https://account.getblock.io/sign-in?ref=ZGE0Y2JmYmUtOTcyNi01YzJhLTgyMWItNGJhNTY3YzBhNWZh) platform to perform on-chain analysis of transactions using Python language.

### What is On-chain Data Analysis?

On-chain data analysis refers to the process of studying the information recorded on a blockchain. It involves analyzing **_transaction details,_ _network behavior_** , and **_market trends_** to gain insights and make informed decisions. By examining the data stored on the blockchain, analysts can understand user behavior, detect patterns, and evaluate the overall health of a blockchain network.

### Why is On-chain Data Analysis important?

On-chain data analysis is important because it allows us to understand and make informed decisions about blockchain networks. By examining the data recorded on the blockchain, we can gain insights into transaction volumes, market trends, and network behavior, helping us detect anomalies, identify patterns, and make data-driven decisions for various use cases such as investment, risk assessment, and protocol development.

### Benefits of On-chain Data Analysis

- A deeper understanding of transactional patterns, market trends, and network behavior.
- Informed decision-making based on data-driven insights.
- Identification of potential risks and vulnerabilities within a blockchain network.
- Uncovering valuable insights that can drive business growth and innovation.
- Enhanced transparency and accountability in blockchain-based transactions.
- Improved market analysis and prediction capabilities.
- Enhanced security through the identification of suspicious or fraudulent activities.
- Facilitation of regulatory compliance and audit processes.
- Efficient monitoring and tracking of specific wallet addresses or smart contracts.
- Identification of market manipulations and trading patterns.

### Key Matrices and Insights

Various matrices can be analyzed from on-chain data, providing valuable insights into different aspects of blockchain networks. Some of the matrices that can be analyzed include:

- **Transaction Volume:** Analyzing the total number of transactions occurring on a blockchain network over a specific period of time.
- Transaction Frequency: Examining the frequency at which transactions are being conducted on the blockchain network.
- Transaction Value: Analyzing the total value of transactions being processed on the blockchain.
- Wallet Balances: Studying the distribution of wallet balances to identify patterns and concentrations of wealth.
- Block Size and Block Time: Analyzing the size of blocks and the time it takes to mine new blocks, which can provide insights into network scalability and congestion.
- **Transaction Fees:** Examining the fees associated with transactions to understand the cost dynamics and network efficiency.
- **Smart Contract Interactions:** Analyzing the usage and activity of smart contracts, including the number of interactions and the volume of funds involved.
- **Network Hashrate:** Assessing the computing power dedicated to mining activities on a blockchain network, which can provide insights into network security and decentralization.
- **Address Clustering:** Identifying clusters of addresses to understand the flow of funds and potential relationships between different entities.
- **Token Distribution:** Analyzing the distribution of tokens among different addresses to assess the fairness and decentralization of a blockchain network.

I can’t cover all of them as this is not the purpose of this post either. I am just going to use the getBlock.io API endpoint to fetch the number of transactions recorded on the network after a certain period of time. Before we dive in, let’s set up the [getblock.io](https://account.getblock.io/sign-in?ref=ZGE0Y2JmYmUtOTcyNi01YzJhLTgyMWItNGJhNTY3YzBhNWZh) account.

### Setting up the account

Setting up an account is pretty easy. You can connect your MetaMask wallet, email, or Google. Signup by visiting this [link](https://account.getblock.io/sign-in?ref=ZGE0Y2JmYmUtOTcyNi01YzJhLTgyMWItNGJhNTY3YzBhNWZh)( _It’s an affiliate link that could help me to earn a few bucks 😊_ ). Once you are registered, you will be welcomed by a screen something similar to this( _June 2023_):

![](https://cdn-images-1.medium.com/max/1024/0*m_VFmEfRckRRJrB8.png)

You give your project name. On clicking the “Key” icon you will get your API key. So far so good.

Before we move on further to our desired API, let’s make a testing request. Head over to the docs, click on **Nodes Endpoints** where you will find both Main and TestNet API endpoints of the different currencies.

![](https://cdn-images-1.medium.com/max/1024/0*Y6SwH23HmXHdOnTt.png)

Now head on to the next section, **Available Nodes Methods,** here you will find a list of currencies. Since I am focusing on BTC so I just select it. You will be looking at something similar below:

![](https://cdn-images-1.medium.com/max/1024/0*6iQ8pew5Z6p6-266.png)

On the left you see a list of methods offered by Bitcoin Network, this is the same thing if you install a local node and _bitcoin-cli_ tool. Anyways! I am going to use the very first method for the testing purpose. I just copy the CURL request and test on CLI:

![](https://cdn-images-1.medium.com/max/1024/0*WO0TAgoLWsFCoErP.png)

And it generates the following output:

![](https://cdn-images-1.medium.com/max/1024/0*XlWJDKB9V5YOgs4t.png)

Needless to say that the _API-KEY_ given here has been fabricated with the help of [chatGPT](https://blog.adnansiddiqi.me/create-stock-sentiment-analysis-in-python-using-chatgpt/) so **_don’t try this at home_**  😉

Alright! So our target is to get transaction-related info. Here is another example. The endpoint/function I am going to is [getchaintxstats](https://developer.bitcoin.org/reference/rpc/getchaintxstats.html) which computes statistics about the total number and rate of transactions in the chain. After you create your own end-point, you can make a cURL request like the one below as well:

![](https://cdn-images-1.medium.com/max/1024/0*fkRqDLpCoR9avL-i.png)

getBlock supports both REST and JSON-RPC. I am using JSON-RPC here.

Alright!! So the code I am going to write is going to store the total transactions that occurred in the latest block. The info will be stored in the database. Later we will visualize it by plotting a graph.

Our basic script, as I said, going to fetch the # of transactions in the latest block for the Ethereum blockchain. This will be done in two steps. In the first step, we will be using [_eth\_blockNumber_](https://getblock.io/docs/available-nodes-methods/ETH/JSON-RPC/eth_blockNumber/) which will fetch the latest block in hex which will eventually be converted into a decimal number. Later, [_eth\_getBlockTransactionCountByNumber_](https://getblock.io/docs/available-nodes-methods/ETH/JSON-RPC/eth_getBlockTransactionCountByNumber/) function returns the transaction count in the given block. Below is the code:

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

The slick interface of the dashboard tells you which methods were called and how many times:

![](https://cdn-images-1.medium.com/max/1024/0*NcbDlhApj11mca8I.png)

API\_KEY is being picked from.env file. Later, the relevant requests are made to the getBlock.io website. Once the info is available, it is stored in an SQLite DB. The data produces the chart like the one below:

![](https://cdn-images-1.medium.com/max/1024/0*PETbaSGCcKcBYBvZ.png)

The visualization you see above is simple yet very important.

The graph depicting the transaction count in the latest block over time provides valuable insights into the dynamics and health of a blockchain network. Understanding the importance of this information is crucial for several reasons:

1. **Network Activity and Adoption:** Monitoring the transaction count in the latest block allows users to gauge the level of activity and adoption of the blockchain network. A higher transaction count indicates increased usage and demand, suggesting a thriving ecosystem and growing user base.

2. **Scalability Assessment:** By analyzing the transaction count over time, one can assess the network’s scalability. A consistently increasing transaction count, accompanied by stable or decreasing confirmation times, indicates a network capable of handling growing demand and suggests scalability.

3. **Performance and Efficiency:** Fluctuations in the transaction count can reveal the performance and efficiency of the blockchain network. Spikes or sudden drops in transaction counts may indicate congestion, network limitations, or significant events impacting transaction activity, highlighting the need for scalability improvements or further analysis.

4. **Fee Dynamics and Prioritization:** Transaction count influences fee dynamics, as higher transaction volumes can lead to increased competition for block space, resulting in higher transaction fees. Users can utilize this information to make informed decisions about transaction prioritization and fee optimization.

### Conclusion

On-chain analysis besides Fundamental and Technical Analysis for Cryptocurrency can give you some useful insights that could help you in your next trade. The getblock.io makes it easy and seamless to connect with various blockchain networks by using their standard APIs. You do not need to worry about setting up a node or other things. Pretty cool.

getBlock.io provides both free and paid plans.

![](https://cdn-images-1.medium.com/max/1024/0*pgBWdAulB5Fc5mgy.png)

You can play with it using the free tier to make 40K requests per day. Once you are satisfied you can go for the paid plan to make millions of requests. **Oh and if you signup** by using [my referral](https://account.getblock.io/sign-in?ref=ZGE0Y2JmYmUtOTcyNi01YzJhLTgyMWItNGJhNTY3YzBhNWZh), it could help me to earn a few bucks to maintain my blog in the future 🙂

_Originally published at_ [_https://blog.adnansiddiqi.me_](https://blog.adnansiddiqi.me/getting-started-with-on-chain-data-analysis-in-python-using-getblock-io/) _on June 20, 2023._

* * *