# syedahmedzeshaan/cex architecture

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

- Interactive diagram: https://gitdiagram.com/syedahmedzeshaan/cex
- Repository: https://github.com/syedahmedzeshaan/cex

## Overview

This repository implements a mock trading exchange with a browser trading interface. Users browse markets, authenticate, inspect charts and order depth, place or cancel orders, and review fills and balances. The backend matches orders against in-memory books and balances, persists exchange records, and publishes market updates; candle history and simulated trading bots are additional capabilities. The supplied excerpts show matching, persistence, candles, and frontend data flows. Backend route details and several UI or bot implementations were not sampled, so those connections are omitted or marked unverified.

Main entry points: [`be/src/orderService.ts`](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/orderService.ts) (Order service), [`fe/src/routes/Routes.tsx`](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/routes/Routes.tsx) (App routes), [`fe/src/components/trading/Trading.tsx`](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/trading/Trading.tsx) (Trading screen), [`fe/src/hooks/useCandles.ts`](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/hooks/useCandles.ts) (Candle data hook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Trading interface"]
  node_routes["App routes<br/>[Routes.tsx]"]
  node_auth["Authentication<br/>[AuthContext.tsx]"]
  node_landing["Landing and home<br/>[LandingPage.tsx]"]
  node_trading["Trading screen<br/>[Trading.tsx]"]
  node_orderform["Order form<br/>[OrderForm.tsx]"]
  node_charts["Candle charts<br/>[CandleChart.tsx]"]
  node_orderbookview["Order book view<br/>[OrderBook.tsx]"]
  node_accountviews["Account and activity<br/>[PortfolioPage.tsx]"]
  node_api["HTTP API client<br/>[api.ts]"]
  node_wsclient["WebSocket client<br/>[websocket.ts]"]
  node_candlehook["Candle data hook<br/>[useCandles.ts]"]
  node_depthhook["Depth data hook<br/>[useDepth.ts]"]
end

subgraph group_exchange["Exchange engine"]
  node_backend["Exchange API<br/>[index.ts]"]
  node_orderservice["Order service<br/>[orderService.ts]"]
  node_matcher["Matching engine<br/>[matchingEngine.ts]"]
  node_book["Order books<br/>[orderbook.ts]"]
  node_balances["In-memory balances"]
  node_database[("Prisma database<br/>[prisma.ts]")]
end

subgraph group_marketdata["Market data"]
  node_candles["Candle manager<br/>[CandleManager.ts]"]
  node_subscriptions["Market subscriptions"]
end

subgraph group_simulation["Trading simulation"]
  node_botsetup["Bot setup<br/>[setupBots.ts]"]
  node_botmanager["Bot manager<br/>[botManager.ts]"]
  node_marketmaker["Market maker<br/>[marketMaker.ts]"]
end

node_trader(("Trader"))
node_backendapi["Exchange HTTP service"]

node_trader -->|"uses"| node_routes
node_routes -->|"checks auth"| node_auth
node_routes -->|"routes to"| node_landing
node_routes -->|"routes to"| node_trading
node_routes -->|"routes to"| node_accountviews
node_trading -->|"renders"| node_orderform
node_trading -->|"renders"| node_charts
node_trading -->|"renders"| node_orderbookview
node_trading -->|"loads candles"| node_candlehook
node_orderbookview -->|"loads depth"| node_depthhook
node_candlehook -->|"loads history"| node_api
node_candlehook -->|"subscribes"| node_wsclient
node_depthhook -->|"loads depth"| node_api
node_depthhook -->|"subscribes"| node_wsclient
node_api -->|"HTTP requests"| node_backendapi
node_backend -.->|"dispatches orders"| node_orderservice
node_orderservice -->|"matches orders"| node_matcher
node_matcher -->|"reads and updates"| node_book
node_matcher -->|"updates balances"| node_balances
node_orderservice -->|"reads depth"| node_book
node_orderservice -->|"persists orders"| node_database
node_book -->|"loads active orders"| node_database
node_candles -->|"persists candles"| node_database
node_candles -->|"broadcasts candles"| node_subscriptions
node_orderservice -->|"broadcasts depth"| node_subscriptions
node_botsetup -->|"registers bots"| node_botmanager
node_botsetup -->|"configures"| node_marketmaker
node_marketmaker -->|"places orders"| node_orderservice

click node_routes "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/routes/Routes.tsx"
click node_auth "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/context/AuthContext.tsx"
click node_landing "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/landing/LandingPage.tsx"
click node_trading "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/trading/Trading.tsx"
click node_orderform "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/trading/OrderForm.tsx"
click node_charts "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/chart/CandleChart.tsx"
click node_orderbookview "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/orderbook/OrderBook.tsx"
click node_accountviews "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/portfolio/PortfolioPage.tsx"
click node_api "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/services/api.ts"
click node_wsclient "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/services/websocket.ts"
click node_candlehook "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/hooks/useCandles.ts"
click node_depthhook "https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/hooks/useDepth.ts"
click node_backend "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/index.ts"
click node_orderservice "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/orderService.ts"
click node_matcher "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/matchingEngine.ts"
click node_book "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/orderbook.ts"
click node_balances "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/inMemoryBalances.ts"
click node_database "https://github.com/syedahmedzeshaan/cex/blob/main/be/lib/prisma.ts"
click node_candles "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/CandleManager.ts"
click node_subscriptions "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/websocket/SubscriptionsManager.ts"
click node_botsetup "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/setupBots.ts"
click node_botmanager "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/botManager.ts"
click node_marketmaker "https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/marketMaker.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_routes,node_auth,node_landing,node_trading,node_orderform,node_charts,node_orderbookview,node_accountviews,node_api,node_wsclient,node_candlehook,node_depthhook toneBlue
class node_backend,node_orderservice,node_matcher,node_book,node_balances,node_database toneAmber
class node_candles,node_subscriptions toneMint
class node_botsetup,node_botmanager,node_marketmaker toneRose
class node_trader,node_backendapi toneIndigo
```

## Components

### Trading interface

- [App routes](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/routes/Routes.tsx): `fe/src/routes/Routes.tsx`
- [Authentication](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/context/AuthContext.tsx): `fe/src/context/AuthContext.tsx`
- [Landing and home](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/landing/LandingPage.tsx): `fe/src/components/landing/LandingPage.tsx`
- [Trading screen](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/trading/Trading.tsx): `fe/src/components/trading/Trading.tsx`
- [Order form](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/trading/OrderForm.tsx): `fe/src/components/trading/OrderForm.tsx`
- [Candle charts](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/chart/CandleChart.tsx): `fe/src/components/chart/CandleChart.tsx`
- [Order book view](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/orderbook/OrderBook.tsx): `fe/src/components/orderbook/OrderBook.tsx`
- [Account and activity](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/components/portfolio/PortfolioPage.tsx): `fe/src/components/portfolio/PortfolioPage.tsx`
- [HTTP API client](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/services/api.ts): `fe/src/services/api.ts`
- [WebSocket client](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/services/websocket.ts): `fe/src/services/websocket.ts`
- [Candle data hook](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/hooks/useCandles.ts): `fe/src/hooks/useCandles.ts`
- [Depth data hook](https://github.com/syedahmedzeshaan/cex/blob/main/fe/src/hooks/useDepth.ts): `fe/src/hooks/useDepth.ts`

### Exchange engine

- [Exchange API](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/index.ts): `be/src/index.ts`
- [Order service](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/orderService.ts): `be/src/orderService.ts`
- [Matching engine](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/matchingEngine.ts): `be/src/matchingEngine.ts`
- [Order books](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/orderbook.ts): `be/src/orderbook.ts`
- [In-memory balances](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/inMemoryBalances.ts): `be/src/inMemoryBalances.ts`
- [Prisma database](https://github.com/syedahmedzeshaan/cex/blob/main/be/lib/prisma.ts): `be/lib/prisma.ts`

### Market data

- [Candle manager](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/CandleManager.ts): `be/src/CandleManager.ts`
- [Market subscriptions](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/websocket/SubscriptionsManager.ts): `be/src/websocket/SubscriptionsManager.ts`

### Trading simulation

- [Bot setup](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/setupBots.ts): `be/src/bots/setupBots.ts`
- [Bot manager](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/botManager.ts): `be/src/bots/botManager.ts`
- [Market maker](https://github.com/syedahmedzeshaan/cex/blob/main/be/src/bots/marketMaker.ts): `be/src/bots/marketMaker.ts`

### Other components

- **Trader**
- **Exchange HTTP service**

## Connections

- Trader → App routes: uses (evidence: `fe/src/routes/Routes.tsx`)
- App routes → Authentication: checks auth (evidence: `fe/src/routes/Routes.tsx`)
- App routes → Landing and home: routes to (evidence: `fe/src/routes/Routes.tsx`)
- App routes → Trading screen: routes to (evidence: `fe/src/routes/Routes.tsx`)
- App routes → Account and activity: routes to (evidence: `fe/src/routes/Routes.tsx`)
- Trading screen → Order form: renders (evidence: `fe/src/components/trading/Trading.tsx`)
- Trading screen → Candle charts: renders (evidence: `fe/src/components/trading/Trading.tsx`)
- Trading screen → Order book view: renders (evidence: `fe/src/components/trading/Trading.tsx`)
- Trading screen → Candle data hook: loads candles (evidence: `fe/src/components/trading/Trading.tsx`)
- Order book view → Depth data hook: loads depth (evidence: `fe/src/components/orderbook/OrderBook.tsx`)
- Candle data hook → HTTP API client: loads history (evidence: `fe/src/hooks/useCandles.ts`)
- Candle data hook → WebSocket client: subscribes (evidence: `fe/src/hooks/useCandles.ts`)
- Depth data hook → HTTP API client: loads depth (evidence: `fe/src/hooks/useDepth.ts`)
- Depth data hook → WebSocket client: subscribes (evidence: `fe/src/hooks/useDepth.ts`)
- HTTP API client → Exchange HTTP service: HTTP requests (evidence: `fe/src/services/api.ts`)
- Exchange API → Order service: dispatches orders (evidence: `be/src/index.ts`)
- Order service → Matching engine: matches orders (evidence: `be/src/orderService.ts`)
- Matching engine → Order books: reads and updates (evidence: `be/src/matchingEngine.ts`)
- Matching engine → In-memory balances: updates balances (evidence: `be/src/matchingEngine.ts`)
- Order service → Order books: reads depth (evidence: `be/src/orderService.ts`)
- Order service → Prisma database: persists orders (evidence: `be/src/orderService.ts`)
- Order books → Prisma database: loads active orders (evidence: `be/src/orderbook.ts`)
- Candle manager → Prisma database: persists candles (evidence: `be/src/CandleManager.ts`)
- Candle manager → Market subscriptions: broadcasts candles (evidence: `be/src/CandleManager.ts`)
- Order service → Market subscriptions: broadcasts depth (evidence: `be/src/orderService.ts`)
- Bot setup → Bot manager: registers bots (evidence: `be/src/bots/setupBots.ts`)
- Bot setup → Market maker: configures (evidence: `be/src/bots/setupBots.ts`)
- Market maker → Order service: places orders (evidence: `be/src/bots/marketMaker.ts`)
