# drift-labs/gateway architecture

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

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

## Overview

##### Main Components
- API Server: At the heart is an HTTP/REST API server (as evidenced by the “controller.rs” and “main.rs” files) that handles incoming HTTP requests for endpoints like “/v2/orders”, “/v2/markets”, etc.
- WebSocket Module: The “websocket.rs” file indicates a dedicated module for managing WebSocket connections for live updates (e.g., order fills, cancellations, funding payments).
- Business Logic & Types: The “types.rs” file sets up data structures and types that underpin the domain logic (e.g., orders, positions, events).
- Configuration & Deployment: The project uses environment variables (e.g., DRIFT_GATEWAY_KEY, INIT_RPC_THROTTLE), a Dockerfile for containerized builds, and GitHub workflows for CI/CD.
- External Dependencies: The API communicates with external systems, such as the Solana RPC endpoints and, indirectly, Drift market data feeds. The README shows that configuration parameters (like market lists and RPC URLs) are passed into the gateway during startup.

##### Relationships & Interactions
- Client Interactions: Clients interact with the gateway using HTTP endpoints for placing orders, querying market data, and checking balances. They can also connect via WebSockets for real-time event streams.
- API Server and Business Logic: The controller (API layer) routes requests to the appropriate functions which then use the defined types and logic to create, modify, or cancel orders. This module is responsible for validating and processing incoming requests.
- External Communication: The API server sends requests to, and receives responses from, external RPC nodes (Solana) and manages delegated signing, order confirmations, and transaction events.

##### Architectural Patterns & Design Principles
- Modular Design: The code organization separates concerns (e.g., controllers, types, WebSocket handling) which is indicative of a modular, layered architecture.
- API Gateway Pattern: The gateway aggregates multiple interactions (HTTP, WebSocket) and orchestrates calls to external systems (Solana RPC).
- Configuration-driven Deployment: Environment variables, Docker, and GitHub workflows highlight a CI/CD and containerized deployment pipeline.

##### Relevant Technologies & Frameworks
- Rust: The language (indicated by the Cargo files and Rust source files) is central to implementation.
- Docker: Containerization support via the provided Dockerfile.
- GitHub Actions: Continuous integration and deployment pipelines set up in the .github/workflows directory.
- Solana RPC and Drift Protocol: External services with which the gateway interacts.

- That any critical configuration paths (like the use of environment variables or Docker settings) are clearly marked.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Drift Gateway"
        A["Main Application Entrypoint"] 
        B["HTTP API Controller"]
        C["Business Logic & Data Structures"]
        D["WebSocket Module"]
        A --> B
        A --> D
        B -->|"process"| C
        D -->|"shared_logic"| C
    end

    X["HTTP Client Requests"]
    Y["WebSocket Client Connections"]
    E["External RPC & Drift Protocol Interface"]
    H["Environment Configuration"]
    F["Containerization/Deployment"]
    G["CI/CD Pipeline"]

    X -->|"HTTP_call"| B
    Y -->|"WS_connect"| D
    C -->|"external_calls"| E
    H -->|"config"| A
    F -->|"deployment"| A
    G -->|"deployment"| A

    class A,B,C,D internal
    class X,Y,E external
    class F,G,H config

    classDef internal fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#b38600,stroke-width:2px;
    classDef config fill:#d5f5e3,stroke:#1d8348,stroke-width:2px;

    click B "https://github.com/drift-labs/gateway/blob/master/src/controller.rs"
    click A "https://github.com/drift-labs/gateway/blob/master/src/main.rs"
    click C "https://github.com/drift-labs/gateway/blob/master/src/types.rs"
    click D "https://github.com/drift-labs/gateway/blob/master/src/websocket.rs"
    click F "https://github.com/drift-labs/gateway/tree/master/Dockerfile"
    click G "https://github.com/drift-labs/gateway/tree/master/.github/workflows"
```
