# hamhmin/bithama_future_trade architecture

> Architecture diagram of the GitHub repository hamhmin/bithama_future_trade, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hamhmin/bithama_future_trade
- Repository: https://github.com/hamhmin/bithama_future_trade

## Overview

BITHAMA is a simulated futures-trading platform: users enter the terminal, view Binance market data, and submit orders that update simulated positions and balances. The backend also handles candle history, funding settlements, liquidation and TP/SL checks, authentication, rankings, and administration. The graph combines sampled call/import evidence with README-documented product behavior; several frontend routes and backend subsystems were not inspected in full, so their internal links are omitted.

Main entry points: [`backend/src/index.ts`](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/index.ts) (HTTP and socket server), [`backend/src/prisma.ts`](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/prisma.ts) (MySQL via Prisma), [`frontend/component/future/OrderPanel.tsx`](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/OrderPanel.tsx) (Order panel), [`backend/src/routes/future.ts`](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/future.ts) (Orders and positions API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Trading interface"]
  node_terminal["Futures terminal<br/>[page.tsx]"]
  node_future_layout["Terminal layout"]
  node_market_views["Market views<br/>[OrderBook.tsx]"]
  node_chart["Trading chart<br/>[TradingChart.tsx]"]
  node_order_panel["Order panel<br/>[OrderPanel.tsx]"]
  node_order_forms["Order and close forms<br/>[OrderForm.tsx]"]
  node_margin_leverage["Margin and leverage"]
  node_positions_ui["Positions and history<br/>[PositionTable.tsx]"]
end

subgraph group_trading["Trading services"]
  node_backend_entry["HTTP and socket server<br/>[index.ts]"]
  node_future_api["Orders and positions API<br/>[future.ts]"]
  node_state_machine["Trade state rules<br/>[stateMachine.ts]"]
  node_funding["Funding settlement<br/>[funding.ts]"]
end

subgraph group_market["Market data"]
  node_websocket["Live trading and checks<br/>[websocket.ts]"]
  node_candles["Candle sync<br/>[candle.ts]"]
  node_candle_api["Candle API<br/>[candle.ts]"]
end

subgraph group_accounts["Accounts and operations"]
  node_auth_api["Authentication API<br/>[auth.ts]"]
  node_auth_middleware["JWT authentication<br/>[auth.ts]"]
  node_ranking_api["Rankings API<br/>[ranking.ts]"]
  node_admin_api["Admin API<br/>[admin.ts]"]
  node_database[("MySQL via Prisma<br/>[prisma.ts]")]
end

node_trader(("Trader"))
node_binance["Binance market streams"]

node_trader -->|"opens"| node_terminal
node_terminal -.->|"renders"| node_future_layout
node_future_layout -.->|"presents"| node_market_views
node_future_layout -.->|"presents"| node_chart
node_future_layout -.->|"presents"| node_order_panel
node_order_panel -->|"renders"| node_margin_leverage
node_order_panel -->|"renders"| node_order_forms
node_trader -.->|"submits trade"| node_order_panel
node_order_panel -->|"posts orders"| node_future_api
node_future_api -->|"reads and writes"| node_database
node_future_api -->|"validates transitions"| node_state_machine
node_backend_entry -->|"mounts route"| node_future_api
node_backend_entry -->|"mounts route"| node_auth_api
node_backend_entry -->|"mounts route"| node_ranking_api
node_backend_entry -->|"mounts route"| node_admin_api
node_backend_entry -->|"mounts route"| node_candle_api
node_backend_entry -->|"starts streams"| node_websocket
node_backend_entry -->|"starts sync"| node_candles
node_backend_entry -->|"starts scheduler"| node_funding
node_websocket -->|"opens streams"| node_binance
node_websocket -->|"checks and updates"| node_database
node_websocket -->|"validates transitions"| node_state_machine
node_candles -->|"stores data"| node_database
node_candle_api -->|"reads candles"| node_database
node_funding -->|"settles balances"| node_database
node_funding -->|"notifies users"| node_websocket
node_auth_api -->|"protects routes"| node_auth_middleware
node_auth_api -->|"reads and updates"| node_database
node_future_api -->|"protects routes"| node_auth_middleware
node_ranking_api -->|"reads rankings"| node_database
node_admin_api -->|"manages records"| node_database

click node_terminal "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/app/(main)/future/page.tsx"
click node_future_layout "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/FutureClientLayout.tsx"
click node_market_views "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/OrderBook.tsx"
click node_chart "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/TradingChart.tsx"
click node_order_panel "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/OrderPanel.tsx"
click node_order_forms "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/order/OrderForm.tsx"
click node_margin_leverage "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/order/MarginLeverageBar.tsx"
click node_positions_ui "https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/position/PositionTable.tsx"
click node_backend_entry "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/index.ts"
click node_future_api "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/future.ts"
click node_state_machine "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/lib/stateMachine.ts"
click node_funding "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/funding.ts"
click node_websocket "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/websocket.ts"
click node_candles "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/candle.ts"
click node_candle_api "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/candle.ts"
click node_auth_api "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/auth.ts"
click node_auth_middleware "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/middleware/auth.ts"
click node_ranking_api "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/ranking.ts"
click node_admin_api "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/admin.ts"
click node_database "https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/prisma.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_terminal,node_future_layout,node_market_views,node_chart,node_order_panel,node_order_forms,node_margin_leverage,node_positions_ui toneBlue
class node_backend_entry,node_future_api,node_state_machine,node_funding toneAmber
class node_websocket,node_candles,node_candle_api toneMint
class node_auth_api,node_auth_middleware,node_ranking_api,node_admin_api,node_database toneRose
class node_trader,node_binance toneIndigo
```

## Components

### Trading interface

- [Futures terminal](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/app/(main)/future/page.tsx): `frontend/app/(main)/future/page.tsx`
- [Terminal layout](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/FutureClientLayout.tsx): `frontend/component/future/FutureClientLayout.tsx`
- [Market views](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/OrderBook.tsx): `frontend/component/future/OrderBook.tsx`
- [Trading chart](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/TradingChart.tsx): `frontend/component/future/TradingChart.tsx`
- [Order panel](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/OrderPanel.tsx): `frontend/component/future/OrderPanel.tsx`
- [Order and close forms](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/order/OrderForm.tsx): `frontend/component/future/order/OrderForm.tsx`
- [Margin and leverage](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/order/MarginLeverageBar.tsx): `frontend/component/future/order/MarginLeverageBar.tsx`
- [Positions and history](https://github.com/hamhmin/bithama_future_trade/blob/main/frontend/component/future/position/PositionTable.tsx): `frontend/component/future/position/PositionTable.tsx`

### Trading services

- [HTTP and socket server](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/index.ts): `backend/src/index.ts`
- [Orders and positions API](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/future.ts): `backend/src/routes/future.ts`
- [Trade state rules](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/lib/stateMachine.ts): `backend/src/lib/stateMachine.ts`
- [Funding settlement](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/funding.ts): `backend/src/funding.ts`

### Market data

- [Live trading and checks](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/websocket.ts): `backend/src/websocket.ts`
- [Candle sync](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/candle.ts): `backend/src/candle.ts`
- [Candle API](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/candle.ts): `backend/src/routes/candle.ts`

### Accounts and operations

- [Authentication API](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/auth.ts): `backend/src/routes/auth.ts`
- [JWT authentication](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/middleware/auth.ts): `backend/src/middleware/auth.ts`
- [Rankings API](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/ranking.ts): `backend/src/routes/ranking.ts`
- [Admin API](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/routes/admin.ts): `backend/src/routes/admin.ts`
- [MySQL via Prisma](https://github.com/hamhmin/bithama_future_trade/blob/main/backend/src/prisma.ts): `backend/src/prisma.ts`

### Other components

- **Trader**
- **Binance market streams**

## Connections

- Trader → Futures terminal: opens
- Futures terminal → Terminal layout: renders
- Terminal layout → Market views: presents
- Terminal layout → Trading chart: presents
- Terminal layout → Order panel: presents
- Order panel → Margin and leverage: renders (evidence: `frontend/component/future/OrderPanel.tsx`)
- Order panel → Order and close forms: renders (evidence: `frontend/component/future/OrderPanel.tsx`)
- Trader → Order panel: submits trade
- Order panel → Orders and positions API: posts orders (evidence: `frontend/component/future/OrderPanel.tsx`)
- Orders and positions API → MySQL via Prisma: reads and writes (evidence: `backend/src/routes/future.ts`)
- Orders and positions API → Trade state rules: validates transitions (evidence: `backend/src/routes/future.ts`)
- HTTP and socket server → Orders and positions API: mounts route (evidence: `backend/src/index.ts`)
- HTTP and socket server → Authentication API: mounts route (evidence: `backend/src/index.ts`)
- HTTP and socket server → Rankings API: mounts route (evidence: `backend/src/index.ts`)
- HTTP and socket server → Admin API: mounts route (evidence: `backend/src/index.ts`)
- HTTP and socket server → Candle API: mounts route (evidence: `backend/src/index.ts`)
- HTTP and socket server → Live trading and checks: starts streams (evidence: `backend/src/index.ts`)
- HTTP and socket server → Candle sync: starts sync (evidence: `backend/src/index.ts`)
- HTTP and socket server → Funding settlement: starts scheduler (evidence: `backend/src/index.ts`)
- Live trading and checks → Binance market streams: opens streams (evidence: `backend/src/websocket.ts`)
- Live trading and checks → MySQL via Prisma: checks and updates (evidence: `backend/src/websocket.ts`)
- Live trading and checks → Trade state rules: validates transitions (evidence: `backend/src/websocket.ts`)
- Candle sync → MySQL via Prisma: stores data (evidence: `backend/src/candle.ts`)
- Candle API → MySQL via Prisma: reads candles (evidence: `backend/src/routes/candle.ts`)
- Funding settlement → MySQL via Prisma: settles balances (evidence: `backend/src/funding.ts`)
- Funding settlement → Live trading and checks: notifies users (evidence: `backend/src/funding.ts`)
- Authentication API → JWT authentication: protects routes (evidence: `backend/src/routes/auth.ts`)
- Authentication API → MySQL via Prisma: reads and updates (evidence: `backend/src/routes/auth.ts`)
- Orders and positions API → JWT authentication: protects routes (evidence: `backend/src/routes/future.ts`)
- Rankings API → MySQL via Prisma: reads rankings (evidence: `backend/src/routes/ranking.ts`)
- Admin API → MySQL via Prisma: manages records (evidence: `backend/src/routes/admin.ts`)
