Skip to content

A real-time order execution engine that processes order submissions, routes them dynamically based on pricing rules, and streams execution updates to clients through a WebSocket connection.

Notifications You must be signed in to change notification settings

AdityaRaj-007/EternaLabs-Assignment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

28 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Order Execution Engine

A high-performance backend system for executing Solana DEX Market Orders with intelligent routing, concurrent background workers, real-time WebSocket updates, and an extensible job-based architecture.

This project demonstrates concurrency, queues, workers,WebSockets, and Mock DEX price routing.

๐Ÿง  Key Design Decisions

1. Market-Order-First Architecture

Market orders were implemented first because they:

  • Require instant execution

  • Stress-test latency, routing, and concurrency

  • Exercise the full pipeline:

    API โ†’ Queue โ†’ Worker โ†’ Router โ†’ DB โ†’ Pub/Sub โ†’ WebSocket
    
  • Showcase real-time updating via Fastify WebSocket + Redis Pub/Sub

This forms a strong foundation for future order types.


2. Queue-Driven, Extensible Order System

Built on BullMQ, enabling new order types without refactoring core logic.

Future extensibility:

โœ” Limit Orders

A Price Watcher service can enqueue jobs once the target price is hit.

โœ” Sniper Orders

A Mempool Listener can enqueue jobs during liquidity spikes.

The core pipeline remains the same:

Routing โ†’ Execution โ†’ Database Updates โ†’ WebSocket Events

๐Ÿ— Architecture Overview

graph LR
    Client[Client] -- POST /execute --> API[Fastify API]
    Client -- WebSocket --> WS[WS Gateway]

    API -- Queue job --> Queue[BullMQ]

    Queue -- Process --> Worker[Background Worker]
    Worker -- Fetch Quotes --> Router[DEX Mock Router]
    Worker -- Execute Swap --> SolanaMock[Solana Mock Execution]

    Worker -- Update --> DB
    Worker -- Publish --> RedisPub[Redis Pub/Sub]
    RedisPub -- Push --> WS
    WS -- Notify --> Client
Loading

๐Ÿ“ Folder Structure

drizzle/                         # Database migrations
src/
โ”œโ”€โ”€ db/                          # Drizzle ORM schemas
โ”œโ”€โ”€ services/                    # Queue setup
โ”œโ”€โ”€ test/                        # Automated tests + load tests
โ”œโ”€โ”€ utils/                       # Redis setup
|   โ””โ”€โ”€ redisConnection.ts
โ”‚โ”€โ”€ dex-mock-router.ts          # Simulates Raydium/Meteora best routing
โ”‚โ”€โ”€ server.ts                   # Fastify HTTP + WebSocket server
โ”‚โ”€โ”€ types.ts                    # Interface, enums, validations
โ”‚โ”€โ”€ worker.ts                   # BullMQ order execution worker
โ”œโ”€โ”€ .env                         # Environment variables
โ”œโ”€โ”€ drizzle.config.ts
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ””โ”€โ”€ readme.md

๐Ÿ›  Tech Stack

Component Tech
Runtime Node.js + TypeScript
API Framework Fastify
Queue System BullMQ
Messaging Redis Pub/Sub
Database PostgreSQL + Drizzle ORM

โš™ Environment Setup

Create a .env file:

PORT=3000
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/eterna_orders"
REDIS_HOST="localhost"
REDIS_PORT=6379
HOST="localhost"
ENDPOINT=/api/orders/execute

๐Ÿงช Useful Scripts

npm run dev            # Start API (watch mode)
npm run worker         # Start BullMQ worker
npm run db:push        # Apply Drizzle migrations
npm run build          # Compile TS โ†’ JS
npm start              # Start prod server

๐Ÿš€ Running the Project

1๏ธโƒฃ Install dependencies

npm install

2๏ธโƒฃ Start Redis + Postgres

npm run db:push

4๏ธโƒฃ Start API + Worker

npm run start

5๏ธโƒฃ Test order execution

Single order:

npx ts-node src/test/client-test.ts

Concurrent load test:

npx ts-node src/test/concurrent-load-test.ts

๐Ÿ“ก API Reference

POST /api/orders/execute

Submit a market order.

Example Request

{
  "inputToken": "SOL",
  "outputToken": "USDC",
  "amount": 10
}

Example Response

{
  "orderId": "uuid",
  "message": "Order placed successfully!"
}

๐Ÿ”Œ WebSocket Live Updates

Connect to the same endpoint:

GET /api/orders/execute?orderId=<id>

Event Stream:

{ "event": "pending" }
{ "status": "routing" }
{ "status": "building" }
{ "status": "confirmed" }
{ "status": "failed" }

โšก Features

โœ” High Concurrency

Worker runs with 10 concurrent jobs.

โœ” Automatic Retries

3 retries with exponential backoff.

โœ” Intelligent DEX Router

Simulates:

  • Raydium
  • Meteora
  • Best-price selection

๐Ÿ”ฎ Future Improvements

  • Real Solana DEX integration (Raydium/Meteora)
  • Limit order engine
  • Sniper orders via mempool monitoring

About

A real-time order execution engine that processes order submissions, routes them dynamically based on pricing rules, and streams execution updates to clients through a WebSocket connection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published