# polygant/opencex-backend architecture

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

- Interactive diagram: https://gitdiagram.com/polygant/opencex-backend
- Repository: https://github.com/polygant/opencex-backend

## Overview

##### Main Components
- User Interfaces & APIs:
- Public Website and Web UI: Represented by the public_api module (with templates, static assets) and parts of “exchange” handling basic site routes and documentation.
- Admin Panel & Admin REST: Depicted as separate components. The “admin_panel” provides a web-based admin interface (with dashboard, menus, forms, templates) while “admin_rest” exposes REST endpoints for administrative operations.
- Core Business Logic:
- Core Module: This is the heart of the exchange, handling authentication, order book management, wallet management, API URL routing, and more (as seen in the “core” directory with its views, models, serializers, and websockets).
- Exchange Module: Manages environment-specific settings (e.g., database, logging, celery, and task management) that tie together configuration and operational orchestration.
- Bots: A dedicated module (in the “bots” directory) for trading bots, automated order processing, and exchange interactions featuring tasks, migrations, and integrations with external coin exchanges.
- Cryptocurrency & Blockchain Integration:
- Cryptocoins: This module handles coin-specific behaviors. It contains submodules for various coins (like BTC, ETH, MATIC, TRX, and USDT), wallet management, connections to blockchain nodes, and interfaces with external APIs.
- External Integration & Services: Lib/Services and parts of “cryptocoins” (especially under interface, evm, and data_sources) represent communication with external blockchain nodes, third-party APIs, and other exchange-related integrations.
- Supporting Components & Auxiliary Services:
- Notifications: Manages alerts, email templates, and communication for actions such as order changes or withdrawals.
- Dashboard REST and SEO: Modules that provide additional API endpoints (for dashboards) and SEO functionality.
- Sci, lib, and other utilities: These modules or libraries provide helper functions, caching utilities, and scheduled tasks (e.g., via Celery) to support the exchange’s dynamic operations.

##### Detail Relationships and Interactions
- Users interact with the Public Website and Admin interfaces.
- Requests (both UI and API calls) flow from the front end into the Core and Exchange modules.
- Core business processes call the Cryptocoins module for wallet or coin-specific actions (e.g., handling deposits, withdrawals, and real-time order book updates).
- Bots operate either independently or are triggered by the Core to perform automated trading, updating orders and executing strategies.
- Notifications and the Public API serve data externally, including displaying exchange stats and real-time market information.

##### Architectural Patterns & Design Principles
- Modular Django Structure: Each top-level directory (admin_panel, admin_rest, core, cryptocoins, etc.) corresponds to a Django “app” following the separation of concerns. Show how each app connects to the central DB.
- RESTful API design in admin_rest and public_api: Highlight API gateways that handle external requests.
- Asynchronous Task Processing: Illustrate components using Celery (located in the “exchange” directory) for background processing and scheduled tasks.
- Real-time Communications: Represent the use of websockets (in core/websockets) for live order book updates or notifications.
- Integration Layer for External Services: Indicate modules handling blockchain connections (coins, data_sources, lib/services, etc.) and third-party integrations for notifications and scoring.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% User Interface Layer
    subgraph "User Interface Layer"
        PublicAPI["Public Website & API"]:::uiLayer
        AdminPanel["Admin Panel"]:::uiLayer
        AdminREST["Admin REST"]:::uiLayer
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        CoreModule["Core Module"]:::business
        ExchangeModule["Exchange Module"]:::business
        Bots["Bots"]:::business
    end

    %% Blockchain Integration Layer
    subgraph "Blockchain Integration Layer"
        Cryptocoins["Cryptocoins Module"]:::integration
        ExternalInt["External Integrations"]:::integration
    end

    %% Supporting Services
    subgraph "Supporting Services"
        Notifications["Notifications"]:::support
        DashboardREST["Dashboard REST"]:::support
        SEO["SEO"]:::support
        SciUtilities["Sci & Utilities"]:::support
    end

    %% Central Database
    CentralDB["Central Database"]:::database

    %% Connections from UI Layer to Business Logic Layer
    PublicAPI -->|"UI/API"| CoreModule
    AdminPanel -->|"AdminUI"| CoreModule
    AdminREST -->|"REST"| CoreModule

    %% Connections within Business Logic Layer
    CoreModule -->|"Logic"| ExchangeModule
    CoreModule -->|"TriggerBots"| Bots
    CoreModule -->|"Wallet/Order"| Cryptocoins
    CoreModule -->|"Notify"| Notifications
    CoreModule -->|"DashboardReq"| DashboardREST
    CoreModule -->|"SEOContent"| SEO
    CoreModule -->|"UtilOps"| SciUtilities

    ExchangeModule -->|"AsyncTasks"| Bots

    %% Connections in Blockchain Integration Layer
    Cryptocoins -->|"IntegrationCall"| ExternalInt

    %% Connections to Central Database
    PublicAPI -->|"Content"| CentralDB
    AdminREST -->|"APIData"| CentralDB
    CoreModule -->|"DBOps"| CentralDB
    ExchangeModule -->|"DBOps"| CentralDB
    Bots -->|"DBOps"| CentralDB
    Cryptocoins -->|"DBOps"| CentralDB
    Notifications -->|"DBOps"| CentralDB
    DashboardREST -->|"DashboardData"| CentralDB
    SEO -->|"SEOData"| CentralDB
    SciUtilities -->|"UtilOps"| CentralDB

    %% Styles for layers
    classDef uiLayer fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef business fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef integration fill:#FFA500,stroke:#000,stroke-width:1px;
    classDef support fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef database fill:#FFFFE0,stroke:#000,stroke-width:1px;

    %% Click Events
    click PublicAPI "https://github.com/polygant/opencex-backend/tree/master/public_api/"
    click AdminPanel "https://github.com/polygant/opencex-backend/tree/master/admin_panel/"
    click AdminREST "https://github.com/polygant/opencex-backend/tree/master/admin_rest/"
    click CoreModule "https://github.com/polygant/opencex-backend/tree/master/core/"
    click ExchangeModule "https://github.com/polygant/opencex-backend/tree/master/exchange/"
    click Bots "https://github.com/polygant/opencex-backend/tree/master/bots/"
    click Cryptocoins "https://github.com/polygant/opencex-backend/tree/master/cryptocoins/"
    click ExternalInt "https://github.com/polygant/opencex-backend/tree/master/lib/services/"
    click Notifications "https://github.com/polygant/opencex-backend/tree/master/notifications/"
    click DashboardREST "https://github.com/polygant/opencex-backend/tree/master/dashboard_rest/"
    click SEO "https://github.com/polygant/opencex-backend/tree/master/seo/"
    click SciUtilities "https://github.com/polygant/opencex-backend/tree/master/sci/"
```
