From 85bacb97d16736619c9f96f3d74c1ef889ecada7 Mon Sep 17 00:00:00 2001 From: Teppy Date: Thu, 16 May 2024 17:11:09 -0400 Subject: [PATCH] Buggy but making progress --- src/main.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index b021024..3a77e4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,6 +58,7 @@ struct Market { asset_name2num: HashMap, asset_num2name: HashMap, asset_count:i32, + money_supply: HashMap, offers: HashMap<(i32,i32),PQueue>, traders: Vec, trader_name2num: HashMap, @@ -69,27 +70,46 @@ impl Market { asset_name2num: HashMap::new(), asset_num2name: HashMap::new(), asset_count:0, + money_supply: HashMap::new(), offers: HashMap::new(), traders: Vec::new(), trader_name2num: HashMap::new(), } } + fn sanity_check(&self) { + println!("Sanity Checking Market..."); + for (cur,amt) in self.money_supply.iter() { + println!("Money Supply {}: {}",self.number_to_name(*cur),fxp_to_fp(*amt)); + let mut acc_offers=0u64; + for (ac,pq) in &self.offers { if ac.0==*cur { + for off in &*pq.v { acc_offers+=off.sell_remain; } + } } + let mut acc_traders=0u64; + for t in &self.traders { acc_traders+=t.get_balance(*cur); } + let acc=acc_offers+acc_traders; + println!(" {}: Offers {} Traders {} Total {} Should Be {}",self.number_to_name(*cur),fxp_to_fp(acc_offers),fxp_to_fp(acc_traders),fxp_to_fp(acc),fxp_to_fp(*amt)); + } + } fn register_trader(&mut self, name:&str) -> i32 { // Add error checking for inserting a trader twice let rval=self.traders.len() as i32; self.trader_name2num.insert(String::from(name),self.traders.len() as i32); self.traders.push(Trader::new(name,rval)); - rval + rval } + // These are the only ways to get money into or out of the market. fn add_trader_balance(&mut self, who:i32, cur:i32, delta: u64) { self.traders[who as usize].add_balance(cur,delta); + *self.money_supply.get_mut(&cur).unwrap()+=delta; } fn sub_trader_balance(&mut self, who:i32, cur:i32, delta: u64) { self.traders[who as usize].sub_balance(cur,delta); + *self.money_supply.get_mut(&cur).unwrap()-=delta; } fn register_asset(&mut self, name:&str) -> i32 { self.asset_count+=1; self.asset_name2num.insert(String::from(name),self.asset_count); self.asset_num2name.insert(self.asset_count,String::from(name)); + self.money_supply.insert(self.asset_count,0); self.asset_count } fn name_to_number(&self, name:&str) -> i32 { @@ -115,6 +135,7 @@ impl Market { { // Comments assume someone is already selling 1 Bitcoin for 48000 USD (Bitcoin, Dollars, 1, 48000). Then someone comes along and bids $64000 for 1 Bitcoin (sells 64000 USD to buy 1 Bitcoin. let mut t0=&mut self.traders[owner as usize]; t0.sub_balance(sell_type,sell_qty_initial); + println!("Subtracting {} currency type {}",fxp_to_fp(sell_qty_initial),sell_type); println!("Making offer to sell {} {} and get {} {}",fxp_to_fp(sell_qty_initial),self.number_to_name(sell_type),fxp_to_fp(buy_qty_initial),self.number_to_name(buy_type)); let mut sell_qty=sell_qty_initial; // 64000 Dollars let mut buy_qty=buy_qty_initial; // 1 Bitcoin @@ -131,12 +152,17 @@ impl Market { if sell_rate>=ask_rate { // Transact at ask_rate // Can we eat the entire ask? if buy_qty>=elt.sell_remain { + let d=fxp_div(elt.sell_remain,buy_qty_initial); + let m=fxp_mult(sell_qty,d); + let nsq=sell_qty-m; sell_qty-=fxp_mult(sell_qty,fxp_div(elt.sell_remain,buy_qty_initial)); buy_qty-=elt.sell_remain; + println!("Adding {} {}",fxp_to_fp(elt_buy_remain),sell_type); t1.add_balance(sell_type,elt_buy_remain); asks.pop(); } else if elt_buy_remain>=sell_qty { // Can we eat the rest of the offer? elt.sell_remain-=buy_qty; + println!("Adding {} {}",fxp_to_fp(sell_qty),sell_type); t1.add_balance(sell_type,sell_qty); sell_qty=0; buy_qty=0; @@ -145,6 +171,7 @@ impl Market { elt.sell_remain-=fxp_div(transact,ask_rate); buy_qty-=fxp_div(transact,ask_rate); let mut sell_delta=fxp_mult(transact,fxp_div(sell_rate,ask_rate)); + println!("Adding {} {}",fxp_to_fp(sell_delta),sell_type); t1.add_balance(sell_type,sell_delta); sell_qty-=sell_delta; if elt.sell_remain==0 { println!("Popping asks"); asks.pop(); } @@ -277,6 +304,8 @@ fn main() { m.dump(); m.make_offer(teppy,btc,usd,fxp_from_int(1),fxp_from_int(60000)); m.dump(); - m.make_offer(luni ,usd,btc,fxp_from_int(128000),fxp_from_int(2)); // Buy 1 BTC for 64000 USD - m.dump(); + m.sanity_check(); +// m.make_offer(luni ,usd,btc,fxp_from_int(128000),fxp_from_int(2)); // Buy 1 BTC for 64000 USD +// m.dump(); +// m.sanity_check(); }