Skip to content

Zak-JS/lme-trading-platform

Repository files navigation

LME Trading Platform

A real-time trading platform for London Metal Exchange base metals, demonstrating modern full-stack development with React and Node.js.

TypeScript React Node.js Deployed

πŸš€ Live Demo

https://trading-platform-production-3db5.up.railway.app


πŸ“š Documentation

Document Description
REQUIREMENTS.md Business requirements, user flows with diagrams, technical decisions
ARCHITECTURE.md System architecture, data flow diagrams, deployment details
PROJECT_STRUCTURE.md Folder structure, naming conventions, file organization

Quick Start

# Install dependencies
pnpm install

# Setup database
pnpm run db:generate
pnpm run db:migrate
pnpm run db:seed

# Start development servers
pnpm run dev

Tech Stack & Rationale

Frontend

Technology Purpose Why This Choice
React 18 UI Framework Industry standard, demonstrates component architecture
TypeScript Type Safety Catches errors at compile time, self-documenting code
TanStack Query Server State Handles caching, background refetching, optimistic updates
TailwindCSS Styling Rapid prototyping, consistent design system
Recharts Data Visualization Composable charts for portfolio allocation
Vite Build Tool Fast HMR, optimized production builds

Backend

Technology Purpose Why This Choice
Fastify HTTP Server High performance, TypeScript-first, plugin ecosystem
WebSocket Real-time Updates Sub-second price updates without polling
Drizzle ORM Database Type-safe queries, lightweight, excellent DX
SQLite Database Zero-config, portable, sufficient for demo scale
Zod Validation Runtime validation with TypeScript inference

Monorepo Structure

β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ shared/     # Types, schemas, constants (used by both)
β”‚   β”œβ”€β”€ server/     # Fastify REST + WebSocket API
β”‚   β”œβ”€β”€ client/     # React SPA
β”‚   └── server-java/ # (Placeholder) Spring Boot alternative

Why Monorepo? Shared types between frontend and backend eliminate API contract drift and enable end-to-end type safety.


Architecture Patterns

1. Feature-Based Organization (Frontend)

src/features/
β”œβ”€β”€ trading/
β”‚   β”œβ”€β”€ api/          # React Query hooks
β”‚   β”œβ”€β”€ components/   # Feature-specific UI
β”‚   └── hooks/        # Custom hooks
└── portfolio/
    └── ...

Scales better than grouping by file type. Each feature is self-contained.

2. Service Layer (Backend)

Routes β†’ Services β†’ Database
  • Routes: HTTP/validation only
  • Services: Business logic, reusable across routes
  • Database: Drizzle schema + queries

3. Real-Time Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    WebSocket    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Client  │◄───────────────►│ Server  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚                           β”‚
     β”‚  React Query              β”‚  Price Service
     β”‚  (cache sync)             β”‚  (simulation)
     β–Ό                           β–Ό
  UI Updates              Broadcast to all clients

Price updates flow via WebSocket, React Query cache is updated, UI re-renders automatically.

4. Backend-Agnostic Design

The API contract is documented separately, allowing the backend to be reimplemented in any language (Java/Spring Boot scaffold included) while the frontend remains unchanged.


Key Features

Feature Implementation
Live Price Grid WebSocket subscription, flash animations on price change
Trade Execution Form validation, optimistic updates, position recalculation
Portfolio View Positions table, P&L calculation, allocation pie chart
Connection Status Visual indicator, automatic reconnection with backoff

API Overview

REST Endpoints

Method Endpoint Description
GET /api/prices All current metal prices
GET /api/trades Trade history
POST /api/trades Execute a trade
GET /api/positions Current holdings
GET /api/positions/summary Portfolio summary

WebSocket Events

Event Direction Description
PRICE_SNAPSHOT Server β†’ Client Initial prices on connect
PRICE_UPDATE Server β†’ Client Single price change
TRADE_EXECUTED Server β†’ Client Trade confirmation
PING/PONG Bidirectional Heartbeat

Project Structure

Metal exchange/
β”œβ”€β”€ README.md                 # This file
β”œβ”€β”€ REQUIREMENTS.md           # Domain research & requirements gathering
β”œβ”€β”€ package.json              # Root workspace config
β”œβ”€β”€ pnpm-workspace.yaml
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ shared/               # Shared types & validation
β”‚   β”‚   └── src/
β”‚   β”‚       β”œβ”€β”€ constants/    # Metal definitions
β”‚   β”‚       β”œβ”€β”€ types/        # TypeScript interfaces
β”‚   β”‚       └── schemas/      # Zod validation schemas
β”‚   β”œβ”€β”€ server/               # Node.js backend
β”‚   β”‚   └── src/
β”‚   β”‚       β”œβ”€β”€ db/           # Drizzle schema & migrations
β”‚   β”‚       β”œβ”€β”€ services/     # Business logic
β”‚   β”‚       β”œβ”€β”€ routes/       # REST endpoints
β”‚   β”‚       └── websocket/    # Real-time handler
β”‚   β”œβ”€β”€ client/               # React frontend
β”‚   β”‚   └── src/
β”‚   β”‚       β”œβ”€β”€ api/          # Axios, React Query, WebSocket
β”‚   β”‚       β”œβ”€β”€ components/   # Shared UI components
β”‚   β”‚       β”œβ”€β”€ features/     # Feature modules
β”‚   β”‚       └── layouts/      # Page layouts
β”‚   └── server-java/          # (Future) Spring Boot backend
└── docs/                     # Detailed technical docs (reference)

Development Scripts

pnpm run dev          # Start all services
pnpm run build        # Production build
pnpm run db:seed      # Seed sample data
pnpm run typecheck    # Type checking

Future Enhancements

  • Authentication (JWT)
  • Order book depth visualization
  • Historical price charts (candlestick)
  • Trade blotter with filtering
  • Java/Spring Boot backend implementation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors