跳轉至

Depths Query

Retrieve order book depth data at each price level.

Try It Now

curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "{ depths(first: 20, where: { market: \"0xd99802ee8f16d6ff929e27546de15d03fdcce4bd\", rawAmount_gt: \"0\" }, orderBy: priceIndex) { priceIndex price isBid rawAmount baseAmount } }"}' \
  https://api.goldsky.com/api/public/project_cmicv6kkbhyto01u3agb155hg/subgraphs/sera-pro/1.0.9/gn
import requests

SUBGRAPH_URL = "https://api.goldsky.com/api/public/project_cmicv6kkbhyto01u3agb155hg/subgraphs/sera-pro/1.0.9/gn"

response = requests.post(SUBGRAPH_URL, json={
    "query": """
        query GetDepth($market: String!) {
            bids: depths(
                where: { market: $market, isBid: true, rawAmount_gt: "0" }
                orderBy: priceIndex
                orderDirection: desc
                first: 10
            ) { priceIndex price rawAmount }
            asks: depths(
                where: { market: $market, isBid: false, rawAmount_gt: "0" }
                orderBy: priceIndex
                orderDirection: asc
                first: 10
            ) { priceIndex price rawAmount }
        }
    """,
    "variables": {"market": "0xd99802ee8f16d6ff929e27546de15d03fdcce4bd"}
})
print(response.json())

Schema

type Depth {
  id: ID!              # Unique identifier (market-isBid-priceIndex)
  market: Market!      # Market this depth belongs to
  priceIndex: BigInt!  # Price level index
  price: BigInt!       # Actual price at this level
  isBid: Bool!         # true = bid side, false = ask side
  rawAmount: BigInt!   # Total order amount at this price (raw units)
  baseAmount: BigInt!  # Total amount in base token units
}

Example Queries

Get Order Book (Both Sides)

query GetOrderBook($market: String!) {
  bids: depths(
    where: { market: $market, isBid: true, rawAmount_gt: "0" }
    orderBy: priceIndex
    orderDirection: desc
    first: 20
  ) {
    priceIndex
    price
    rawAmount
    baseAmount
  }
  asks: depths(
    where: { market: $market, isBid: false, rawAmount_gt: "0" }
    orderBy: priceIndex
    orderDirection: asc
    first: 20
  ) {
    priceIndex
    price
    rawAmount
    baseAmount
  }
}

Get Best Bid/Ask

query GetBestPrices($market: String!) {
  bestBid: depths(
    where: { market: $market, isBid: true, rawAmount_gt: "0" }
    orderBy: priceIndex
    orderDirection: desc
    first: 1
  ) {
    priceIndex
    price
    rawAmount
  }
  bestAsk: depths(
    where: { market: $market, isBid: false, rawAmount_gt: "0" }
    orderBy: priceIndex
    orderDirection: asc
    first: 1
  ) {
    priceIndex
    price
    rawAmount
  }
}

Calculate Spread

const data = await querySubgraph(GET_BEST_PRICES_QUERY, { market: MARKET_ID });

const bestBid = data.bestBid[0];
const bestAsk = data.bestAsk[0];

if (bestBid && bestAsk) {
  const spreadTicks = Number(bestAsk.priceIndex) - Number(bestBid.priceIndex);
  const spreadBps = (BigInt(bestAsk.price) - BigInt(bestBid.price)) * 10000n / BigInt(bestBid.price);

  console.log(`Spread: ${spreadTicks} ticks (${Number(spreadBps) / 100}%)`);
}

Response Example

{
  "data": {
    "bids": [
      {
        "priceIndex": "19500",
        "price": "1950000000000000000000",
        "rawAmount": "5000",
        "baseAmount": "2564102564102564102"
      },
      {
        "priceIndex": "19400",
        "price": "1940000000000000000000",
        "rawAmount": "3000",
        "baseAmount": "1546391752577319587"
      }
    ],
    "asks": [
      {
        "priceIndex": "19600",
        "price": "1960000000000000000000",
        "rawAmount": "4000",
        "baseAmount": "2040816326530612244"
      }
    ]
  }
}

Building an Order Book Display

async function getOrderBook(marketId, levels = 10) {
  const query = `
    query GetOrderBook($market: String!, $first: Int!) {
      bids: depths(
        where: { market: $market, isBid: true, rawAmount_gt: "0" }
        orderBy: priceIndex
        orderDirection: desc
        first: $first
      ) { priceIndex price rawAmount }
      asks: depths(
        where: { market: $market, isBid: false, rawAmount_gt: "0" }
        orderBy: priceIndex
        orderDirection: asc
        first: $first
      ) { priceIndex price rawAmount }
    }
  `;

  const data = await querySubgraph(query, { market: marketId, first: levels });

  return {
    bids: data.bids.map(d => ({
      price: formatPrice(d.price),
      amount: d.rawAmount,
      priceIndex: d.priceIndex
    })),
    asks: data.asks.map(d => ({
      price: formatPrice(d.price),
      amount: d.rawAmount,
      priceIndex: d.priceIndex
    }))
  };
}