kimia-perp is the core derivatives program. It owns the orderbook, tracks all
positions and funding, validates Pyth Hermes oracle posts, and runs the spot
pool that delta-vault hedges against.
Accounts
ExchangeConfig
Singleton.["exchange_config"].
Market
One per market.["market", market_index_le].
UserAccount
["user", authority].
Orderbook
Zero-copy.["orderbook", market_index_le].
- Max 32 bids (sorted descending) and 32 asks (sorted ascending).
- FIFO within price.
Order { owner, order_id, price, base_remaining, reserved_margin, direction, is_active }.
SpotPool
["spot_pool", base_mint, quote_mint].
Simple oracle-priced wSOL/USDC pool used by delta-vault. Swaps at oracle ±
spread_bps; no curve.
Instructions
place_order
PlaceOrderParams { price, base_amount, direction, post_only, is_market, max_slippage_bps }.
- Validate oracle and clamp mark price to oracle ± 10%.
- Reserve
notional × initial_margin_ratioout offree_collateral(). - If
is_market, walk the book up to 4 fills:- skip self-trades
- apply taker fee (to
fee_vault) - adjust both counterparties’
base_amountandquote_entry - emit
OrderFilled
- Any remainder becomes a resting order (if not
post_onlyand not market).
liquidate
- Validate oracle.
health ≤ 0check (NotLiquidatableotherwise).- Cancel all resting orders, their reserved margin goes to
insurance_vault. - Close the full position at oracle price.
- Charge 5% liquidation fee: 2.5% to liquidator, 2.5% to insurance.
- If collateral insufficient, drain insurance fund. If still insufficient,
emit
BadDebtDetectedand pause the market.
Events
MarketInitialized, PositionOpened, PositionClosed,
FundingRateUpdated, FundingSettled, CollateralDeposited,
CollateralWithdrawn, Liquidated, OrderPlaced, OrderFilled,
OrderCancelled, MarkPriceUpdated, SelfTradeSkipped, BadDebtDetected,
SpotPoolInitialized, SpotSwapped.
Error codes
See errors reference. The main buckets:6000..6002 oracle,
6010..6012 market, 6020..6022 margin, 6030..6033 position, 6040..6041
funding, 6050..6051 liquidation, 6110..6117 orderbook.
Read next
Funding rate concept
Formula, clamp, and settlement.
Trade a perpetual
Walkthrough.

