Docs · Concepts

Clearing at one price

The rule that sets a spot market's clearing price in a batch, a worked example, how orders at the price are filled, and how pools, perps and vaults clear.

Every fill in a spot market in one batch happens at the same price. The price is computed on chain from the batch's opened orders and the market's liquidity vault, after the orders can no longer change.

The rule

A market's liquidity vault holds reserves x of the base asset and y of the quote asset, with k = x · y. Its marginal price is y / x.

For a candidate price p, two quantities are compared:

  • Net demand. The base amount that buy orders with a limit at or above p want, less the base amount that sell orders with a limit at or below p offer. Market orders count at every price.
  • What the vault sells at p. To move its marginal price to p the vault's base reserve becomes x' = sqrt(k / p), so it sells x − x'. Below its current price that is negative: the vault buys.

The clearing price is the p where the two are equal. Net demand falls as p rises and the vault's sale rises with p, so they cross once.

Net demand only changes at limit prices. Between two neighbouring limits it is a constant N, and the crossing there is

text
p = k / (x − N)²

If the crossing falls exactly on a limit, where demand jumps across the vault's line, the clearing price is that limit.

How an order is sized

A sell order locks the base amount it sells. A buy order locks quote: a limit buy of q base at limit L locks q · L, the most it could spend, and a market buy names the quote amount it spends. At settlement a buy receives what it spent divided by the clearing price, and the quote it did not spend returns to the balance.

Who fills

  • An order whose limit is strictly better than p fills whole.
  • An order whose limit is worse than p fills nothing, and its funds return at settlement.
  • Orders at exactly p on the long side of the book fill pro rata, each by the same fraction, so that the batch balances.

Every fill is at p, including fills of orders that would have accepted a worse price. A batch with no orders in a market records the vault's own price.

A worked example

A SOL/USDC vault holds 1,000 SOL and 180,000 USDC, so k is 180,000,000 and its price is 180.00. A batch opens with two orders:

SideAmountLimit
Buy10 SOL185.00
Sell4 SOL178.00

For any price from 178.00 to 185.00 both orders are in, and net demand is N = 10 − 4 = 6 SOL. The crossing is

text
p = 180,000,000 / (1,000 − 6)² = 180,000,000 / 988,036 ≈ 182.18

which lies between the two limits, so the clearing price is 182.18.

TradesAt 182.18
BuyerReceives 10 SOLPays 1,821.80 USDC of the 1,850.00 locked. 28.20 USDC returns.
SellerGives 4 SOLReceives 728.72 USDC
VaultSells 6 SOLReceives 1,093.08 USDC

The seller's 4 SOL went to the buyer at the same price the vault traded at. The vault filled only the 6 SOL the two orders did not cover between them. Amounts here are before the market's fee.

Fees on a fill

A market's fee_bps is taken from the output of each fill: from the SOL a buyer receives, or the USDC a seller receives. The protocol share of it is recorded as owed to the protocol, and the rest stays in the vault's reserves, where it belongs to the vault's liquidity providers. Rates are in Fees and MESA.

Why the price cannot be chosen

clear takes no price from its caller. The program is handed the batch's opened orders for the market, checks that their number equals the count it recorded at open, and computes p itself. Every order in that set was committed before the batch closed and was opened against its commitment. Anyone can recompute the price from the batch record: the opened orders and the vault's reserves are public.

How the other targets clear

Clearing a pool, a perp market or a vault produces totals instead of a price.

A lending pool first accrues interest up to the present from its rate curve. It then nets the batch. Supplies add cash and mint shares at the share price after accrual, (cash + borrows − reserves) / supply_shares. Redeems burn shares and remove cash at the same share price, and a redeem beyond the cash the pool has is Refused. Borrows are taken in the order they were placed, up to the cash left, and any beyond that are Refused. This is the only moment a pool's borrow index moves, so every proof made between two clears uses the same index.

A perp market reads its oracle once and records the price. Every perp closed in the batch settles at it, paid from the perps vault in the order the closes were placed. A close whose gain the vault can no longer pay is Refused, and the position stays open. A perp opened in the batch keeps the oracle price it was placed at as its entry, and the clear adds it to open interest, up to the market's max_oi; an open beyond that is Refused. Funding is updated from the imbalance between long and short open interest.

A spot liquidity vault clears after its market in the same batch. Deposits mint shares at the ratio of the reserves as the market's clear left them, and whichever leg of a deposit exceeds that ratio is refunded. Withdrawals pay out both reserves pro rata. The perps vault mints and redeems at quote_reserve / total_shares.