From a4e8bf1643fa67982cef62179c99623f095b883e Mon Sep 17 00:00:00 2001 From: Teppy Date: Tue, 30 Jul 2024 15:27:25 -0400 Subject: [PATCH] changes --- src/main.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 37507a2..eccf2f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -385,7 +385,7 @@ impl Market { } } } - fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> Result { + fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum, execute_if_possible:bool) -> Result { let mut log:Vec=Vec::new(); let initial_balance=self.traders[owner].get_balance(sell_type); let asset=self.number_to_asset(sell_type); @@ -401,7 +401,7 @@ impl Market { let mut royalty_acc=FiNum::zero(); let mut commission_acc=FiNum::zero(); let new_order_id:usize; - while buy_qty>FiNum::new(0) && self.orders.contains_key(&ap) && self.orders.get(&ap).unwrap().v.len()>0 { + while execute_if_possible && buy_qty>FiNum::new(0) && self.orders.contains_key(&ap) && self.orders.get(&ap).unwrap().v.len()>0 { let mut elt=(*(self.orders.get(&ap).unwrap().v[0].borrow())).clone(); if sell_qty/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate let qty=std::cmp::min(elt.sell_remain,buy_qty); @@ -572,7 +572,7 @@ impl Market { sell_qty=buy_qty*rng.gen_range(60..=70).into(); } let mut _success=false; - if let Result::Ok=self.make_order(seller,sell_type,buy_type,sell_qty,buy_qty) { count+=1; _success=true; }; + if let Result::Ok=self.make_order(seller,sell_type,buy_type,sell_qty,buy_qty,true) { count+=1; _success=true; }; if count>=1000000 { break; } tries+=1; } @@ -665,6 +665,7 @@ enum Command { AddFunds { user_id: usize, asset_id: usize, amt: FiNum }, SubFunds { user_id: usize, asset_id: usize, amt: FiNum }, Order { user_id: usize, sell_type: usize, sell_qty: FiNum, buy_type: usize, buy_qty: FiNum }, + OrderBatch { user_id: usize, sell_type: usize, sell_qty: FiNum, buy_type: usize, buy_qty: FiNum }, Retract { order_id: usize }, Error(String), NOP(String), @@ -686,6 +687,8 @@ impl Command { => format!("SF {} {} {}",user_id,asset_id,amt.serialize()), Self::Order { user_id, sell_type, sell_qty, buy_type, buy_qty } => format!("OR {} {} {} {} {}",user_id,sell_type,sell_qty.serialize(),buy_type,buy_qty.serialize()), + Self::OrderBatch { user_id, sell_type, sell_qty, buy_type, buy_qty } + => format!("ORB {} {} {} {} {}",user_id,sell_type,sell_qty.serialize(),buy_type,buy_qty.serialize()), Self::Retract { order_id } => format!("RE {}",order_id), Self::Error(str) => format!("NOP Error: {}",str), Self::NOP(str) => format!("NOP {}",clean(str)), @@ -705,7 +708,10 @@ impl Command { ["SF",user_id,asset_id,amt] => Self::SubFunds { user_id: user_id.parse::().unwrap(), asset_id: asset_id.parse::().unwrap(), amt: FiNum::new_deserialize(amt) }, ["OR",user_id,sell_type,sell_qty,buy_type,buy_qty] => - Self::Order { user_id: user_id.parse::().unwrap(), sell_type: sell_type.parse::().unwrap(), sell_qty: FiNum::new_deserialize(sell_qty), + Self::Order { user_id: user_id.parse::().unwrap(), sell_type: sell_type.parse::().unwrap(), sell_qty: FiNum::new_deserialize(sell_qty), + buy_type: buy_type.parse::().unwrap(), buy_qty: FiNum::new_deserialize(buy_qty) }, + ["ORB",user_id,sell_type,sell_qty,buy_type,buy_qty] => + Self::OrderBatch { user_id: user_id.parse::().unwrap(), sell_type: sell_type.parse::().unwrap(), sell_qty: FiNum::new_deserialize(sell_qty), buy_type: buy_type.parse::().unwrap(), buy_qty: FiNum::new_deserialize(buy_qty) }, ["NOP", many_things @ ..] => Self::NOP(clean(&line)), _ => Self::Error("Unimplemented Parse".to_string()), @@ -765,7 +771,10 @@ impl Market { } } Command::Order { user_id, sell_type, sell_qty, buy_type, buy_qty } => { - self.make_order(*user_id,*sell_type,*buy_type,*sell_qty,*buy_qty) + self.make_order(*user_id,*sell_type,*buy_type,*sell_qty,*buy_qty,true) + } + Command::OrderBatch { user_id, sell_type, sell_qty, buy_type, buy_qty } => { + self.make_order(*user_id,*sell_type,*buy_type,*sell_qty,*buy_qty,false) } Command::Error(str) => Result::Error(format!("Command Error")), Command::NOP(str) => Result::Ok, @@ -841,6 +850,17 @@ fn tokens_to_command(m: &Market, logged_in: usize, tokens: Vec<&str>,line: &str) else if qty1.is_zero() { Command::Error("Qty1 is must be > 0".to_string()) } else { Command::Order { user_id: logged_in, sell_type: *cur0.unwrap(), sell_qty: qty0, buy_type: *cur1.unwrap(), buy_qty: qty1 } } } + ["orderbatch", qty0, cur0, qty1, cur1 ] => { + let qty0=FiNum::new_str(qty0); + let cur0=m.name_to_number(cur0); + let qty1=FiNum::new_str(qty1); + let cur1=m.name_to_number(cur1); + if !cur0.is_some() { Command::Error("Count not find currency".to_string()) } + else if !cur1.is_some() { Command::Error("Count not find currency".to_string()) } + else if qty0.is_zero() { Command::Error("Qty0 is must be > 0".to_string()) } + else if qty1.is_zero() { Command::Error("Qty1 is must be > 0".to_string()) } + else { Command::Orderbatch { user_id: logged_in, sell_type: *cur0.unwrap(), sell_qty: qty0, buy_type: *cur1.unwrap(), buy_qty: qty1 } } + } _ => { Command::Error(line.to_string()) }, }; cmd