# 0x-maker/brew-mev-ts architecture

> Architecture diagram of the GitHub repository 0x-maker/brew-mev-ts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0x-maker/brew-mev-ts
- Repository: https://github.com/0x-maker/brew-mev-ts

## Overview

##### Project Type & Core Purpose
- Brew-MEV is a full-stack Solana MEV trading bot with both a modern Next.js web dashboard and a CLI interface. It manages wallet encryption, real-time trading settings, and bot control via API endpoints while interacting with the Solana blockchain.
- The solution also integrates with external DEX services (Raydium, Jupiter) and uses strong security measures (AES-256 encryption) for wallet management.

##### Analyzing the File Structure
- The top-level “app” directory contains the Next.js application:
- “app/api” houses multiple API endpoints that control various functionalities: bot start/stop, debugging, QR code generation, wallet management, and settings updates.
- “app/components” holds React components for the dashboard, settings dialogs, wallet sections, and reusable UI components (like buttons, card, dialog, etc.).
- “app/lib” includes service modules for services such as wallet-service, settings-service, bot-settings, crypto utilities, and balance service, which are used by both API and other application parts.
- The “cli” directory represents the command-line interface:
- “cli/src” and “cli/utils” mirror much of the functionality found in the app’s libraries (handling bot operations, wallet management, settings, encryption, etc.).
- Other configuration and build files (e.g., next.config.js, tsconfig.json, tailwind.config.js) provide context on the tech stack (Next.js, TypeScript, TailwindCSS).

a. Main Components to Represent:
- Web Dashboard (Frontend)
- Next.js pages and components
- UI components (dashboard, theme-switcher, wallet-management, settings)
- API Layer (Backend)
- Next.js API endpoints under “app/api” (e.g., routes for bot start/stop, wallet functions, debug functions)
- Service & Business Logic Layer
- Libraries in “app/lib” handling wallet services, bot settings, transaction balancing, and encryption
- CLI Interface
- CLI executable logic in “cli/src” and utilities in “cli/utils” that reuse business logic for terminal-based control
- External Integration
- Solana blockchain interaction through Solana Web3.js
- External DEX APIs (Jupiter, Raydium) for real-time trading data
- Security Module
- Encryption utilities (AES-256 encryption and crypto-js) for secure key management
- Configuration & Deployment
- Config files and scripts (migrations, typescript setup, etc.)

b. Relationships and Data Flow:
- From both interfaces, indicate flows to the blockchain component where transactions and wallet operations occur.
- Intercommunication between API endpoints and internal service modules (wallet-service, settings-service) that enforce encryption and business rules.
- External connections from the bot’s core logic to DEX services and Solana nodes.

c. Architectural Patterns & Design Principles:
- Separation of Concerns: Divide the system into a UI layer (dashboard and CLI), an API/backend layer (Next.js API routes), and a service/business logic layer (in “app/lib” and “cli/utils”).
- Reusability: Both the CLI and dashboard rely on common libraries for wallet management, settings, and encryption.
- Security by Design: Indicate the encryption routines and security best practices that protect private keys.

d. Relevant Technologies & Frameworks:
- Frontend: Next.js 14, React, TypeScript, TailwindCSS, Shadcn/ui.
- Backend: Node.js, Next.js API Routes.
- Blockchain Interactions: Solana Web3.js.
- CLI Tools: Inquirer, Chalk, Figlet.
- Security: Crypto-js and AES-256 for encryption.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend (Web Dashboard)
    subgraph "Web Dashboard (Frontend)"
        DPage["Dashboard Page"]
        DComp["Dashboard Component"]
        TS["Theme Switcher"]
        WM["Wallet Manager"]
        WSec["Wallet Section"]
        Layout["Layout"]
        Global["Global Styles"]
    end

    %% API Layer (Backend Endpoints)
    subgraph "API Endpoints (Backend)"
        BotEP["Bot Endpoint"]
        DebugEP["Debug Endpoint"]
        QrEP["QRCode Endpoint"]
        WalletEP["Wallet Endpoint"]
        SettingsEP["Settings Endpoint"]
    end

    %% Service & Business Logic Layer
    subgraph "Service & Business Logic"
        WSrv["Wallet Service"]
        BotSet["Bot Settings"]
        SetSrv["Settings Service"]
        BalSrv["Balance Service"]
        CryptoUtil["Crypto Utils"]
        CommonUtil["Common Utils"]
    end

    %% CLI Interface
    subgraph "CLI Interface"
        CLICore["CLI Core"]
        CLIBot["CLI Bot Service"]
        CLICrypto["CLI Crypto"]
        CLISet["CLI Settings Service"]
        CLIWallet["CLI Wallet Service"]
    end

    %% External Systems
    Solana["Solana Blockchain"]
    DEX["External DEX Services"]

    %% Connections from Frontend to API Endpoints
    DPage -->|"calls"| BotEP
    DPage -->|"calls"| WalletEP
    DPage -->|"calls"| SettingsEP

    %% Connections from CLI Core to API Endpoints and CLI Utilities
    CLICore -->|"calls"| BotEP
    CLICore -->|"calls"| WalletEP
    CLICore -->|"calls"| SettingsEP
    CLICore -->|"calls"| CLIBot
    CLICore -->|"calls"| CLISet
    CLICore -->|"calls"| CLIWallet

    %% API Endpoints invoking Business Logic
    BotEP -->|"processes"| BotSet
    WalletEP -->|"processes"| WSrv
    SettingsEP -->|"processes"| SetSrv
    DebugEP -->|"logs"| CommonUtil
    QrEP -->|"generates"| CommonUtil

    %% CLI Utilities reusing Business Logic
    CLIBot -->|"reuses"| BotSet
    CLISet -->|"reuses"| SetSrv
    CLIWallet -->|"reuses"| WSrv
    CLICrypto -->|"reuses"| CryptoUtil

    %% Business Logic interacting with External Systems
    WSrv -->|"executes"| Solana
    BotSet -->|"executes"| Solana
    SetSrv -->|"executes"| Solana
    BalSrv -->|"monitors"| Solana
    CryptoUtil -->|"secures"| Solana
    CommonUtil -->|"assists"| DEX
    BotSet -->|"trades"| DEX

    %% Class Definitions for Colors
    classDef frontend fill:#D1E8FF,stroke:#000,stroke-width:2px;
    classDef apiLayer fill:#FFE4B5,stroke:#000,stroke-width:2px;
    classDef business fill:#C1FFC1,stroke:#000,stroke-width:2px;
    classDef cli fill:#FFCCCC,stroke:#000,stroke-width:2px;
    classDef external fill:#E6E6FA,stroke:#000,stroke-width:2px;

    %% Assign Classes to Nodes
    class DPage,DComp,TS,WM,WSec,Layout,Global frontend;
    class BotEP,DebugEP,QrEP,WalletEP,SettingsEP apiLayer;
    class WSrv,BotSet,SetSrv,BalSrv,CryptoUtil,CommonUtil business;
    class CLICore,CLIBot,CLICrypto,CLISet,CLIWallet cli;
    class Solana,DEX external;

    %% Click Events for Frontend
    click DPage "https://github.com/0x-maker/brew-mev-ts/blob/main/app/page.tsx"
    click DComp "https://github.com/0x-maker/brew-mev-ts/blob/main/app/components/dashboard.tsx"
    click TS "https://github.com/0x-maker/brew-mev-ts/blob/main/app/components/theme-switcher.tsx"
    click WM "https://github.com/0x-maker/brew-mev-ts/blob/main/app/components/wallet-manager.tsx"
    click WSec "https://github.com/0x-maker/brew-mev-ts/blob/main/app/components/wallet-section.tsx"
    click Layout "https://github.com/0x-maker/brew-mev-ts/blob/main/app/layout.tsx"
    click Global "https://github.com/0x-maker/brew-mev-ts/blob/main/app/styles/globals.css"

    %% Click Events for API Endpoints
    click BotEP "https://github.com/0x-maker/brew-mev-ts/tree/main/app/api/bot"
    click DebugEP "https://github.com/0x-maker/brew-mev-ts/tree/main/app/api/debug"
    click QrEP "https://github.com/0x-maker/brew-mev-ts/tree/main/app/api/qrcode"
    click WalletEP "https://github.com/0x-maker/brew-mev-ts/tree/main/app/api/wallet"
    click SettingsEP "https://github.com/0x-maker/brew-mev-ts/tree/main/app/api/settings"

    %% Click Events for Business Logic
    click WSrv "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/wallet-service.ts"
    click BotSet "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/bot-settings.ts"
    click SetSrv "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/settings-service.ts"
    click BalSrv "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/balance-service.ts"
    click CryptoUtil "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/crypto-utils.ts"
    click CommonUtil "https://github.com/0x-maker/brew-mev-ts/blob/main/app/lib/utils.ts"

    %% Click Events for CLI Interface
    click CLICore "https://github.com/0x-maker/brew-mev-ts/tree/main/cli/src"
    click CLIBot "https://github.com/0x-maker/brew-mev-ts/blob/main/cli/utils/bot-service.ts"
    click CLICrypto "https://github.com/0x-maker/brew-mev-ts/blob/main/cli/utils/crypto.ts"
    click CLISet "https://github.com/0x-maker/brew-mev-ts/blob/main/cli/utils/settings-service.ts"
    click CLIWallet "https://github.com/0x-maker/brew-mev-ts/blob/main/cli/utils/wallet-service.ts"
```
