Skip to content

codingirldev/AMM

Β 
Β 

Repository files navigation

πŸš€ 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.

Solana Anchor Next.js

✨ 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

  1. 🏁 Initialize Pool - Create new token trading pairs
  2. πŸ’§ Add Liquidity - Deposit tokens and receive LP tokens
  3. πŸ’Έ Remove Liquidity - Burn LP tokens to withdraw underlying assets
  4. πŸ”„ Swap Aβ†’B - Exchange token A for token B with fee
  5. πŸ”„ 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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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.


Built with ❀️ on Solana

Website β€’ Twitter

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 77.6%
  • Rust 17.8%
  • CSS 4.2%
  • JavaScript 0.4%