Technical DetailsCFMM Implementation

CFMM Implementation in Mage Labs

Mage Labs Constant Function Market Maker (CFMM) integrates dynamic reserve management with a dual-reserve system to achieve both price precision and liquidity stability.


Core Implementation

Platform Integration

Built for the Solana Virtual Machine (SVM) using the Anchor framework, ensuring efficient, secure, and composable on-chain execution.

Dual-Reserve System

Mage Labs uses two layers of reserves:

  • Virtual Reserves: Enhance price precision and minimize slippage by simulating deeper liquidity.
  • Real Reserves: Represent actual on-chain token balances and enforce solvency limits.

The product of virtual reserves remains constant through all swaps, preserving the CFMM invariant and ensuring consistent pricing.


Dynamic Reserve Adjustment

Each swap recalculates both virtual and real reserves to maintain the invariant:

xvyv=kx_v \cdot y_v = k

where:

  • xvx_v = virtual reserve of Token A
  • yvy_v = virtual reserve of Token B
  • kk = invariant constant

Swap Operations

Buy Operation (Token A → Token B)

  1. Compute invariant:

    k=xv×yvk = x_v \times y_v
  2. Adjust virtual reserve of Token B:

    yv=yvΔyy_v' = y_v - \Delta y
  3. Derive new virtual reserve of Token A:

    xv=kyv+1x_v' = \frac{k}{y_v'} + 1
  4. Determine Token A cost:

    CostA=xvxv\text{Cost}_A = x_v' - x_v

Sell Operation (Token B → Token A)

  1. Increase Token A reserve:

    xv=xv+Δxx_v' = x_v + \Delta x
  2. Recalculate Token B reserve:

    yv=kxvy_v' = \frac{k}{x_v'}
  3. Determine Token B output:

    OutputB=yvyv\text{Output}_B = y_v - y_v'

Liquidity & Recalibration

Liquidity events such as deposits or withdrawals trigger a recalibration routine to synchronize virtual and real reserves:

xv=f(xr,Δx),yv=f(yr,Δy)x_v = f(x_r, \Delta x), \quad y_v = f(y_r, \Delta y)

This ensures that on-chain liquidity changes are immediately reflected in trade pricing.


Fee and Reward Structure

Swap Fee (per trade)

Fee=Trade Amount×0.00007  (0.007%)\text{Fee} = \text{Trade Amount} \times 0.00007 \; (0.007\%)

The Mage Liquidity Layer offers 7 fee tiers ranging from 0.0001% to 1%, with 0.007% being one of the most competitive tiers.

Fee Distribution (post-ICO)

  • Liquidity Providers (LPs): receive 70% of all fees
  • Protocol Share (30%) split as:
    • 15% → Token Staking Pool
    • 10% → Treasury
    • 5% → NFT Staking Pool

Loyalty Boost (Time-Weighted)

LP Rewardi=(Pool Fees×siS)×B(ti)\text{LP Reward}_i = \left( \frac{\text{Pool Fees} \times s_i}{S} \right) \times B(t_i)

Where:

  • sis_i = LP’s share of liquidity
  • SS = total pool liquidity
  • B(ti)B(t_i) = loyalty multiplier (2× or 3× after 90 days)

Early and long-term capital endpoints earn an increased percentage of liquidity node fees, with boost levels reaching 2-3x after approximately 90 days.


Summary

Mage Labs CFMM model maintains a dual-reserve invariant, enabling:

  • Tight spreads and minimal slippage
  • Consistent pricing across all trade sizes
  • Transparent and adaptive fee distribution
  • Market-responsive pricing through virtual bonding curves
  • On-chain solvency via real reserve constraints

Through precise virtual and real reserve synchronization, Mage Labs achieves a robust, fair, and scalable liquidity infrastructure layer on Solana.


Additional Resources

For more information on Mage Labs fee structure and liquidity incentives, see: