# palindrome-eng/reflect-program-library architecture

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

- Interactive diagram: https://gitdiagram.com/palindrome-eng/reflect-program-library
- Repository: https://github.com/palindrome-eng/reflect-program-library

## Overview

##### Main Components
- On-Chain Programs:
– Insurance Fund: Located in the “insurance-fund” directory, this component contains the core smart contract logic (in Rust) along with its IDL definitions, migration scripts, instructions (admin, CPI, crank, slash, swap, user), events, and state management.
– Reflect Single Pool: Found under “reflect-single-pool”, this appears to include CLI tools (Rust code) as well as a program with tests. This module is designed for pool management on chain.
– Reflect Tokenised Bonds: Under “reflect-tokenised-bonds”, this program comprises its own smart contract code, state definitions, instructions, and an accompanying SDK.
– Solana Stake Market (SSM): Housed below the “ssm/solana-stake-market” directory, it includes the program’s on-chain code, state definitions, and a supporting SDK (in both TypeScript and Rust).

- Off-Chain SDKs and Clients:
– Each on-chain program has a corresponding SDK (found in the “sdk” folders inside the respective subdirectories) that contain generated accounts and instructions in TypeScript. These act as the API layer enabling client-side interactions.
– There are CLI tools as well (for example, within “reflect-single-pool/cli”).

- Scripts and Migrations:
– Deployment and migration scripts (migrations/*.ts, scripts/helpers) are used to deploy and interact with the programs on the Solana blockchain.

- Testing Frameworks:
– The “tests” directories for each project cover end-to-end and unit tests, ensuring the contracts behave as expected.

##### Relationships and Interactions
- Each on-chain program interacts with the Solana blockchain using Anchor (as evidenced by Anchor.toml files).
- The off-chain SDK components wrap the smart contract instructions (exposed via generated IDL and types) so that client applications or CLIs can call the on-chain programs.
- Deployment scripts and migration files interact with the blockchain to setup and update the programs.
- Testing modules simulate on-chain interactions either by directly calling the program instructions or via the SDK.
- External dependencies (such as price oracles in “get_price_from_pyth” and “get_price_from_switchboard” helpers) are used to fetch off-chain data and influence on-chain logic.

##### Architectural Patterns and Design Principles
- Modularity: Each protocol component (insurance, pool, bonds, stake-market) is developed as an independent on-chain program with its own SDK, thereby allowing modular development and testing.
- Layered Architecture: The design clearly separates the on-chain logic (smart contracts), the off-chain API/SDK layer (TypeScript modules and CLI tools), and the deployment/testing infrastructure.
- Standardization with Anchor: The project uses Anchor patterns for Solana programs which simplify program development and enforce structured program layouts.
- API Generation: The use of autogenerated files (IDLs, instructions, types) from the Rust code ensures consistency between on-chain programs and off-chain integrations.

##### Capturing Essential Elements
- Do not over-complicate—the goal is to break the system into as many essential and self-contained components as possible.
- Clear component boundaries and emphasize the flow of interactions.
- Use annotations to highlight notable technologies (Rust, Anchor, TypeScript, Solana) and their roles in the system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Off-Chain Interfaces
    subgraph "Off-Chain Interfaces"
        IF_SDK["Insurance Fund SDK"]:::offChain
        RSP_CLI["Reflect Single Pool CLI Tools"]:::offChain
        RSP_SDK_Classic["Reflect Single Pool SDK (Classic)"]:::offChain
        RSP_SDK_Modern["Reflect Single Pool SDK (Modern)"]:::offChain
        RTB_SDK["Reflect Tokenised Bonds SDK"]:::offChain
        SSM_SDK["Solana Stake Market SDK"]:::offChain
    end

    %% On-Chain Programs
    subgraph "On-Chain Programs"
        IF_P["Insurance Fund (Program)"]:::onChain
        RSP_P["Reflect Single Pool (Program)"]:::onChain
        RTB_P["Reflect Tokenised Bonds (Program)"]:::onChain
        SSM_P["Solana Stake Market (Program)"]:::onChain
    end

    %% Development & Deployment
    subgraph "Development & Deployment"
        IF_IDL["Insurance Fund IDL/Migrations"]:::dev
        RTB_IDL["Reflect Tokenised Bonds IDL/Migrations"]:::dev
        SSM_IDL["Solana Stake Market IDL/Migrations"]:::dev
        DEPLOY["Deployment Scripts"]:::dev
    end

    %% Testing Frameworks
    subgraph "Testing Frameworks"
        IF_TEST["Insurance Fund Tests"]:::test
        RSP_TEST["Reflect Single Pool Tests"]:::test
        RTB_TEST["Reflect Tokenised Bonds Tests"]:::test
        SSM_TEST["Solana Stake Market Tests"]:::test
    end

    %% External Services
    subgraph "External Services"
        ORACLE["Oracle Services"]:::external
    end

    %% Solana Blockchain
    BLOCKCHAIN["Solana Blockchain"]:::blockchain

    %% Off-Chain Interfaces -> On-Chain Programs
    IF_SDK         -->|"calls"| IF_P
    RSP_CLI        -->|"calls"| RSP_P
    RSP_SDK_Classic-->|"calls"| RSP_P
    RSP_SDK_Modern -->|"calls"| RSP_P
    RTB_SDK        -->|"calls"| RTB_P
    SSM_SDK        -->|"calls"| SSM_P

    %% Development & Deployment -> On-Chain Programs
    IF_IDL  -->|"defines"| IF_P
    RTB_IDL -->|"defines"| RTB_P
    SSM_IDL -->|"defines"| SSM_P
    DEPLOY  -->|"deploys"| IF_P
    DEPLOY  -->|"deploys"| RSP_P
    DEPLOY  -->|"deploys"| RTB_P
    DEPLOY  -->|"deploys"| SSM_P

    %% Testing Frameworks -> On-Chain Programs
    IF_TEST  -->|"tests"| IF_P
    RSP_TEST -->|"tests"| RSP_P
    RTB_TEST -->|"tests"| RTB_P
    SSM_TEST -->|"tests"| SSM_P

    %% External Services -> On-Chain Programs
    ORACLE -->|"feeds"| IF_P
    ORACLE -->|"feeds"| RTB_P
    ORACLE -->|"feeds"| SSM_P

    %% On-Chain Programs -> Blockchain
    IF_P  -->|"runs_on"| BLOCKCHAIN
    RSP_P -->|"runs_on"| BLOCKCHAIN
    RTB_P -->|"runs_on"| BLOCKCHAIN
    SSM_P -->|"runs_on"| BLOCKCHAIN

    %% Deployment Scripts -> Blockchain
    DEPLOY -->|"deploys_to"| BLOCKCHAIN

    %% Click Events for Component Mapping
    click IF_P "https://github.com/palindrome-eng/reflect-program-library/tree/main/insurance-fund/programs/insurance-fund"
    click IF_IDL "https://github.com/palindrome-eng/reflect-program-library/tree/main/insurance-fund/migrations"
    click IF_SDK "https://github.com/palindrome-eng/reflect-program-library/tree/main/insurance-fund/sdk"
    click RSP_P "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-single-pool/program"
    click RSP_CLI "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-single-pool/cli"
    click RSP_SDK_Classic "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-single-pool/js/packages/classic"
    click RSP_SDK_Modern "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-single-pool/js/packages/modern"
    click RTB_P "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-tokenised-bonds/programs/reflect-tokenised-bonds"
    click RTB_IDL "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-tokenised-bonds/migrations"
    click RTB_SDK "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-tokenised-bonds/sdk"
    click SSM_P "https://github.com/palindrome-eng/reflect-program-library/tree/main/ssm/solana-stake-market/programs/solana-stake-market"
    click SSM_IDL "https://github.com/palindrome-eng/reflect-program-library/tree/main/ssm/solana-stake-market/migrations"
    click SSM_SDK "https://github.com/palindrome-eng/reflect-program-library/tree/main/ssm/solana-stake-market/sdk"
    click IF_TEST "https://github.com/palindrome-eng/reflect-program-library/tree/main/insurance-fund/tests"
    click RSP_TEST "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-single-pool/program/tests"
    click RTB_TEST "https://github.com/palindrome-eng/reflect-program-library/tree/main/reflect-tokenised-bonds/tests"
    click SSM_TEST "https://github.com/palindrome-eng/reflect-program-library/tree/main/ssm/solana-stake-market/tests"

    %% Styles
    classDef onChain fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef offChain fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef dev fill:#ccccff,stroke:#0000cc,stroke-width:2px;
    classDef test fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef external fill:#ffebcc,stroke:#cc6600,stroke-width:2px;
    classDef blockchain fill:#e6e6e6,stroke:#333,stroke-width:2px;
```
