Skip to main content

Overview

The swap instruction is the core trading operation of the Multiliquid Program, enabling atomic swaps between Real World Asset (RWA) tokens and stablecoins. It supports both swap directions and both exact-in and exact-out modes.

Key Responsibilities

  • Execute bidirectional swaps between RWAs and stablecoins
  • Calculate swap amounts using NAV-based pricing
  • Apply protocol fees and LP fees
  • Validate all pause states before execution
  • Emit comprehensive swap events

Swap Instruction

Function Signature

Parameters

Swap Direction

Swap Type

Required Accounts

Validation Checks

Before executing the swap, the instruction validates:
  1. Pause States: Program, both assets, pair, and LP config must be unpaused
  2. Asset Types: RWA must be AssetType::Rwa, stablecoin must be AssetType::Stable
  3. Amount: Must be greater than zero
  4. NAV Prices: Both assets must return valid, non-zero NAV prices
  5. Slippage: Calculated output/input must satisfy min/max constraints
  6. Combined StableToAsset Fees: protocol_fees_bps + redemption_fee_bps must not exceed 10000

Events

Swap Calculation

Deterministic Pricing: The Multiliquid Program uses NAV-based pricing, not AMM-style liquidity curves. Prices are determined by asset NAV values, not by liquidity depth. There is no price impact from trade size.
The swap amount is calculated using the exchange rate between asset NAVs:

Fee Application

Fees are applied in a specific order depending on swap direction:

StableToAsset (Stablecoin → RWA)

  1. Combined Fees: Protocol fees and redemption fee are calculated simultaneously from the input amount
  2. Exchange Rate: Apply NAV ratio to the post-fee amount to calculate RWA output

AssetToStable (RWA → Stablecoin)

  1. Exchange Rate: Apply NAV ratio to calculate stablecoin equivalent
  2. Discount Rate: Apply LP discount fee
  3. Protocol Fees: Deducted from discounted amount

Decimal Normalization

All calculations use 128-bit arithmetic with proper decimal handling:
  • NAV prices normalized to 9 decimals
  • Intermediate calculations use combined decimal precision
  • Final amounts rounded appropriately (ceil for fees, floor for outputs)

Usage Examples

ExactIn: Stable → RWA

Swap exactly 1000 USDC for RWA tokens with minimum output protection:

ExactOut: Stable → RWA

Receive exactly 1000 RWA tokens, spending at most a specified USDC amount:

ExactIn: RWA → Stable

Swap exactly 1000 RWA tokens for stablecoins:

ExactOut: RWA → Stable

Receive exactly 1000 USDC, spending at most a specified RWA amount:

Error Codes

Access Control

Access: Permissionless Any wallet can execute swaps provided:
  • All pause states are inactive
  • User has sufficient token balance
  • User has approved token transfers
  • Valid NAV prices are available

Next: Global Configuration

Learn about program-wide configuration management