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 capital provider 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, fee asset, pair, and applicable capital provider configs must be unpaused
  2. Asset Types: Each Stable side requires its capital provider stable config; RWA/RWA pairs are also supported
  3. Amount: Must be greater than zero
  4. NAV Prices: Both assets and the configured fee token must return valid, non-zero NAV prices
  5. Slippage: Calculated output/input must satisfy min/max constraints
  6. Pair Fees: redemption_fee_bps + discount_rate_bps must be less than 10000

Treasury Remaining Accounts

When the capital provider enables Treasury routing, the first 14 remaining accounts are supplied in this order: The bundle is followed by the counted Treasury NAV accounts, the counted APY accounts, and then the remaining oracle and transfer-hook accounts. The Treasury program ID is fixed to the deployment listed on the Deployments page. Transfer-hook accounts must not introduce additional signers. The program uses idle vault liquidity first, redeems eligible Stable output shortfalls, and sweeps eligible Stable input balances after receiving the user’s tokens. The Treasury routing conditions apply; RWA inputs are not swept during swaps. The SDK resolves this account plan automatically.

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. Protocol fees are paid from a separate capital provider fee-token ATA, using GlobalConfig.fee_token_mint; they are not transferred from the user’s input or the capital provider liquidity vault. The protocol’s spread share also affects the user-facing price.

QuoteToBase

  1. Apply the pair redemption fee and protocol spread rate to the quote input.
  2. Apply the NAV ratio to the remaining amount to calculate base output.

BaseToQuote

  1. Apply the NAV ratio to calculate the quote-token equivalent.
  2. Apply the pair discount fee and protocol spread rate to calculate quote output.
For RWA/RWA pairs, the pair fee is the sum of redemption and discount fees in either direction. The protocol spread rate is derived from both pair fee settings and the configured spread take; a nonzero spread take has a minimum one-basis-point effective rate. Base protocol fees are calculated separately on the quote-token fee basis and converted to fee-token units using NAV and token decimals.

Decimal Normalization

Calculations use checked 128-bit arithmetic and 256-bit intermediates for fee-token conversion:
  • NAV prices normalized to 9 decimals
  • Intermediate calculations use combined decimal precision
  • Final amounts rounded appropriately (ceil for fees, floor for outputs)

Usage Examples

These Anchor examples assume an existing pair with stablecoin as quote and RWA as base, with Treasury disabled. Direction follows the stored pair order; newly created pairs use canonical mint ordering. program is an Anchor client generated from the matching program IDL, separate from the Kit SDK.

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
  • The capital provider’s fee-token account has sufficient balance to pay protocol fees
  • User signs the transaction and supplies the required token accounts
  • Valid NAV prices are available

Next: Global Configuration

Learn about program-wide configuration management