An Automated Market Maker (AMM) program implemented on Solana using the Anchor framework. This project implements a liquidity pool system with token swapping based on the constant product formula (x * y = k).
AMM client repo: https://github.com/franRappazzini/amm-client
- Protocol Initialization: Configuration of protocol and swap fees
- Liquidity Pool Creation: Create pools for token pairs
- Liquidity Deposits: Users can deposit tokens and receive LP tokens
- Liquidity Withdrawals: Redeem LP tokens for underlying tokens
- Token Swaps: Exchange tokens with slippage protection
- Protocol Fees: Fee system with withdrawal capability
The AMM program is deployed on Solana Devnet and can be used without any issues:
Program ID: amm58rutPHRg7dSBYyKdWiCzwSJ7ehhDYAbHnzWt3sD
You can interact with this program directly on devnet without needing to deploy it locally.
For a comprehensive and detailed explanation of the project including architecture, workflows, implementation details, and more, visit:
Complete Project Documentation by DeepWiki
Before starting, make sure you have installed:
- Rust (latest stable version)
- Solana CLI (v1.17 or higher)
- Anchor CLI (v0.32.1 or higher)
- Node.js (v18 or higher)
- Yarn (package manager)
- Trident (for fuzz testing) - Optional
rustc --version
solana --version
anchor --version
node --version
yarn --versioncargo install trident-cliVerify Trident installation:
trident --versiongit clone git@github.com:franRappazzini/amm.git
cd ammyarn installSet up your Solana wallet and make sure you're on the localnet network:
# Create a new wallet if you don't have one
solana-keygen new
# Set cluster to localnet
solana config set --url localhostIn a separate terminal, run:
solana-test-validator -rKeep this terminal open while running tests.
anchor buildanchor deployRun all tests:
anchor test --skip-local-validatorOr use the yarn script:
yarn test# Main test
yarn ts-mocha -p ./tsconfig.json -t 1000000 tests/amm-local.test.ts
# Decimals test
yarn ts-mocha -p ./tsconfig.json -t 1000000 tests/amm-decimals-local.test.ts
# Slippage test
yarn ts-mocha -p ./tsconfig.json -t 1000000 tests/amm-slippage-local.test.tsThis project includes fuzz tests built with Trident, a powerful fuzzing framework for Solana programs. Fuzz testing helps discover edge cases and vulnerabilities by automatically generating random inputs.
- Trident CLI installed (see installation instructions above)
- Honggfuzz dependencies (installed automatically by Trident)
- Navigate to the trident-tests directory:
cd trident-tests- Build the fuzz tests:
trident fuzz build fuzz_0- Run the fuzz tests:
# Run with default settings
trident fuzz run fuzz_0
# Run with specific number of iterations
trident fuzz run fuzz_0 -- --iterations 10000
# Run with time limit (in seconds)
trident fuzz run fuzz_0 -- --timeout 300- View fuzz test metrics (if enabled):
The fuzz tests include metrics and dashboard functionality. After running tests, metrics will be displayed in the terminal.
trident-tests/
βββ Cargo.toml # Fuzz test dependencies
βββ Trident.toml # Trident configuration
βββ fuzz_0/ # Fuzz test suite
βββ test_fuzz.rs # Main fuzz test logic
βββ fuzz_accounts.rs # Account configurations
βββ types.rs # Type definitions
The fuzz tests automatically test various scenarios including:
- Protocol initialization with random fee parameters
- Liquidity pool creation with random token amounts
- Deposits and withdrawals with varying amounts
- Token swaps with different slippage parameters
- Edge cases and boundary conditions
amm/
βββ programs/
β βββ amm/
β βββ src/
β βββ lib.rs # Program entry point
β βββ constants.rs # Program constants
β βββ errors.rs # Custom errors
β βββ instructions/ # Instruction logic
β βββ states/ # Program data structures
β βββ utils/ # Helper functions
βββ tests/
β βββ amm-local.test.ts # Main tests
β βββ amm-decimals-local.test.ts # Decimals tests
β βββ amm-slippage-local.test.ts # Slippage tests
β βββ utils/ # Testing utilities
βββ trident-tests/
β βββ Trident.toml # Trident configuration
β βββ Cargo.toml # Fuzz test dependencies
β βββ fuzz_0/ # Fuzz test suite
β βββ test_fuzz.rs # Main fuzz test logic
β βββ fuzz_accounts.rs # Account configurations
β βββ types.rs # Type definitions
βββ target/
β βββ idl/
β βββ amm.json # Generated IDL
βββ Anchor.toml # Anchor configuration
βββ Cargo.toml # Rust configuration
βββ package.json # Node.js dependencies
βββ MATH.md # AMM mathematical documentation
Initializes the protocol's global configuration with fees.
Creates a new liquidity pool for a token pair.
Allows users to deposit tokens into a pool and receive LP tokens.
Allows users to burn LP tokens and receive the underlying tokens.
Allows swapping one token for another with slippage protection.
Allows withdrawing accumulated protocol fees.
The AMM uses the constant product formula:
x * y = L
Where:
x= amount of token A in the pooly= amount of token B in the poolL= liquidity constant
For more details about the formulas and calculations, see MATH.md.
# Check formatting
yarn lint
# Auto-fix formatting
yarn lint:fixanchor buildsolana logs# Clean the ledger and restart
solana-test-validator --reset# Clean and rebuild
anchor clean
anchor build# Make sure the validator is running
# Verify the program is deployed
solana program show 92NnZLZ8TS5Ay1UwAnQmtbYWbAFcEWtZcn7MwVkLhhMZContributions are welcome. Please open an issue or pull request for suggestions or improvements.
Note: This project is configured to work on localnet by default. To deploy on devnet or mainnet, modify the configuration in Anchor.toml.