# meteoraag/cpi-examples architecture

> Architecture diagram of the GitHub repository meteoraag/cpi-examples, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/meteoraag/cpi-examples
- Repository: https://github.com/meteoraag/cpi-examples

## Overview

##### Project type and purpose
- This is a set of Anchor/Solana smart-contract examples demonstrating “Cross-Program Invocation” (CPI) to three external programs (DLMM, Dynamic AMM, M3M3).
- It is not a full application but an on-chain library of instruction handlers plus a local test harness and deployment scripts.

##### File structure
- Top-level:
- idls/ – JSON interfaces for each target program (dlmm.json, dynamic_amm.json, m3m3.json)
- migrations/ – a TypeScript script (`deploy.ts`) that deploys the CPI example to a local or test Solana cluster
- programs/cpi-example/ – the Anchor program source in Rust, organized by instruction group:
– instructions/dlmm_cpi/…, dynamic_amm_cpi/…, m3m3_cpi/…
– lib.rs and `Cargo.toml` for building the on-chain program
- tests and fixtures under programs/cpi-example/tests/ – integration tests that run against a localnet
- package.json, tsconfig.json – manage the TypeScript migration environment
- Anchor.toml, Cargo.* – Anchor and Rust configuration

##### README for additional insights
- Confirms the repository is exclusively example code for CPI into DLMM and Dynamic AMM (and one M3M3)
- Lists the specific instruction handlers implemented, which map 1:1 to examples in the Rust source
- Notes the required toolchain versions (Anchor, Solana CLI, Rust)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Off-chain Tools
    subgraph "Off-Chain Toolchain & Dev Env"
        direction TB
        AnchorCLI["Anchor CLI"]:::offchain
        RustCompiler["Rust Compiler"]:::offchain
        NodeTS["Node/TypeScript"]:::offchain
        MigrationScript["Migration Script"]:::offchain
        TestSuite["Test Suite (Mocha/Jest)"]:::offchain
        AnchorCLI -->|build program| RustCompiler
        RustCompiler -->|compile cpi-example| CPIProgram
        NodeTS -->|run migrations| MigrationScript
        MigrationScript -->|deploy| SolanaCluster
        TestSuite -->|run tests| SolanaCluster
    end

    %% Solana Cluster
    SolanaCluster["Solana Cluster\n(localnet/devnet)"]:::cluster

    %% On-Chain Programs
    subgraph "On-Chain Programs"
        direction TB
        CPIProgram["cpi-example Program"]:::onchain
        DLMMProg["DLMM Program"]:::external
        DynamicAMMProg["Dynamic AMM Program"]:::external
        M3M3Prog["M3M3 Program"]:::external
        CPIProgram -->|CPI calls| DLMMProg
        CPIProgram -->|CPI calls| DynamicAMMProg
        CPIProgram -->|CPI calls| M3M3Prog
    end

    %% Persistent State
    subgraph "On-Chain State"
        direction TB
        PoolState["Pool State"]:::state
        VaultState["Vault State"]:::state
        LPTokens["LP Token Mints"]:::state
        FeeAccounts["Fee Accounts"]:::state
        DLMMProg --> PoolState
        DynamicAMMProg --> VaultState
        DynamicAMMProg --> LPTokens
        DynamicAMMProg --> FeeAccounts
        M3M3Prog --> VaultState
    end

    SolanaCluster --> CPIProgram
    SolanaCluster --> DLMMProg
    SolanaCluster --> DynamicAMMProg
    SolanaCluster --> M3M3Prog

    %% IDL Definitions
    subgraph "IDL Definitions"
        direction TB
        DLMMIDL["dlmm.json"]:::idl
        DynamicAMMIDL["dynamic_amm.json"]:::idl
        M3M3IDL["m3m3.json"]:::idl
        CPIProgram --> DLMMIDL
        CPIProgram --> DynamicAMMIDL
        CPIProgram --> M3M3IDL
    end

    %% File & Config Mappings
    AnchorConfig["Anchor.toml\nCargo.toml\nCargo.lock"]:::config
    TSConfig["package.json\ntsconfig.json"]:::config
    CPIFiles["CPI Program Source"]:::code
    CPIFiles --> libRS
    CPIFiles --> CPIEntrypoint
    subgraph "CPI Instruction Handlers"
        direction TB
        DLCPI["dlmm_cpi handlers"]:::code
        AMMCPI["dynamic_amm_cpi handlers"]:::code
        M3CPI["m3m3_cpi handlers"]:::code
    end
    CPIEntrypoint["instructions/mod.rs"]:::code
    libRS["src/lib.rs"]:::code
    CargoProg["programs/cpi-example/Cargo.toml"]:::code

    %% Tests & Fixtures
    TestScript["test.sh"]:::code
    IntegrationTests["tests/integration/"]:::code
    Fixtures["tests/fixtures/"]:::code

    %% Click Events
    click DLMMIDL "https://github.com/meteoraag/cpi-examples/blob/main/idls/dlmm.json"
    click DynamicAMMIDL "https://github.com/meteoraag/cpi-examples/blob/main/idls/dynamic_amm.json"
    click M3M3IDL "https://github.com/meteoraag/cpi-examples/blob/main/idls/m3m3.json"
    click MigrationScript "https://github.com/meteoraag/cpi-examples/blob/main/migrations/deploy.ts"
    click AnchorConfig "https://github.com/meteoraag/cpi-examples/blob/main/Anchor.toml"
    click AnchorConfig "https://github.com/meteoraag/cpi-examples/blob/main/Cargo.toml"
    click TSConfig "https://github.com/meteoraag/cpi-examples/blob/main/package.json"
    click TSConfig "https://github.com/meteoraag/cpi-examples/blob/main/tsconfig.json"
    click CargoProg "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/Cargo.toml"
    click libRS "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/lib.rs"
    click DLCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dlmm_cpi/mod.rs"
    click DLCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dlmm_cpi/swap.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/mod.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/swap.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/lock_liquidity.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/claim_fee.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/initialize_customizable_permissionless_pool.rs"
    click AMMCPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/dynamic_amm_cpi/initialize_permissionless_pool_with_config.rs"
    click M3CPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/m3m3_cpi/mod.rs"
    click M3CPI "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/m3m3_cpi/initialize_vault.rs"
    click CPIEntrypoint "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/src/instructions/mod.rs"
    click TestScript "https://github.com/meteoraag/cpi-examples/blob/main/programs/cpi-example/test.sh"
    click IntegrationTests "https://github.com/meteoraag/cpi-examples/tree/main/programs/cpi-example/tests/integration/"
    click Fixtures "https://github.com/meteoraag/cpi-examples/tree/main/programs/cpi-example/tests/fixtures/"

    %% Styles
    classDef offchain fill:#d4f1c5,stroke:#6aa84f,color:#333;
    classDef onchain fill:#c9daf8,stroke:#3d85c6,color:#333;
    classDef external fill:#f9cb9c,stroke:#e69138,color:#333;
    classDef state fill:#fff2cc,stroke:#bf9000,color:#333,shape:cylinder;
    classDef idl fill:#eaf4fc,stroke:#6fa8dc,color:#333;
    classDef cluster fill:#eeeeee,stroke:#999999,color:#333,stroke-dasharray: 5 5;
    classDef config fill:#fce5cd,stroke:#f6b26b,color:#333;
    classDef code fill:#ececec,stroke:#b7b7b7,color:#333;
```
