Developers
Search…
Balancer Subgraph

Why should I use the Balancer Subgraph?

The Subgraph features easy-to-query data using GraphQL, and can log data in such a way that you can easily access data that's difficult to query on-chain. For example, there is no on-chain list of all Balancer pools (similar to how there's no on-chain list of all ERC20 tokens), but on the Subgraph, you can easily query all pools, even filtering by PoolType.
Explaining what TheGraph is and how it works is outside the scope of this article, but if you'd like to know more, please refer to their documentation.

Code Walkthrough

Select your desired programming language in the tabs below for the relevant tutorial.
Python
Let's step through an example chunk by chunk of getting Balancer Pools and their tokens from the Subgraph.

Dependencies

1
import json
2
3
# thegraph queries
4
from gql import gql, Client
5
from gql.transport.requests import RequestsHTTPTransport
Copied!
This sample relies on TheGraph's gql library to query TheGraph and on a few other libraries. These dependencies can be found in the requirements.txt file in the Python sample repository.

Initialize TheGraph Connection

1
# Initialize subgraph
2
subgraph_url = "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2"
3
balancer_transport=RequestsHTTPTransport(
4
url=subgraph_url,
5
verify=True,
6
retries=3
7
)
8
client = Client(transport=balancer_transport)
Copied!
Here, we are creating the client for the Subgraph, pointing it to the Balancer Ethereum mainnet Subgraph. There are Subgraphs for each network on which Balancer runs. We will use this client to make all of our calls.

Craft a Query and Request Data (Getting Pools)

1
query_string = '''
2
query {{
3
pools(first: {first}, skip: {skip}) {{
4
id
5
address
6
poolType
7
strategyType
8
swapFee
9
amp
10
}}
11
}}
12
'''
13
num_pools_to_query = 100
14
formatted_query_string = query_string.format(first=num_pools_to_query, skip=0)
15
response = client.execute(gql(formatted_query_string))
Copied!
Here, we create a query_string that is written in GraphQL. We request pools with a variety of attributes for each one, specifying the maximum number of pools we want in this request. The first/skip notation allows us to query pools in batches. We then get a response from the client, formatted as a Python dict.

Request Tokens for each Pool

1
for pool in response["pools"]:
2
pool_token_query = '''
3
query {{
4
poolTokens(first: 8, where: {{ poolId: "{pool_id}" }}) {{
5
id
6
symbol
7
name
8
decimals
9
address
10
balance
11
invested
12
investments
13
weight
14
}}
15
}}
16
'''
17
formatted_query_string = pool_token_query.format(pool_id=pool["id"])
18
token_response = client.execute(gql(formatted_query_string))
19
pool["poolTokens"] = token_response["poolTokens"]
20
21
print(json.dumps(response["pools"], indent=4))
Copied!
Using the list of pools from the previous call, we can now request poolTokens data for each pool. The where specifier in the query header filters for the specific pool that we're interested in. We can now add the poolTokens response into our pool dict to keep all our data in the same structure.
Finally, we print out the data formatted nicely as a json. See below for some sample output data:
1
...
2
{
3
"address": "0x0b09dea16768f0799065c475be02919503cb2a35",
4
"amp": null,
5
"id": "0x0b09dea16768f0799065c475be02919503cb2a3500020000000000000000001a",
6
"poolType": "Weighted",
7
"strategyType": 2,
8
"swapFee": "0.0027",
9
"poolTokens": [
10
{
11
"address": "0x6b175474e89094c44da98b954eedeac495271d0f",
12
"balance": "41417760.60370376773502331",
13
"decimals": 18,
14
"id": "0x0b09dea16768f0799065c475be02919503cb2a3500020000000000000000001a-0x6b175474e89094c44da98b954eedeac495271d0f",
15
"invested": "0",
16
"investments": [],
17
"name": "Dai Stablecoin",
18
"symbol": "DAI",
19
"weight": "0.4"
20
},
21
{
22
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
23
"balance": "19467.723570242016061162",
24
"decimals": 18,
25
"id": "0x0b09dea16768f0799065c475be02919503cb2a3500020000000000000000001a-0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
26
"invested": "0",
27
"investments": [],
28
"name": "Wrapped Ether",
29
"symbol": "WETH",
30
"weight": "0.6"
31
}
32
]
33
},
34
...
Copied!