# mystenlabs/deepbookv3 architecture

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

- Interactive diagram: https://gitdiagram.com/mystenlabs/deepbookv3
- Repository: https://github.com/mystenlabs/deepbookv3

## Overview

##### Main Components
- Smart Contracts (Move Language):
- Located under the packages/deepbook and packages/token directories.
- These include the core on-chain logic such as the Balance Manager, Pool (with Book, State, and Vault components), and token functionality.
- Off-chain Indexer and Schema:
- Found in crates/indexer and crates/schema.
- The indexer (in Rust) processes events (e.g., order fills, updates, proposals, flash loans) and likely maintains or syncs state. The schema component shows the migrations (using Diesel, for example) for the database.
- Transaction Scripts and Utilities:
- Located under scripts (with its subdirectories for transactions and utilities).
- These scripts, written in TypeScript, orchestrate transaction flows, interacting with the on-chain contracts.
- CI/CD and Build Infrastructure:
- The .github workflows indicate automated builds and testing, ensuring continuous integration.
- Configuration and Dependency Files:
- Files like Cargo.toml, package.json, and Move.toml define dependencies, build setups, and other configurations.

##### Relationships and Interactions
- On-Chain Components (Smart Contracts):
- The Balance Manager is central: it holds balances, controls account owners and traders, and is a required input for all interaction with DeepBook.
- The Pool is made of three parts:
- Book: Manages order book reading/writing.
- State: Manages user data, volumes, and governance data.
- Vault: Handles funds settlement.
- Additionally, DEEP token-related contracts manage fee payments, staking, and governance.
- Off-Chain Components:
- The Rust-based indexer listens for blockchain events, processes data from various handlers (balances, flash loans, order updates, etc.), and likely populates a database based on the schema.
- The schema module (with migrations and models) defines how the blockchain data is stored and queried.
- The TypeScript scripts serve as a client layer to create and execute transactions against the blockchain, as well as possibly facilitating interactions with the smart contracts.
- Data Flow and Integration:
- When a user places an order or executes a swap, the transaction flows from the UI (or calling script) → goes to the smart contracts (Balance Manager, Pool components) → then indexed by the Rust indexer → and stored in the database (using the schema migrations).
- Transaction scripts call and coordinate between the components by sending transactions, while CI/CD is employed through GitHub workflows to ensure quality and deployment readiness.

##### Architectural Patterns and Design Principles
- Separation of On-Chain and Off-Chain Logic:
- On-chain components (declared in Move) handle the critical state and logic regarding order management, fund settlement, and governance.
- Off-chain components (Rust indexer and TypeScript scripts) are used for monitoring, event processing, and transaction orchestration.
- Modular Design:
- The project is organized into separate crates and packages. For instance, “crates/indexer” for various event handlers, “crates/schema” for database schema definitions, and “packages/deepbook” for smart contract sources.
- Event-Driven or Asynchronous Updates:
- The indexer’s multiple handlers indicate an event-based design, capturing different actions (like order fills, trade parameter updates, etc.) and processing them asynchronously.
- Microservices-like Structure in Off-Chain Components:
- The clear division of responsibilities (indexing, schema management, and transaction scripts) helps scalability and maintainability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Transaction Scripts Subgraph
    subgraph "Transaction Scripts (TypeScript)"
        T1("Transactions"):::ts
        T2("Utilities"):::ts
        T3("Configuration"):::ts
    end
    T1 --> TS("Transaction Coordinator"):::ts
    T2 --> TS
    T3 --> TS

    %% Sui Blockchain with On-chain Smart Contracts Subgraph
    TS --> SUI["Sui Blockchain"]:::blockchain
    subgraph SUI_Blockchain [ ]
        subgraph "On-chain Smart Contracts"
            BM("Balance Manager"):::onchain
            PB("Pool: Book"):::onchain
            PS("Pool: State"):::onchain
            PV("Pool: Vault"):::onchain
            DT("DEEP Token Contract"):::onchain
        end
    end
    TS --> BM

    %% Event Flow from On-chain to Off-chain Indexer and Database
    BM -->|"emitsEvents"| IDX("Off-chain Indexer (Rust)"):::offchain
    IDX -->|"storesData"| DB("Database (Diesel Schema)"):::database

    %% CI/CD Pipeline Node
    CI("CI/CD Pipeline"):::cicd
    CI --- TS
    CI --- BM
    CI --- IDX

    %% Dependency & Configuration Files Subgraph
    subgraph "Dependency & Configuration Files"
        CF1("Cargo.toml"):::config
        CF2("package.json"):::config
        CF3("Move.toml"):::config
        CF4("packages/deepbook/Move.toml"):::config
        CF5("packages/token/Move.toml"):::config
    end
    CF1 --- TS
    CF2 --- TS
    CF3 --- BM
    CF4 --- BM
    CF5 --- DT

    %% Click Events for Component Mapping
    click BM "https://github.com/mystenlabs/deepbookv3/tree/main/packages/deepbook"
    click DT "https://github.com/mystenlabs/deepbookv3/tree/main/packages/token"
    click IDX "https://github.com/mystenlabs/deepbookv3/tree/main/crates/indexer"
    click DB "https://github.com/mystenlabs/deepbookv3/tree/main/crates/schema"
    click T1 "https://github.com/mystenlabs/deepbookv3/tree/main/scripts/transactions"
    click T2 "https://github.com/mystenlabs/deepbookv3/tree/main/scripts/utils"
    click T3 "https://github.com/mystenlabs/deepbookv3/tree/main/scripts/config"
    click CI "https://github.com/mystenlabs/deepbookv3/tree/main/.github/workflows"
    click CF1 "https://github.com/mystenlabs/deepbookv3/blob/main/Cargo.toml"
    click CF2 "https://github.com/mystenlabs/deepbookv3/blob/main/package.json"
    click CF3 "https://github.com/mystenlabs/deepbookv3/blob/main/Move.toml"
    click CF4 "https://github.com/mystenlabs/deepbookv3/blob/main/packages/deepbook/Move.toml"
    click CF5 "https://github.com/mystenlabs/deepbookv3/blob/main/packages/token/Move.toml"

    %% Class Definitions for Color Coding
    classDef onchain fill:#F8D7DA,stroke:#721c24,stroke-width:2px;
    classDef offchain fill:#D4EDDA,stroke:#155724,stroke-width:2px;
    classDef ts fill:#D1ECF1,stroke:#0C5460,stroke-width:2px;
    classDef blockchain fill:#CCE5FF,stroke:#004085,stroke-width:2px;
    classDef cicd fill:#D6D8D9,stroke:#1B1E21,stroke-width:2px;
    classDef config fill:#E2E3E5,stroke:#383D41,stroke-width:2px;
    classDef database fill:#FFF3CD,stroke:#856404,stroke-width:2px;
```
