Query Examples

This doc will teach you how to query MarginX 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 endpoint to get fresh data.

Global Data

Global data refers to data points about the MarginX protocol as a whole. Some examples of global data points are total value locked in the protocol, total pairs deployed, or total transaction counts. Thus, to query global data you must pass in the MarginX Factory address 0x9E229BE3812228454499FAf771b296bedFe8c904 and select the desired fields. Reference the full factory schema to see all possible fields.

Current Global Data

An example querying total pair count, transaction count, and total volume in USD and FX:

{
  fxswapFactory(id: "0x9E229BE3812228454499FAf771b296bedFe8c904") {
    pairCount
    txCount
    totalVolumeUSD
    totalVolumeETH
  }
}

Historical Global Data

You can also query historical data by specifying a block number.

{
  fxswapFactory(id: "0x1F98431c8aD98523631AE4a59f267346ea31F984", block: {number: 15899629}){
    pairCount
    txCount
    totalVolumeUSD
    totalVolumeETH
  }
}

Pair Data

To get data about a certain pair, pass in the pair address. Reference the full pair schema and adjust the query fields to retrieve the data points you want.

General Pair Query

The query below returns the token0 reserve, token1 reserve, and transaction count for the PUNDIX-FX pair.

{
  pair(id: "0x7ed74ebda2f2ad577d9ef2aa6b6573b15fc14e39") {
    token0 {
      symbol
      id
      decimals
    }
    token1 {
      symbol
      id
      decimals
    }
    reserve0
    reserve1
    txCount
  }
}

All Possible Pairs

The maximum items you can query at once is 1000. Thus to get all possible pairs, you can iterate using the skip variable. To get pairs beyond the first 1000 you can also set the skip as shown below.

Skipping First 1000 Pairs

This query sets the skip value and returns the first 10 responses after the first 1000.

{
  pairs(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 pairs each time, and continually adjust skip by 1000 until all pair responses are returned.

query pairs( $skip: Int!) {
    pairs(
      first: 1000
      skip: $skip
      orderDirection: asc
    ) {
      id
      txCount
      token0 {
        id
      }
      token1 {
        id
      }
    }
  }

Most Liquid Pairs

Retrieve the top 1000 most liquid pairs. You can use this similar set up to orderBy other variables like number of swaps or volume.

{
 pairs(orderBy: reserveUSD, orderDirection: desc) {
    id
 }
}

Pair Daily Aggregated

This query returns daily aggregated data for the first 10 days since the given timestamp for the PUNDIX-FX pair.

{
  pairDayDatas(
    first: 10
    orderBy: date
    where: {
      pairAddress: "0x7ed74ebda2f2ad577d9ef2aa6b6573b15fc14e39", 
      date_gt: 1714528526
    }) {
    date
    reserveUSD
    dailyVolumeToken0
    dailyVolumeToken1
  }
}

Swap Data

General Swap Data

To query data about a particular swap, input the transaction hash + "-" + the index in the swaps the transaction array. This is the reference for the full swap schema.

This query fetches data about the sender, receiver, amounts, transaction data, and timestamp for a particular swap.

{
   swap(id: "0xdedc2f209d9db5e0d4b1d3160d10f1f474b60dcac4de08c323995abf926f5b1e-0") {
    id
    sender
    to
    amount0In
    amount0Out
    amount1In
    amount1Out
    transaction {
      id
      blockNumber
    }
    timestamp
  }
 }

Recent Swaps Within a Pair

You can set the where field to filter swap data by pair address. This example fetches data about swaps for the PUNDIX-FX pair, ordered by timestamp.

{
  swaps(
    orderBy: timestamp
    orderDirection: desc
    where: {pair: "0x7ed74ebda2f2ad577d9ef2aa6b6573b15fc14e39"}
  ) {
    id
    pair {
      token0 {
        id
        symbol
      }
      token1 {
        id
        symbol
      }
    }
    sender
    to
    amount0In
    amount0Out
    amount1In
    amount1Out
  }
}

Token Data

Input the the token contract address to fetch token data. Any token that exists in at least one MarginX pair can be queried. The output will aggregate data across all pairs that include the token.

General Token Data

This queries the decimals, symbol, name, transaction count, and volume in USD for the FX token. Reference the full token schema for all possible fields you can query.

{
  token(id: "0x80b5a32e4f032b2a058b4f29ec95eefeeb87adcd") {
    symbol
    name
    decimals
    tradeVolumeUSD
    txCount
  }
}

Token Daily Aggregated

You can fetch aggregate data about a specific token over a 24-hour period. This query gets 10-days of the 24-hour volume data for the FX token ordered from oldest to newest.

{
  tokenDayDatas(
    first: 10
    where: {token: "0x80b5a32e4f032b2a058b4f29ec95eefeeb87adcd"}
    orderBy: date
    orderDirection: asc
  ) {
    token {
      id
      symbol
    }
    dailyVolumeUSD
  }
}

All Tokens

Similar to retrieving all pairs, you can fetch all tokens by using skip. Note: This query will not work in the graph sandbox and more resembles the structure of a query you'd pass to some graphql middleware like Apollo.

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

Last updated