The Bancor subgraph stores every swap on the platform. To calculate trading volume, you could iterate through hundreds of thousands of swaps. Or, you can use time-travel queries from The Graph.
How it works
- Choose a converter and token pair.
- Choose a time range.
- Calculate volume.
Example case: ETHBNT 24h Volume
Step 1. Choose a converter and token pair
We're going to check volume in the ETHBNT converter because of the recent airdrop of ETHBNT tokens. The details for this converter are:
Converter: 0xd3ec78814966ca1eb4c923af4da86bf7e6c743ba
ETH Token: 0xc0829421c1d260bd3cb3e0f06cfe2d52db2ce315
BNT Token: 0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c
Step 2. Choose a time range.
We are interested in the last 24 hours as of writing.
Time ranges must be provided in blocks. We will take a block at the time of writing and a block that is 24 hours older.
End block: #9292572 Jan-16-2020 01:50:28 PM +UTC
Start block: #9286090 Jan-15-2020 01:50:32 PM +UTC
For more information about converting timestamps to block numbers, see my followup post Ethereum Blocks Subgraph: for Time Travelers
Step 3. Calculate volume
Total volume for each converter is saved after every trade. You can pull volume data for any given block and compare it to a previous block to see the difference.
We will run this query on the subgraph. The raw results (directly below) may not look like much and need to be transformed into something human-readable.
{
"data": {
"endBntBuyVolume": [
{
"totalAmountReturned": "6237569890668822489725678"
}
],
"endBntSellVolume": [
{
"totalAmountPurchased": "7944553056942192988136912"
}
],
"startBntBuyVolume": [
{
"totalAmountReturned": "5812329121958058809197030"
}
],
"startBntSellVolume": [
{
"totalAmountPurchased": "7582447494161263774269180"
}
]
}
}
Transforming the data
These figures are the total buy volume and sell volume of BNT tokens at two periods in time. We will transform the data to show total volume between these periods.
Normalize the numbers. The figures are displayed as integers. Convert them to decimals by dividing each by 10 ^ 18 (BNT and ETH token both have 18 decimals). For example:
{integer value} / 10 ^ {token decimals} = {decimal value}
6237569890668822489725678 / 10 ^ 18 = 6,237,569.89067
Calculate the total volume of each snapshot. Total volume is the sum of its Buy and Sell volume. For example:
{endblock buy volume} + {endblock sell volume} = {endblock volume}
6,237,569.89067 + 79,44,553.05694 = 14,182,122.9476 BNT
Calculate the difference between snapshots. Identify the volume for the period by deducting “start block” from “end block” data. For example:
{endblock volume} - {startblock volume} = {volume for timeframe}
14,182,122.9476 - 13,394,776.6161 = 787,346.3315 BNT
We can see trading volume for this converter during this time period was 787k BNT.
Where to go from here
Now you can select any start and end blocks to identify volume for any given timeframe. You can also change the converter address or do more advanced queries.
- Get individual swaps - example query
- Get network-wide volumes for each pair - see query
- Use the Ethereum Blocks subgraph - read post