# kamino-finance/klend architecture

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

- Interactive diagram: https://gitdiagram.com/kamino-finance/klend
- Repository: https://github.com/kamino-finance/klend

## Overview

##### Project Type Analysis
This is a specialized blockchain smart contract project, specifically a lending protocol built on Solana. The architecture should reflect both the program's on-chain components and its interaction patterns.

##### Main Components

a. Core Protocol Components:
- Lending Market Module
- Reserve System
- Obligation System
- Farms System
- Flash Loan System
- Price Oracle Integration

b. External Integrations:
- Pyth Oracle
- Switchboard Oracle
- Scope Oracle
- Solana Token Program

##### Specific Component Relationships

- Blue: Core lending operations
- Green: External integrations
- Yellow: State management
- Red: Critical paths (liquidations, flash loans)
- Purple: Administrative functions

##### Key Architectural Elements to Highlight

a. Handler Pattern:
- How each handler (from programs/klend/src/handlers) connects to core operations

b. State Management:
- Relationship between state modules (reserve.rs, obligation.rs, lending_market.rs)

c. Price Oracle Integration:
- Multi-oracle support system (Pyth, Switchboard, Scope)

d. Security Boundaries:
- Validation and constraint checks
- Permission boundaries

##### Implementation Details

- Component name
- Key responsibilities
- Critical dependencies
- State access patterns

##### State management and validation flow

- The modular nature of the handlers
- The central role of the lending market
- The security-first approach with multiple validation layers
- The integration with multiple price oracle systems
- The farm and flash loan subsystems

Remember to maintain Solana-specific architectural patterns while showing how the lending protocol's business logic is implemented securely and efficiently.

This architecture should reflect both the technical implementation details visible in the file structure and the business requirements of a lending protocol, while maintaining clarity about security boundaries and state management patterns.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef core fill:#2b6cb0,stroke:#1a365d,color:white
    classDef external fill:#48bb78,stroke:#22543d,color:white
    classDef state fill:#ecc94b,stroke:#744210,color:black
    classDef critical fill:#e53e3e,stroke:#742a2a,color:white
    classDef admin fill:#805ad5,stroke:#322659,color:white
    
    %% Entry Points Layer
    subgraph Handlers
        EP["Entry Points/Handlers"]:::core
    end
    click EP "https://github.com/Kamino-Finance/klend/tree/master/programs/klend/src/handlers/"

    %% Core Business Logic Layer
    subgraph CoreOps
        LM["Lending Market"]:::core
        FO["Farm Operations"]:::core
        FL["Flash Loan Operations"]:::core
        WC["Withdrawal Cap Operations"]:::core
        LO["Lending Operations"]:::core
        LQ["Liquidation Operations"]:::critical
    end
    click LM "https://github.com/Kamino-Finance/klend/tree/master/programs/klend/src/lending_market/"
    click FO "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/lending_market/farms_ixs.rs"
    click FL "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/lending_market/flash_ixs.rs"
    click WC "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/lending_market/withdrawal_cap_operations.rs"
    click LO "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/lending_market/lending_operations.rs"
    click LQ "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/state/liquidation_operations.rs"

    %% State Management Layer
    subgraph StateManagement
        RS["Reserve State"]:::state
        OS["Obligation State"]:::state
        LMS["Lending Market State"]:::state
        RFS["Referral State"]:::state
    end
    click StateManagement "https://github.com/Kamino-Finance/klend/tree/master/programs/klend/src/state/"
    click RS "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/state/reserve.rs"
    click OS "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/state/obligation.rs"
    click RFS "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/state/referral.rs"

    %% External Integrations Layer
    subgraph PriceOracles
        POI["Price Oracle Integration"]:::external
        PO["Pyth Oracle"]:::external
        SO["Switchboard Oracle"]:::external
        SCO["Scope Oracle"]:::external
    end
    click POI "https://github.com/Kamino-Finance/klend/tree/master/programs/klend/src/utils/prices/"
    click PO "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/utils/prices/pyth.rs"
    click SO "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/utils/prices/switchboard.rs"
    click SCO "https://github.com/Kamino-Finance/klend/blob/master/programs/klend/src/utils/prices/scope.rs"

    %% External Programs
    TP["Solana Token Program"]:::external

    %% Relationships
    EP --> LM
    EP --> FO
    EP --> FL
    EP --> WC
    EP --> LO
    EP --> LQ

    LM --> RS
    LM --> OS
    LM --> LMS
    LM --> RFS

    LO --> RS
    LO --> OS
    
    FL -.-> RS
    FL -.-> OS
    
    FO --> RS
    FO --> OS
    
    WC --> RS
    
    LQ --> RS
    LQ --> OS

    RS --> POI
    OS --> POI
    
    POI --> PO
    POI --> SO
    POI --> SCO
    
    LM --> TP
    
    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["External Systems"]:::external
        L3["State Management"]:::state
        L4["Critical Operations"]:::critical
        L5["Admin Functions"]:::admin
    end
```
