A production-grade Constant Product Market Maker (CPMM) implementation on Solana, following Raydium's architecture. Built with Anchor framework.
| Detail | Value |
|---|---|
| Program ID | C6TCz92bpYjWgty9mwrAoNh7u6RSdmyBRB4dMoBGgMrA |
| Network | Solana Devnet |
| Status | β Deployed & Tested |
- Constant Product AMM - x * y = k invariant
- Multi-tier Fee System - Trade, protocol, fund, and creator fees
- Dual Swap Modes - Base input (exact input) and base output (exact output)
- Full Liquidity Management - Deposit, withdraw with slippage protection
- Fee Collection - Separate collection for protocol, fund, and creator fees
- Production Security - PDA validation, checked arithmetic, owner checks
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β AMM Config β
β (Fee rates, protocol/fund owners, pool creation settings) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Pool State β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Token 0 β β Token 1 β β LP Mint β β
β β Vault β β Vault β β β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
β Fee Accumulators: protocol_fee, fund_fee, creator_fee β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Instruction | Description |
|---|---|
create_config |
Create AMM configuration with fee rates |
create_pool |
Initialize a new liquidity pool |
deposit |
Add liquidity, receive LP tokens |
withdraw |
Remove liquidity, burn LP tokens |
swap |
Swap with exact input amount |
swap_base_output |
Swap for exact output amount |
collect_protocol_fee |
Collect accumulated protocol fees |
collect_fund_fee |
Collect accumulated fund fees |
collect_creator_fee |
Collect accumulated creator fees |
- β PDA Validation - All derived accounts verified with seeds
- β Checked Arithmetic - No overflow/underflow possible
- β Owner Checks - Admin, protocol, fund, creator permissions
- β Slippage Protection - Min/max amount enforcement
- β K Invariant - Constant product verified on every swap
- β Open Time Gating - Pools can have delayed activation
# Run all tests on localnet
anchor test
# Run tests on devnet (after deployment)
anchor test --skip-local-validator --skip-deploy| Test Suite | Tests | Status |
|---|---|---|
| create-config | 1 | β |
| create-pool | 1 | β |
| deposit | 1 | β |
| withdraw | 1 | β |
| swap (base input) | 1 | β |
| swap (base output) | 1 | β |
| collect-fees | 3 | β |
| Total | 9 | β All Passing |
- Rust 1.70+
- Solana CLI 1.17+
- Anchor 0.30+
- Node.js 18+
anchor build# Configure for devnet
solana config set --url devnet
# Get devnet SOL
solana airdrop 2
# Deploy
anchor deployprograms/veerbal_cpmm/src/
βββ lib.rs # Program entrypoint
βββ constants.rs # PDA seeds
βββ error.rs # Custom errors
βββ states/
β βββ config.rs # AmmConfig account
β βββ pool.rs # PoolState account
βββ curve/
β βββ constant_product.rs # x*y=k math
β βββ fees.rs # Fee calculations
βββ instructions/
βββ create_config.rs
βββ initialize.rs # create_pool
βββ deposit.rs
βββ withdraw.rs
βββ swap_base_input.rs
βββ swap_base_output.rs
βββ collect_creator_fee.rs
βββ collect_protocol_fee.rs
βββ collect_fund_fee.rs
Fees are calculated as parts per million (1,000,000 = 100%):
| Fee Type | Description |
|---|---|
trade_fee_rate |
Total fee taken from swaps |
protocol_fee_rate |
Portion of trade fee to protocol |
fund_fee_rate |
Portion of trade fee to fund |
creator_fee_rate |
Portion of trade fee to pool creator |
This implementation follows Raydium's CPMM architecture as a learning exercise.
MIT