Skip to main content
The backend handles order matching, on-chain execution, and real-time updates.

Project Structure

backend/
├── src/
│   ├── api/
│   │   ├── orders.ts      # Order endpoints
│   │   ├── markets.ts     # Market data
│   │   ├── gasless.ts     # Gasless operations
│   │   └── auth.ts        # Authentication
│   ├── matching/
│   │   ├── engine.ts      # Matching engine
│   │   ├── orderbookService.ts
│   │   ├── orderBuilder.ts
│   │   └── executor.ts    # On-chain execution
│   ├── ws/
│   │   └── orderbook.ts   # WebSocket
│   ├── db/
│   │   └── redis.ts       # Redis connection
│   └── index.ts           # Entry point

Matching Engine

Uses nodejs-order-book - a high-performance CLOB library.

Capabilities

  • 300,000+ matches per second
  • Price-time priority
  • Partial fills support
  • Market and Limit orders

How It Works

Matching Example

Orderbook before:
BIDS (buys):           ASKS (sells):
55c - 100 tokens       52c - 50 tokens
50c - 200 tokens       55c - 100 tokens
45c - 150 tokens       60c - 75 tokens
New SELL order at 50c for 250 tokens:
1

First Match

Match with bid 55c (100 tokens) - Trade at 55c
2

Second Match

Match with bid 50c (150 tokens) - Trade at 50c
3

Complete

Order fully filled (0 remaining)
Orderbook after:
BIDS:                  ASKS:
50c - 50 tokens        52c - 50 tokens
45c - 150 tokens       55c - 100 tokens
                       60c - 75 tokens

Order Signing

Uses @polymarket/order-utils for EIP-712 signatures.

Why This Library

  • Signatures compatible with CTFExchange
  • Audited and production-proven
  • Correct type handling

Gasless Signing Flow

Key insight: The user signs nothing. The operator signs for them (gasless).

Executor

Settles matched orders on-chain.

Execution Flow

Data Storage

Redis (Hot Data)

  • Active orderbooks
  • Open orders
  • Recent trades (last 50)

Convex (Persistent)

  • Market definitions
  • User profiles
  • Trade history
  • User positions

Blockchain (Source of Truth)

  • Actual token balances
  • Finalized transactions
  • Contract state