How to get accurate transaction volume for Bitcoin

Getting Bitcoin real time transaction volume using simple Bitcoin API Implementation with Python

How to get accurate transaction volume for Bitcoin

One of the essential things in the financial world is to know exactly what’s going on in global trades before everyone else to make a proper design. That’s why most of the financial trading companies have been putting servers directly inside the same network where that trades happen, to be able to know about it within a few milliseconds.

Things are a bit different with the blockchain; you still need to get accurate and fast information about what is happening inside blockchain transactions, but available for public access. Everyone who runs a blockchain node is getting that real-time data, making it even more competitive to act fast when there is a spike in blockchain transactions.

Bitcoin Volume in Exchanges

Almost all centralized exchanges like Binance, Coinbase, etc... are not executing trades when someone does BTC/USDC trade because they have an internal system to handle that. BTC Wallet is shared across many other customers, so your transaction goes to an actual Blockchain whenever you try to Widthrow your BTC outside of that centralized exchanges. That means that the BTC volume you see inside your exchange does not represent an actual Bitcoin Network volume and could harm your investment design making or mislead your automated trades.

The most difficult part of looking at centralized exchange volumes is that even if the exchange itself will have some automated bots to open and close large trades, you would never know because they are not visible in an actual blockchain transactions list. So to get an accurate view of what is going on for a specific currency that you are tracking, you have to get a precise blockchain volume.

Transactions that haven’t been confirmed yet (Mempool)

Bitcoin has this concept called Mempool, which represents unconfirmed transactions be confirmed or rejected when the next block is mined. The idea is quite simple, where the overall picture is that each transaction will have a specific fee to pay miners for confirming the next block, which makes the possibility that miners can reject transactions if, for example, they provide a lower fee than it has been expected. When a new block is mined, transactions have been verified, and there wouldn’t be any changes later on.

Mempool is a representation of “in-flight” transactions, which might help you to understand the overall health of BTC. If transaction volume is growing, more and more people are actively using that currency, and prices might go up. Well, this is not investment advice, just a thing to consider 🙂

Getting Mempool Transactions

Blockpulsar.com makes it simple to get a list of Mempool transactions without having an actual Bitcoin Node setup or paying for real-time data. After getting a FREE API Key and API Secret from https://blockpulsar.com, you can get a list of current transaction ids by just making a simple HTTP Request like this

curl -H "x-api-key=<api key>" -H "x-api-secret=<api secret>" \
      https://api.blockpulsar.com/btc/getrawmempool

The result will be a list of transaction IDs in Mempool at the moment. For example, if you follow a specific transaction that you’ve submitted or know an id, it will help you track it whenever it gets confirmed or rejected.

To calculate an actual transaction volume, we have to do the following

  • Get a list of transactions that are in Mempool at the moment

  • Fetch transaction data based on id and calculate its input value

  • Loop over transactions data and calculate a sum of their value

To implement this logic, we will use Python, but you can do this in any other language because this is a pretty matchmaking HTTP Requests to Blockpulsar’s REST API Endpoints.

To get started, lets grab a list of transactions inside Mempool and fetch their data based on Blockpulsar’s Documentation

import requests

client = requests.Session()
client.headers['x-api-key'] = "<your api key>"
client.headers['x-api-secret'] = "<your api secret>"

r = client.get("https://api.blockpulsar.com/btc/getrawmempool")
transaction_ids = r.json()

transactions = []
for txid in transaction_ids:
   r = client.get(
 "https://api.blockpulsar.com/btc/getmempooltransaction?txid" + txid
   )
   transactions.append(r.json())

Now inside the transactions array, we have a list of mempool transactions with complete Transaction data, which we can use to calculate transaction input BTC value, representing how many BTCs have been sent over with that transaction.

Calculating transaction input value

Each BTC transaction contains vin and vout fields, which represent how many BTCs have been used as an input and how to match been transferred to which addresses as an output. For our use case here, we will focus on vin because it represents how to match BTC been used to transact, which includes newly mined BTCs as well.

def getTransactionInputVolume(transaction):
   total_value = 0
   for vin in transaction["vin"]:
     total_value += vin["value"]
   return total_value

This function loops over transaction inputs and calculates how many BTCs have been used as an input for this transaction.

To finalize our logic we will get something like this

import requests

client = requests.Session()
client.headers['x-api-key'] = "<your api key>"
client.headers['x-api-secret'] = "<your api secret>"

r = client.get("https://api.blockpulsar.com/btc/getrawmempool")
transaction_ids = r.json()

def getTransactionInputVolume(transaction):
   total_value = 0
   for vin in transaction["vin"]:
     total_value += vin["value"]
   return total_value

total_volume = 0
for txid in transaction_ids:
   r = client.get(
 "https://api.blockpulsar.com/btc/getmempooltransaction?txid" + txid
   )
   transaction = r.json()
   total_volume += getTransactionInputVolume(transaction)

print("Current BTC Mempool Volume -> ", total_volume)

This looks quite simple, but because there are too many transactions within a minute or even less, we might get a bit outdated data if we want to get precise millisecond delays. The main reason is handling all of this network traffic on our laptop or even server because there are around 5–10K transactions that we have to fetch. For that specific use-case, we have a custom aggregations feature for business customers in Blockplsar.com, so if that’s your requirement, it is doable 🙂

How can you use this data?

The main intention of getting mempool volume is to know if BTC Network is active or not and how to match BTC is overall being transacted over some time. That information you can use to decide as an investor what to do, or if you are writing some trading bot, knowing the overall Bitcoin activity will help with your trading precision.

Please let us know if you want to discuss some of the use-cases with Mempool or transaction tracking in general.

Deploy Your First Solana Smart Contract Now!

Blockpulsar provides seamless abstraction layers for building and deploying Solana Programs (smart-contracts) without having to have local Solana Validator setup or complicated key management. You can focus on building an actual smart-contract logic, while we handle the rest.

Blockpulsar.com

Blockpulsar makes smart contract development simple, flexible and scalable by providing CI/CD for smart contracts.

© 2022 Copyright Blockpulsar Inc.