Skip to content

[Claude] Add unit tests and REST API server#1

Draft
jbowen93 wants to merge 3 commits intomainfrom
jbowen93/claude
Draft

[Claude] Add unit tests and REST API server#1
jbowen93 wants to merge 3 commits intomainfrom
jbowen93/claude

Conversation

@jbowen93
Copy link
Copy Markdown
Member

@jbowen93 jbowen93 commented May 28, 2025

NOTE: ALL VIBE CODED

Added unit tests and REST API server

🚀 Server Features

  • Framework: Built with Axum (modern Rust async web framework)
  • Port: Runs on http://127.0.0.1:3000
  • CORS: Enabled for web client access
  • JSON API: Full JSON request/response handling

📡 API Endpoints

  1. GET /health - Health check
  2. GET /orderbook - Get current bid/ask prices
  3. POST /orders - Create new orders (limit, market, post_only, fill_or_kill)
  4. DELETE /orders/:side/:order_id - Cancel orders
  5. GET /orders/:order_id - Check order existence (placeholder)

🧪 Tested Functionality

  • ✅ Creating limit orders (bid/ask)
  • ✅ Order book state tracking
  • ✅ Market order execution and matching
  • ✅ Price-time priority matching
  • ✅ Transaction logging

📁 Files Created

  • src/bin/server.rs - REST API server binary
  • examples/api_demo.sh - Demo script with example API calls
  • API.md - Complete API documentation

🏃‍♂️ How to Run

Start the server

cargo run --bin server

Test with the demo script

./examples/api_demo.sh

Or test manually

curl -X POST http://127.0.0.1:3000/orders
-H "Content-Type: application/json"
-d '{"order_type": "limit", "side": "Bid", "price": 5000, "quantity": 100}'

The API successfully demonstrates:

  • Order Creation: Different order types with proper validation
  • Order Matching: Market orders executing against limit orders
  • State Management: Real-time order book updates
  • Transaction Logging: Complete audit trail of all operations

You now have a fully functional order book REST API that you can interact with locally!

jbowen93 added 2 commits May 28, 2025 14:00
  🚀 Server Features

  - Framework: Built with Axum (modern Rust async web framework)
  - Port: Runs on http://127.0.0.1:3000
  - CORS: Enabled for web client access
  - JSON API: Full JSON request/response handling

  📡 API Endpoints

  1. GET /health - Health check
  2. GET /orderbook - Get current bid/ask prices
  3. POST /orders - Create new orders (limit, market, post_only, fill_or_kill)
  4. DELETE /orders/:side/:order_id - Cancel orders
  5. GET /orders/:order_id - Check order existence (placeholder)

  🧪 Tested Functionality

  - ✅ Creating limit orders (bid/ask)
  - ✅ Order book state tracking
  - ✅ Market order execution and matching
  - ✅ Price-time priority matching
  - ✅ Transaction logging

  📁 Files Created

  - src/bin/server.rs - REST API server binary
  - examples/api_demo.sh - Demo script with example API calls
  - API.md - Complete API documentation

  🏃‍♂️ How to Run

  # Start the server
  cargo run --bin server

  # Test with the demo script
  ./examples/api_demo.sh

  # Or test manually
  curl -X POST http://127.0.0.1:3000/orders \
    -H "Content-Type: application/json" \
    -d '{"order_type": "limit", "side": "Bid", "price": 5000, "quantity": 100}'

  The API successfully demonstrates:
  - Order Creation: Different order types with proper validation
  - Order Matching: Market orders executing against limit orders
  - State Management: Real-time order book updates
  - Transaction Logging: Complete audit trail of all operations

  You now have a fully functional order book REST API that you can interact with locally!
@jbowen93 jbowen93 changed the title claude created tests [Claude] Add unit tests and REST API server May 29, 2025
🆕 New Endpoint

  - GET /orderbook/orders - Retrieve all orders currently in the book

  📊 Response Structure

  The endpoint returns orders organized by:
  - Side: Separate arrays for bids and asks
  - Price Levels: Each price level contains all orders at that price
  - Time Priority: Orders within each level are ordered FIFO (first in, first out)

  🔍 Example Response

  {
    "bids": [
      {
        "price": 5000,
        "orders": [
          {
            "id": "495b672f-e5ff-4d91-8b06-6646e8768263",
            "type_": "Limit",
            "time_in_force": "GoodTillCanceled",
            "side": "Bid",
            "quantity": 100,
            "price": 5000,
            "stop_price": 0,
            "slippage": null,
            "post_only": false
          },
          {
            "id": "ff98abd6-e4c5-46fa-8556-6022da6f772a",
            "type_": "Limit",
            "time_in_force": "GoodTillCanceled",
            "side": "Bid",
            "quantity": 30,
            "price": 5000,
            "stop_price": 0,
            "slippage": null,
            "post_only": false
          }
        ]
      }
    ],
    "asks": [...]
  }

  🏗️ Implementation Details

  - Added orders() method to Level struct to retrieve all orders at a price level
  - Added all_orders() method to Half struct to get orders organized by price
  - Added all_orders() method to Book struct returning (bids, asks) tuple
  - Added all_orders() method to MatchEngine for API exposure
  - Created new response types: PriceLevel and OrderBookOrders

  ✅ Tested Features

  - ✅ Empty order book returns empty arrays
  - ✅ Multiple orders at same price level show correct FIFO ordering
  - ✅ Price levels are correctly ordered (bids descending, asks ascending)
  - ✅ All order details are properly serialized

  📚 Updated Documentation

  - Updated API.md with new endpoint documentation
  - Updated demo script to showcase the new endpoint
  - Added usage examples and response format details

  This new endpoint provides complete visibility into the order book state, showing not just the best bid/ask prices
  but all pending orders with their full details, organized by price-time priority exactly as they exist in the
  matching engine! 🎯
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant