Skip to main content

Overview

Fetches the complete orderbook for a prediction market by reading all escrow applications created by the market contract. The orderbook is organized into YES and NO sides, each containing bids (buy orders) and asks (sell orders). Only includes limit orders (slippage = 0) with remaining unfilled quantity.

Method Signature

async getOrderbook(marketAppId: number): Promise<Orderbook>

Parameters

marketAppId
number
required
The market application ID on the Algorand blockchain

Returns

Returns an Orderbook object structured as follows:
yes
OrderbookSide
All orders for the YES outcome
yes.bids
OrderbookEntry[]
Buy orders for YES tokens
yes.asks
OrderbookEntry[]
Sell orders for YES tokens
no
OrderbookSide
All orders for the NO outcome
no.bids
OrderbookEntry[]
Buy orders for NO tokens
no.asks
OrderbookEntry[]
Sell orders for NO tokens

OrderbookEntry Type

Each OrderbookEntry contains:
price
number
Price in microunits (e.g., 500000 = $0.50)
quantity
number
Remaining quantity in microunits (e.g., 1000000 = 1 share)
escrowAppId
number
The escrow application ID for this order
owner
string
Algorand address of the order owner

Example Usage

import { AlphaClient } from '@alpha-arcade/sdk';
import algosdk from 'algosdk';

const algodClient = new algosdk.Algodv2('', 'https://mainnet-api.algonode.cloud', 443);
const indexerClient = new algosdk.Indexer('', 'https://mainnet-idx.algonode.cloud', 443);
const account = algosdk.mnemonicToSecretKey('your mnemonic...');

const client = new AlphaClient({
  algodClient,
  indexerClient,
  signer: algosdk.makeBasicAccountTransactionSigner(account),
  activeAddress: account.addr.toString(),
  matcherAppId: 741347297,
  usdcAssetId: 31566704,
});

// Fetch markets and get orderbook
const markets = await client.getLiveMarkets();
const orderbook = await client.getOrderbook(markets[0].marketAppId);

console.log('YES Bids:', orderbook.yes.bids);
console.log('YES Asks:', orderbook.yes.asks);
console.log('NO Bids:', orderbook.no.bids);
console.log('NO Asks:', orderbook.no.asks);

Finding Best Bid/Ask

To find the best available prices:
const orderbook = await client.getOrderbook(marketAppId);

// Best bid for YES (highest buy price)
const bestYesBid = orderbook.yes.bids
  .sort((a, b) => b.price - a.price)[0];

if (bestYesBid) {
  console.log(`Best YES bid: $${bestYesBid.price / 1e6}`);
  console.log(`Quantity available: ${bestYesBid.quantity / 1e6} shares`);
}

// Best ask for YES (lowest sell price)
const bestYesAsk = orderbook.yes.asks
  .sort((a, b) => a.price - b.price)[0];

if (bestYesAsk) {
  console.log(`Best YES ask: $${bestYesAsk.price / 1e6}`);
  console.log(`Quantity available: ${bestYesAsk.quantity / 1e6} shares`);
}

How It Works

  1. Discovers escrow apps: Queries the Algorand indexer for all applications created by the market’s contract address
  2. Reads global state: Fetches and decodes the global state of each escrow app
  3. Filters limit orders: Only includes orders with slippage === 0 and unfilled quantity
  4. Categorizes orders: Organizes by position (YES/NO) and side (buy/sell)

Notes

  • Quantities are in microunits (1,000,000 = 1 share)
  • Prices are in microunits (500,000 = $0.50)
  • Only active limit orders are included (market orders with slippage > 0 are excluded)
  • The orderbook is fetched directly from the blockchain, so it reflects real-time on-chain state

Build docs developers (and LLMs) love