From 2217cab8b19fb82840776bb2c4609d2e3a8e14bf Mon Sep 17 00:00:00 2001 From: Akash Shaw Date: Tue, 6 Aug 2024 22:38:21 +0530 Subject: [PATCH 1/4] chore: adds copy and clone trait --- crates/common/src/types/order.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/crates/common/src/types/order.rs b/crates/common/src/types/order.rs index e938d92..2dc58a3 100644 --- a/crates/common/src/types/order.rs +++ b/crates/common/src/types/order.rs @@ -1,20 +1,20 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(rename_all = "UPPERCASE")] pub enum OrderSide { SELL, BUY, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(rename_all = "UPPERCASE")] pub enum OrderType { MARKET, LIMIT, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(rename_all = "UPPERCASE")] pub enum TimeInForce { GTC, @@ -22,7 +22,7 @@ pub enum TimeInForce { FOK, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone)] #[serde(rename_all = "UPPERCASE")] pub enum OrderStatus { NEW, @@ -66,9 +66,29 @@ pub struct OrderResponse { pub fills: Vec, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct Fill { pub price: f64, pub qty: f64, pub commission: f64, + pub client_order_id: String, + pub side: OrderSide, + pub filled: f64, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Order { + pub symbol: String, + pub order_id: u64, + pub client_order_id: String, + pub transact_time: u64, + pub price: f64, + pub orig_qty: f64, + pub executed_ty: f64, + pub status: OrderStatus, + pub time_in_force: TimeInForce, + #[serde(rename = "type")] + pub type_: OrderType, + pub side: OrderSide, + pub fills: Vec, } From c28e3dde2b59a10f365e1c5fe2c1f58fd9a51a77 Mon Sep 17 00:00:00 2001 From: Akash Shaw Date: Tue, 6 Aug 2024 22:38:52 +0530 Subject: [PATCH 2/4] chore: inits a order book class --- crates/engine/src/trade/orderbook.rs | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 crates/engine/src/trade/orderbook.rs diff --git a/crates/engine/src/trade/orderbook.rs b/crates/engine/src/trade/orderbook.rs new file mode 100644 index 0000000..21e2a6f --- /dev/null +++ b/crates/engine/src/trade/orderbook.rs @@ -0,0 +1,45 @@ +use common::types::order::Order; + +pub struct Orderbook { + pub bids: Vec, + pub asks: Vec, + pub base_asset: String, + pub quote_asset: String, + pub current_price: f64, + pub last_trade_id: String, +} + +impl Orderbook { + pub fn new( + bids: &Vec, + asks: &Vec, + base_asset: &str, + quote_asset: &str, + current_price: f64, + last_trade_id: &str, + ) -> Orderbook { + Orderbook { + bids: bids.clone(), + asks: asks.clone(), + base_asset: base_asset.to_string(), + quote_asset: quote_asset.to_string(), + current_price, + last_trade_id: last_trade_id.to_string(), + } + } + + pub fn ticker(&self) -> String { + format!("{}_{}", self.base_asset, self.quote_asset) + } + + pub fn get_snapshot(&self) -> Orderbook { + Orderbook { + bids: self.bids.clone(), + asks: self.asks.clone(), + base_asset: self.base_asset.clone(), + quote_asset: self.quote_asset.clone(), + current_price: self.current_price, + last_trade_id: self.last_trade_id.clone(), + } + } +} From face330878557c742ba4027125d70ae3acbd80b4 Mon Sep 17 00:00:00 2001 From: Akash Shaw Date: Tue, 6 Aug 2024 22:39:15 +0530 Subject: [PATCH 3/4] chore: inits a engine class --- crates/engine/src/trade/engine.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 crates/engine/src/trade/engine.rs diff --git a/crates/engine/src/trade/engine.rs b/crates/engine/src/trade/engine.rs new file mode 100644 index 0000000..9ae5687 --- /dev/null +++ b/crates/engine/src/trade/engine.rs @@ -0,0 +1,15 @@ +use super::orderbook::Orderbook; +use std::collections::HashMap; + +pub struct Bs { + pub available: f64, + pub locked: f64, +} + +type UserBalance = HashMap; +type Balances = HashMap; + +pub struct Engine { + orderbook: Orderbook, + balances: Balances, +} From 5030e7059cb2613498b1ead24bc06bb4110b6907 Mon Sep 17 00:00:00 2001 From: Akash Shaw Date: Tue, 6 Aug 2024 22:39:39 +0530 Subject: [PATCH 4/4] chore: some files change --- crates/engine/src/main.rs | 3 ++- crates/engine/src/trade/mod.rs | 2 ++ crates/engine/src/types/mod.rs | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/trade/mod.rs create mode 100644 crates/engine/src/types/mod.rs diff --git a/crates/engine/src/main.rs b/crates/engine/src/main.rs index d42aeb2..1f47089 100644 --- a/crates/engine/src/main.rs +++ b/crates/engine/src/main.rs @@ -4,6 +4,7 @@ use tokio; use tokio::signal; mod process; +mod trade; use common::redis::initialize_redis_pool; use process::handle_process; @@ -17,7 +18,7 @@ async fn main() { .parse::() .expect("NO_WORKER_THREADS must be a positive integer"); - let queues = vec!["user_email_verify"]; + let queues = vec!["order"]; let pool = Arc::new(initialize_redis_pool()); diff --git a/crates/engine/src/trade/mod.rs b/crates/engine/src/trade/mod.rs new file mode 100644 index 0000000..bb34610 --- /dev/null +++ b/crates/engine/src/trade/mod.rs @@ -0,0 +1,2 @@ +pub mod engine; +pub mod orderbook; diff --git a/crates/engine/src/types/mod.rs b/crates/engine/src/types/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/engine/src/types/mod.rs @@ -0,0 +1 @@ +