From d65eee0a516b02a81ed5ddabfab50bc69c3b10a4 Mon Sep 17 00:00:00 2001 From: Teppy Date: Sun, 19 May 2024 13:15:58 -0400 Subject: [PATCH] Buggy --- src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 37c394d..7bc82f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,6 +143,7 @@ impl Market { while buy_qty>FiNum::new(0) && self.offers.contains_key(&ap) && self.offers.get(&ap).unwrap().v.len()>0 { let elt=&mut self.offers.get_mut(&ap).unwrap().v[0]; if sell_qty_initial/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate + println!("Transacting at {}/{}, not at {}/{}",elt.buy_qty,elt.sell_qty,sell_qty_initial,buy_qty_initial); let qty=std::cmp::min(elt.sell_remain,buy_qty); elt.sell_remain-=qty; buy_qty-=qty; @@ -202,7 +203,7 @@ impl PQueue { fn bubble_up(&mut self, pos: usize) { if pos>0 { let parent=(pos-1)/2; - if self.v[parent]self.v[pos] { self.v.swap(parent,pos); self.bubble_up(parent); } @@ -213,9 +214,9 @@ impl PQueue { let child0=pos*2+1; let child1=pos*2+2; if child0self.v[child0] { pivot=child0; } if child1self.v[child1] { pivot=child1; } } if pivot!=pos { self.v.swap(pivot,pos); @@ -247,6 +248,14 @@ impl PQueue { fn main() { + let mut pq:PQueue=PQueue::new(); + pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:4.into() } ); + pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:2.into() } ); + pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:3.into() } ); + println!("Pop: {}",pq.pop().unwrap().buy_qty); + println!("Pop: {}",pq.pop().unwrap().buy_qty); + println!("Pop: {}",pq.pop().unwrap().buy_qty); + let mut rng: StdRng=StdRng::seed_from_u64(1u64); let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12 let teppy=m.register_trader("Teppy"); @@ -261,12 +270,12 @@ fn main() { if rng.gen_bool(0.5) { sell_type=btc; buy_type=usd; - sell_qty=rng.gen_range(2..=5).into(); + sell_qty=1.into(); //rng.gen_range(2..=5).into(); buy_qty=sell_qty*rng.gen_range(60000..=70000).into(); } else { sell_type=usd; buy_type=btc; - buy_qty=rng.gen_range(2..=5).into(); + buy_qty=1.into(); //rng.gen_range(2..=5).into(); sell_qty=buy_qty*rng.gen_range(60000..=70000).into(); } println!("Index {} Sell {} {} to buy {} {} ({})",i,sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),m.traders[seller as usize].name);