# drift-labs/driftpy architecture

> Architecture diagram of the GitHub repository drift-labs/driftpy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/drift-labs/driftpy
- Repository: https://github.com/drift-labs/driftpy

## Overview

##### Main Components

a) Core SDK Layer:
- Client Interface (drift_client.py)
- User Management (drift_user.py, drift_user_stats.py)
- Account Management System
- Transaction Handling System

b) Account Subscription Systems:
- WebSocket Subscription
- Polling Subscription
- Cache System
- Demo Mode

c) Market Components:
- DLOB (Decentralized Limit Order Book)
- Market Mapping
- Oracle Integration
- Event System

##### Key Architectural Elements

**┌─ External Layer ─────────────────────┐**
- Solana RPC / WebSocket Connections   │
- ┬─────────────────────┘

**┌─ Interface Layer ───────────────────┐**
- Account Subscription Configuration   │
- Transaction Handlers                │
- ┬───────────────────────┘

**┌─ Core Layer ──────────────────────┐**
- Drift Client                      │
- User Management                   │
- Market Management                 │
- ┬──────────────────────┘

**┌─ Utility Layer ────────────────────┐**
- Mathematics                        │
- Constants                          │
- Types                             │
- ┘

##### Connection and Data Flow

- Bidirectional communication with Solana network
- Data flow between subscription systems and core components
- Event propagation paths
- Oracle price feed integration

##### Implementation Details

- Multiple subscription modes (demo/cached/polling)
- Transaction handling systems (standard/fast/jito)
- Market data flow and order book management
- Event system architecture

##### Component Relationships

Clear connections between:
- DriftClient and User management
- Market mapping and DLOB
- Oracle integration points
- Event subscription system

##### Additional Elements

Include:
- Configuration management
- Error handling flow
- Testing infrastructure
- Development tooling

##### Notes

- Modular nature of the system
- Clear separation between core functionality and utilities
- Flexibility in subscription modes
- Demonstrate error handling and recovery paths

Remember to include version compatibility notes and any specific requirements (like Python 3.10+ requirement) in supplementary documentation.

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Layer
    subgraph External
        SolanaRPC["Solana RPC"]
        WebSocket["WebSocket Connection"]
        Oracle["Oracle Price Feed"]:::external
    end

    %% Interface Layer
    subgraph Interface
        SubConfig["Account Subscription Config"]
        TxHandlers["Transaction Handlers"]:::tx
        click TxHandlers "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/tx/"
    end

    %% Core Layer
    subgraph Core
        DriftClient["Drift Client"]:::core
        click DriftClient "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/drift_client.py"
        
        UserMgmt["User Management"]:::core
        click UserMgmt "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/drift_user.py"
        
        MarketMgmt["Market Management"]:::core
        click MarketMgmt "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/market_map/market_map.py"
    end

    %% Subscription Systems
    subgraph Subscriptions
        WSSystem["WebSocket System"]:::sub
        click WSSystem "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/accounts/ws/"
        
        PollingSystem["Polling System"]:::sub
        click PollingSystem "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/accounts/polling/"
        
        CacheSystem["Cache System"]:::sub
        click CacheSystem "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/accounts/cache/"
        
        DemoMode["Demo Mode"]:::sub
        click DemoMode "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/accounts/demo/"
    end

    %% Market Components
    subgraph Markets
        DLOB["DLOB"]:::market
        click DLOB "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/dlob/dlob.py"
        
        MarketMap["Market Mapping"]:::market
        click MarketMap "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/market_map/market_map.py"
        
        OracleInteg["Oracle Integration"]:::market
        click OracleInteg "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/oracles/"
        
        EventSystem["Event System"]:::market
        click EventSystem "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/events/"
    end

    %% Utility Layer
    subgraph Utils
        Math["Mathematics"]:::util
        click Math "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/math/"
        
        Constants["Constants"]:::util
        click Constants "https://github.com/drift-labs/driftpy/tree/master/src/driftpy/constants/"
        
        Types["Types"]:::util
        click Types "https://github.com/drift-labs/driftpy/blob/master/src/driftpy/types.py"
    end

    %% Connections
    SolanaRPC --> SubConfig
    WebSocket --> SubConfig
    Oracle --> OracleInteg
    SubConfig --> DriftClient
    TxHandlers --> DriftClient
    DriftClient --> UserMgmt
    DriftClient --> MarketMgmt
    MarketMgmt --> DLOB
    MarketMgmt --> MarketMap
    OracleInteg --> MarketMgmt
    WSSystem --> SubConfig
    PollingSystem --> SubConfig
    CacheSystem --> SubConfig
    DemoMode --> SubConfig
    EventSystem --> DriftClient
    Utils --> Core

    %% Styling
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef external fill:#4CAF50,stroke:#2E7D32,color:white
    classDef sub fill:#FFC107,stroke:#FFA000,color:black
    classDef market fill:#FF9800,stroke:#F57C00,color:white
    classDef util fill:#9E9E9E,stroke:#616161,color:white
    classDef tx fill:#E91E63,stroke:#C2185B,color:white

    %% Legend
    subgraph Legend
        CoreComp["Core Component"]:::core
        ExtComp["External Interface"]:::external
        SubComp["Subscription System"]:::sub
        MarketComp["Market Component"]:::market
        UtilComp["Utility Module"]:::util
        TxComp["Transaction Handler"]:::tx
    end
```
