# solana-foundation/anchor architecture

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

- Interactive diagram: https://gitdiagram.com/solana-foundation/anchor
- Repository: https://github.com/solana-foundation/anchor

## Overview

##### Project Type & Purpose
- This is a developer framework for the Solana Sealevel runtime. Its primary purpose is to simplify the development, testing, and deployment of Solana programs (smart contracts). The framework provides:
– A Rust eDSL (via packages like “anchor-lang”, “anchor-spl”, “anchor-client”) that developers use to author on-chain programs.
– A CLI and workspace management suite (in the “cli” directory) to orchestrate projects.
– An IDL (interface description language) system that automatically generates client libraries.
– TypeScript packages (under “ts/packages” and “@coral-xyz/anchor”) that allow client-side integration.
- The README reinforces that the framework is analogous to environments like Solidity/Truffle but is tailored for Solana.

##### File Structure
- Top-Level Directories:
- “avm”, “cli”, “client”, “docs”, “examples”, “idl”, “lang”, “spl”, “tests”, “ts” – each corresponds to a major component of the framework.
- “lang” in particular is a large component that includes macro definitions, code generation for account handling, IDL interpretation, and more.
- Build & CI:
- The provided GitHub Actions workflows, Makefile, and various CI scripts in “.github” indicate integration with automated testing and release pipelines.
- Package Separation & Multi-Language Support:
- Rust packages (Cargo.toml files in folders like “avm”, “cli”, “client”, “lang”, “spl”, “idl”) handle the core on-chain aspects.
- TypeScript packages (inside “ts/packages” and other TS folders) provide client-side and tooling support.

##### High-Level Architecture Components
a. Core Framework Libraries:
- “anchor-lang”: Provides Rust primitives and eDSL for writing Solana programs.
- “anchor-spl”: Supplies CPI (cross-program invocation) clients for interfacing with SPL programs.
- “anchor-client”: Contains a Rust client library for interacting with deployed programs.
- “lang”: Handles annotations, derive macros, and code generation (using syn and custom attribute handling), which form the backbone of the DSL.
- “idl”: Manages the interface description language for automatically generating client code.
b. Tooling & CLI:
- “cli”: Provides commands for building, testing, and deploying Anchor projects.
- “avm”: Likely an Anchor version manager, assisting with environment and version control.
c. Client & Integration Layers:
- “client”: Offers example integration for on-chain client interactions.
- “ts”: Contains TypeScript packages and tests (including client wrappers and SDKs) for generating clients from the IDL.
d. Documentation & Examples:
- “docs” and “examples”: Provide guidance and sample projects to show how to use the framework.
e. Testing & CI Infrastructure:
- “tests”: A comprehensive suite for end-to-end tests, integration tests, and benchmarking.
- GitHub Actions workflows and scripts to automate testing, version bumping, and release processes.

##### Relationships & Data Flow
- The Anchor DSL in Rust (via “anchor-lang” and “lang”) compiles smart contract code that then gets deployed to the Solana blockchain.
- Upon compiling, the build process also generates an IDL (interface description) that the TypeScript side (“ts/packages/anchor” and others) uses to create client libraries.
- The CLI (“cli”) ties everything together by managing the project workspace, facilitating testing, deployment, and upgrade operations.
- Automated pipelines (via GitHub Actions, CI scripts) run tests against different packages (e.g., those in “tests”) and build documentation (“docs”) on code changes.

##### Architectural Patterns & Principles
- Modular Design: Each directory/package encapsulates a distinct concern (e.g., smart contract language, client generation, version management, testing).
- Layered Architecture: Separate layers exist for on-chain program definitions (Rust libraries), interface generation (IDL), and client integrations (TypeScript packages).
- Build and Test Automation: CI/CD pipelines are integrated deeply (via GitHub Actions, Makefiles, shell scripts in “docker” and “tests”) to enforce code integrity.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Framework Architecture
    subgraph "Anchor Framework Architecture"
        %% Core Smart Contract Libraries
        subgraph "Core Smart Contract Libraries"
            A1["Anchor-lang & DSL (Rust)"]:::rust
            A2["Anchor-SPL (Rust)"]:::rust
            A3["Anchor-Client (Rust)"]:::rust
            A4["IDL Generation"]:::rust
        end

        %% Tooling & CLI
        subgraph "Tooling & CLI"
            B1["CLI & Workspace Management"]:::cli
            B2["AVM (Version/Env Manager)"]:::cli
        end

        %% Client SDKs & Integration
        subgraph "Client SDKs & Integration"
            C1["TypeScript Client Integration"]:::ts
        end

        %% Documentation & Examples
        subgraph "Documentation & Examples"
            D1["Documentation"]:::docs
            D2["Examples"]:::docs
        end

        %% Testing & CI/CD
        subgraph "Testing & CI/CD"
            E1["Testing Suite"]:::ci
            E2["CI/CD & Build Scripts"]:::ci
        end

        %% External Services
        subgraph "External Services"
            F1["Solana Network"]:::external
        end
    end

    %% Data Flow Connections
    A1 -->|"compilesTo"| A4
    A4 -->|"generatesIDL"| C1
    A2 -->|"deployedOn"| F1
    A3 -->|"deployedOn"| F1
    B1 -->|"triggersTests"| E1
    E1 -->|"notifies"| E2
    B1 -->|"deploysTo"| F1
    C1 -->|"interactsWith"| F1
    F1 -->|"respondsTo"| C1
    F1 -->|"feedback"| B1

    %% Click Events
    click A1 "https://github.com/solana-foundation/anchor/tree/master/lang"
    click A2 "https://github.com/solana-foundation/anchor/tree/master/spl"
    click A3 "https://github.com/solana-foundation/anchor/tree/master/client"
    click A4 "https://github.com/solana-foundation/anchor/tree/master/idl"
    click B1 "https://github.com/solana-foundation/anchor/tree/master/cli"
    click B2 "https://github.com/solana-foundation/anchor/tree/master/avm"
    click C1 "https://github.com/solana-foundation/anchor/tree/master/ts/packages/anchor"
    click D1 "https://github.com/solana-foundation/anchor/tree/master/docs"
    click D2 "https://github.com/solana-foundation/anchor/tree/master/examples"
    click E1 "https://github.com/solana-foundation/anchor/tree/master/tests"
    click E2 "https://github.com/solana-foundation/anchor/blob/master/.github"

    %% Styles
    classDef rust fill:#b3ffb3,stroke:#333,stroke-width:2px;
    classDef cli fill:#ffcc99,stroke:#333,stroke-width:2px;
    classDef ts fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef docs fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef ci fill:#ffb6c1,stroke:#333,stroke-width:2px;
    classDef external fill:#ff9999,stroke:#333,stroke-width:2px;
```
