A modular order execution engine for Solana DEX trading with DEX routing, WebSocket status updates, and queue management.
This system processes MARKET orders (extensible to LIMIT and SNIPER orders) with intelligent DEX routing between Raydium and Meteora. It features:
- HTTP β WebSocket Pattern: Single endpoint for order submission and live status updates
- DEX Router: Automatically selects best execution venue (Raydium or Meteora)
- Queue Management: Handles up to 10 concurrent orders, 100 orders/minute
- Retry Logic: Exponential backoff with up to 3 attempts
- Modular Design: Easy to switch from mock to real Solana devnet execution
MARKET orders were chosen as the initial implementation because:
- They represent the most common order type in DEX trading
- Immediate execution simplifies the initial architecture
- Provides a solid foundation for extending to LIMIT and SNIPER orders
Extending to Other Order Types:
- LIMIT Orders: Add price monitoring service that checks if limit price is reached before execution
- SNIPER Orders: Add token launch detection service that monitors new token deployments
The same execution engine can handle all three types - only the trigger mechanism differs.
- Node.js 18+
- Docker & Docker Compose (for PostgreSQL and Redis)
- npm or yarn
- Clone and install dependencies:
git clone [<repository-url>](https://github.com/baptonic3/market-order-execution-engine.git)
cd market-order-execution-engine
npm install- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration- Start dependencies (PostgreSQL & Redis):
docker-compose up -d- Run database migrations:
The database schema is automatically created on first startup.
- Start the server:
npm run devThe server will start on http://localhost:3000
Submit a new market order.
Example using curl:
curl -X POST http://localhost:3000/api/orders/execute \
-H "Content-Type: application/json" \
-d '{
"userId": "user-123",
"type": "market",
"tokenIn": "SOL",
"tokenOut": "USDC",
"amountIn": 1.5,
"slippageTolerance": 0.01
}'Request Body:
{
"userId": "user-123",
"type": "market",
"tokenIn": "SOL",
"tokenOut": "USDC",
"amountIn": 1.5,
"slippageTolerance": 0.01
}Response:
{
"orderId": "550e8400-e29b-41d4-a716-446655440000",
"status": "pending",
"message": "Order submitted successfully"
}Connection using JavaScript:
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:3000/api/orders/ORDER_ID/status');
ws.on('open', () => {
console.log('Connected to order status stream');
});
ws.on('message', (data) => {
const update = JSON.parse(data);
console.log('Status update:', update);
// {
// "orderId": "...",
// "status": "routing",
// "message": "Comparing DEX prices",
// "dexProvider": "raydium",
// "txHash": "...",
// "executedPrice": 100.5,
// "error": null
// }
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});Status Flow:
pendingβ Order received and queuedroutingβ Comparing DEX pricesbuildingβ Creating transactionsubmittedβ Transaction sent to networkconfirmedβ Transaction successful (includestxHash)failedβ Order execution failed (includeserror)
Get order details by ID.
Get all orders for a user.
Get queue statistics (waiting, active, completed, failed).
Health check endpoint.
Run the test suite:
# Run all tests
npm test
# Run with coverage
npm run test:coverage
# Watch mode
npm run test:watchThe test suite includes:
- Unit tests for DEX router, order service, and queue service
- Integration tests for complete order flow
- Route tests for API endpoints
Test Coverage: β₯10 tests covering routing logic, queue behavior, and WebSocket lifecycle.
βββ src/
β βββ config/ # Configuration
β βββ db/ # Database connection & migrations
β βββ models/ # Data models
β βββ routes/ # API routes
β βββ services/ # Business logic
β β βββ dex/ # DEX router (mock & real)
β βββ types/ # TypeScript types
β βββ utils/ # Utilities
β βββ index.ts # Application entry point
βββ tests/ # Test suite
βββ docker-compose.yml # Local development dependencies
βββ package.json
βββ README.md
Key configuration options in .env:
MAX_CONCURRENT_ORDERS: Maximum concurrent order processing (default: 10)ORDERS_PER_MINUTE: Rate limit for order processing (default: 100)MAX_RETRY_ATTEMPTS: Maximum retry attempts (default: 3)USE_MOCK_DEX: Use mock DEX implementation (default: true)
Build and run with Docker:
docker-compose up -dThis starts PostgreSQL and Redis. The application can be deployed to any Node.js hosting platform.
- Queue statistics:
GET /api/orders/queue/stats - Order history: Query PostgreSQL
orderstable - Logs: Check application logs for routing decisions and errors
Import postman_collection.json into Postman for easy API testing.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
Note: This is a mock implementation. For production use with Solana devnet, implement the RealDexRouter.
