A backend engine that processes market orders, routes them to the best Decentralized Exchange (DEX) based on price, and streams real-time updates via WebSockets.
- Live Demo URL: []
- Demo Video: []
I chose Market Orders to prioritize speed of execution and immediate liquidity, which is critical for a routing engine demonstration. To extend this system for Limit or Sniper orders, the worker logic would be modified to check a target price or launch time trigger before proceeding to the "Routing" phase.
- WebSockets over HTTP: The system uses a single WebSocket connection for both order submission and status updates. This reduces network overhead and ensures the client never misses an update due to connection latency.
- Queue-Based Concurrency: BullMQ is used to decouple the API from the execution logic. This ensures the server remains responsive even under high load, processing orders at a controlled rate.
- Redis Pub/Sub: A dedicated Redis channel is created for each order ID. This allows the worker to publish updates asynchronously without blocking the main thread.
- Runtime: Node.js, TypeScript
- Framework: Fastify (Chosen for high-performance WebSocket support)
- Database: PostgreSQL (Persistence), Redis (Queue & Real-time Pub/Sub)
- ORM: Prisma
- Queue: BullMQ
-
Prerequisites Ensure Docker and Node.js are installed.
-
Start Infrastructure Run the following command to start PostgreSQL and Redis containers:
docker-compose up -d
-
Install Dependencies
npm install
-
Initialize Database Generate the Prisma client and push the schema to the database:
npx prisma generate npx prisma migrate dev --name init
-
Run the Server
npm run dev
The server will start at
http://localhost:3000.
URL: ws://localhost:3000/api/orders/connect
Request Payload: Send this JSON immediately after connecting:
{
"action": "execute_order",
"tokenIn": "SOL",
"tokenOut": "USDC",
"amountIn": 5
}Response Stream:
- Pending: Order received and queued.
- Routing: Comparing prices between Raydium and Meteora.
- Building: Constructing the transaction.
- Submitted: Transaction sent to the network.
- Confirmed: Transaction successful (includes TX Hash).
Run the unit and integration test suite (covers routing logic and queue behavior):
npm test