Sai Core (For Devs)
Sai's perpetual exchange app is implemented in Rust, a powerful language for secure, sandboxed programming, that compiles into Wasm bytecode. 5 key contracts manage everything on Sai, fully onchain.
How Sai the Sai Smart Contracts Work
perp(Perpetuals trading core):Maintains markets, positions, margin, and liquidations.
Calculates execution price with price impact, applies trading/borrowing/funding fees, and updates PnL.
Enforces risk constraints (maintenance margin, max leverage, funding payments).
Entry points (conceptually): open/close position, modify collateral, place/execute orders, liquidate under-margined positions.
Reads prices from
oracle, collects/streams fees, and settles PnL againstvault.
EVM interface (
PerpVaultEvmInterface.sol):Lets MetaMask/EVM users interact with the Wasm contracts.
Handles token conversion and cross-VM calls to
perpandvault.Mirrors the essential user flows (deposit, withdraw, open/close) for EVM wallets.
vault(Liquidity provider pool): Implements SLP Vaults.Aggregates LP liquidity to underwrite trader PnL.
Issues and accounts for shares against total assets; supports deposits and epoch-based withdrawals.
Absorbs trader PnL: when traders win, the vault pays out; when traders lose, the vault accrues profits.
Coordinates with
vault-token-minterfor share token mint/burn and withperpfor settlement transfers.
oracle(Price feed and permissions):Stores and serves the canonical mark price per market.
Controls who can update prices and under what conditions.
Provides prices to
perpfor margin checks, execution, funding, and liquidation thresholds.
vault-token-minter(Token factory bridge for shares):Controls minting/burning of vault share tokens via Nibiru’s token factory.
Enforces whitelist/ownership checks to prevent unauthorized supply changes.
Called by
vaultto mint on deposit and burn on withdrawal/redemption.
Interaction model
Liquidity lifecycle
LPs deposit assets into
vault→vaultmints shares viavault-token-minter.LPs request withdrawals →
vaultqueues and later exits via epochs, burning shares on completion.
Trading lifecycle
Trader requests an action (EVM or Wasm): open/close/modify position in
perp.perpfetches price fromoracle, computes price impact, fees, and checks margin/leverage constraints.perpupdates position state, accrues funding/borrowing fees, and computes PnL deltas.
Settlement path
Trader losses → profits accrue to
vault.Trader gains →
perpsources payout fromvault.
Funding and price alignment
perpperiodically computes funding based on price deltas between mark and index; longs/shorts pay/receive accordingly.Funding transfers are accounted in positions and net settle via
vaultover time.
Liquidations
If a position’s margin fraction drops below maintenance,
perpallows a liquidator to close the position.perpcalculates close price (with impact), applies penalties/fees, and settles PnL withvault.Any residual collateral after penalties is returned to the trader; bad debt is socialized to
vaultwithin configured limits.
Access and safety
Ownership/roles restrict sensitive ops: oracle price updates, mint permissions, parameter changes.
perpenforces invariants (max leverage, min maintenance, fee bounds).vaultenforces epoch rules and share/accounting correctness.Cross-VM calls are designed to avoid re-entrancy and maintain atomic accounting on state-changing operations.
Data and accounting at a glance
perpstate: markets, position records (size, entry price, collateral, accumulated funding/borrowing), pending orders, parameters.vaultstate: total assets, total shares, per-account share balances, withdrawal epochs/queues.oraclestate: price per market with update metadata.vault-token-minterstate: mint/burn permissions and token metadata.
Typical end-to-end flows
Open a long:
User calls
perp.open_position→perpgets price fromoracle, applies price impact and fees, locks collateral, records position.
Close a long in profit:
perpcomputes PnL at currentoracleprice (with impact), deducts fees, and transfers profit fromvaultto user.
LP exit:
User requests withdraw →
vaultqueues for epoch → on execution,vaultburns shares viavault-token-minterand releases assets.
The contracts’ business roles are:
perp(trade logic and risk),vault(liquidity and PnL sink/source),oracle(truthful prices),vault-token-minter(controlled share supply), with an EVM facade enabling wallet-native access.
Sai and the EVM
Sai is written as a Multi VM app with contracts written in both Rust and Solidity. This lets the application benefit from the security benefits of working with Wasm while remaining fully capable of tapping into the flexibility and familiarity that comes with the Ethereum Virtual Machine (EVM).
Last updated
