Overview
Theswap 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:- Pause States: Program, both assets, fee asset, pair, and applicable capital provider configs must be unpaused
- Asset Types: Each Stable side requires its capital provider stable config; RWA/RWA pairs are also supported
- Amount: Must be greater than zero
- NAV Prices: Both assets and the configured fee token must return valid, non-zero NAV prices
- Slippage: Calculated output/input must satisfy min/max constraints
- Pair Fees:
redemption_fee_bps + discount_rate_bpsmust 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
NAV-Based Pricing
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.
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, usingGlobalConfig.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
- Apply the pair redemption fee and protocol spread rate to the quote input.
- Apply the NAV ratio to the remaining amount to calculate base output.
BaseToQuote
- Apply the NAV ratio to calculate the quote-token equivalent.
- Apply the pair discount fee and protocol spread rate to calculate quote output.
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