This project implements a Uniswap V2-like Automated Market Maker (AMM) on Solana using the Anchor framework. It features:
- Creation of AMM configurations
- Pool creation for token pairs
- Liquidity provision and withdrawal
- Token swapping with constant product formula
- Fee mechanism for liquidity providers
The program uses the constant product formula (x * y = k) to determine swap amounts and maintain price equilibrium. Fees are collected on each swap and distributed to liquidity providers.
Amm
: This account stores the AMM configuration, including the fee percentage and admin authority.Pool
: This account represents a liquidity pool for a pair of tokens. It stores references to the token mints and the AMM it belongs to.Mint
: Standard SPL Token mint accounts are used for the pool tokens and the liquidity token.TokenAccount
: Standard SPL Token accounts are used to hold token balances for the pool and users.
initialize_amm
: Creates a newAmm
account with specified parameters.initialize_pool
: Creates a newPool
account for a given token pair.deposit_liquidity
: Allows users to provide liquidity to a pool.withdraw_liquidity
: Allows users to withdraw their liquidity from a pool.swap
: Performs a token swap with a specified input amount.
This repo contains the Solana program source code and client-side program tests written in TypeScript.
├── keys # Program keypairs
├── programs # Solana program source code
│ ├── src # Program source folder
│ │ ├── instructions # Contains all the program instructions
│ │ ├── state # Contains all the program accounts
│ │ ├── constants.rs # Program shared constants
│ │ ├── errors.rs # Program custom errors
│ │ ├── lib.rs # Program entrypoint
├── tests # TypeScript tests source folder
├── ... # Other misc. project config files
└── README.md
- Install Rust, Solana, Anchor (0.29.0): https://book.anchor-lang.com/chapter_2/installation.html
- Install the Solana CLI
- Install the Node 18
- M1 Mac? - Use Anchor Verifiable Builds
- In 1st terminal clone the repo and change directory
- Set the anchor version using
avm use 0.29.0
- Run
anchor keys sync
for updating keys inCargo.toml
andlib.rs
- Start
solana-test-validator -r
in 2nd terminal
- For building program, run
anchor build
oranchor build --verifiable
- Optional (Only for verifiable builds) - run
cp target/verifiable/solana_uniswap_v2.so target/deploy/solana_uniswap_v2.so
- Set the Solana network using
solana config set --url localnet
- For deploying program, run
anchor deploy
- For tests, run
anchor run test
Optionally if you are on VS Code, you can use Tasks: Run Task
option and select Solana Anchor Dev Pipeline
to build, deploy and run tests easily for faster local development. Source code is available here.