Bunex is a high-performance, modular crypto exchange project developed in TypeScript. Built with Bun, a fast JavaScript runtime, Bunex is designed to handle large volumes of orders across multiple trading pairs efficiently. Each trading pair's order book operates on an independent TCP server, allowing for a scalable, event-driven architecture.
In this project, a Red-Black Tree (RBT) data structure is used within the OrderBook
to efficiently store, retrieve, and manage orders. Here's why the RBT is advantageous in this setup:
-
Self-Balancing Properties: The Red-Black Tree balances itself automatically with every insertion or deletion, maintaining an
O(log n)
search time for order lookups and modifications. This is critical for efficient order matching in high-frequency trading scenarios. -
Efficient Price-Level Access: Orders are sorted by price within the tree, allowing for quick access to the highest buy orders and lowest sell orders. This makes it easier to retrieve the best price level on each side of the order book, a crucial step in order matching.
-
Flexible Insertions and Deletions: With every new order, the Red-Black Tree ensures the proper positioning in the tree structure, dynamically adjusting and maintaining performance with every buy or sell.
- Buy Orders (Bid Side): Each price level is stored as a node in the Red-Black Tree, and within each node, individual orders are managed as a list.
- Sell Orders (Ask Side): Similarly, sell orders are managed in a separate Red-Black Tree for quick access to the lowest available price.
The Red-Black Tree structure enables the OrderBook
to handle large volumes of data while ensuring efficient matching, updating, and retrieval of orders.
- Multi-pair Trading: Support for various trading pairs, with each pair operating on its own server.
- TCP Order Book: Orders are placed over TCP connections, enabling real-time communication.
- Modular Structure: Each order book instance can be run independently on a unique port.
- Event-Driven Architecture: Events manage order placement, matching, and state updates for scalability.
- TypeScript: Full type safety and modern JavaScript features for maintainability and reliability.
- Bun: Ensure you have Bun v1.1.30 or higher installed. Install it here.
git clone https://github.com/your-username/bunex.git cd bunex
Install project dependencies using Bun:
bun install
To start the primary Bunex server:
bun run index.ts
Each trading pair has its own order book server. To start an order book server, specify the port and trading pair:
// Example in code (for `BTC/USDT`pair on port`3000`)
startOrderBookServer(3000, 'BTC', 'USDT');
You can run additional trading pairs on separate ports by calling startOrderBookServer
with different parameters.
index.ts
: Main entry point for starting the Bunex server.models/
: Contains core models, includingOrder
,OrderBook
,Pair
, andAsset
.__tests__/
: Contains tests for core functionality.utils/
: Utility functions for parsing, encoding messages, etc.
You can manage ports and trading pairs in a configuration file for easier deployment:
// config.json
{
"pairs": [
{ "pair": "BTC/USDT", "port": 3000 },
{ "pair": "ETH/USDT", "port": 3001 }
]
}`
Load this configuration in index.ts
to initialize multiple servers dynamically.
-
Place an Order: Connect to a specific trading pair's TCP server (e.g.,
localhost:3000
for BTC/USDT) and send a formatted order string.telnet localhost 3000
Send a message like:
BUY 0.5 BTC @ 20000 USDT
-
Monitor Order Book: The server returns responses to the client. Use tools like
telnet
ornc
to interact with each server.
To run tests:
bun test
- Bun - JavaScript runtime for fast builds and efficient server-side applications.
- TypeScript - Statically typed JavaScript for maintainable code.
This project is licensed under the MIT License.
- Bun for their high-performance runtime environment.
- Community resources on event-driven architecture in TypeScript and crypto exchanges.