Skip to content

Commit

Permalink
Merge pull request #38 from 8shaws/trade_engin
Browse files Browse the repository at this point in the history
chore: init orderbook
  • Loading branch information
shawakash authored Aug 6, 2024
2 parents 350350f + 5030e70 commit 1b332e9
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
30 changes: 25 additions & 5 deletions crates/common/src/types/order.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
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,
IOC,
FOK,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderStatus {
NEW,
Expand Down Expand Up @@ -66,9 +66,29 @@ pub struct OrderResponse {
pub fills: Vec<Fill>,
}

#[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<Fill>,
}
3 changes: 2 additions & 1 deletion crates/engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tokio;
use tokio::signal;

mod process;
mod trade;

use common::redis::initialize_redis_pool;
use process::handle_process;
Expand All @@ -17,7 +18,7 @@ async fn main() {
.parse::<usize>()
.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());

Expand Down
15 changes: 15 additions & 0 deletions crates/engine/src/trade/engine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::orderbook::Orderbook;
use std::collections::HashMap;

pub struct Bs {
pub available: f64,
pub locked: f64,
}

type UserBalance = HashMap<String, Bs>;
type Balances = HashMap<String, UserBalance>;

pub struct Engine {
orderbook: Orderbook,
balances: Balances,
}
2 changes: 2 additions & 0 deletions crates/engine/src/trade/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod engine;
pub mod orderbook;
45 changes: 45 additions & 0 deletions crates/engine/src/trade/orderbook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use common::types::order::Order;

pub struct Orderbook {
pub bids: Vec<Order>,
pub asks: Vec<Order>,
pub base_asset: String,
pub quote_asset: String,
pub current_price: f64,
pub last_trade_id: String,
}

impl Orderbook {
pub fn new(
bids: &Vec<Order>,
asks: &Vec<Order>,
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(),
}
}
}
1 change: 1 addition & 0 deletions crates/engine/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 1b332e9

Please sign in to comment.