Query Examples

This document demonstrates how to query Storyhunt V3 analytics using GraphQL on the subgraph.

Subgraph Query Examples

This document demonstrates how to query Storyhunt V3 analytics using GraphQL on the subgraph. Queries can fetch data points such as:

  • Collected fees for a position

  • Current liquidity of a pool

  • Volume on a specific day

Below are example queries. Copy and paste them into a GraphQL explorer to retrieve live data.

Global Data

Global data refers to analytics about the Storyhunt V3 protocol as a whole. Examples include total value locked, number of pools, or transaction counts. To query global data, pass the FACTORY_ADDRESS and specify the desired fields.

Current Global Data

{
  factory(id: "FACTORY_ADDRESS") {
    poolCount
    txCount
    totalVolumeUSD
    totalVolumeETH
  }
}

Historical Global Data

Specify a block number to query historical data:

{
  factory(id: "FACTORY_ADDRESS", block: {number: 13380584}) {
    poolCount
    txCount
    totalVolumeUSD
    totalVolumeETH
  }
}

Pool Data

To query a specific pool, pass the POOL_ADDRESS and adjust the query fields as needed.

General Pool Query

This query fetches the fee tier, spot price, and liquidity for a specific pool:

{
  pool(id: "POOL_ADDRESS") {
    tick
    token0 {
      symbol
      id
      decimals
    }
    token1 {
      symbol
      id
      decimals
    }
    feeTier
    sqrtPrice
    liquidity
  }
}

All Possible Pools

To retrieve pools in batches, use the skip variable:

Skipping First 1000 Pools

{
  pools(first: 10, skip: 1000) {
    id
    token0 {
      id
      symbol
    }
    token1 {
      id
      symbol
    }
  }
}

Creating a Skip Variable

In your environment, iterate through the skip variable to query all pools:

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:

{
  pools(first: 1000, orderBy: liquidity, orderDirection: desc) {
    id
  }
}

Pool Daily Aggregated Data

Query daily aggregated data for a pool:

{
  poolDayDatas(first: 10, orderBy: date, where: {
    pool: "POOL_ADDRESS",
    date_gt: 1633642435
  }) {
    date
    liquidity
    sqrtPrice
    token0Price
    token1Price
    volumeToken0
    volumeToken1
  }
}

Swap Data

General Swap Data

Query data about a specific swap using the transaction hash and index:

{
  swap(id: "TRANSACTION_HASH#INDEX") {
    sender
    recipient
    amount0
    amount1
    transaction {
      id
      blockNumber
      gasUsed
      gasPrice
    }
    timestamp
    token0 {
      id
      symbol
    }
    token1 {
      id
      symbol
    }
  }
}

Recent Swaps Within a Pool

Fetch recent swaps for a specific pool:

{
  swaps(orderBy: timestamp, orderDirection: desc, where: {
    pool: "POOL_ADDRESS"
  }) {
    pool {
      token0 {
        id
        symbol
      }
      token1 {
        id
        symbol
      }
    }
    sender
    recipient
    amount0
    amount1
  }
}

Token Data

General Token Data

Query token information such as decimals, symbol, and volume:

{
  token(id: "TOKEN_ADDRESS") {
    symbol
    name
    decimals
    volumeUSD
    poolCount
  }
}

Token Daily Aggregated Data

Fetch aggregate token data over a 24-hour period:

{
  tokenDayDatas(first: 10, where: {token: "TOKEN_ADDRESS"}, orderBy: date, orderDirection: asc) {
    date
    token {
      id
      symbol
    }
    volumeUSD
  }
}

All Tokens

Retrieve all tokens in batches using the skip variable:

query tokens($skip: Int!) {
  tokens(first: 1000, skip: $skip) {
    id
    symbol
    name
  }
}

Position Data

General Position Data

Query fees and liquidity for a specific position:

{
  position(id: 3) {
    id
    collectedFeesToken0
    collectedFeesToken1
    liquidity
    token0 {
      id
      symbol
    }
    token1 {
      id
      symbol
    }
  }
}