MarginX 2.0 gitbook v1.1
  • 🚀Getting Started: MarginX 2.0
  • MARGINX 2.0 & ALO
    • Vision
    • Mechanic & Foundation of ALO
    • Comparison Between Order Book And Swap
    • Risk and Reward of Liquidity Providers
    • Product Comparison
    • MarginX ALO: Whitepaper
  • Technical structure
    • Contract Structure
      • CoastDAO Module
    • The Fund Module
      • Contract Specific Design
      • Cross-Chain Description
    • Contract Events
      • Strategy Factory
      • Strategy Pair
      • DssCdpManager
      • DSProxyFactory
      • Vat
    • Contract Invocation Method
      • DSProxyFactory
      • ProxyRegistry
      • DSProxy
      • The Price of the Collateral Assets
      • DssProxyActions
        • Adding Liquidity For New Perpetual Markets (First Time)
        • Creating Order (Providing Liquidity)
        • Request For Withdrawing Liquidity
        • Withdrawal
      • StrategyFactory
  • MarginX Spot
    • Swap
    • Pool
      • Add Liquidity
      • Remove Liquidity
    • Farm
      • Deposit LP Tokens
      • Withdraw LP Token
    • Supported Networks
  • MARGINX SUBGRAPH
    • Overview
    • Query Examples
  • MarginX 1.0
    • Built On f(x)Core
    • Decentralized Order Book
    • Referral Programme
    • MarginX NFTs
    • Maker Liquidity Pool
    • MarginX 1.0 Litepaper
  • ERC20 Token Factory
    • Token Factory Supported Networks
    • Create Token
    • Provide Token Details
    • Interact with Token
    • Role Management
  • Help desk
    • Perpetual Trading 101
    • User Guide (Video)
    • MarginX 2.0 FAQ
    • Contact us
Powered by GitBook
On this page
  • Global Data​
  • Pair Data​
  • Swap Data​
  • Token Data​
  1. MARGINX SUBGRAPH

Query Examples

PreviousOverviewNextBuilt On f(x)Core

Last updated 1 year ago

This doc will teach you how to query MarginX analytics by writing GraphQL queries on the subgraph. You can fetch data points like :

  • of a pair

and much more. Below are some example queries. To run a query copy and paste it into the 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 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
  }
}

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
  }
}

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.

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
    }
  }
}

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
      }
    }
  }

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
 }
}

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
  }
}

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
  }
}

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.

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

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
  }
}

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
  }
}

Pair Data

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

General Pair Query

All Possible Pairs

Skipping First 1000 Pairs

Creating a Skip Variable

Most Liquid Pairs

Pair Daily Aggregated

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 .

Token Data

General Token Data

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

Token Daily Aggregated

All Tokens

current liquidity
volume on a certain day
endpoint
​
factory schema
​
​
​
pair schema
​
​
​
​
​
​
​
​
swap schema
​
​
token schema
​
​