Overview
Alpha uses a fully on-chain orderbook where every limit order is a smart contract (escrow app) on the Algorand blockchain. There are no off-chain components—all matching happens on-chain via atomic transactions.Orderbook Structure
The orderbook is organized by outcome (YES/NO) and side (bid/ask):Each
OrderbookEntry represents a single limit order. Multiple orders at the same price level appear as separate entries.Fetching the Orderbook
The SDK providesgetOrderbook() to read all open limit orders for a market:
How getOrderbook() Works
How getOrderbook() Works
From
src/modules/orderbook.ts:- Discover all escrow apps created by the market app address
- Read global state for each escrow app
- Filter to open limit orders (unfilled quantity > 0, slippage === 0)
- Categorize by position and side:
position: 1, side: 1→ YES bidsposition: 1, side: 0→ YES asksposition: 0, side: 1→ NO bidsposition: 0, side: 0→ NO asks
Understanding Bids and Asks
- YES Token Orders
- NO Token Orders
YES Bids (Buying YES)
- User wants to buy YES tokens at a specific price
- Escrow holds USDC collateral
- Matches against YES asks (sellers)
- User wants to sell YES tokens at a specific price
- Escrow holds YES tokens
- Matches against YES bids (buyers)
Complementary Matching
Alpha supports complementary matching: a YES buy at pricep can match against a NO sell at price 1 - p.
This works because:
- 1 YES + 1 NO = 1 USDC (always)
- Buying YES at 0.30
Example
650000 + 350000 = 1_000_000 ($1.00).
Order Matching Mechanics
When you place a market order, the SDK:- Fetches the orderbook for the specified position (YES/NO)
- Finds matching orders (opposite side, compatible price)
- Builds an atomic transaction group:
- One transaction per matched order
- Calls the matcher contract to execute the swap
- Updates escrow global state (quantity filled)
- Creates a limit order for any unfilled quantity
Matching Algorithm
Matching Algorithm
From
src/utils/matching.ts, the matching engine:- Sorts bids by price (highest first)
- Sorts asks by price (lowest first)
- Matches greedily until quantity is filled or no compatible orders remain
- Accounts for fees when calculating effective prices
Escrow Global State
Every limit order is stored in an escrow app with global state:slippage === 0→ Limit order (sits on the book)slippage > 0→ Market order (matches immediately or fails)quantity - quantity_filled→ Remaining quantity available
Aggregated Orderbook
For display purposes, you can aggregate multiple orders at the same price level:The SDK does not provide a built-in aggregation function. You’ll need to implement this yourself by grouping
OrderbookEntry items by price.Open Orders for a Wallet
To fetch all open orders belonging to a specific wallet:OpenOrder Type
OpenOrder Type
Next Steps
Create Limit Order
Learn how to place a limit order
Cancel Order
Learn how to cancel an open order
