# arrayappy/solana-uniswap-v2 architecture

> Architecture diagram of the GitHub repository arrayappy/solana-uniswap-v2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arrayappy/solana-uniswap-v2
- Repository: https://github.com/arrayappy/solana-uniswap-v2

## Overview

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: [`programs/solana-uniswap-v2/src/lib.rs`](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/lib.rs) (AMM program), [`programs/solana-uniswap-v2/src/instructions/initialize_pool.rs`](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/initialize_pool.rs) (Init pool), [`programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs`](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs) (Deposit), [`programs/solana-uniswap-v2/src/state/pool.rs`](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/state/pool.rs) (Pool state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_onchain["On-chain program"]
  node_n_program["AMM program<br/>solana program<br/>[lib.rs]"]
  node_n_consts["Constants<br/>shared config<br/>[constants.rs]"]
  node_n_errors["Errors<br/>[errors.rs]"]
  node_n_amm[("AMM state<br/>account<br/>[amm.rs]")]
  node_n_pool[("Pool state<br/>account<br/>[pool.rs]")]
  node_n_init_amm["Init AMM<br/>instruction<br/>[initialize_amm.rs]"]
  node_n_init_pool["Init pool<br/>instruction<br/>[initialize_pool.rs]"]
  node_n_deposit["Deposit<br/>instruction"]
  node_n_withdraw["Withdraw<br/>instruction"]
  node_n_swap["Swap<br/>instruction<br/>[swap.rs]"]
  node_n_spl{{"SPL Token<br/>external program"}}
end

subgraph group_g_offchain["Off-chain harness"]
  node_n_anchor["Anchor app<br/>build/deploy<br/>[Anchor.toml]"]
  node_n_deploy["Deploy script<br/>deployment<br/>[deploy.ts]"]
  node_n_tests["Test suite<br/>client tests"]
  node_n_utils["Test utils<br/>test support<br/>[utils.ts]"]
end

node_n_anchor -->|"builds"| node_n_program
node_n_deploy -->|"deploys"| node_n_program
node_n_tests -->|"calls"| node_n_program
node_n_utils -->|"supports"| node_n_tests
node_n_program -->|"routes"| node_n_init_amm
node_n_program -->|"routes"| node_n_init_pool
node_n_program -->|"routes"| node_n_deposit
node_n_program -->|"routes"| node_n_withdraw
node_n_program -->|"routes"| node_n_swap
node_n_init_amm -->|"writes"| node_n_amm
node_n_init_pool -->|"reads"| node_n_amm
node_n_init_pool -->|"writes"| node_n_pool
node_n_deposit -->|"reads"| node_n_amm
node_n_deposit -->|"updates"| node_n_pool
node_n_withdraw -->|"updates"| node_n_pool
node_n_swap -->|"reads/updates"| node_n_pool
node_n_init_pool -->|"uses"| node_n_spl
node_n_deposit -->|"custodies"| node_n_spl
node_n_withdraw -->|"custodies"| node_n_spl
node_n_swap -->|"custodies"| node_n_spl
node_n_program -->|"uses"| node_n_consts
node_n_program -->|"uses"| node_n_errors

click node_n_anchor "https://github.com/arrayappy/solana-uniswap-v2/blob/main/Anchor.toml"
click node_n_deploy "https://github.com/arrayappy/solana-uniswap-v2/blob/main/migrations/deploy.ts"
click node_n_tests "https://github.com/arrayappy/solana-uniswap-v2/tree/main/tests"
click node_n_utils "https://github.com/arrayappy/solana-uniswap-v2/blob/main/tests/utils.ts"
click node_n_program "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/lib.rs"
click node_n_consts "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/constants.rs"
click node_n_errors "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/errors.rs"
click node_n_amm "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/state/amm.rs"
click node_n_pool "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/state/pool.rs"
click node_n_init_amm "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/initialize_amm.rs"
click node_n_init_pool "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/initialize_pool.rs"
click node_n_deposit "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs"
click node_n_withdraw "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/withdraw_liquidity.rs"
click node_n_swap "https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/swap.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_program,node_n_consts,node_n_errors,node_n_amm,node_n_pool,node_n_init_amm,node_n_init_pool,node_n_deposit,node_n_withdraw,node_n_swap,node_n_spl toneBlue
class node_n_anchor,node_n_deploy,node_n_tests,node_n_utils toneAmber
```

## Components

### On-chain program

Anchor-based Solana AMM program and its persistent accounts.

- [AMM program](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/lib.rs): solana program · Single Anchor/Solana program that implements the Uniswap v2-style AMM. · `programs/solana-uniswap-v2/src/lib.rs`
- [Constants](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/constants.rs): shared config · Program-wide fixed values used by instruction handlers. · `programs/solana-uniswap-v2/src/constants.rs`
- [Errors](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/errors.rs): shared errors · Custom program error definitions shared across handlers. · `programs/solana-uniswap-v2/src/errors.rs`
- [AMM state](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/state/amm.rs): account · Global protocol configuration, including admin and fee settings. · `programs/solana-uniswap-v2/src/state/amm.rs`
- [Pool state](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/state/pool.rs): account · Token-pair pool metadata and linkage back to the parent AMM. · `programs/solana-uniswap-v2/src/state/pool.rs`
- [Init AMM](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/initialize_amm.rs): instruction · Creates the protocol-level AMM configuration. · `programs/solana-uniswap-v2/src/instructions/initialize_amm.rs`
- [Init pool](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/initialize_pool.rs): instruction · Creates a liquidity pool for a specific token pair. · `programs/solana-uniswap-v2/src/instructions/initialize_pool.rs`
- [Deposit](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs): instruction · Adds liquidity and updates LP accounting. · `programs/solana-uniswap-v2/src/instructions/deposit_liquidity.rs`
- [Withdraw](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/withdraw_liquidity.rs): instruction · Removes liquidity and returns underlying assets. · `programs/solana-uniswap-v2/src/instructions/withdraw_liquidity.rs`
- [Swap](https://github.com/arrayappy/solana-uniswap-v2/blob/main/programs/solana-uniswap-v2/src/instructions/swap.rs): instruction · Executes token exchange against pool reserves with fees. · `programs/solana-uniswap-v2/src/instructions/swap.rs`
- **SPL Token**: external program · Standard token program used for custody, transfers, minting, and burning.

### Off-chain harness

Localnet deployment and TypeScript tests that drive the program.

- [Anchor app](https://github.com/arrayappy/solana-uniswap-v2/blob/main/Anchor.toml): build/deploy · Workspace and Anchor configuration for building, deploying, and running the program. · `Anchor.toml`
- [Deploy script](https://github.com/arrayappy/solana-uniswap-v2/blob/main/migrations/deploy.ts): deployment · TypeScript migration used to deploy the program in local development. · `migrations/deploy.ts`
- [Test suite](https://github.com/arrayappy/solana-uniswap-v2/tree/main/tests): client tests · TypeScript instruction tests that exercise the AMM locally. · `tests`
- [Test utils](https://github.com/arrayappy/solana-uniswap-v2/blob/main/tests/utils.ts): test support · Shared setup and helpers used across the test files. · `tests/utils.ts`

## Connections

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