Skip to main content

Crate percolator

Crate percolator 

Source
Expand description

Formally Verified Risk Engine for Perpetual DEX — v12.1.0

Implements the v12.1.0 spec: Native 128-bit Architecture.

This module implements a formally verified risk engine that guarantees:

  1. Protected principal for flat accounts
  2. PNL warmup prevents instant withdrawal of manipulated profits
  3. ADL via lazy A/K side indices on the opposing OI side
  4. Conservation of funds across all operations (V >= C_tot + I)
  5. No hidden protocol MM — bankruptcy socialization through explicit A/K state only

§Atomicity Model

Public functions suffixed with _not_atomic can return Err after partial state mutation. Callers MUST abort the entire transaction on Err — they must not retry, suppress, or continue with mutated state.

On Solana SVM, any Err return from an instruction aborts the transaction and rolls back all account state automatically. This is the expected deployment model.

Public functions WITHOUT the suffix (deposit, top_up_insurance_fund, deposit_fee_credits, accrue_market_to) use validate-then-mutate: Err means no state was changed.

Internal helpers (enqueue_adl, liquidate_at_oracle_internal, etc.) are not individually atomic — they rely on the calling _not_atomic method to propagate Err to the transaction boundary.

Re-exports§

pub use i128::I128;
pub use i128::U128;

Modules§

i128
wide_math

Structs§

Account
Unified account (spec §2.1)
CrankOutcome
Outcome of a keeper crank operation
InstructionContext
Instruction context for deferred reset scheduling (spec §5.7-5.8)
InsuranceFund
Insurance fund state
RiskEngine
Main risk engine state (spec §2.2)
RiskParams
Risk engine parameters

Enums§

LiquidationPolicy
Liquidation policy (spec §10.6)
RiskError
Side
Determine which side a signed position is on. Positive = long, negative = short.
SideMode
Side mode for OI sides (spec §2.4)

Constants§

ACCOUNTS_PER_CRANK
ADL_ONE
ADL_ONE = 1_000_000 (spec §1.3)
BITMAP_WORDS
GC_CLOSE_BUDGET
LIQ_BUDGET_PER_CRANK
MAX_ABS_FUNDING_BPS_PER_SLOT
MAX_ABS_FUNDING_BPS_PER_SLOT = 10000 (spec §1.4)
MAX_ACCOUNTS
MAX_ACCOUNT_NOTIONAL
MAX_ACCOUNT_POSITIVE_PNL
MAX_FUNDING_DT
MAX_FUNDING_DT = 65535 (spec §1.4)
MAX_LIQUIDATION_FEE_BPS
MAX_MAINTENANCE_FEE_PER_SLOT
MAX_MARGIN_BPS
MAX_MATERIALIZED_ACCOUNTS
MAX_OI_SIDE_Q
MAX_ORACLE_PRICE
MAX_ORACLE_PRICE = 1_000_000_000_000 (spec §1.4)
MAX_PNL_POS_TOT
MAX_POSITION_ABS_Q
MAX_PROTOCOL_FEE_ABS
MAX_ROUNDING_SLACK
MAX_TRADE_SIZE_Q
MAX_TRADING_FEE_BPS
MAX_VAULT_TVL
MIN_A_SIDE
MIN_A_SIDE = 1_000 (spec §1.4)
POS_SCALE
POS_SCALE = 1_000_000 (spec §1.2)

Functions§

checked_u128_mul_i128
Multiply a u128 by an i128 returning i128 (checked). Computes u128 * i128 → i128. Used for A_side * delta_p in accrue_market_to.
compute_trade_pnl
Compute trade PnL: floor_div_signed_conservative(size_q * price_diff, POS_SCALE) Uses native i128 arithmetic (spec §1.5.1 shows trade slippage fits in i128).

Type Aliases§

Result