A fully functional Automated Market Maker (AMM) built using Solana + Anchor, inspired by protocols like Raydium and Uniswap V2.
This project implements a secure, PDA-based constant product liquidity pool with swap functionality, protocol fee accounting, slippage protection, and invariant enforcement.
Devnet Program ID:
7kkDWEga2EJyMARYWH7SwjEBqCfpPWbzdQLDZB5psQ4F
View on Solana Explorer:
https://explorer.solana.com/address/7kkDWEga2EJyMARYWH7SwjEBqCfpPWbzdQLDZB5psQ4F?cluster=devnet
This AMM is deployed and verifiable on Solana Devnet.
https://explorer.solana.com/address/7kkDWEga2EJyMARYWH7SwjEBqCfpPWbzdQLDZB5psQ4F?cluster=devnet
- Open the program link above.
- Click on recent transactions.
- Inspect
createPool,addLiquidity, andswapinstructions. - Observe CPI calls to the SPL Token Program.
- Confirm reserve updates in Pool PDA account.
This demonstrates real on-chain execution — not a local simulation.
- ✅ Initialize treasury (protocol fee collection)
- ✅ Create liquidity pool (PDA-based)
- ✅ Add liquidity (LP minting)
- ✅ Remove liquidity (LP burning)
- ✅ Token swaps (A ↔ B)
- ✅ Slippage protection
- ✅ Constant product invariant enforcement
- ✅ Protocol + LP fee splitting
- ✅ Secure vault authority using PDAs
- ✅ Success & failure test coverage
This AMM follows the constant product formula:
x * y = k
Where:
x= reserve of Token Ay= reserve of Token Bk= invariant constant
After every swap:
k_after >= k_before
Fees ensure that liquidity providers gain value over time.
User
│
▼
User Token Accounts
│
▼
Vault Token Accounts (PDA owned)
│
▼
Pool Account (reserves + config)
│
▼
Treasury (protocol fees)
| Account | Purpose |
|---|---|
| Pool PDA | Stores reserves & configuration |
| Vault A/B | Holds liquidity tokens |
| Vault Authority | PDA signer for vault transfers |
| LP Mint | Represents liquidity share |
| Treasury | Accumulates protocol fees |
- PDA-based vault authority prevents unauthorized withdrawals
- Slippage protection prevents bad trades
- Checked arithmetic prevents overflow
- Invariant enforcement protects pool integrity
- Deterministic pool PDA ensures uniqueness per token pair
Total swap fee is split into:
- LP Fee → stays inside pool (increases
k) - Protocol Fee → sent to treasury
This ensures:
- LPs earn yield
- Protocol earns sustainable revenue
amount_out =
(reserve_out * amount_in_with_fee)
-----------------------------------
(reserve_in + amount_in_with_fee)
amm-capstone/
│
├── programs/
│ └── amm-capstone/
│ ├── instructions/
│ ├── state/
│ ├── math.rs
│ ├── errors.rs
│ └── lib.rs
│
├── tests/
│ └── amm-capstone.ts
│
├── Anchor.toml
├── Cargo.toml
└── README.md
Install:
- Rust
- Node.js (>= 18)
- Yarn
- Solana CLI (recommended: 1.18.x)
- Anchor CLI (0.32.x)
Clone repository:
git clone https://github.com/mepavankumar15/raydium-protocol-amm.git
cd raydium-protocol-ammInstall dependencies:
yarn installsolana-test-validator --resetKeep this terminal running.
solana config set --url localhostAirdrop SOL:
solana airdrop 10anchor buildanchor deployanchor test --skip-local-validatorSwitch to devnet:
solana config set --url https://api.devnet.solana.com
solana airdrop 2Deploy:
anchor deploy --provider.cluster devnetRun tests against devnet:
anchor test --provider.cluster devnet --skip-local-validatorCreates protocol fee account.
Initializes pool PDA and vault accounts.
Deposits tokens and mints LP tokens.
Verifies:
- User balances change
- Reserves update
- Invariant holds
Example swap output:
Before Swap:
Reserve A: 500000000
Reserve B: 500000000
After Swap:
Reserve A: 599940000
Reserve B: 416875105
Ensures swaps fail when min_out is unrealistic.
https://explorer.solana.com/address/7kkDWEga2EJyMARYWH7SwjEBqCfpPWbzdQLDZB5psQ4F?cluster=devnet
From test logs, copy poolPda and open:
https://explorer.solana.com/address/POOL_PDA?cluster=devnet
Initialize Treasury
↓
Create Pool
↓
Add Liquidity
↓
Swap Tokens
↓
Collect Fees
↓
Remove Liquidity
This project demonstrates:
- Deep understanding of Solana PDAs
- CPI interaction with SPL Token program
- On-chain invariant enforcement
- DeFi swap mechanics
- Protocol fee modeling
- Slippage protection design
- Proper test coverage (success + failure cases)
This is not a template — it is a fully functioning on-chain AMM.
MIT License
Pavan Kumar Kuchibhotla (avyu.rs) Solana | Rust | DeFi Engineering follow me on twitter , DM for collab ;)