Orderbook
On-chain limit order book with bid/ask matching
Pending
Anchor 0.32
Program ID
41q8aXfcpqREjcFRK276KzBRRXWvmfYGqCDHZQ4CPwnX
Repository
ExpertVagabond/solana-orderbook
Binary Size
273K
Cluster
Devnet
Instructions
IX
initialize_market
Creates a new trading market for a base/quote token pair with a configurable fee.
| Parameter | Type | Description |
|---|---|---|
fee_bps | u16 | Trading 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.
| Parameter | Type | Description |
|---|---|---|
side | Side | Order side: Bid or Ask |
price | u64 | Limit price in quote token units |
quantity | u64 | Order quantity in base token units |
IX
fill_order
Fills an existing order, partially or completely, transferring tokens between parties.
| Parameter | Type | Description |
|---|---|---|
quantity | u64 | Amount to fill from the existing order |
IX
cancel_order
Cancels an open order and returns unfilled tokens to the owner.
| Parameter | Type | Description |
|---|---|---|
| No parameters | ||
Accounts
Market
| Field | Type | Description |
|---|---|---|
authority | Pubkey | Market creator and fee authority |
base_mint | Pubkey | Mint address of the base token |
quote_mint | Pubkey | Mint address of the quote token |
fee_bps | u16 | Trading fee in basis points |
order_count | u64 | Total number of orders placed |
bump | u8 | PDA bump seed |
Order
| Field | Type | Description |
|---|---|---|
market | Pubkey | The market this order belongs to |
owner | Pubkey | Wallet that placed the order |
id | u64 | Sequential order identifier |
side | Side | Order side (Bid or Ask) |
price | u64 | Limit price in quote units |
quantity | u64 | Total order quantity |
filled | u64 | Amount already filled |
timestamp | i64 | Unix timestamp when the order was placed |
bump | u8 | PDA bump seed |
Enum: Side
| Variant | Description |
|---|---|
Bid | Buy order - offering quote tokens to receive base tokens |
Ask | Sell 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();