Querying Data

Querying Open Markets

Mangrove has a built-in function in its reader contract that returns a list of all open markets directly from the blockchain. This function performs two consecutive RPC calls, one to get the open markets, and another to retrieve detailed information about their tokens. In addition to standard Viem parameters, this function requires two additional inputs:

  • cashnesses — a dictionary describing the cachness of a token

  • symbolOverrides — an optional dictionary for overriding returned token symbols

Understanding Token "Cashness"

The cashness of a token determines whether it acts as a base or quote asset in a market. While you can assign these values freely, the Mangrove team follows a few general guidelines:

  • Variable assets: lower cashness (e.g., ~100)

  • Stable assets: higher cashness (e.g., ~1000)

  • Assets with higher TVL have higher cashness

If asset A has a higher cashness than asset B:

  • A will be the quote asset

  • B will be the base asset

Example - Getting All Open Markets

Below is an example implementation using the mgv SDK:

src/markets.ts
import type { MarketParams } from "@mangrovedao/mgv";
import { client } from "./client";

export async function getOpenMarkets(): Promise<MarketParams[]> {
  return client.getOpenMarkets({
    cashnesses: { WETH: 100, WBTC: 200, USDC: 1000 },
    symbolOverrides: {
      USDT0: "USDT",
    },
  });
}

Querying the Order Book

Now that we have chosen a market, we are ready to query it's order book. The function makes a single multicall to fetch both order data (up to the desired depth) and market configuration parameters.

src/book.ts
import {
  publicMarketActions,
  type Book,
  type MarketParams,
} from "@mangrovedao/mgv";
import { client } from "./client";
import { mangroveConfig } from "./config";

export async function getBook(
  market: MarketParams,
  depth = 100n
): Promise<Book> {
  const marketClient = client.extend(
    publicMarketActions(mangroveConfig, market)
  );
  return marketClient.getBook({ depth });
}

Next Steps

With your market and order book data in place, we’re now ready to create market and limit orders.

Last updated