Skip to main content
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 }.
  1. Validate oracle and clamp mark price to oracle ± 10%.
  2. Reserve notional × initial_margin_ratio out of free_collateral().
  3. If is_market, walk the book up to 4 fills:
    • skip self-trades
    • apply taker fee (to fee_vault)
    • adjust both counterparties’ base_amount and quote_entry
    • emit OrderFilled
  4. Any remainder becomes a resting order (if not post_only and not market).

liquidate

  1. Validate oracle.
  2. health ≤ 0 check (NotLiquidatable otherwise).
  3. Cancel all resting orders, their reserved margin goes to insurance_vault.
  4. Close the full position at oracle price.
  5. Charge 5% liquidation fee: 2.5% to liquidator, 2.5% to insurance.
  6. If collateral insufficient, drain insurance fund. If still insufficient, emit BadDebtDetected and 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.

Funding rate concept

Formula, clamp, and settlement.

Trade a perpetual

Walkthrough.