๐ Solana AMM (Automated Market Maker)
A production-ready decentralized exchange (DEX) built on Solana blockchain featuring support for both SPL Token and Token-2022 standards. This AMM implements the battle-tested constant product formula (xรy=k) with configurable fees, comprehensive security features, and a modern Next.js frontend.
โจ Key Features
- ๐ Dual Token Standard Support - Compatible with SPL Token and Token-2022
- ๐ Constant Product Formula - Proven xรy=k mathematical model for price discovery
- โ๏ธ Configurable Fees - Flexible basis points system (0-10000 BP)
- ๐ก๏ธ Slippage Protection - User-defined minimum output validation
- ๐ Security First - Program Derived Addresses and checked arithmetic
- ๐จ Modern Frontend - React-based UI with wallet integration
- ๐งช Comprehensive Testing - Full test coverage for all operations
๐๏ธ System Architecture
Core Components
| Component | Description |
|---|---|
| Pool | Main state account storing token mints, vaults, and configuration |
| LP Mint | ERC20-like tokens representing liquidity provider ownership |
| Token Vaults | Associated token accounts holding the actual token reserves |
Program Derived Addresses (PDAs)
// Pool account derivation Pool PDA: ["pool", token_a_mint, token_b_mint] // LP mint derivation LP Mint PDA: ["lp_mint", pool_account_key]
Available Instructions
- ๐ Initialize Pool - Create new token trading pairs
- ๐ง Add Liquidity - Deposit tokens and receive LP tokens
- ๐ธ Remove Liquidity - Burn LP tokens to withdraw underlying assets
- ๐ Swap AโB - Exchange token A for token B with fee
- ๐ Swap BโA - Exchange token B for token A with fee
๐ Getting Started
Prerequisites
Ensure you have the following installed:
- Node.js v18 or higher
- Rust and Anchor CLI v0.31.1
- Solana CLI v1.18+
- pnpm package manager
Installation & Setup
# Clone the repository git clone https://github.com/Shradhesh71/AMM.git cd AMM # Install dependencies pnpm install # Build the Anchor program pnpm anchor-build # Generate program types pnpm anchor build # Run comprehensive tests pnpm anchor-test
Local Development Environment
# Terminal 1: Start Solana test validator solana-test-validator # Terminal 2: Deploy program locally pnpm anchor deploy # Terminal 3: Start the frontend development server pnpm dev
The frontend will be available at http://localhost:3000
๐ AMM Mathematics
Liquidity Provision Formulas
Initial Liquidity (Bootstrap):
LP_tokens = โ(amount_a ร amount_b)
Subsequent Liquidity (Proportional):
LP_tokens = min(
(amount_a ร current_lp_supply) / reserve_a,
(amount_b ร current_lp_supply) / reserve_b
)
Trading Formulas
Constant Product Invariant:
reserve_a ร reserve_b = k (constant before and after trade)
Swap Output Calculation:
amount_in_after_fee = amount_in ร (10000 - fee_rate) / 10000
amount_out = (reserve_out ร amount_in_after_fee) / (reserve_in + amount_in_after_fee)
๐งช Testing & Quality Assurance
Test Coverage
- โ Pool Initialization - Both SPL Token and Token-2022 pools
- โ Liquidity Management - Add/remove operations with proper LP calculations
- โ Token Swapping - Bidirectional swaps with fee validation
- โ Security Checks - PDA validation, slippage protection, arithmetic safety
- โ Edge Cases - Zero amounts, insufficient liquidity, overflow protection
Running Tests
# Run all tests with coverage pnpm anchor-test # Run tests with detailed output cd anchor && anchor test --skip-lint --verbose # Run specific test suite cd anchor && anchor test --skip-lint --grep "AMM"
๐ Deployment Guide
Devnet Deployment
# Set cluster to devnet anchor config set --provider.cluster devnet # Ensure you have devnet SOL solana airdrop 2 --url devnet # Deploy to devnet anchor deploy --provider.cluster devnet
โ๏ธ Configuration Options
Fee Structure
pub struct Pool { pub fee_rate: u16, // Basis points (100 = 1%, 10000 = 100%) // ... other fields }
Recommended Fee Ranges:
- Low-volume pairs: 300 BP (3%)
- Stablecoin pairs: 25-50 BP (0.25%-0.5%)
- Volatile pairs: 100-300 BP (1%-3%)
๐ก๏ธ Security Features
| Feature | Implementation |
|---|---|
| PDA Security | All accounts use cryptographically secure Program Derived Addresses |
| Arithmetic Safety | Checked operations prevent integer overflow/underflow |
| Access Control | Proper authority validation for all sensitive operations |
| Slippage Protection | User-defined minimum output amounts prevent MEV attacks |
| State Validation | Comprehensive pool state and balance checks |
๐ Project Structure
anchor-AMM/
โโโ anchor/ # Solana program
โ โโโ programs/amm/src/
โ โ โโโ lib.rs # Program entry point
โ โ โโโ states.rs # Pool state definition
โ โ โโโ errors.rs # Custom error types
โ โ โโโ instructions/ # Instruction handlers
โ โ โโโ initialize_pool.rs # Pool creation logic
โ โ โโโ add_liquidity.rs # Liquidity provision
โ โ โโโ remove_liquidity.rs # Liquidity withdrawal
โ โ โโโ swap.rs # Token swapping logic
โ โ โโโ helper.rs # Utility functions
โ โโโ tests/ # Comprehensive test suite
โโโ src/ # Next.js frontend
โ โโโ app/ # App router (Next.js 14)
โ โโโ components/ # React components
โ โ โโโ ui/ # Reusable UI components
โ โ โโโ solana/ # Solana-specific components
โ โ โโโ amm/ # AMM-specific components
โ โโโ lib/ # Utility functions
โโโ public/ # Static assets
โโโ package.json # Dependencies and scripts
๐ค Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure all tests pass and follow the existing code style.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.