# screamonei2/drift-copy-trader architecture

> Architecture diagram of the GitHub repository screamonei2/drift-copy-trader, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/screamonei2/drift-copy-trader
- Repository: https://github.com/screamonei2/drift-copy-trader

## Overview

##### Project type and purpose
- This is a backend-focused, Node.js / TypeScript “copy-trader” service for the Drift protocol on Solana.
- It provides an automated, real-time copy-trading engine (DriftCopyTrader), risk management, monitoring, and CLI/SDK interfaces.

##### File structure
- Top-level entry points and config:
- src/index.ts – the main service bootstrap
- src/config/index.ts – environment/config loader
- Core logic:
- src/trailing-stop.ts & src/managers/TrailingStopManagerAtomic.ts – position-risk modules
- src/utils/TransactionManager.ts – trade execution and confirmation
- src/utils/RateLimitManager.ts – API rate-limiting
- src/utils/SoundNotifier.ts – audio notifications (for on-premises monitoring)
- Types and interfaces:
- src/types/index.ts
- Examples & debug scripts:
- examples/*.ts – basic and advanced usage samples
- debug*.ts & test-sound*.ts – local testing and debugging harnesses
- Docker & deployment:
- Dockerfile, docker-compose.yml, SOUND_SYSTEM_DOCKER.md, DOCKER.md – containerization and orchestration

##### README for additional insights
- Technologies: Node.js 18+, TypeScript, Drift SDK, Solana RPC (HTTP & WebSocket), dotenv
- Features: automatic trade replication, dynamic filters, risk management, real-time monitoring, dry-run mode
- Patterns: event-driven (listeners for “tradeCopied”, “tradeError”), modular managers, environment-based config

##### Elements
- Clear labels: e.g., “DriftCopyTrader Core”, “RateLimitManager”, “Solana RPC Node”
- RPC → EventListener → Core
- Core → TransactionManager → RPC
- Core → SoundNotifier → Audio Output
- Orange parallelograms for CLI / example clients

##### Detailed capture of essential elements
- Config injection at startup
- Denote dry-run path as a distinct conditional branch
- Advanced-monitoring features (WebSocket vs. polling, concurrency limits) in a sub-domain

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Docker Container Boundary
    subgraph "Docker Container"
        style DockerContainer fill:#FFFFFF,stroke:#000000,stroke-width:2px
        DockerContainer[ ]
        %% Internal Modules
        ConfigLoader["Config Loader"]:::internal
        Core["DriftCopyTrader Core"]:::internal
        EventListener["WebSocket / Polling Listener"]:::internal
        RateLimit["RateLimitManager"]:::internal
        TxManager["TransactionManager"]:::internal
        TrailingAtomic["TrailingStopManagerAtomic"]:::internal
        TrailingLogic["trailing-stop.ts"]:::internal
        SoundNotifier["SoundNotifier"]:::internal
        Types["Types & Interfaces"]:::internal

        %% Relationships inside container
        ConfigLoader -->|"inject config"| Core
        SolanaRPC -->|"events"| EventListener
        EventListener -->|"emit new trade"| Core
        Core -->|"check rate limits"| RateLimit
        RateLimit --> Core
        Core -->|"apply risk checks"| TrailingAtomic
        TrailingAtomic --> TrailingLogic
        Core -->|"execute trade"| TxManager
        TxManager -->|"submit tx"| SolanaRPC
        Core -->|"notify events"| SoundNotifier

        %% Dry-run optional flow
        Core -.->|Dry-run simulation| TxManager

        %% Types used by modules
        Types --> ConfigLoader
        Types --> Core
        Types --> EventListener
        Types --> RateLimit
        Types --> TxManager
        Types --> TrailingAtomic
        Types --> SoundNotifier
    end

    %% External Services
    subgraph "External Services"
        SolanaRPC["Solana RPC Node\n(HTTP & WS)"]:::external
        DriftProtocol["Drift Protocol On-Chain"]:::external
        TargetWallets["Target Wallets"]:::external
        AudioOutput["Audio Output"]:::external
    end

    SolanaRPC --> DriftProtocol
    SoundNotifier --> AudioOutput
    Core --> TargetWallets

    %% Client Examples
    subgraph "CLI / Examples" 
        BasicExample["basic-copy-trader.ts"]:::client
        AdvancedExample["advanced-monitoring.ts"]:::client
        DebugScripts["debug & test scripts"]:::client
        BasicExample --> Core
        AdvancedExample --> Core
        DebugScripts --> Core
    end

    %% Deployment Files
    subgraph "Deployment Config"
        Dockerfile["Dockerfile"]:::client
        Compose["docker-compose.yml"]:::client
        MD1["DOCKER.md"]:::client
        MD2["SOUND_SYSTEM_DOCKER.md"]:::client
        Dockerignore[".dockerignore"]:::client
    end

    %% Click Events
    click ConfigLoader "https://github.com/screamonei2/drift-copy-trader/blob/main/src/config/index.ts"
    click Core "https://github.com/screamonei2/drift-copy-trader/blob/main/src/index.ts"
    click TrailingAtomic "https://github.com/screamonei2/drift-copy-trader/blob/main/src/managers/TrailingStopManagerAtomic.ts"
    click TrailingLogic "https://github.com/screamonei2/drift-copy-trader/blob/main/src/trailing-stop.ts"
    click RateLimit "https://github.com/screamonei2/drift-copy-trader/blob/main/src/utils/RateLimitManager.ts"
    click TxManager "https://github.com/screamonei2/drift-copy-trader/blob/main/src/utils/TransactionManager.ts"
    click SoundNotifier "https://github.com/screamonei2/drift-copy-trader/blob/main/src/utils/SoundNotifier.ts"
    click Types "https://github.com/screamonei2/drift-copy-trader/blob/main/src/types/index.ts"
    click BasicExample "https://github.com/screamonei2/drift-copy-trader/blob/main/examples/basic-copy-trader.ts"
    click AdvancedExample "https://github.com/screamonei2/drift-copy-trader/blob/main/examples/advanced-monitoring.ts"
    click DebugScripts "https://github.com/screamonei2/drift-copy-trader/blob/main/debug.ts"
    click Dockerfile "https://github.com/screamonei2/drift-copy-trader/tree/main/Dockerfile"
    click Compose "https://github.com/screamonei2/drift-copy-trader/blob/main/docker-compose.yml"
    click MD1 "https://github.com/screamonei2/drift-copy-trader/blob/main/DOCKER.md"
    click MD2 "https://github.com/screamonei2/drift-copy-trader/blob/main/SOUND_SYSTEM_DOCKER.md"
    click Dockerignore "https://github.com/screamonei2/drift-copy-trader/blob/main/.dockerignore"

    %% Styles
    classDef internal fill:#BBDEFB,stroke:#0D47A1,color:#0D47A1
    classDef external fill:#C8E6C9,stroke:#1B5E20,color:#1B5E20
    classDef client fill:#FFE0B2,stroke:#E65100,color:#E65100
    classDef optional stroke-dasharray: 5 5;color:#000000
    class DockerContainer internal
```
