diff --git a/src/main.rs b/src/main.rs index 71cce45..96b8c1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![allow(unsafe_code)] + use std::collections::HashMap; use std::cmp::Ordering; use std::cmp::min; @@ -143,17 +145,22 @@ impl Market { } fn make_offer(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1 { - if self.traders[owner as usize].get_balance(sell_type)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); +// println!("Transacting {} at {}/{}, not at {}/{}. Elt.sell_remain is {}",qty,elt.buy_qty,elt.sell_qty,sell_qty_initial,buy_qty_initial,elt.sell_remain); elt.sell_remain-=qty; buy_qty-=qty; - let pay_qty=qty*elt.buy_qty/elt.sell_qty; + let pay_qty=qty*elt.buy_qty/elt.sell_qty; // 1.8499*194623/2.9744 +// println!("qty {} * elt.buy_qty {} / elt.sell_qty {} = pay_qty {} ",qty,elt.buy_qty,elt.sell_qty,pay_qty); +// println!("qty {} * elt.buy_qty {} = pay_qty {} ",qty,elt.buy_qty, qty*elt.buy_qty); +// println!("Pay_qty is {}. Buy_qty is now {}. Balance is {}",pay_qty,buy_qty,self.traders[owner as usize].get_balance(sell_type)); self.traders[owner as usize].sub_balance(sell_type,pay_qty); self.traders[owner as usize].add_balance(buy_type ,qty); self.traders[elt.owner as usize].add_balance(sell_type,pay_qty); @@ -261,24 +268,31 @@ fn main() { let luni =m.register_trader("Luni"); let usd =m.register_asset("USD"); let btc =m.register_asset("BTC"); - m.add_trader_balance(teppy,btc,100.into()); - m.add_trader_balance(luni ,usd,1000000.into()); - for i in 1..=1000000 { + m.add_trader_balance(teppy,btc,100000.into()); + m.add_trader_balance(luni ,usd,650000000.into()); + let mut count=0; + let mut tries=0; + for i in 1..=10000000 { let seller=if rng.gen_bool(0.5) { teppy } else { luni }; let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum); if rng.gen_bool(0.5) { sell_type=btc; buy_type=usd; sell_qty=rng.gen_range(1..=5).into(); - buy_qty=sell_qty*rng.gen_range(60000..=70000).into(); + buy_qty=sell_qty*rng.gen_range(60..=70).into(); } else { sell_type=usd; buy_type=btc; buy_qty=rng.gen_range(1..=5).into(); - sell_qty=buy_qty*rng.gen_range(60000..=70000).into(); + sell_qty=buy_qty*rng.gen_range(60..=70).into(); } - m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty); + let mut success=false; + if m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty) { count+=1; success=true; }; +// println!("{} selling {} {} to buy {} {}, {}",seller,sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),if success { "success" } else { "failure" }); + if count>=1000000 { break; } + tries+=1; } - m.dump(); + println!("Tries: {} Trades: {}",tries,count); +// m.dump(); m.sanity_check(); }