Skip to content

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)

Notifications You must be signed in to change notification settings

franRappazzini/amm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AMM - Automated Market Maker on Solana

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

πŸš€ Features

  • 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

Auditware Radar audit

🌐 Devnet Deployment

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.

πŸ“š Documentation

For a comprehensive and detailed explanation of the project including architecture, workflows, implementation details, and more, visit:

Complete Project Documentation by DeepWiki

πŸ“‹ Prerequisites

Before starting, make sure you have installed:

Verify installations

rustc --version
solana --version
anchor --version
node --version
yarn --version

Install Trident (Optional - for fuzz testing)

cargo install trident-cli

Verify Trident installation:

trident --version

πŸ”§ Installation

1. Clone the repository

git clone git@github.com:franRappazzini/amm.git
cd amm

2. Install dependencies

yarn install

3. Configure Solana CLI

Set 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 localhost

πŸ§ͺ Local Testing

1. Start a local Solana validator

In a separate terminal, run:

solana-test-validator -r

Keep this terminal open while running tests.

2. Build the program

anchor build

3. Deploy the program locally

anchor deploy

4. Run the tests

Run all tests:

anchor test --skip-local-validator

Or use the yarn script:

yarn test

Run specific tests

# 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.ts

🎯 Fuzz Testing with Trident

This 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.

Prerequisites for Fuzz Testing

  • Trident CLI installed (see installation instructions above)
  • Honggfuzz dependencies (installed automatically by Trident)

Running Fuzz Tests

  1. Navigate to the trident-tests directory:
cd trident-tests
  1. Build the fuzz tests:
trident fuzz build fuzz_0
  1. 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
  1. View fuzz test metrics (if enabled):

The fuzz tests include metrics and dashboard functionality. After running tests, metrics will be displayed in the terminal.

Fuzz Test Structure

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

What the Fuzz Tests Cover

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

πŸ“ Project Structure

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

πŸ”‘ Program Instructions

Initialize

Initializes the protocol's global configuration with fees.

Create Liquidity Pool

Creates a new liquidity pool for a token pair.

Deposit Liquidity

Allows users to deposit tokens into a pool and receive LP tokens.

Redeem LP

Allows users to burn LP tokens and receive the underlying tokens.

Swap

Allows swapping one token for another with slippage protection.

Withdraw Protocol Fees

Allows withdrawing accumulated protocol fees.

πŸ“ Mathematical Model

The AMM uses the constant product formula:

x * y = L

Where:

  • x = amount of token A in the pool
  • y = amount of token B in the pool
  • L = liquidity constant

For more details about the formulas and calculations, see MATH.md.

πŸ› οΈ Development

Linting

# Check formatting
yarn lint

# Auto-fix formatting
yarn lint:fix

Build program only

anchor build

View program logs

solana logs

πŸ” Troubleshooting

Validator won't start

# Clean the ledger and restart
solana-test-validator --reset

Build errors

# Clean and rebuild
anchor clean
anchor build

Tests fail

# Make sure the validator is running
# Verify the program is deployed
solana program show 92NnZLZ8TS5Ay1UwAnQmtbYWbAFcEWtZcn7MwVkLhhMZ

🀝 Contributing

Contributions 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.

About

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)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published