Skip to main content

Overview

The Omnipair program is a Solana smart contract built with Anchor that implements an oracle-less AMM with integrated lending markets.
Program ID: omnixgS8fnqHfCcTGKWj6JtKjzpJZ1Y5y9pyFkQDkYESource Code: github.com/omnipair/omnipair-rs

Instructions

Liquidity Management

Creates a new trading pair with initial liquidity.
pub fn initialize(ctx: Context<InitializeAndBootstrap>, args: InitializeAndBootstrapArgs) -> Result<()>
Arguments:
FieldTypeDescription
token0PubkeyFirst token mint (lexicographically smaller)
token1PubkeySecond token mint
amount0u64Initial token0 amount
amount1u64Initial token1 amount
swap_fee_bpsu16Swap fee in basis points
half_lifeu64EMA half-life in milliseconds
fixed_cf_bpsOption<u16>Fixed collateral factor (or None for dynamic)
Accounts:
  • pair - The new pair account (PDA)
  • token0_mint - Token0 mint account
  • token1_mint - Token1 mint account
  • lp_mint - LP token mint (created)
  • rate_model - Interest rate model account
  • futarchy_authority - Protocol authority
Adds liquidity to an existing pair.
pub fn add_liquidity(ctx: Context<AdjustLiquidity>, args: AddLiquidityArgs) -> Result<()>
Arguments:
FieldTypeDescription
amount0_desiredu64Desired token0 amount
amount1_desiredu64Desired token1 amount
amount0_minu64Minimum token0 (slippage)
amount1_minu64Minimum token1 (slippage)
Returns: LP tokens minted proportional to liquidity provided.
Removes liquidity and burns LP tokens.
pub fn remove_liquidity(ctx: Context<AdjustLiquidity>, args: RemoveLiquidityArgs) -> Result<()>
Arguments:
FieldTypeDescription
liquidityu64LP tokens to burn
amount0_minu64Minimum token0 out
amount1_minu64Minimum token1 out
A 1% withdrawal fee is charged and distributed to remaining LPs.

Spot Trading

Executes a token swap using the constant product formula.
pub fn swap(ctx: Context<Swap>, args: SwapArgs) -> Result<()>
Arguments:
FieldTypeDescription
amount_inu64Input token amount
min_amount_outu64Minimum output (slippage protection)
is_token0_inboolTrue if swapping token0 for token1
Formula:
amount_out = (amount_in_after_fee × reserve_out) / (reserve_in + amount_in_after_fee)
Fee Distribution:
  • LP share → Added to reserves
  • Protocol share → Accumulated as surplus for Futarchy

Lending Operations

Deposits collateral to enable borrowing.
pub fn add_collateral(ctx: Context<AddCollateral>, args: AdjustCollateralArgs) -> Result<()>
Arguments:
FieldTypeDescription
amountu64Collateral amount to deposit
tokenPubkeyToken mint of collateral
Withdraws collateral (subject to health check).
pub fn remove_collateral(ctx: Context<CommonAdjustCollateral>, args: AdjustCollateralArgs) -> Result<()>
Requirements:
  • Position must remain healthy after withdrawal
  • Blocked in reduce-only mode if debt exists
Borrows tokens against deposited collateral.
pub fn borrow(ctx: Context<CommonAdjustDebt>, args: AdjustDebtArgs) -> Result<()>
Arguments:
FieldTypeDescription
amountu64Amount to borrow
tokenPubkeyToken mint to borrow
Constraints:
  • Borrow limit = Collateral Value × Collateral Factor × 0.95
  • Uses impact-aware valuation at pessimistic EMA price
Repays borrowed tokens.
pub fn repay(ctx: Context<CommonAdjustDebt>, args: AdjustDebtArgs) -> Result<()>
Arguments:
FieldTypeDescription
amountu64Amount to repay
tokenPubkeyToken mint being repaid
Liquidates an undercollateralized position.
pub fn liquidate(ctx: Context<Liquidate>) -> Result<()>
Liquidation Flow:
  1. Verify position health: debt ≥ borrow_limit
  2. Calculate collateral to seize with price impact
  3. Apply 3% penalty (0.5% to liquidator, 2.5% to LPs)
  4. Write off debt shares proportionally
Close Factor: 50% for partial liquidations
Executes an uncollateralized flash loan.
pub fn flashloan(ctx: Context<Flashloan>, args: FlashloanArgs) -> Result<()>
Arguments:
FieldTypeDescription
amount0u64Token0 to borrow
amount1u64Token1 to borrow
Fee: 0.05% (5 bps)
Must repay borrowed amount + fee within the same transaction.

Governance (Futarchy)

Claims accumulated protocol fees (permissionless).
pub fn claim_protocol_fees(ctx: Context<ClaimProtocolFees>) -> Result<()>
Distribution: Fees are split according to RevenueDistribution:
  • Futarchy Treasury
  • Buybacks Vault
  • Team Treasury
Anyone can call this instruction to trigger fee distribution.
Enables/disables global reduce-only mode (authority only).
pub fn set_global_reduce_only(ctx: Context<SetGlobalReduceOnly>, args: SetGlobalReduceOnlyArgs) -> Result<()>
When enabled, blocks new borrows and liquidity additions across all pairs.

Accounts

Pair

The core account storing pool state.
pub struct Pair {
    // Token addresses
    pub token0: Pubkey,
    pub token1: Pubkey,
    pub lp_mint: Pubkey,

    // Parameters
    pub rate_model: Pubkey,
    pub swap_fee_bps: u16,
    pub half_life: u64,
    pub fixed_cf_bps: Option<u16>,

    // Virtual Reserves (r_virtual = r_cash + r_debt)
    pub reserve0: u64,
    pub reserve1: u64,

    // Cash Reserves
    pub cash_reserve0: u64,
    pub cash_reserve1: u64,

    // Price EMAs
    pub last_price0_ema: LastPriceEMA,
    pub last_price1_ema: LastPriceEMA,

    // Debt tracking
    pub total_debt0: u64,
    pub total_debt1: u64,
    pub total_debt0_shares: u128,
    pub total_debt1_shares: u128,

    // Collateral tracking
    pub total_collateral0: u64,
    pub total_collateral1: u64,

    // LP supply
    pub total_supply: u64,

    // Metadata
    pub token0_decimals: u8,
    pub token1_decimals: u8,
    pub reduce_only: bool,
}
PDA Seeds: ["gamm_pair", token0, token1, params_hash]

UserPosition

Tracks individual user’s collateral and debt.
pub struct UserPosition {
    pub owner: Pubkey,
    pub pair: Pubkey,

    // Collateral
    pub collateral0: u64,
    pub collateral1: u64,

    // Debt shares
    pub debt0_shares: u128,
    pub debt1_shares: u128,

    // Locked-in liquidation CFs
    pub collateral0_liquidation_cf_bps: u16,
    pub collateral1_liquidation_cf_bps: u16,
}
PDA Seeds: ["gamm_position", pair, owner]

RateModel

Configurable interest rate model parameters.
pub struct RateModel {
    pub target_util_start_bps: u64,  // Lower utilization target (default: 50%)
    pub target_util_end_bps: u64,    // Upper utilization target (default: 85%)
    pub rate_half_life_ms: u64,      // Rate adjustment speed
    pub min_rate_bps: u64,           // Rate floor
    pub max_rate_bps: u64,           // Rate ceiling (0 = uncapped)
    pub initial_rate_bps: u64,       // Starting rate
}

FutarchyAuthority

Protocol-level governance account.
pub struct FutarchyAuthority {
    pub authority: Pubkey,
    pub recipients: RevenueRecipients,
    pub revenue_share: RevenueShare,
    pub revenue_distribution: RevenueDistribution,
    pub global_reduce_only: bool,
}

Events

The program emits events for indexing and tracking:
EventDescription
PairCreatedEventNew pair initialized
SwapEventSwap executed
AdjustLiquidityEventLiquidity added/removed
AdjustCollateralEventCollateral deposited/withdrawn
AdjustDebtEventBorrow/repay executed
UserPositionLiquidatedEventPosition liquidated
FlashloanEventFlash loan executed
ClaimProtocolFeesEventFees distributed
UpdatePairEventPair state updated (interest accrual)

Error Codes

Common errors you may encounter:
ErrorDescription
BorrowingPowerExceededBorrow amount exceeds collateral limit
UndercollateralizedPosition health below threshold
NotUndercollateralizedCannot liquidate healthy position
SlippageExceededOutput below minimum requested
InsufficientLiquidityNot enough reserves for operation
ReduceOnlyModeOperation blocked in reduce-only mode
BorrowExceedsReserveCannot borrow more than cash reserves

Verifiable Builds

All releases use Anchor’s verifiable build system:
# Verify on-chain program matches source
solana-verify verify-from-repo \
  --remote \
  --program-id omnixgS8fnqHfCcTGKWj6JtKjzpJZ1Y5y9pyFkQDkYE \
  https://github.com/omnipair/omnipair-rs \
  --library-name omnipair \
  --bpf-flag "features=production"

View on OtterSec Registry

Verified build status and audit submissions