Mastering OKTC Subgraph Queries for Swap, Tokens, and Transactions

·

Exploring decentralized finance (DeFi) on OKTC requires access to real-time and historical data. The OKTC Subgraph, powered by The Graph protocol, enables developers, analysts, and traders to retrieve critical insights about swaps, token activity, liquidity pools, user transactions, and more. Whether you're building analytics dashboards or tracking market movements, understanding how to query the subgraph effectively is essential.

This guide walks you through practical GraphQL query examples for retrieving block data, pair statistics, token metrics, and transaction histories—all directly from the OKTCSwap subgraph.

👉 Discover real-time DeFi data with powerful subgraph queries on OKX.


Understanding Block Queries

Block data provides the foundation for time-sensitive and historical analysis across the OKTC blockchain.

Fetching the Latest Block

To monitor the current state of the blockchain, retrieve the latest synchronized block. This helps ensure your application or analysis is up to date.

{
  blocks(first: 1, orderBy: timestamp, orderDirection: desc) {
    id
    number
    timestamp
  }
}

This query returns the most recent block number and timestamp, enabling synchronization with the chain's latest activity.

Time-Travel Queries for Historical Snapshots

One of the most powerful features of The Graph is its support for time-travel queries—fetching data as it existed at a specific block in the past.

Use the block parameter in your query to inspect historical states:

{
  pair(id: "0x...", block: { number: 1234567 }) {
    reserveUSD
    totalSupply
  }
}

This allows for accurate backtesting, audit trails, and price or liquidity analysis at any point in time.


Accessing Pair Data in OKTCSwap

Liquidity pairs are central to automated market makers (AMMs). Querying pair data helps identify top-performing pools and track trading activity.

Pair Overview: Real-Time Liquidity Snapshot

Get a live snapshot of a specific trading pair—such as USDT/WOKT—with key metrics like reserves, volume, and liquidity.

{
  pair(id: "0x...") {
    token0 {
      symbol
      derivedUSD
    }
    token1 {
      symbol
      derivedUSD
    }
    reserve0
    reserve1
    reserveUSD
    volumeUSD
    liquidityProviderCount
  }
}

This data supports real-time dashboards and portfolio tracking tools.

Retrieving All Pairs in OKTCSwap

Due to The Graph’s limit of 1000 entities per request, fetching all pairs requires pagination using the skip parameter:

{
  pairs(first: 1000, skip: 0, orderBy: reserveUSD, orderDirection: desc) {
    id
    token0 { symbol }
    token1 { symbol }
    reserveUSD
  }
}

Loop this query with increasing skip values (e.g., 0, 1000, 2000) to collect complete pair listings.

👉 Access comprehensive DeFi analytics using subgraph-powered insights.

Identifying the Most Liquid Pairs

Sort pairs by reserveUSD to discover the highest-liquidity pools—ideal for traders seeking low slippage and strong market depth.

{
  pairs(first: 10, orderBy: reserveUSD, orderDirection: desc) {
    id
    token0 { symbol }
    token1 { symbol }
    reserveUSD
    volumeUSD
  }
}

High-liquidity pairs often include major assets like WOKT, USDT, and OKB.

Tracking Recent Swaps Within a Pair

Monitor trading activity by querying recent swaps. This is useful for detecting trends or sudden price movements.

{
  swaps(
    first: 100
    orderBy: timestamp
    orderDirection: desc
    where: { pair: "0x..." }
  ) {
    transaction {
      id
      timestamp
    }
    amountUSD
    amount0In
    amount1Out
  }
}

Include token details to enrich output with human-readable symbols and prices.

Aggregated Daily Pair Data

For charting and trend analysis, use daily aggregated entities to retrieve metrics in time-based buckets.

{
  pairDayDatas(
    first: 100
    where: {
      pairAddress: "0x..."
      date_gt: 1672531200
    }
    orderBy: date
    orderDirection: asc
  ) {
    date
    dailyVolumeUSD
    reserveUSD
  }
}

This enables visualization of volume trends, liquidity changes, and user engagement over time.


Querying Token Data Across OKTCSwap

Tokens involved in liquidity pools generate rich datasets that reflect their ecosystem usage and demand.

Token Overview: Key Metrics at a Glance

Retrieve comprehensive statistics for any token traded on OKTCSwap:

{
  token(id: "0x...") {
    name
    symbol
    totalLiquidity
    derivedUSD
    volumeUSD
    txCount
    allPairs(first: 200, orderBy: reserveUSD) {
      id
      reserveUSD
    }
  }
}

For wrapped assets like WOKT, this reveals integration depth across multiple pairs.

Listing All Tokens in Swap

Similar to pairs, you can paginate through all tokens using skip:

{
  tokens(first: 1000, skip: 0) {
    id
    symbol
    name
  }
}

Note: This query may not work in The Graph’s sandbox but functions in production environments with GraphQL clients like Apollo.

Analyzing Token Transactions

To see all mints, swaps, and burns involving a specific token:

  1. First, fetch all pairs containing the token.
  2. Then query transactions filtered by those pair addresses.

Example:

{
  mints(
    first: 30
    where: { pair_in: ["0xUSDT-WOKT", "0xUSDT-OKB"] }
  ) {
    transaction {
      id
      timestamp
    }
    pair {
      token0 { symbol }
      token1 { symbol }
    }
    amountUSD
  }
}

Repeat for swaps and burns to get a full transaction history.

Daily Aggregated Token Data

Track daily performance with time-series data:

{
  tokenDayDatas(
    where: { token: "0x..." }
    orderBy: date
    orderDirection: asc
  ) {
    date
    priceUSD
    dailyVolumeUSD
    totalLiquidityToken
  }
}

Sort ascending to build chronological charts showing price evolution and volume spikes.


Frequently Asked Questions

Q: Where can I test these GraphQL queries?
A: Visit the OKTC Subgraph GraphQL endpoint to run and experiment with queries in real time.

Q: Why are only 1000 results returned per query?
A: The Graph enforces a limit of 1000 entities per request. Use pagination with the skip parameter to retrieve larger datasets incrementally.

Q: How do I query data from a past date?
A: Use the block argument with a specific block number or timestamp to perform time-travel queries and access historical states.

Q: Can I get USD values for tokens even if they don’t trade against stablecoins?
A: Yes. The subgraph derives USD prices based on available liquidity paths and reference oracles, allowing consistent valuation across tokens.

Q: What tools can I use to integrate these queries into my app?
A: Popular options include Apollo Client, Relay, or simple HTTP requests to the GraphQL endpoint. These allow seamless integration into web or mobile applications.

Q: Is there documentation for all available entities and fields?
A: Yes. Explore the subgraph schema directly on the GraphQL playground or refer to official Graph documentation for field definitions and relationships.

👉 Start building with live DeFi data from OKTC today.


Core Keywords

By mastering these query patterns, developers and analysts gain deep visibility into OKTC’s decentralized exchange ecosystem—enabling smarter decisions, better products, and enhanced user experiences.