> Feedback: If these docs are stale, missing, or confusing, post sanitized feedback to `https://docs.tempo.xyz/api/feedback` with `source: "mcp"`, a short `message`, and any relevant `toolName`, `relatedResource`, or `client`.
# View the Orderbook

:::warning\[Recipes being updated]
These SQL recipes don't yet account for [flip orders](/docs/protocol/exchange/providing-liquidity#flip-order-indexing), which keep the same `orderId` and re-list via `OrderFlipped` when filled — results may omit or misreport flipped orders. Updated queries are in progress.
:::

Query and inspect the orderbook to see available liquidity, price levels, and individual orders on Tempo's Stablecoin DEX.

## Recommended Approach

We recommend using indexed data to query the orderbook for better performance and ease of use. While you can query logs and transactions directly from an RPC node, indexed data providers offer structured SQL interfaces that make complex queries simpler and more efficient.

In this guide, we use [Index Supply](https://www.indexsupply.net) as our indexing provider, but you're free to choose your own indexing solution or query the chain directly based on your needs.

## Recipes

### Get the current spread

Query the best bid and ask prices to calculate the current spread for a token pair.

These examples use the mainnet `USDC.e / pathUSD` orderbook; `pathUSD` is the quote token for `USDC.e` — see [Quote Tokens](/docs/protocol/exchange/quote-tokens).

Find the highest bid prices (buyers) for the `USDC.e / pathUSD` book. This query filters out fully filled and cancelled orders, groups by price level (tick), and shows the top 5 bid prices with their total liquidity.

<IndexSupplyQuery
  chainId={4217}
  title={'Best Bid Prices for USDC.e'}
  query={`SELECT
  tick as price,
  SUM(amount) as total_liquidity
FROM orderplaced
WHERE token = '0x20c000000000000000000000b9537d11c60e8b50'
  AND "isBid" = true
  AND NOT EXISTS (
    SELECT 1 FROM orderfilled
    WHERE orderfilled."orderId" = orderplaced."orderId"
      AND orderfilled."partialFill" = false
  )
  AND NOT EXISTS (
    SELECT 1 FROM ordercancelled
    WHERE ordercancelled."orderId" = orderplaced."orderId"
  )
GROUP BY price
ORDER BY price DESC
LIMIT 5`}
  signatures={["OrderPlaced", "OrderFilled", "OrderCancelled"]}
/>

Find the lowest ask prices (sellers) for the `USDC.e / pathUSD` book. The spread is the difference between the highest bid and lowest ask price.

<IndexSupplyQuery
  chainId={4217}
  title={'Best Ask Prices for USDC.e'}
  query={`SELECT
  tick as price,
  SUM(amount) as total_liquidity
FROM orderplaced
WHERE token = '0x20c000000000000000000000b9537d11c60e8b50'
  AND "isBid" = false
  AND NOT EXISTS (
    SELECT 1 FROM orderfilled
    WHERE orderfilled."orderId" = orderplaced."orderId"
      AND orderfilled."partialFill" = false
  )
  AND NOT EXISTS (
    SELECT 1 FROM ordercancelled
    WHERE ordercancelled."orderId" = orderplaced."orderId"
  )
GROUP BY price
ORDER BY price
LIMIT 5`}
  signatures={["OrderPlaced", "OrderFilled", "OrderCancelled"]}
/>

### Inspect order depth

View aggregated liquidity at each price level to understand the orderbook structure.

This query shows all active orders in the `USDC.e / pathUSD` book, including both regular and flip orders.

<IndexSupplyQuery
  chainId={4217}
  title={'USDC.e Order Depth by Price Level'}
  query={`SELECT
  tick as price,
  "isBid" as is_bid,
  COUNT("orderId") as num_orders,
  SUM(amount) as total_amount
FROM orderplaced
WHERE token = '0x20c000000000000000000000b9537d11c60e8b50'
  AND NOT EXISTS (
    SELECT 1 FROM orderfilled
    WHERE orderfilled."orderId" = orderplaced."orderId"
      AND orderfilled."partialFill" = false
  )
  AND NOT EXISTS (
    SELECT 1 FROM ordercancelled
    WHERE ordercancelled."orderId" = orderplaced."orderId"
  )
GROUP BY price, is_bid
ORDER BY is_bid DESC, price
LIMIT 50`}
  signatures={["OrderPlaced", "OrderFilled", "OrderCancelled"]}
/>

### Inspect an individual order

#### Order details

Get detailed information about a specific order including its placement details, fill history, and cancellation status.

This query inspects the details of the most recent order in the `USDC.e / pathUSD` book. It shows when the order was created, at what price (tick), the order size, whether it's a flip order, and who placed it.

<IndexSupplyQuery
  chainId={4217}
  title={'Order Placement Details for USDC.e'}
  query={`SELECT
  "orderId",
  tick,
  "isBid",
  amount,
  "isFlipOrder",
  "flipTick",
  maker,
  token,
  block_num,
  tx_hash
FROM orderplaced
WHERE token = '0x20c000000000000000000000b9537d11c60e8b50'
ORDER BY block_num DESC
LIMIT 1`}
  signatures={["OrderPlaced"]}
/>

#### Order fill status

Check if recent `USDC.e` orders have been partially or fully filled. This query shows up to 5 fill events for the `USDC.e / pathUSD` market, including the amount filled in each transaction and whether it was a partial fill.

<IndexSupplyQuery
  chainId={4217}
  title={'Order Fill History for USDC.e'}
  query={`SELECT
  ofl."orderId",
  ofl."amountFilled",
  ofl."partialFill",
  ofl.block_num,
  ofl.tx_hash
FROM orderfilled ofl
INNER JOIN orderplaced op
  ON ofl."orderId" = op."orderId"
WHERE op.token = '0x20c000000000000000000000b9537d11c60e8b50'
ORDER BY ofl.block_num DESC
LIMIT 5`}
  signatures={["OrderFilled", "OrderPlaced"]}
/>

#### Cancelled orders

Check if a `USDC.e` order has been cancelled. This query returns a recent order from the `USDC.e / pathUSD` book that was explicitly cancelled by the maker before being fully filled.

<IndexSupplyQuery
  chainId={4217}
  title={'Order Cancellation Status for USDC.e'}
  query={`SELECT
  oc."orderId",
  oc.block_num,
  oc.tx_hash
FROM ordercancelled oc
INNER JOIN orderplaced op ON oc."orderId" = op."orderId"
WHERE op.token = '0x20c000000000000000000000b9537d11c60e8b50'
ORDER BY oc.block_num DESC
LIMIT 1`}
  signatures={["OrderCancelled", "OrderPlaced"]}
/>

### Get recent trade prices

View the last prices a token traded at to understand recent market activity.

This query joins order fill events with their corresponding placement details to show the price tick and amount for recent trades in the `USDC.e / pathUSD` book.

<IndexSupplyQuery
  chainId={4217}
  title={'Recent Trade Prices for USDC.e'}
  query={`SELECT
  ofl.block_num,
  ofl."orderId",
  o.tick as price,
  ofl."amountFilled",
  o."isBid"
FROM orderfilled ofl
INNER JOIN orderplaced o
  ON ofl."orderId" = o."orderId"
WHERE o.token = '0x20c000000000000000000000b9537d11c60e8b50'
ORDER BY ofl.block_num DESC
LIMIT 10`}
  signatures={["OrderFilled", "OrderPlaced"]}
/>
