Subgraph Query Examples
This doc will teach you how to query Uniswap v3 analytics by writing GraphQL queries on the subgraph. You can fetch data points like :
and much more. Below are some example queries. To run a query copy and paste it into the v3 explorer to get fresh data.
Global Data
Global data refers to data points about the Uniswap v3 protocol as a whole. Some examples of global data points are:
- Total value locked in the protocol,
- Total pools deployed,
- Total transaction counts.
Thus, to query global data you must pass in the Uniswap v3 Factory address 0x1F98431c8aD98523631AE4a59f267346ea31F984
and select the desired fields. Reference the full factory schema to see all possible fields.
Current Global Data
An example querying total pool count, transaction count, and total volume in USD and ETH:
{
factory(id: "0x1F98431c8aD98523631AE4a59f267346ea31F984" ) {
poolCount
txCount
totalVolumeUSD
totalVolumeETH
}
}
Historical Global Data
You can also query historical data by specifying a block number.
{
factory(
id: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
block: {
number: 13380584
}
) {
poolCount
txCount
totalVolumeUSD
totalVolumeETH
}
}
Pool Data
To get data about a certain pool, pass in the pool address. Reference the full pool schema and adjust the query fields to retrieve the data points you want.
General Pool Query
The query below returns the feeTier, spot price, and liquidity for the ETH-USDC pool.
{
pool(id: "0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8") {
tick
token0 {
symbol
id
decimals
}
token1 {
symbol
id
decimals
}
feeTier
sqrtPrice
liquidity
}
}
All Possible Pools
The maximum items you can query at once is 1000. Thus to get all possible pools, you can iterate using the skip variable. To get pools beyond the first 1000 you can also set the skip as shown below.
Skipping First 1000 Pools
This query sets the skip value and returns the first 10 responses after the first 1000.
{
pools(first:10, skip:1000) {
id
token0 {
id
symbol
}
token1 {
id
symbol
}
}
}
Creating a Skip Variable
This next query sets a skip variable. In your language and environment of choice you can then iterate through a loop, query to get 1000 pools each time, and continually adjust skip by 1000 until all pool responses are returned.
Check out this example from our interface for poolDayData that does something similar.
Note This query will not work in the graph explorer and more resembles the structure of a query you'd pass to some GraphQL middleware like Apollo.
query pools($skip: Int!) {
pools(
first: 1000,
skip: $skip,
orderDirection: asc
) {
id
sqrtPrice
token0 {
id
}
token1 {
id
}
}
}
Most Liquid Pools
Retrieve the top 1000 most liquid pools. You can use this similar set up to orderBy other variables like number of swaps or volume.
{
pools(
first: 1000,
orderBy: liquidity,
orderDirection: desc
) {
id
}
}
Pool Daily Aggregated
This query returns daily aggregated data for the first 10 days since the given timestamp for the UNI-ETH pool.
{
poolDayDatas(
first: 10,
orderBy: date,
where: {
pool: "0x1d42064fc4beb5f8aaf85f4617ae8b3b5b8bd801",
date_gt: 1633642435
}
) {
date
liquidity
sqrtPrice
token0Price
token1Price
volumeToken0
volumeToken1
}
}