diff --git a/README.md b/README.md index ec62d9a..5095100 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Current version: - [ ] more tests - [ ] profiling >> perf + flamegraph / tokio-console/TSan - [ ] performance test >> criterion/wrk(http) -- [ ] add all to Github Actions (CI/CD) +- [x] add all to Github Actions (CI/CD) ## Configuration diff --git a/src/models/model.rs b/src/models/model.rs index 81f4654..0a44b6a 100644 --- a/src/models/model.rs +++ b/src/models/model.rs @@ -1,29 +1,10 @@ pub mod models { - use std::collections::BTreeMap; - use crate::OrderedFloat; - use std::fmt; - use colored::Colorize; - use uuid::Uuid; - use tonic::{ Request, Response, Status }; use serde::Deserialize; pub mod orderbook { tonic::include_proto!("orderbook"); } - use std::sync::Arc; - use tokio::sync::{ Mutex, mpsc }; - - use orderbook::order_book_server::OrderBook; - use orderbook::{ - OrderBookRequest, - OrderBookResponse, - OrderRequest, - OrderResponse, - TradeBookRequest, - TradeBookResponse, - }; - #[derive(Debug, Deserialize)] pub struct Config { pub kraken: KrakenConfig, @@ -35,128 +16,4 @@ pub mod models { pub persist: String, pub offline: Vec, } - - // For Orderbook - #[derive(Debug, Clone, PartialEq, Eq)] - pub struct Order { - pub id: Uuid, - pub price: OrderedFloat, - pub volume: OrderedFloat, - pub side: String, - pub timestamp: String, - pub order_type: String, - } - - //For Tradebook - #[derive(Debug, Clone, PartialEq, Eq)] - pub struct Trade { - pub id: Uuid, - pub trader: String, - pub pair: String, - pub price: OrderedFloat, - pub volume: OrderedFloat, - pub side: String, - pub timestamp: String, - pub order_type: String, - pub status: String, - } - - #[derive(Debug)] - pub struct OrderBookService { - pub order_books: Arc>>>, - pub order_tx: mpsc::Sender, - pub trade_books: Arc>>>, - } - - // Implement the OrderBook trait for OrderBookService to handle gRPC requests (core) - #[tonic::async_trait] - impl OrderBook for Arc { - async fn get_order_book( - &self, - request: Request - ) -> Result, Status> { - let pair: String = request.into_inner().pair; - let order_books: tokio::sync::MutexGuard< - BTreeMap> - > = self.order_books.lock().await; - if let Some(orders) = order_books.get(&pair) { - Ok( - Response::new(OrderBookResponse { - orders: orders - .iter() - .map(|o: &Order| orderbook::Order { - price: o.price.into_inner(), - volume: o.volume.into_inner(), - }) - .collect(), - }) - ) - } else { - Err(Status::not_found("Order book not found")) - } - } - - async fn place_market_order( - &self, - request: Request - ) -> Result, Status> { - let market_order: OrderRequest = request.into_inner(); - if let Err(_) = self.order_tx.send(market_order).await { - return Err(Status::internal("Failed to process order")); - } - Ok( - Response::new(OrderResponse { - status: "new".into(), - message: "order registerted and is being processed".into(), - }) - ) - } - - async fn get_trade_book( - &self, - request: Request - ) -> Result, Status> { - let trader: String = request.into_inner().trader; - let trade_books: tokio::sync::MutexGuard< - BTreeMap> - > = self.trade_books.lock().await; - if let Some(trades) = trade_books.get(&trader) { - Ok( - Response::new(TradeBookResponse { - trades: trades - .iter() - .map(|t: &Trade| orderbook::Trade { - id: t.id.to_string(), - trader: t.trader.clone(), - order_type: t.order_type.clone(), - pair: t.pair.clone(), - side: t.side.clone(), - price: t.price.into_inner(), - volume: t.volume.into_inner(), - timestamp: t.timestamp.clone(), - status: t.status.clone(), - }) - .collect(), - }) - ) - } else { - Err(Status::not_found("Trade book not found")) - } - } - } - - // Implement the Display trait for the Order struct (more readable output with colors) - impl core::fmt::Display for Order { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let output: String = format!( - "Price: {:.5}, Volume: {:.3}, Side: {}, ID: {}, Timestamp: {}", - self.price, self.volume, self.side, self.id, self.timestamp - ); - if self.side == "ask" { - write!(f, "{}", output.red()) - } else { - write!(f, "{}", output.green()) - } - } - } } \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index e32de2f..178c853 100644 --- a/src/server.rs +++ b/src/server.rs @@ -56,19 +56,6 @@ pub struct Order { order_type: String, } -// impl Default for Order { -// fn default() -> Self { -// Order { -// id: Uuid::nil(), // Default UUID (nil UUID) -// price: OrderedFloat(0.0), // Default price -// volume: OrderedFloat(0.0), // Default volume -// side: String::from("unknown"), // Default side -// timestamp: String::from("1970-01-01T00:00:00Z"), // Default timestamp -// order_type: String::from("unknown"), // Default order type -// } -// } -// } - //For Tradebook #[derive(Debug, Clone, PartialEq, Eq)] pub struct Trade {