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.
- π 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
| 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 |
// Pool account derivation
Pool PDA: ["pool", token_a_mint, token_b_mint]
// LP mint derivation
LP Mint PDA: ["lp_mint", pool_account_key]- π 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
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
# 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# 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 devThe frontend will be available at http://localhost:3000
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
)
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)
- β 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
# 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"# 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 devnetpub 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%)
| 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 |
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
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.
This project is licensed under the MIT License - see the LICENSE file for details.