How arrayappy/solana-uniswap-v2 works
1. This repository is split into two clear layers: an on-chain Solana program written in Rust/Anchor, and an off-chain TypeScript test/deployment harness. The architecture is centered on a single AMM program, with tests exercising its instruction flow on localnet.
2. The on-chain boundary is programs/solana-uniswap-v2. Its entrypoint is src/lib.rs, which wires Anchor instruction handlers into the program. The program is not a multi-service system; it is one Solana BPF program with state stored in accounts.
3. Core persistent state lives in src/state:
- Amm stores global AMM configuration, including admin authority and fee settings.
- Pool stores a token-pair-specific liquidity pool and links that pool back to its parent AMM.
These accounts are the main data model for the protocol and are the primary diagram nodes.
4. The business logic is organized by instruction in src/instructions:
- initialize_amm creates protocol-level configuration.
- initialize_pool creates a pool for a specific token pair.
- deposit_liquidity adds user funds and mints/updates LP position accounting.
- withdraw_liquidity removes liquidity and returns underlying assets.
- swap performs token exchange against the pool.
Each handler is a separate execution path, but they all read/write the same AMM and Pool state.
5. The protocol follows the Uniswap V2 constant-product AMM model. The important computational boundary is between user input amounts and pool reserve math: swaps update reserves according to x * y = k, with fees applied on each trade. That fee logic is part of the on-chain program, not an external service.
6. The program depends on standard SPL Token accounts and mints rather than custom token storage. Pool reserves and user balances flow through SPL token accounts, while the program accounts only store protocol metadata and pool relationships. This is a key boundary in any architecture diagram: “program state” versus “token custody.”
7. Shared program-wide helpers are separated into src/constants.rs and src/errors.rs. These modules support the instruction handlers with fixed values and custom error handling, but they are not standalone subsystems.
8. Build and deployment are managed by Anchor. Anchor.toml, Cargo.toml, and the migrations/deploy.ts script define the local build/deploy pipeline, with keys/keypair.json and related program key management used for program identity during deployment.
9. The off-chain test layer is in tests/. It contains one TypeScript file per instruction area (initialize-amm.ts, initialize-pool.ts, deposit-liquidity.ts, swap.ts, withdraw-liquidity.ts) plus utils.ts for shared test setup. These tests are the main consumer of the program API and represent the client-side execution flow.
10. The repository is designed for local validator development. The README’s workflow uses solana-test-validator, anchor build, anchor deploy, and anchor run test, so the runtime architecture is anchored around localnet rather than a separate backend or hosted API.
11. If you are drawing the architecture, the main flow is:
user/test client → Anchor instruction call → on-chain AMM program → read/write Amm and Pool accounts → transfer/mint/burn SPL tokens → return updated balances and state.
12. The main trust and coupling boundaries are:
- Anchor-generated account validation around each instruction.
- SPL Token program for asset custody and transfers.
- Program-owned accounts for AMM configuration and pool metadata.
This separation is the key to understanding how the system stays deterministic and Solana-native.
13. There are no additional application tiers, databases, or external APIs in this repo. The architecture is intentionally compact: one Solana program, two main state accounts, five instruction paths, and a TypeScript test/deploy harness.
Main entry points: lib.rs AMM program · initialize_pool.rs Init pool · deposit_liquidity.rs Deposit · pool.rs Pool state
Components 15
On-chain program
Anchor-based Solana AMM program and its persistent accounts.
- AMM program · Single Anchor/Solana program that implements the Uniswap v2-style AMM.
programs/solana-uniswap-v2/src/lib.rs - Constants · Program-wide fixed values used by instruction handlers.
programs/solana-uniswap-v2/src/constants.rs - Errors · Custom program error definitions shared across handlers.
programs/solana-uniswap-v2/src/errors.rs - AMM state · Global protocol configuration, including admin and fee settings.
programs/solana-uniswap-v2/src/state/amm.rs - Pool state · Token-pair pool metadata and linkage back to the parent AMM.
programs/solana-uniswap-v2/src/state/pool.rs - Init AMM · Creates the protocol-level AMM configuration.
programs/solana-uniswap-v2/src/instructions/initialize_amm.rs - Init pool · Creates a liquidity pool for a specific token pair.
programs/solana-uniswap-v2/src/instructions/initialize_pool.rs - Deposit · Adds liquidity and updates LP accounting.
programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs - Withdraw · Removes liquidity and returns underlying assets.
programs/solana-uniswap-v2/src/instructions/withdraw_liquidity.rs - Swap · Executes token exchange against pool reserves with fees.
programs/solana-uniswap-v2/src/instructions/swap.rs - SPL Token · Standard token program used for custody, transfers, minting, and burning.
Off-chain harness
Localnet deployment and TypeScript tests that drive the program.
- Anchor app · Workspace and Anchor configuration for building, deploying, and running the program.
Anchor.toml - Deploy script · TypeScript migration used to deploy the program in local development.
migrations/deploy.ts - Test suite · TypeScript instruction tests that exercise the AMM locally.
tests - Test utils · Shared setup and helpers used across the test files.
tests/utils.ts
Connections 22
- Anchor app → AMM program: builds · Anchor compiles and packages the on-chain program.
- Deploy script → AMM program: deploys · Migration script deploys the program to localnet/devnet.
- Test suite → AMM program: calls · Tests invoke program instructions through the client API.
- Test utils → Test suite: supports · Shared helpers are consumed by the instruction tests.
- AMM program → Init AMM: routes · Entrypoint wires Anchor instruction handlers.
- AMM program → Init pool: routes · Entrypoint wires Anchor instruction handlers.
- AMM program → Deposit: routes · Entrypoint wires Anchor instruction handlers.
- AMM program → Withdraw: routes · Entrypoint wires Anchor instruction handlers.
- AMM program → Swap: routes · Entrypoint wires Anchor instruction handlers.
- Init AMM → AMM state: writes · Initializes global AMM configuration.
- Init pool → AMM state: reads · Uses AMM authority/configuration when creating a pool.
- Init pool → Pool state: writes · Creates token-pair pool metadata.
- Deposit → AMM state: reads · Uses protocol config and fee parameters.
- Deposit → Pool state: updates · Adjusts pool-linked liquidity accounting.
- Withdraw → Pool state: updates · Updates pool accounting while returning assets.
- Swap → Pool state: reads/updates · Consumes reserves and updates pool balances using constant-product math.
- Init pool → SPL Token: uses · Pool initialization depends on token mints and token accounts.
- Deposit → SPL Token: custodies · Liquidity flows through SPL token accounts.
- Withdraw → SPL Token: custodies · Underlying assets are transferred via SPL token program.
- Swap → SPL Token: custodies · Trade input and output tokens move through SPL token accounts.
- AMM program → Constants: uses · Handlers rely on fixed program values.
- AMM program → Errors: uses · Handlers emit custom protocol errors.