Serum

Serum order book matching followed price-time priority and distinct execution rules

Serum order book matching used on-chain price-time priority on Solana, while order types controlled immediate execution and the unfilled remainder. Standard limit orders could take available matches and leave eligible remaining quantities resting on the book. Immediate-or-cancel orders accepted available fills without leaving a waiting remainder. Post-only orders could add liquidity only when their prices did not cross opposing orders. Price priority came first; earlier orders took precedence within a price level. The original mainnet program became defunct following the November 2022 collapse of FTX and Alameda Research. These legacy rules explain historical order records, including differences between a submitted request, a resting order, and an account balance.

Key takeaway: A post-only order could rest only when its price did not cross the opposing quote during processing.

Order types and unfilled quantities

Limit, ImmediateOrCancel, and PostOnly controlled whether the matching engine could trade immediately, post remaining quantity, or release unused trading funds. A Limit order allowed both matching and posting. ImmediateOrCancel, usually abbreviated IOC, allowed matching and prevented posting. PostOnly prevented taking liquidity while permitting a non-crossing order to rest. Each choice still carried a side, limit price, and quantity. Choosing IOC did not remove the price constraint, and choosing Limit did not require the order to wait before trading.

The differences mattered most when an order could not execute completely. A remaining Limit quantity could retain a book entry and committed funds. IOC cancelled its unfilled remainder, releasing funds no longer needed for that request. A crossing PostOnly request left no resting order. These outcomes described the order's execution policy; they did not establish whether the entire Solana transaction succeeded. Account validation, funding, or an execution error could prevent an otherwise valid order from producing any committed changes.

Book entries and OpenOrders balances

A resting book entry identified its price, remaining quantity, and OpenOrders account, while the account tracked associated trading balances. The market's base token was the asset bought or sold; its quote token denominated the price. Buy orders committed quote funds, and sell orders committed base funds. Market vaults held the tokens behind those records. A quantity displayed in the order book therefore represented outstanding trading interest, rather than a token balance already available in the owner's receiving account.

Fill events connected the matching engine's changes to account bookkeeping. NewOrderV3 updated the incoming order's proceeds during processing, while resting makers received their balance updates through event consumption. That distinction explained why a removed book entry and an updated maker balance could appear at different stages. Free balances in OpenOrders could fund another order in that market. Settlement transferred free funds to the appropriate token accounts; it did not move funds still committed to a resting order.

What determined priority at the same price?

Earlier program-generated order sequences had priority over later sequences among resting orders at the same price. Serum encoded price and sequence information together in each native order ID. The engine selected the highest-priced bid or lowest-priced ask first. Within the selected price, its ordering favored the earlier sequence. This sequence came from program processing, so a local clock reading or the moment someone clicked an interface control did not determine the order's position.

Price priority applied before time priority. An earlier order at a less competitive price did not jump ahead of a better quote. Individual orders also kept their own identities, even when an interface combined their quantities into one depth row. That aggregate row described available size at a price, without revealing every order's place within it. Partial execution reduced a resting order's quantity while retaining its native ID, preserving its sequence priority for the remaining quantity.

When did a limit order take liquidity?

A limit order took liquidity when its price crossed an eligible resting order and its request met funding and matching constraints.

A buy limit crossed an ask at or below its ceiling. A sell limit crossed a bid at or above its floor. Equality also allowed a match. The resting order's price determined each execution. A buy order could therefore fill below its ceiling, and a sell order could fill above its floor. The limit constrained acceptable execution prices. It did not establish one guaranteed price for the complete quantity.

Available depth determined how much the engine could match at each level. Multiple resting orders could contribute fills to the incoming request. Once one price level emptied, further execution required another eligible order within the limit. A broad price limit still could not create missing liquidity.

A Limit order could take liquidity first and later provide liquidity through its resting remainder. Maker and taker described those execution roles, rather than permanent categories of trader. Trading fees followed the applicable execution role and fee rules, while committed funds supported whatever quantity remained open.

Post-only handling at the spread

PostOnly kept an incoming order from taking liquidity, and a crossing price prevented that request from entering the resting book. A non-crossing price permitted posting subject to funding and other program checks. A crossing request could finish without a fill or a resting entry, releasing its unused trading funds within OpenOrders. That outcome differed from an execution error: a successful transaction alone did not prove the post-only order had joined the queue.

Visual outline: Serum order book - Post-only handling at the spread

Open full-size image

Immediate-or-cancel and bounded matching

IOC matched eligible resting liquidity during processing and cancelled any quantity the request could not execute within its constraints. Its immediate behavior referred to the matching operation. It did not promise a fixed interval between submission and Solana confirmation. An empty opposing book, an incompatible price, or insufficient usable depth could leave the request with no fills, even when its transaction otherwise succeeded.

The matching instruction also limited how much processing work the engine performed. Price compatibility alone therefore did not guarantee the engine would exhaust every available match in one instruction. The engine also refused to post a remainder still crossing the opposing book. Funding imposed another bound: a buy request supplied a maximum quote-token commitment including trading fees, alongside its maximum base quantity. The smaller effective allowance constrained execution. An apparently large quantity field could not override the quote budget, and an unused budget did not imply the full requested quantity had traded.

How did Serum prevent trades within one OpenOrders account?

Serum compared the OpenOrders accounts recorded on each order and applied the incoming request's selected self-trade behavior when those accounts matched.

The comparison did not extend across every account a wallet controlled. DecrementTake reduced the overlapping quantities on both the incoming order and the opposing resting order without executing a trade between them. The overlapping amount did not represent proceeds from an exchange.

CancelProvide removed the opposing resting order that would self-match. It allowed the incoming request to continue processing other eligible orders, within the remaining matching allowance. This setting could change which existing order remained open, even when the incoming request produced no trade against that account.

AbortTransaction returned an error at the self-trade boundary. A self-trade setting could thus change the outcome of an otherwise price-compatible request. Reading the selected behavior explained whether a quantity reduction, removal, or failed execution reflected this account-level rule.

Decision checklist for an IOC buy request

Consider a historical request to buy within a chosen price ceiling through NewOrderV3, with no resting remainder. Funding was a prerequisite: available quote funds in OpenOrders and the payer token account had to cover the required commitment.

With adequate funding and eligible asks, the request matched within its limits and cancelled any unfilled quantity. The incoming OpenOrders account reflected the acquired base funds and released unused quote funds. Settlement transferred the free funds into the receiving token account.

If funding fell short, NewOrderV3 returned an insufficient-funds error before matching. Increasing available funding or reducing the required commitment addressed that boundary. Switching the request to Limit or PostOnly did not bypass it, and a failed request created no resting order to cancel.

Cancellation and replacement priority

Cancellation removed the outstanding quantity of a resting order, while replacement introduced a new order with a new sequence position. Earlier fills remained completed trades. Reusing an application-level identifier did not recover the cancelled order's old priority. Replacement also applied its new price and execution policy to the book conditions at processing time, so a previously resting order's replacement could become immediately marketable.

OpenBook continued the order-book approach through a separately deployed community fork. Shared ancestry did not combine its markets, balances, or queues with the original Serum deployment. Within the legacy book, retaining an existing order preserved its place among same-priced peers. Replacing it allowed changed terms while giving the new order a later sequence position at its chosen price.

Common questions about Serum order book

Did client order IDs influence Serum's matching priority?

Client order IDs did not determine matching priority in Serum's legacy book. The native order ID carried the price and program-generated sequence used for ordering. Applications could supply client identifiers for tracking and cancellation, but choosing a smaller identifier did not move an order ahead of other orders at its price.

Were stop-loss triggers built into Serum's native order types?

The legacy native order-type choices were Limit, ImmediateOrCancel, and PostOnly, without a stop-trigger order type. A stop-loss label in an application required logic beyond those matching policies. Its trigger and submission behavior therefore belonged to that application's implementation; the label alone did not establish a stop order resting in Serum's book.

Why could an entered order size shrink to a whole-lot quantity?

The legacy TypeScript client converted base-token sizes into whole trading lots and rounded down at the lot-conversion step. A market's base lot size determined the representable quantity. This could make the submitted quantity smaller than the entered amount; a size converting to zero lots failed the client's size check.

Could Serum remove a resting order when book storage filled?

The legacy engine could evict a resting order when insertion ran out of book storage. It removed the least competitive existing bid or ask on the affected side, then inserted the new order. An associated out event released the removed order's committed funds through account bookkeeping; disappearance alone did not establish a fill.

Was a higher network fee enough to advance an existing resting order?

A higher network fee did not rewrite an existing resting order's priority key. Solana transaction prioritization concerned transaction processing, while Serum's resting queue used the stored price and sequence. A later transaction could cancel or replace an order; replacement created a new priority position.

Did PostOnly automatically adjust the price to keep an order resting?

Legacy PostOnly did not automatically move the specified limit price away from the opposing quote. Its matching policy prevented taking liquidity and required a non-crossing price for posting. Any application feature that adjusted prices needed separate behavior; the native selection alone did not supply automatic repricing or continuous quote maintenance.

Why could an OpenOrders account remain full after its orders had filled?

A legacy OpenOrders account had 128 order slots, and pending completion events could keep slots occupied after matching removed their book entries. Event consumption cleared the completed order records and freed their slots. A screen showing fewer resting orders therefore did not establish how many slots the account could use for new requests.

Updated -