# glamsystems/glam-sdk architecture

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

- Interactive diagram: https://gitdiagram.com/glamsystems/glam-sdk
- Repository: https://github.com/glamsystems/glam-sdk

## Overview

##### Project Type and Purpose
- This is a developer-facing SDK (client library) that provides APIs to interact with smart contracts defined by the GLAM Protocol.
- The README highlights how to create a GLAM client, perform token operations like wrapping SOL, and work with vault states. This clearly identifies the core purpose—to abstract complexities of interacting with the underlying blockchain protocol (Solana) and smart contract logic.

##### Analyzing the File Structure
- Top-level Files and Directories:
- README.md: Provides usage examples, installation instructions, and basic client usage.
- package.json: Contains dependency information and build scripts.
- idl directory: Contains JSON files (glam_policies.json and glam_protocol.json) which define the interface definitions for the protocol, likely used to generate client code or validate transactions.
- remapping directory (and the associated remapping.tar.gz): Contains multiple JSON files that seem to map addresses or config details—these are likely critical for correctly linking the SDK with on-chain programs.
- src directory: Houses the core source code of the SDK.
▹ src/client.ts and the client/ subdirectory clearly indicate modular functions targeting different protocol features (e.g., drift, investor, jupiter, staking, etc.). This separation suggests a modular architectural approach.
▹ src/react includes React components, indicating additional support for building web applications with integrated protocol interactions.
▹ src/utils, src/models, and error handling files that support the SDK’s functionality.
- types directory: Contains TypeScript interfaces for GLAM policies and protocol details.

##### Architectural Breakdown

A. Core SDK Layer:
- Glam Client (e.g., in src/client.ts and src/glamExports.ts) – this is the central entry point encapsulating the higher-level API for GLAM protocol interactions.
- The individual client modules (located in src/client/) for specific functionality such as drift, investor, jupiter, staking, mint, price, state, etc.
- Utility functions and helpers found in src/utils which support lower-level operations such as blockhash management and fee calculations.

B. IDL & Type System:
- The idl directory containing the protocol and policy definitions.
- The types directory providing TypeScript type definitions for strong type safety. This layer ensures that the data contract between the SDK and the on-chain protocol is maintained.

C. React Integration Layer:
- Components under src/react (e.g., cluster-provider.tsx, glam.tsx, index.tsx) which facilitate the integration of the SDK into React applications. These modules allow developers to easily use the protocol’s functions within a web interface.

D. Remapping and Configuration:
- The remapping directory contains various JSON files that map or configure addresses and keys. They are essential for ensuring the client correctly interacts with specific on-chain programs.

E. Integration with Blockchain:
- Although not directly visible in the file tree, the usage example in the README (with dependencies on @coral-xyz/anchor and @solana/web3.js) highlights that the SDK interacts with the blockchain. This external dependency forms an important component in the architecture.

##### Relationships and Data Flow
- Connection from these modules to the IDL definitions (idl/glam_protocol.json, idl/glam_policies.json) on which these modules rely.
- How TypeScript type definitions (from types directory) interface with the high-level components to enforce type safety.
- For the React components, illustrate how they integrate with the main client layer and potentially observe state changes or trigger protocol interactions.

##### Architectural Patterns and Design Principles
- Modularity: Separate modules for each specific functionality (client modules, utilities, React components) show adherence to separation-of-concerns.
- Layered Architecture: A clear division between SDK core functionalities, configuration (remapping and idl), and presentation/UI (React components).
- Strong Typing and Contract Enforcement: Use of TypeScript’s type system to manage interactions and dependencies reliably.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core SDK Layer
    subgraph "Core SDK Layer"
        A1["Glam Client (client.ts)"]:::sdk
        A2["Glam Exports (glamExports.ts)"]:::sdk
        A3["Client Modules"]:::modules
        A4["Utility Functions"]:::sdk
    end

    %% IDL & Type System
    subgraph "IDL & Type System"
        B1["GLAM Policies (IDL)"]:::config
        B2["GLAM Protocol (IDL)"]:::config
        B3["TypeScript Types"]:::config
    end

    %% React Integration Layer
    subgraph "React Integration Layer"
        C1["React Components"]:::react
    end

    %% Remapping & Configuration
    subgraph "Remapping & Configuration"
        D1["Remapping & Config"]:::config
    end

    %% External Blockchain Integration
    E1["Blockchain/Smart Contracts"]:::external

    %% Connections
    A1 -->|"calls"| A3
    A2 ---|"augments"| A1
    A1 -->|"utilizes"| A4
    A3 -->|"reliesOn"| B1
    A3 -->|"reliesOn"| B2
    A3 -->|"uses"| B3
    A1 -->|"configures"| D1
    A1 -->|"interactsWith"| E1
    A3 -->|"interactsWith"| E1
    C1 -->|"integratesWith"| A1

    %% Click Events
    click A1 "https://github.com/glamsystems/glam-sdk/blob/main/src/client.ts"
    click A2 "https://github.com/glamsystems/glam-sdk/blob/main/src/glamExports.ts"
    click A3 "https://github.com/glamsystems/glam-sdk/tree/main/src/client/"
    click A4 "https://github.com/glamsystems/glam-sdk/tree/main/src/utils"
    click B1 "https://github.com/glamsystems/glam-sdk/blob/main/idl/glam_policies.json"
    click B2 "https://github.com/glamsystems/glam-sdk/blob/main/idl/glam_protocol.json"
    click B3 "https://github.com/glamsystems/glam-sdk/tree/main/types"
    click C1 "https://github.com/glamsystems/glam-sdk/tree/main/src/react"
    click D1 "https://github.com/glamsystems/glam-sdk/tree/main/remapping"

    %% Styles
    classDef sdk fill:#fde2e2,stroke:#d9534f,stroke-width:2px;
    classDef modules fill:#d9edf7,stroke:#5bc0de,stroke-width:2px;
    classDef config fill:#dff0d8,stroke:#5cb85c,stroke-width:2px;
    classDef react fill:#d1ecf1,stroke:#17a2b8,stroke-width:2px;
    classDef external fill:#fcf8e3,stroke:#f0ad4e,stroke-width:2px;
```
