feat(defi): add order book example#41
Merged
Merged
Conversation
35851f4 to
fcc49fd
Compare
…b matching engine Central Limit Order Book (CLOB) Anchor program for a single base/quote token pair. Matches resting limit orders in price-time priority using a critbit binary radix trie ported from Openbook v2, stored as a ~180 KB zero-copy account. Instruction handlers: - initialize_market — create Market PDA, three vaults, and OrderBook account - create_market_user — per-(user, market) PDA tracking open orders and unsettled balances - place_order — lock funds, match against caller-supplied maker pairs, rest remainder - cancel_order — credit unsettled balance, remove from book - settle_funds — transfer unsettled balances from vaults to user ATAs - withdraw_fees — authority-gated drain of the fee vault Key design points: - Funds lock into program-owned vaults on place_order; matching only updates unsettled_* counters; settle_funds is the single payout step - Caller supplies maker pairs as remaining_accounts (Openbook v2 style); program enforces price-time order and ownership on what is passed - OrderBook uses a critbit slab (balanced-by-construction binary radix trie) for O(log n) insert/lookup/delete, preventing degenerate-input attacks that would exhaust the compute budget - Zero-copy AccountLoader for the ~180 KB OrderBook; two 1024-slot slabs back to back, well under Solana's per-account ceiling - 23 LiteSVM integration tests covering matching, partial fills, price-time priority, cancel/settle round trips, and fee accounting https://claude.ai/code/session_01G6iaAjzg8aoFwe8ZWWG9VR
961024d to
d084fa3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a central limit order book (CLOB) example at
defi/order-book/anchor/. Complete Anchor 1.0 implementation with a zero-copy critbit slab order book ported from Openbook v2, a price-time priority matching engine, 23 LiteSVM Rust tests, and a README written for both finance beginners and Solana developers.Program
Six instruction handlers:
initialize_market— creates a market for a base/quote token pair with market-owned base, quote, and fee vaults.create_market_user— per-(user, market) PDA tracking open orders and unsettled balances; seeds["market_user", market, owner].place_order— limit order with price-time priority matching viaremaining_accountsmaker pairs; residual rests on the book.cancel_order— owner-only cancel; locked funds credited to unsettled balance for retrieval viasettle_funds.settle_funds— withdraws unsettled base/quote from vault to user token accounts; checks-effects-interactions ordering (counters zeroed before CPIs).withdraw_fees— market authority sweeps accumulated taker fees from the fee vault.Marketcarrieshas_oneconstraints binding vaults and mints to their stored addresses, blocking account-substitution attacks.Order book
Zero-copy critbit (binary radix trie) slab ported from
openbook-dex/openbook-v2— seesrc/state/slab/LICENSE-OPENBOOK. Each side holds up to 1024 leaves. Critbit is balanced-by-construction: depth is bounded by the bit width of the sort key (128 bits: price in the high 64, sequence number in the low 64), not by insertion order. This makes the monotonic-price DoS attack on a plain BST structurally impossible.Money math
checked_*— no raw+/-/*//on money values.u64money values is widened tou128before multiplying, then narrowed back withtry_into— prevents spurious overflow on large but arithmetically-valid trades.fee_quote <= gross_quoteenforced as a defence-in-depth invariant after each per-fill fee calculation.transfer_checked.Tests
23 LiteSVM integration tests: setup, validation, cancel/settle round-trips, and full matching engine coverage (single fill, partial fill, remainder, multi-maker, price-time priority, price improvement, fee routing).
README
Verification
From
defi/order-book/anchor/:All 23 tests pass.
cargo checkclean modulo two pre-existing lifetime-elision warnings inherited from the upstream Openbook v2 slab.