How swayamsahu/trading-engine-backend works
Project type and purpose
• This is a standalone backend service (a command-line trading engine) written in TypeScript that reads a JSON file of orders for the BTC/USDC pair, matches buy and sell orders, and writes out an order book, trades log, and structured logs.
• It’s not a full-stack web app but rather a modular compute engine with clear input (orders.json), processing components, and output files (orderbook.json, trades.json, trading.log).
File structure
• Top level:
• package.json, tsconfig.json, jest.config.js, .gitignore – build/test/config files
• orders.json – input data
• src/ folder:
• config.ts – central constants (file paths, trading pair)
• engine.ts – orchestration entry point
• fileIO.ts – reading orders.json, writing outputs
• validator.ts – input validation logic
• orderProcessor.ts – matching engine, order-book in memory, trade generation
• logger.ts – structured logging to file/console
• types.ts – shared interfaces/enums
• tests/ folder: unit tests for engine, validator, orderProcessor
README for architectural insights
• The README explicitly calls out five modules: TradingEngine (engine.ts), OrderProcessor, FileIO, Logger, OrderValidator, plus Config and Types as supporting components.
• It highlights single-responsibility design, modularity, testability, extensibility.
Main components and their roles
a. Input/Output layer
• FileIO Module
• orders.json (input)
• orderbook.json & trades.json (output)
• trading.log (structured logs)
b. Core processing
• Engine (engine.ts): orchestrates overall workflow
• Validator (validator.ts): rejects or passes orders
• OrderProcessor (orderProcessor.ts): maintains in-memory order book, performs matching, emits trade records
c. Supporting modules
• Logger (logger.ts): writes INFO/WARN/ERROR messages
• Config (config.ts): file paths, constants
• Types (types.ts): TypeScript interfaces and enums
d. Testing & Tooling
• Jest tests under tests/
• ESLint (via package.json scripts)
• TypeScript compiler (tsconfig.json)
Relationships and data-flow
1) Engine starts → reads config → invokes FileIO to read orders.json
2) For each order:
→ pass to Validator → if valid, forward to OrderProcessor → update in-memory book & generate trade(s)
→ log each step via Logger
3) After all orders processed → Engine calls FileIO to write orderbook.json & trades.json
4) Throughout, Logger writes to trading.log & console
Architectural patterns and design principles
• Single Responsibility: every module does one thing
• Layered architecture: I/O layer, validation layer, business logic layer, logging
• In-memory data store for order book, persisted only at end of run
• Testable, modular code