diff --git a/src/main.rs b/src/main.rs index 60ac043..95f4914 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,7 @@ use std::cell::RefCell; use rand::prelude::*; use rand::rngs::StdRng; use hashbrown::HashMap; +use std::collections::HashSet; mod finum; use finum::FiNum; @@ -60,6 +61,7 @@ struct Trader { password: String, // hash(name..salt..password) id: usize, balances: HashMap, // Maps Currency to Amount + orders: HashSet; } #[derive(Clone)] @@ -89,7 +91,8 @@ impl Trader { name: String::from(name), password: String::from(""), id: id, - balances: HashMap::new() + balances: HashMap::new(), + orders: HashSet::new(), } } fn add_balance(&mut self, cur:usize, delta:FiNum) { @@ -111,8 +114,6 @@ struct Order { royalty_remain: FiNum, // Based on royalty1 commission_remain: FiNum, // Based on commission1 owner: usize, - rt_loc: usize, // Location in the Royalty Tree - pq_loc: usize, // Location in th Priority Queue id: usize, } @@ -364,13 +365,12 @@ impl Market { let mut sorted=pq.v.clone(); sorted.sort_by(|a,b| { (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)) }); for off in sorted.iter() { - println!(" {} @ {} ({}) OrderID: {} PQLoc: {} RTLoc: {} Royalties: {}", + println!(" {} @ {} ({}) OrderID: {} Royalties: {}", off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty, (off.buy_qty/off.sell_qty).fmt_recip(), self.traders[off.owner as usize].name, - off.id,off.pq_loc,off.rt_loc, + off.id, self.royalties.get_mut(&ap.1).unwrap().get_royalty(off.rt_loc)); - //,self.royalties.get(&ap.1).unwrap().tree[off.rt_loc].acc); } } println!("Royalties accumulated but not captured:"); @@ -410,14 +410,6 @@ impl Market { let ap0=(asset_type0,asset_type1); let ap1=(asset_type1,asset_type0); let [bids0,bids1]=self.orders.get_many_mut([&ap0, &ap1]).unwrap(); -/* let sq0=bids0.peek().sell_qty; - let bq0=bids0.peek().buy_qty; - let sq1=bids1.peek().sell_qty; - let bq1=bids1.peek().buy_qty; - let q0=sq0/bq0>=strike0/strike1; - let q1=sq1/bq1>=strike1/strike0; - println!("{}/{} >= {}/{} is {}",sq0,bq0,strike0,strike1,q0); - println!("{}/{} >= {}/{} is {}",sq1,bq1,strike1,strike0,q1);*/ while !bids0.empty() && !bids1.empty() && bids0.peek().sell_qty/bids0.peek().buy_qty>=strike0/strike1 && bids1.peek().sell_qty/bids1.peek().buy_qty>=strike1/strike0 { @@ -433,14 +425,6 @@ impl Market { self.traders[bids1.peek().owner].name,asset0_paying,asset0.name)); if bids0.peek().sell_remain==FiNum::zero() { bids0.pop(); } if bids1.peek().sell_remain==FiNum::zero() { bids1.pop(); } -/* let sq0=bids0.peek().sell_qty; - let bq0=bids0.peek().buy_qty; - let sq1=bids1.peek().sell_qty; - let bq1=bids1.peek().buy_qty; - let q0=sq0/bq0>=strike0/strike1; - let q1=sq1/bq1>=strike1/strike0; - println!("{}/{} >= {}/{} is {}",sq0,bq0,strike0,strike1,q0); - println!("{}/{} >= {}/{} is {}",sq1,bq1,strike1,strike0,q1);*/ } Result::ExecutedBatch(log) } @@ -511,15 +495,15 @@ impl Market { let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()); let rt_loc=rt.next_entry; rt.add_weight(rt_loc,buy_qty); // Weight should really be the quantity you would be able to immediately transact rather than how much you wish for. - let neworder=Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt_loc, pq_loc:0, royalty_remain:royalty1_qty, commission_remain:commission1_qty, id:self.next_order_id }; + let neworder=Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, royalty_remain:royalty1_qty, commission_remain:commission1_qty, id:self.next_order_id }; new_order_id=self.next_order_id; self.next_order_id+=1; rt.next_entry+=1; let pq_loc=bids.insert(neworder); - bids.v[pq_loc].pq_loc=pq_loc; - bids.v[pq_loc].rt_loc=rt_loc; self.traders[owner].sub_balance(sell_type,sell_qty_remain); self.traders[0].add_balance(sell_type,commission0_qty); + self.traders[owner].orders.insert(new_order_id); + self.order_finder.insert(new_order_id,OrderFinder { asset0:sell_type, asset1:buy_type, pq_loc: pq_loc, rt_loc: rt_loc }); log.push(format!("Moved {} {} ({}) to market",sell_qty_remain,self.number_to_name(sell_type),self.traders[owner].name)); } else { new_order_id=0; } } else { new_order_id=0; }