Skip to content

πŸš€ Production-grade Constant Product AMM on Solana β€’ 9 instructions β€’ Full test suite β€’ Deployed on Devnet

Notifications You must be signed in to change notification settings

veerbal1/veerbal_cpmm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

44 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Veerbal CPMM - Constant Product Market Maker on Solana

A production-grade Constant Product Market Maker (CPMM) implementation on Solana, following Raydium's architecture. Built with Anchor framework.

πŸš€ Live on Devnet

Detail Value
Program ID C6TCz92bpYjWgty9mwrAoNh7u6RSdmyBRB4dMoBGgMrA
Network Solana Devnet
Status βœ… Deployed & Tested

View on Solscan

✨ Features

  • 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

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         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          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Instructions

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

πŸ” Security Features

  • βœ… 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

πŸ§ͺ Testing

# Run all tests on localnet
anchor test

# Run tests on devnet (after deployment)
anchor test --skip-local-validator --skip-deploy

Test Coverage

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

πŸ› οΈ Development Setup

Prerequisites

  • Rust 1.70+
  • Solana CLI 1.17+
  • Anchor 0.30+
  • Node.js 18+

Build

anchor build

Deploy to Devnet

# Configure for devnet
solana config set --url devnet

# Get devnet SOL
solana airdrop 2

# Deploy
anchor deploy

πŸ“ Project Structure

programs/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

πŸ“Š Fee Structure

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

πŸ™ Acknowledgments

This implementation follows Raydium's CPMM architecture as a learning exercise.

πŸ“„ License

MIT

About

πŸš€ Production-grade Constant Product AMM on Solana β€’ 9 instructions β€’ Full test suite β€’ Deployed on Devnet

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published