Skip to main content

Orders Query

Retrieve order information including open orders, filled orders, and order history.

Try It Now

curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "{ openOrders(first: 5, where: { market: \"0xd99802ee8f16d6ff929e27546de15d03fdcce4bd\" }, orderBy: createdAt, orderDirection: desc) { id nftId priceIndex price isBid rawAmount status user createdAt } }"}' \
  https://api.goldsky.com/api/public/project_cmicv6kkbhyto01u3agb155hg/subgraphs/sera-pro/1.0.9/gn

Schema

type OpenOrder {
  id: ID!                   # market address + "-" + nftId
  nftId: BigInt!            # Order NFT token ID
  market: Market!           # Parent market
  priceIndex: BigInt!       # Price book index
  price: BigInt!            # Actual price
  isBid: Boolean!           # true = buy order, false = sell order
  orderIndex: BigInt!       # Position in the order queue
  rawAmount: BigInt!        # Original raw amount
  baseAmount: BigInt!       # Original base token amount
  quoteAmount: BigInt!      # Original quote token amount
  rawFilledAmount: BigInt!  # Raw amount that has been filled
  baseFilledAmount: BigInt! # Base tokens filled
  quoteFilledAmount: BigInt! # Quote tokens filled
  rawClaimedAmount: BigInt! # Raw amount claimed
  claimableAmount: BigInt!  # Amount available to claim
  bountyAmount: BigInt!     # Claim bounty in wei
  createdAt: BigInt!        # Unix timestamp
  txHash: String!           # Creation transaction hash
  user: String!             # Order owner address
  status: String!           # open, filled, partial, cancelled, claimed
}

Example Queries

Get User’s Open Orders

query GetUserOrders($user: String!) {
  openOrders(
    where: { 
      user: $user
      status_in: ["open", "partial"]
    }
    orderBy: createdAt
    orderDirection: desc
    first: 100
  ) {
    id
    nftId
    market {
      id
      baseToken { symbol }
      quoteToken { symbol }
    }
    priceIndex
    price
    isBid
    rawAmount
    baseAmount
    quoteAmount
    rawFilledAmount
    claimableAmount
    status
    createdAt
  }
}
Variables:
{
  "user": "0x1234567890123456789012345678901234567890"
}

Get Orders by Market

query GetMarketOrders($marketId: String!) {
  openOrders(
    where: { 
      market: $marketId
      status: "open"
    }
    orderBy: priceIndex
    first: 100
  ) {
    id
    priceIndex
    price
    isBid
    rawAmount
    user
  }
}

Get Order by NFT ID

query GetOrderByNFT($marketId: String!, $nftId: BigInt!) {
  openOrders(
    where: { 
      market: $marketId
      nftId: $nftId
    }
  ) {
    id
    nftId
    priceIndex
    price
    isBid
    rawAmount
    rawFilledAmount
    claimableAmount
    status
    user
    txHash
    createdAt
  }
}

Get Claimable Orders

query GetClaimableOrders($user: String!) {
  openOrders(
    where: { 
      user: $user
      claimableAmount_gt: "0"
    }
    orderBy: claimableAmount
    orderDirection: desc
  ) {
    id
    nftId
    market {
      id
      baseToken { symbol decimals }
      quoteToken { symbol decimals }
    }
    priceIndex
    isBid
    claimableAmount
    bountyAmount
  }
}

Get Recent Orders

query GetRecentOrders($since: BigInt!) {
  openOrders(
    where: { createdAt_gte: $since }
    orderBy: createdAt
    orderDirection: desc
    first: 50
  ) {
    id
    market {
      baseToken { symbol }
      quoteToken { symbol }
    }
    isBid
    price
    rawAmount
    status
    createdAt
    txHash
  }
}
Variables:
{
  "since": "1701600000"
}

Response Example

{
  "data": {
    "openOrders": [
      {
        "id": "0xd99802ee8f16d6ff929e27546de15d03fdcce4bd-42",
        "nftId": "42",
        "market": {
          "id": "0xd99802ee8f16d6ff929e27546de15d03fdcce4bd",
          "baseToken": { "symbol": "EURC" },
          "quoteToken": { "symbol": "USDC" }
        },
        "priceIndex": "2500",
        "price": "2500000000000000000000",
        "isBid": true,
        "rawAmount": "1000",
        "baseAmount": "400000000000000000",
        "quoteAmount": "1000000000",
        "rawFilledAmount": "500",
        "claimableAmount": "200000000000000000",
        "status": "partial",
        "createdAt": "1701648000",
        "user": "0x1234567890123456789012345678901234567890"
      }
    ]
  }
}

Order Status Values

StatusDescription
openOrder is on the book, not yet filled
partialOrder is partially filled
filledOrder is fully filled, proceeds claimable
cancelledOrder was cancelled by owner
claimedProceeds have been claimed

Filtering Patterns

Active Orders (Open or Partial)

query ActiveOrders($user: String!) {
  openOrders(
    where: { 
      user: $user
      status_in: ["open", "partial"]
    }
  ) {
    id
    status
  }
}

Bids Only

query BidsOnly($marketId: String!) {
  openOrders(
    where: { 
      market: $marketId
      isBid: true
      status: "open"
    }
    orderBy: priceIndex
    orderDirection: desc
  ) {
    priceIndex
    rawAmount
  }
}

Asks Only

query AsksOnly($marketId: String!) {
  openOrders(
    where: { 
      market: $marketId
      isBid: false
      status: "open"
    }
    orderBy: priceIndex
    orderDirection: asc
  ) {
    priceIndex
    rawAmount
  }
}

Orders at Specific Price

query OrdersAtPrice($marketId: String!, $priceIndex: BigInt!) {
  openOrders(
    where: { 
      market: $marketId
      priceIndex: $priceIndex
      status: "open"
    }
    orderBy: orderIndex
  ) {
    id
    orderIndex
    rawAmount
    user
  }
}

Calculating Fill Percentage

function getFillPercentage(order) {
  const total = BigInt(order.rawAmount);
  const filled = BigInt(order.rawFilledAmount);
  
  if (total === 0n) return 0;
  return Number((filled * 10000n) / total) / 100;  // Percentage with 2 decimals
}

// Example: rawAmount=1000, rawFilledAmount=500 → 50%

Building Order Book from Orders

async function getOrderBook(marketId, depth = 10) {
  const query = `
    query OrderBook($marketId: String!) {
      bids: openOrders(
        where: { market: $marketId, isBid: true, status: "open" }
        orderBy: priceIndex
        orderDirection: desc
        first: 100
      ) {
        priceIndex
        price
        rawAmount
      }
      asks: openOrders(
        where: { market: $marketId, isBid: false, status: "open" }
        orderBy: priceIndex
        orderDirection: asc
        first: 100
      ) {
        priceIndex
        price
        rawAmount
      }
    }
  `;
  
  const data = await querySubgraph(query, { marketId });
  
  // Aggregate by price level
  const aggregateLevels = (orders) => {
    const levels = new Map();
    for (const order of orders) {
      const existing = levels.get(order.priceIndex) || 0n;
      levels.set(order.priceIndex, existing + BigInt(order.rawAmount));
    }
    return Array.from(levels.entries())
      .slice(0, depth)
      .map(([priceIndex, amount]) => ({ priceIndex, amount }));
  };
  
  return {
    bids: aggregateLevels(data.bids),
    asks: aggregateLevels(data.asks)
  };
}