A Solana DEX implementation inspired by Stabble, featuring Weighted Pools, StableSwap Pools, and an Arbitrage Scanner — demonstrating deep understanding of AMM mathematics and Stabble's Smart Liquidity Architecture (SLA).
🚀 Deployed on Devnet: FURtuxyXWgpnETkNho8PL6mpuRh9mCnVsWgUY14JzusX
I studied Stabble's whitepaper and was impressed by their internal arbitrage concept — capturing MEV profits for LPs instead of external bots. This project demonstrates:
- Deep DeFi math understanding — Implemented Newton-Raphson for StableSwap from scratch
- Multi-pool architecture — Both Weighted and Stable pools with shared math library
- Arbitrage detection — TypeScript scanner comparing prices across pool types (simplified SLA)
mini-stabble/
├── programs/mini-stabble/
│ └── src/
│ ├── lib.rs # Program entrypoint
│ ├── errors.rs # Custom errors
│ ├── constants.rs # Fee precision, etc.
│ ├── math/
│ │ ├── fixed.rs # Fixed-point arithmetic (SCALE = 10^9)
│ │ ├── weighted.rs # Weighted pool math
│ │ └── stable.rs # StableSwap math (Newton-Raphson)
│ ├── state/
│ │ ├── weighted_pool.rs # WeightedPool account
│ │ └── stable_pool.rs # StablePool account
│ └── instructions/
│ ├── initialize_weighted_pool.rs
│ ├── initialize_stable_pool.rs
│ ├── swap.rs # Weighted swap
│ ├── stable_swap.rs # Stable swap
│ ├── deposit.rs # Weighted deposit
│ └── stable_deposit.rs # Stable deposit
├── sdk/src/
│ ├── spotPrice.ts # Spot price calculation
│ └── scanner.ts # Arbitrage opportunity detector
└── tests/
└── mini-stabble.ts # 7 integration tests
All calculations use fixed-point integers to avoid floating-point non-determinism:
SCALE = 10^9 (1 billion)
Example: 0.5 is stored as 500,000,000
1.0 is stored as 1,000,000,000
Invariant:
Where
Swap: Output Given Input
Spot Price:
Invariant (Newton-Raphson):
For 2 tokens, simplified:
Newton's Iteration:
Where:
-
$Ann = A \times n$ (amplification factor scaled) -
$S = \sum B_i$ (sum of balances) $D_P = \frac{D^n}{\prod (n \cdot B_i)}$ -
$P = 1000$ (AMP_PRECISION constant)
Calculating Y (token balance):
Given invariant
Where:
$c = \frac{D^2 \cdot P}{Ann \cdot \Pi} \times B_y$ -
$b = \frac{D \cdot P}{Ann} + S'$ (sum excluding$y$ )
Spot Price (Numerical Approximation):
We calculate calc_out_given_in(ref_amount) with a small reference amount (10^9) to get instantaneous price.
Price Difference:
Fee Consideration:
Profitable if:
| Feature | Status | Notes |
|---|---|---|
| Fixed-Point Math | ✅ | mul_down, mul_up, div_down, div_up, pow_down, pow_up, complement |
| Weighted Pool | ✅ | Initialize, Swap, Deposit |
| StableSwap Pool | ✅ | Initialize, Swap, Deposit with Newton-Raphson |
| Spot Price (SDK) | ✅ | Both pool types |
| Arbitrage Scanner | ✅ | Fee-aware detection |
| Integration Tests | ✅ | 7 tests passing |
cd mini-stabble
# Build
anchor build
# Run all tests
anchor test
# Run Rust unit tests only
cargo test --libTest Output:
mini-stabble
weighted pool
✔ initializes weighted pool
✔ deposits liquidity
✔ swaps tokens
Stable Pool
✔ initializes stable pool
✔ deposits liquidity
✔ swaps tokens
arbitrage
✔ detects arbitrage opportunity
7 passing
import { detectArbitrage } from './sdk/src/scanner';
const result = detectArbitrage(
{
balanceA: 100_000_000_000n,
balanceB: 95_000_000_000n,
weightA: 500_000_000n, // 0.5
weightB: 500_000_000n, // 0.5
swapFee: 3_000_000n, // 0.3%
},
{
balanceA: 100_000_000_000n,
balanceB: 100_000_000_000n,
swapFee: 3_000_000n,
amp: 100_000n, // 100 * AMP_PRECISION
}
);
// Output:
// {
// weightedPrice: 0.95,
// stablePrice: 1.0,
// priceDiffPercent: 5.26,
// totalFeesPercent: 0.6,
// netProfitPercent: 4.66,
// profitable: true,
// direction: 'weighted_to_stable'
// }| Decision | Why |
|---|---|
| Newton-Raphson for StableSwap | Matches Curve's approach for finding invariant D |
| U192 for intermediate math | Prevents overflow in D² calculations |
| Spot price via small swap | More accurate than derivative for imbalanced pools |
| Fee-aware arbitrage | Real-world profitability requires considering both swap fees |
| Shared math library | Code reuse between pool types |
-
execute_arbitrageinstruction (atomic buy→sell) - Profit routing to LPs (Stabble's SLA innovation)
- Multi-hop router
-
Devnet deployment✅
Built by Veerbal Singh | Inspired by Stabble's Smart Liquidity Architecture