Orderbook

On-chain limit order book with bid/ask matching

Pending Anchor 0.32
Program ID 41q8aXfcpqREjcFRK276KzBRRXWvmfYGqCDHZQ4CPwnX
Binary Size 273K
Cluster Devnet

Instructions

IX initialize_market

Creates a new trading market for a base/quote token pair with a configurable fee.

ParameterTypeDescription
fee_bpsu16Trading fee in basis points (1 bps = 0.01%)
IX place_order

Places a new limit order on the book at the specified price and quantity.

ParameterTypeDescription
sideSideOrder side: Bid or Ask
priceu64Limit price in quote token units
quantityu64Order quantity in base token units
IX fill_order

Fills an existing order, partially or completely, transferring tokens between parties.

ParameterTypeDescription
quantityu64Amount to fill from the existing order
IX cancel_order

Cancels an open order and returns unfilled tokens to the owner.

ParameterTypeDescription
No parameters

Accounts

Market

FieldTypeDescription
authorityPubkeyMarket creator and fee authority
base_mintPubkeyMint address of the base token
quote_mintPubkeyMint address of the quote token
fee_bpsu16Trading fee in basis points
order_countu64Total number of orders placed
bumpu8PDA bump seed

Order

FieldTypeDescription
marketPubkeyThe market this order belongs to
ownerPubkeyWallet that placed the order
idu64Sequential order identifier
sideSideOrder side (Bid or Ask)
priceu64Limit price in quote units
quantityu64Total order quantity
filledu64Amount already filled
timestampi64Unix timestamp when the order was placed
bumpu8PDA bump seed

Enum: Side

VariantDescription
BidBuy order - offering quote tokens to receive base tokens
AskSell order - offering base tokens to receive quote tokens

PDA Derivation

market
seeds = [b"market", authority, base_mint, quote_mint]
order
seeds = [b"order", market, order_count.to_le_bytes()]

Error Codes

InvalidSide The order side must be either Bid or Ask
InsufficientQuantity The fill quantity exceeds the remaining unfilled amount
OrderFilled The order has already been completely filled
PriceMismatch The fill price does not match the order limit price
Overflow Arithmetic overflow in token amount calculation

Usage Example

import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";

// Derive the market PDA
const [marketPda] = PublicKey.findProgramAddressSync(
  [Buffer.from("market"), authority.publicKey.toBuffer(), baseMint.toBuffer(), quoteMint.toBuffer()],
  program.programId
);

// Place a bid order
await program.methods
  .placeOrder({ bid: {} }, new BN(100_000), new BN(50))
  .accounts({
    market: marketPda,
    owner: trader.publicKey,
    baseMint,
    quoteMint,
  })
  .signers([trader])
  .rpc();