How jasonlinkai/trading-server works
Step 1: Identify Project Type & Purpose
• This is a Node.js/Express-based, serverless trading microservice written in TypeScript.
• Deployed on Google Cloud Functions, it exposes HTTP API endpoints for placing orders and querying/closing positions on multiple exchanges (currently Binance and BitMEX).
• Core features: IP whitelist middleware, environment-variable configuration, a factory-based service layer to dispatch exchange-specific logic.
Step 2: Analyze File Structure & Architectural Patterns
Top-level
• Dockerfile, package.json, tsconfig.json, .env.example → build & runtime config
• request-*.json, server_log.txt → sample payloads & logs
src/
• constants/ & enums/ → shared values & env-mapping
• middleware/ipWhitelist.ts → Express middleware enforcing IP filtering
• routes/ (order.ts, position.ts) → HTTP route handlers (controllers)
• factories/TradingServiceFactory.ts → Factory pattern to instantiate the correct exchange service
• services/TradingService/… → Exchange-specific implementations (binance.ts, bitmex.ts) behind a common interface
• utils/ (logger.ts, formatting.ts) → cross-cutting concerns
Patterns
• Layered (Ingress → Middleware → Routing/Controllers → Service Factory → Service Implementation → External API)
• Factory pattern for pluggable exchange services
• Interface abstraction for service contracts
Step 3: Extract Key Insights from README
• Deployment target: Google Cloud Functions (stateless, event-driven)
• Endpoints:
POST /api/:exchange/order → place order
(similarly /position, /close-position)
• Uses .env for secrets/config
Step 4: Define Main Components & Their Relationships
Clients / Producers
• TradingView or any HTTP client → sends JSON payload to your API
API Gateway / HTTP Trigger
• Google Cloud Functions front-end
Express Application (inside Cloud Function)
a. Middleware Layer
– IP whitelist filter
b. Routing Layer
– order.ts → handles /api/:exchange/order
– position.ts → handles /api/:exchange/position
c. Controller / Handler
– Validate request, extract exchange, parameters
d. Factory Layer
– TradingServiceFactory → returns an instance of BinanceService or BitmexService depending on “exchange” path param
e. Service Layer
– Exchange-specific logic (REST calls to external exchange APIs, leverage, stop-loss, take-profit)
f. Utility Layer
– Logger → writes structured logs to Cloud Logging or local server_log.txt
– Formatting → request/response transformation
External Dependencies
• Exchange REST/WebSocket APIs (Binance, BitMEX)
• Google Cloud environment (Secret Manager for env vars, Cloud Logging)
– Ingress Zone (Clients → HTTP Trigger)
– Runtime Zone (GCF/Express) subdivided into:
• Middleware (IP Whitelist)
• Routing (Order, Position)
• Factory (TradingServiceFactory)
• Service Implementations (BinanceService, BitmexService)
• Utilities (Logger, Formatter)
– External Zone (Binance API, BitMEX API, Cloud Secret Manager, Cloud Logging)
– Trigger → Middleware → Route Handler → Factory → Service → External API
– External API → Service → Handler → Response → Client
– Handler/Service → Logger → Cloud Logging
c) Patterns
– Group “Factory + Service Implementations” and annotate “Factory Pattern”
– Show interface boundary between route handler and exchange service
d) Technologies
– Indicate “Google Cloud Functions” as hosting platform
– Mark “.env / Secret Manager” as config store
Step 6: Tailoring to This Project
• Clearly that there is no persistent in-process state—each request cold/warm starts the same Express app
• Pluggable service layer (easy to add new exchanges)
Step 7: Diagramming Guidelines