diff --git a/royalty.log b/royalty.log index 9a4ab70..6e5e1be 100644 --- a/royalty.log +++ b/royalty.log @@ -1,12 +1,53 @@ -NOP Error: adduser -AT Teppy -AT Luni -AT Hamza -AA USD -AA BTC -SR 0 19999999 0 0 0 -AF 2 1 300000000 -AF 1 0 186A000000000 -OR 2 1 100000000 0 C35000000000 -OR 2 1 100000000 0 EA6000000000 -OR 1 0 1117000000000 1 80000000 +AF 1 1 ACE83D9EF +OR 2 0 15D5D8C3CB 1 22E47E2200 +OR 3 0 26CB337DD5 1 ACD3E91F2 +AF 3 0 1D3A019779 +OR 3 0 B1DA434FB 0 210C92AE55 +OR 2 0 11DDFBC46C 0 DACB9F3D4 +AF 1 1 E1005209A +OR 2 1 22E75D81E3 0 9CBC333A2 +OR 3 0 3A9242373 1 1AF77C6986 +AF 2 0 1C84BBAE46 +OR 1 1 B879225DF 1 18A0C5B45E +AF 2 0 1452304323 +OR 2 1 C0018AAA3 1 1A500004CD +OR 1 1 135449AF8C 0 1A67D1E710 +AF 2 0 8EE2180B3 +AF 3 1 17121D5C61 +AF 1 0 1E9AB01EB1 +AF 3 0 B2E37729F +AF 1 1 220E7284DA +AF 1 0 123B372ECB +AF 1 0 10A541F58 +OR 3 0 558A3A8AB 0 2131276853 +AF 2 1 925697F87 +OR 1 1 1FD8DF58DA 0 1D97892E00 +OR 2 1 DF6EF82EC 0 2171CC3DC0 +AF 1 1 1101009539 +AF 3 1 19DCE5BB2B +AF 2 0 65B44C278 +OR 2 0 5D93BF9B8 1 275986FEF2 +AF 3 0 20F18F43A1 +AF 1 1 14B5273635 +AF 3 1 1B06E91469 +AF 1 1 A961C7185 +AF 3 0 215A67F066 +AF 2 0 25C8BF3081 +OR 2 0 258E850E53 0 D6FCDD020 +OR 2 1 EFB29F026 0 2354900CA0 +AF 3 0 E3963FDB9 +AF 1 0 E7B15040E +AF 3 1 ECC0EF2D8 +OR 3 0 E3D168148 0 17E3F3BD0D +AF 2 1 148A726A4F +AF 3 1 20500A0096 +OR 1 1 22E37DAED5 1 11A428EBED +OR 2 0 1F5F00B3DC 1 191D073AA5 +OR 2 0 1737501E7A 0 B34594662 +AF 2 1 2264503554 +AF 1 0 152666DDF8 +AF 2 1 13B4F9E9DB +OR 2 1 C01F4E14F 0 4C2212B6E +OR 3 0 A9847B6C4 0 32D75C3D6 +OR 1 0 1D5D97256 1 188F04C616 +OR 1 0 D6D67EF49 1 131201FFB9 diff --git a/royalty0.log b/royalty0.log new file mode 100644 index 0000000..1999090 --- /dev/null +++ b/royalty0.log @@ -0,0 +1,10 @@ +NOP Error: adduser +AT Teppy +AT Luni +AT Hamza +AA USD +AA BTC +SR 0 19999999 0 0 0 +AF 2 1 300000000 +AF 1 0 186A000000000 +OR 2 1 100000000 0 C35000000000 diff --git a/royalty2.log b/royalty2.log index 19436f5..fe8c16f 100644 --- a/royalty2.log +++ b/royalty2.log @@ -8,47 +8,56 @@ SR 0 19999999 0 0 0 AF 2 1 300000000 AF 1 0 186A000000000 OR 2 1 100000000 0 C35000000000 -OR 2 1 100000000 0 EA6000000000 -OR 1 0 1117000000000 1 80000000 -OR 1 1 6EFB69849 0 15B1A8C689 -OR 2 0 18E34B32E1 1 243BBBBA10 -OR 1 0 1F0979696D 1 11E5CB8C5F -AF 2 1 215364483C -AF 3 1 891AFC8C0 -AF 3 1 F04D7CABB -OR 3 1 1B7058942F 1 12573225F2 -AF 1 0 25DA2D6060 -AF 3 0 151F8E8A18 -AF 2 1 1BDE6E5DDA -OR 3 0 59A71E33C 0 10C9FD83D2 -AF 3 1 1F88B59AA8 -AF 3 1 1B6E614618 -OR 3 1 24F94C98D5 1 24F66B030C -OR 2 1 19E28BC7F0 1 5968F1DAD -AF 3 1 21C75333CF -AF 2 1 CFCD48BE0 -OR 2 0 240430F1A5 0 25E13E81E1 -AF 3 0 4E5E96272 -OR 2 0 2299F8EFBF 1 1D39E5B911 -AF 1 1 42C7269F7 -AF 1 1 12F8E79E5F -AF 3 0 16425358DC -AF 1 1 F96CD972D -OR 2 0 82B856AA5 1 34A36393D -OR 3 0 C75D24EBE 1 C6041BD97 -OR 3 1 1DFC18EC34 1 8B9B41EB1 -OR 3 0 19786E00B5 1 1A16FDCAD3 -OR 1 0 1C72D46C4A 1 1DB942B756 -OR 2 1 228C580C87 0 27F3A63C53 -OR 1 1 1778DBCA22 1 1C4D5277D3 -OR 2 1 F28C33FFB 1 323E37A2D -OR 2 1 1C2587E46F 1 22E2E04B31 -OR 1 0 C48222DF7 0 A75B450D4 -AF 3 0 496C0DAD5 -AF 1 1 85260D486 -AF 2 1 364A56B55 -AF 1 0 28FB02AF7 -AF 2 0 210D21849F -AF 1 0 96BDA2649 -AF 3 1 2641D775EC -OR 2 1 1B7225879A 0 20E963C8A8 +AF 1 1 ACE83D9EF +OR 2 0 15D5D8C3CB 1 22E47E2200 +OR 3 0 26CB337DD5 1 ACD3E91F2 +AF 3 0 1D3A019779 +OR 3 0 B1DA434FB 0 210C92AE55 +OR 2 0 11DDFBC46C 0 DACB9F3D4 +AF 1 1 E1005209A +OR 2 1 22E75D81E3 0 9CBC333A2 +OR 3 0 3A9242373 1 1AF77C6986 +AF 2 0 1C84BBAE46 +OR 1 1 B879225DF 1 18A0C5B45E +AF 2 0 1452304323 +OR 2 1 C0018AAA3 1 1A500004CD +OR 1 1 135449AF8C 0 1A67D1E710 +AF 2 0 8EE2180B3 +AF 3 1 17121D5C61 +AF 1 0 1E9AB01EB1 +AF 3 0 B2E37729F +AF 1 1 220E7284DA +AF 1 0 123B372ECB +AF 1 0 10A541F58 +OR 3 0 558A3A8AB 0 2131276853 +AF 2 1 925697F87 +OR 1 1 1FD8DF58DA 0 1D97892E00 +OR 2 1 DF6EF82EC 0 2171CC3DC0 +AF 1 1 1101009539 +AF 3 1 19DCE5BB2B +AF 2 0 65B44C278 +OR 2 0 5D93BF9B8 1 275986FEF2 +AF 3 0 20F18F43A1 +AF 1 1 14B5273635 +AF 3 1 1B06E91469 +AF 1 1 A961C7185 +AF 3 0 215A67F066 +AF 2 0 25C8BF3081 +OR 2 0 258E850E53 0 D6FCDD020 +OR 2 1 EFB29F026 0 2354900CA0 +AF 3 0 E3963FDB9 +AF 1 0 E7B15040E +AF 3 1 ECC0EF2D8 +OR 3 0 E3D168148 0 17E3F3BD0D +AF 2 1 148A726A4F +AF 3 1 20500A0096 +OR 1 1 22E37DAED5 1 11A428EBED +OR 2 0 1F5F00B3DC 1 191D073AA5 +OR 2 0 1737501E7A 0 B34594662 +AF 2 1 2264503554 +AF 1 0 152666DDF8 +AF 2 1 13B4F9E9DB +OR 2 1 C01F4E14F 0 4C2212B6E +OR 3 0 A9847B6C4 0 32D75C3D6 +OR 1 0 1D5D97256 1 188F04C616 +OR 1 0 D6D67EF49 1 131201FFB9 diff --git a/src/main.rs b/src/main.rs index 497b3d1..8577da5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,11 +46,11 @@ use std::fs::OpenOptions; use std::path::Path; use std::io::{self, BufRead, Write}; use std::env; -//use std::collections::HashMap; +use std::collections::HashMap; use std::cmp::Ordering; use rand::prelude::*; use rand::rngs::StdRng; -use hashbrown::HashMap; +//use hashbrown::HashMap; mod finum; use finum::FiNum; @@ -166,7 +166,7 @@ impl RoyaltyTree { 0 } else { let c0=self.count_here_below(wt_left (index).unwrap()); - let c1=self.count_here_below(wt_right(index).unwrap()); + let c1=self.count_here_below(wt_right(index).unwrap()); c0+c1 } } @@ -200,17 +200,23 @@ impl RoyaltyTree { self.tree[index].lazy=FiNum::zero(); } else { let lazy=self.tree[index].lazy; -// println!("Index is {}, lazy.0 {}",index,lazy.value()); +// println!("Index is {}, lazy.0 {}",index,lazy.serialize()); // self.raw_dump(); let d1=if lazy>0.into() { lazy*self.weight_here(index)/self.weight_here_below(index) } else { FiNum::zero() }; let d02=lazy-d1; let index_left =wt_left (index).unwrap(); let index_right=wt_right(index).unwrap(); - let d0=if d02>0.into() { d02*self.weight_here_below(index_left)/self.weight_below(index) } else { FiNum::zero() }; - let d2=d02-d0; - self.sanity(); - if !self.weight_here_below(index_left ).is_zero() { self.tree[index_left ].lazy+=d0; } else { self.spare_change+=d0; } - if !self.weight_here_below(index_right).is_zero() { self.tree[index_right].lazy+=d2; } else { self.spare_change+=d2; } +// println!("D02 is {}, self.weight_here_below({}) is {}, self.weight_below({}) is {}",d02.serialize(),index_left,self.weight_here_below(index_left),index,self.weight_below(index)); + if self.weight_below(index)>0.into() { + let d0=if d02>0.into() { d02*self.weight_here_below(index_left)/self.weight_below(index) } else { FiNum::zero() }; + let d2=d02-d0; + self.sanity(); + if !self.weight_here_below(index_left ).is_zero() { self.tree[index_left ].lazy+=d0; } else { self.spare_change+=d0; } + if !self.weight_here_below(index_right).is_zero() { self.tree[index_right].lazy+=d2; } else { self.spare_change+=d2; } + } + else { + self.spare_change+=d02; + } self.tree[index].acc+=d1; self.tree[index].lazy=FiNum::zero(); } @@ -242,10 +248,10 @@ impl RoyaltyTree { } // We really need two methods: remove an order and redistribute, remove some and return captured amount fn remove_order_id(&mut self, id: usize) { // And zero out in the tree - let pos=*self.order_finder.get(&id).unwrap(); - self.order_finder.remove(&id); - let weight=self.weight_here(pos); - self.sub_weight(pos,weight); + if let Some(pos)=self.order_finder.remove(&id) { + let weight=self.weight_here(pos); + self.sub_weight(pos,weight); + } } fn random_order_id(&self, rng: &mut rand::rngs::StdRng) -> Option { if self.tree.len()==0 { return None } @@ -468,7 +474,17 @@ impl Market { for index in 0..self.assets.len() { let tot=self.royalties.get(&index).unwrap().acc_total(); println!(" For {}: {}",self.number_to_asset(index).name,tot); - } + for indexr in 0..self.royalties.get(&index).unwrap().tree.len() { +// let roy=self.royalties.get(&index).unwrap().get_royalty(indexr); + println!(" Index {} Count {} Weight {} Lazy {} Acc {} Royalty", + indexr, + self.royalties.get(&index).unwrap().tree[indexr].count, + self.royalties.get(&index).unwrap().tree[indexr].weight, + self.royalties.get(&index).unwrap().tree[indexr].lazy, + self.royalties.get(&index).unwrap().tree[indexr].acc); +// self.royalties.get(&index).unwrap().get_royalty(indexr)); + } + } println!("Trader Balances:"); for t in &self.traders { println!(" Trader {}: {}",t.id,t.name); @@ -492,7 +508,7 @@ impl Market { } } } - fn execute_batch(&mut self, asset_type0: usize, strike0: FiNum, asset_type1: usize, strike1: FiNum) -> Result { +/* fn execute_batch(&mut self, asset_type0: usize, strike0: FiNum, asset_type1: usize, strike1: FiNum) -> Result { let mut log:Vec=Vec::new(); let asset0=self.number_to_asset(asset_type0); let asset1=self.number_to_asset(asset_type1); @@ -500,7 +516,7 @@ impl Market { println!("Executing batch {} {} <-> {} {}",strike0,asset0.name,strike1,asset1.name); let ap0=(asset_type0,asset_type1); let ap1=(asset_type1,asset_type0); - let [bids0,bids1]=self.orders.get_many_mut([&ap0, &ap1]).unwrap(); + let [bids0,bids1]=self.orders.get_many_mut([&ap0, &ap1]).unwrap(); // There's a workaround for this where you remove and reinsert the keys. 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 { @@ -519,6 +535,7 @@ impl Market { } Result::ExecutedBatch(log) } +*/ fn retract_order(&mut self, order_id:usize) -> Result { let pair=self.order_finder.get(&order_id).unwrap(); let queue=self.orders.get_mut(pair).unwrap(); @@ -567,7 +584,7 @@ impl Market { let cq0=commission0_qty*pay_qty/sell_qty_initial; let rq1=royalty1_qty *pay_qty/sell_qty_initial; let cq1=commission1_qty*pay_qty/sell_qty_initial; - println!("Transact at ask rate. Qty={} elt.sell_remain={} elt.royalty_remain={} elt.commission_remain={}",qty,elt.sell_remain,elt.royalty_remain,elt.commission_remain); +// println!("Transact at ask rate. Qty={} elt.sell_remain={} elt.royalty_remain={} elt.commission_remain={}",qty,elt.sell_remain,elt.royalty_remain,elt.commission_remain); let rq2=if !elt.sell_remain.is_zero() { elt.royalty_remain *qty/elt.sell_remain } else { FiNum::zero() }; let cq2=if !elt.sell_remain.is_zero() { elt.commission_remain*qty/elt.sell_remain } else { FiNum::zero() }; royalty_acc+=rq0+rq1; @@ -732,7 +749,7 @@ impl Market { self.rng=StdRng::seed_from_u64(seed); } fn random_command(&mut self) -> Command { - match self.rng.gen_range(1..3) { + match self.rng.gen_range(1..4) { 1 => Command::AddFunds { user_id: self.rng.gen_range(1..self.traders.len()), asset_id: self.rng.gen_range(0..self.assets.len()), amt: FiNum::new(self.rng.gen_range(1<<32..20<<32+1)), }, @@ -744,7 +761,7 @@ impl Market { sell_qty: FiNum::new(self.rng.gen_range(1<<32..20<<32+1)), buy_type: self.rng.gen_range(0..self.assets.len()), buy_qty: FiNum::new(self.rng.gen_range(1<<32..20<<32+1)), }, -// 4 => Command::RetractOrder { {*} + 4 => if let Some(id)=self.random_order_id() { Command::RetractOrder { order_id:id } } else { Command::None }, _ => Command::Error("This can never happen".to_string()), } } @@ -982,7 +999,7 @@ impl Market { } Command::AddFunds{ user_id, asset_id, amt } => { self.add_trader_balance(*user_id,*asset_id,*amt); - println!("Added {} {} to {}",*amt,self.number_to_asset(*asset_id).name,self.traders[*user_id].name); +// println!("Added {} {} to {}",*amt,self.number_to_asset(*asset_id).name,self.traders[*user_id].name); Result::FundsRemaining(self.get_trader_balance(*user_id,*asset_id)) } Command::SubFunds { user_id,asset_id,amt } => { @@ -1003,7 +1020,8 @@ impl Market { self.retract_order(*order_id) } Command::ExecuteBatch { asset_type0, strike0, asset_type1, strike1 } => { - self.execute_batch(*asset_type0, *strike0, *asset_type1, *strike1) +// self.execute_batch(*asset_type0, *strike0, *asset_type1, *strike1) + Result::Error(format!("ExecuteBatch not Implemented.")) } Command::Error(str) => Result::Error(format!("Command Error")), Command::NOP(str) => Result::Ok, @@ -1072,7 +1090,7 @@ fn tokens_to_command(m: &Market, logged_in: usize, tokens: Vec<&str>,line: &str) let order_id:usize=p_order_id.parse().unwrap(); if !m.order_finder.contains_key(&order_id) { return Command::Error(format!("Order not found: {}",p_order_id)) } let trader=&m.traders[logged_in]; - let order_type=trader.order_finder.get(&order_id); + let order_type=m.order_finder.get(&order_id); println!("Order_type {:?}",order_type); let order_queue=m.orders.get(order_type.expect("Retrieving Order Queue")); Command::RetractOrder { order_id: order_id } @@ -1164,13 +1182,15 @@ fn interactive(m: &mut Market, mut out: Option) { let qty:u32=qty.parse().unwrap(); for i in 0..qty { let cmd=m.random_command(); - println!("RandomCommand #{}: {}",1+i,cmd.serialize()); +// println!("RandomCommand #{}: {}",1+i,cmd.serialize()); if let Some(ref mut f)=out { let ser=cmd.serialize(); if let Err(e)=writeln!(f,"{}",ser) { eprintln!("An error occurred while writing {}",e); } - } else { println!("{}",cmd.serialize()); } + } else { +// println!("{}",cmd.serialize()); + } let res=m.execute(&cmd); - println!("Result: {}",res.describe()); +// println!("Result: {}",res.describe()); } Command::None }, @@ -1243,7 +1263,7 @@ fn main() { } if mode_count==0 { mode=Mode::Interactive; mode_count+=1; } if mode_count!=1 { println!("You may only select one mode to run in."); return; } - let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, zKN6FBdD SOL type is 12 + 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 if options.contains_key("replay") { m.replay_file(options.get("replay").unwrap()); } match mode { Mode::Interactive => {