Your first example

Build your first chart with BubbleTea

In this example, we will use the on chain data of AAVE to visualize its daily deposit information. Create a hello.py file and paste below code:

import bubbletea
import math
import streamlit as st

url = "https://gateway.thegraph.com/api/[THE_GRAPH_API_TOKEN]/subgraphs/id/0x0d69090672c1e5a94e9aedfbc59558d18a78e1d3-0"
query = """
{
    deposits(
        where:{timestamp_gt:1609459200, timestamp_lt:1610236800}
        orderBy: timestamp
        orderDirection: asc
        bypassPagination: true
    ) {
        amount
        timestamp
        reserve {
            symbol
            decimals
        }
    }
}
"""

data = bubbletea.beta_load_subgraph(url, query)
data = data["deposits"]
data = data[data["reserve.symbol"] == "AAVE"] 
data["amount"] = data["amount"] / math.pow(10, 18)

bubbletea.beta_plot_line(
    title="AAVE Deposits",
    df=data,
    x={"title": "Time", "field": "timestamp"},
    y={
        "title": "Total Deposit", 
        "data":[{
            "title": "Amount", 
            "field": "amount"
        }]
    },
    legend="none"
)

Type bubbletea hello.py in a command line terminal and run it.

Let's break down the code

First, let us prepare the URL and the desired query of your subgraph. In this case, we want to pull the deposit transaction data from Jan 1st to Jan 10th ((unix timestamp 1609459200 - 1610236800).

url = "https://gateway.thegraph.com/api/[API KEY]/subgraphs/id/0x0d69090672c1e5a94e9aedfbc59558d18a78e1d3-0"
query = """
{
    deposits(
        where:{timestamp_gt:1609459200, timestamp_lt:1610236800}
        orderBy: timestamp
        orderDirection: asc
        bypassPagination: true
    ) {
        amount
        timestamp
        reserve {
            symbol
            decimals
        }
    }
}
"""

Q: How do I find the url of a subgraph that I am interested in? A: Check out the Explorer page. You also need to apply for an API key via The Graph Studio page in order to interact with the subgraphs. There is also a legacy page where you can find subgraphs which are free to query. In this example, we use a subgraph from the legacy page.

Q: How do I know what graphQL queries to use? A: Each project page on the Explorer page has a "Playground" tab. This is where you can experiment your queries.

Q: What is "bypassPagination"? A: This is a special flag we created to bypass the API request limitations on the Graph network. Currently, each request can only return up to 1000 items. If you have this flag in your query, the library will continuously fetch the data for you in the background.

Then use the function beta_load_subgraph . It takes two parameters: one is the url of the subgraph and the other is the GraphQL query. By using this function, you do not need to worry about exceeding the limit per request that The Graph API currently has. bypassPagination:true is a special flag that you need to have in your query in order to bypass the API limit. This function also automatically converts the returned data fields to their proper type instead of the default string type.

data = bubbletea.beta_load_subgraph(url, query)
data = data["deposits"]

Here is what the data looks like:

      amount                                                 id           timestamp  reserve.decimals reserve.symbol
1.000000e+17  0xa96fd601723d4987ccdbb71ff2b5e13f8556f32f55e8... 2021-01-01 00:06:10                18           WETH
9.112238e+19  0x4db291d2d1d92ac224065c3f5d9db331513c0500e1d9... 2021-01-01 00:08:11                18            DAI
8.563751e+06  0x313b16e8581ba50d6240f5d0ae7d5854980e029d36e5... 2021-01-01 00:08:13                 8           WBTC
1.400000e+20  0xad3d166c2d20f3c42eeb6067a0d3bac2d769070c9396... 2021-01-01 00:14:15                18            CRV
3.976100e+20  0x24d9e09d6ede9eac919506f91b1433270c7dd6c2d951... 2021-01-01 00:14:19                18            UNI
...           ...                                                               ...               ...           ....
1.140347e+10  0xea7c7f95eac506b6a8e051908ffff2bb17b6c0531aa1... 2021-01-09 23:39:58                 6           USDC
3.000000e+19  0xff19980a305f6b0e50619dfc60c0a7818c475277f866... 2021-01-09 23:43:39                18           WETH
1.749549e+21  0xfe64e3518dbf16a3d221c71f7cbfb13b4c769c885bf2... 2021-01-09 23:50:13                18           AAVE
1.185536e+09  0xe6315122134456e54fc79edbf7d1440eeb794a9446b5... 2021-01-09 23:51:42                 6           USDT
7.400000e+19  0x8d98c1b70af9b0e4b4829b41e58bbd2c806c4dae97c1... 2021-01-09 23:59:20                18           WETH

After getting the data, we will do some transformation. We only need data whose value of the "reserve.symbol" column is AAVE . And we will also convert the "amount" column to a value without the token decimals.

data = data[data["reserve.symbol"] == "AAVE"] 
data["amount"] = data["amount"] / math.pow(10, 18)

Then let us draw a line chart with the function beta_plot_line.

bubbletea.beta_plot_line(
    title="AAVE Deposits",
    df=data,
    x={"title": "Time", "field": "timestamp"},
    y=[{"title": "Amount", "field": "amount"}],
    legend="none"
)

What the chart looks like:

Last updated

Was this helpful?