From 9081338f5d8dc7ad7faa13e1af951cd0cd83f248 Mon Sep 17 00:00:00 2001 From: Teppy Date: Sat, 18 May 2024 14:28:12 -0400 Subject: [PATCH] Uses FiNum type instead of u64 --- src/main.rs | 100 +++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3a77e4e..e975240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ use std::collections::HashMap; use std::cmp::Ordering; use std::cmp::min; -use fixed::types::U32F32; +use finum::FiNum; #[derive(Debug, Clone)] struct Trader { name: String, id: i32, - balances: HashMap, // Maps Currency to Amount + balances: HashMap, // Maps Currency to Amount } impl Trader { @@ -18,22 +18,22 @@ impl Trader { balances: HashMap::new() } } - fn add_balance(&mut self, cur:i32, delta:u64) { + fn add_balance(&mut self, cur:i32, delta:FiNum) { self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta); } - fn sub_balance(&mut self, cur:i32, delta:u64) { + fn sub_balance(&mut self, cur:i32, delta:FiNum) { self.balances.entry(cur).and_modify(|ent| *ent-=delta ); } - fn get_balance(&self, cur:i32) -> u64 { - *self.balances.get(&cur).map(|bal| bal).unwrap_or(&0u64) + fn get_balance(&self, cur:i32) -> FiNum { + *self.balances.get(&cur).map(|bal| bal).unwrap_or(&FiNum::new(0u64)) } } #[derive(Debug, Clone)] struct Offer { - sell_qty: u64, - sell_remain: u64, - buy_qty: u64, + sell_qty: FiNum, + sell_remain: FiNum, + buy_qty: FiNum, owner: i32, } @@ -43,14 +43,14 @@ trait Dumpable { impl Dumpable for Offer { fn dump(&self) { - println!("Giving {}/{} to get {}",fxp_to_fp(self.sell_remain),fxp_to_fp(self.sell_qty),fxp_to_fp(self.buy_qty)); + println!("Giving {}/{} to get {}",self.sell_remain,self.sell_qty,self.buy_qty); } } impl Offer { fn dump(self) { - println!("Remaining: {} Price: {}",self.sell_remain,fxp_to_fp(fxp_div(self.buy_qty,self.sell_qty))); + println!("Remaining: {} Price: {}",self.sell_remain,self.buy_qty/self.sell_qty); } } @@ -58,7 +58,7 @@ struct Market { asset_name2num: HashMap, asset_num2name: HashMap, asset_count:i32, - money_supply: HashMap, + money_supply: HashMap, offers: HashMap<(i32,i32),PQueue>, traders: Vec, trader_name2num: HashMap, @@ -79,15 +79,15 @@ impl Market { 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; + println!("Money Supply {}: {}",self.number_to_name(*cur),*amt); + let mut acc_offers=FiNum::new(0); 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; + let mut acc_traders=FiNum::new(0); 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)); + println!(" {}: Offers {} Traders {} Total {} Should Be {}",self.number_to_name(*cur),acc_offers,acc_traders,acc,*amt); } } fn register_trader(&mut self, name:&str) -> i32 { // Add error checking for inserting a trader twice @@ -97,11 +97,11 @@ impl Market { 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) { + fn add_trader_balance(&mut self, who:i32, cur:i32, delta: FiNum) { 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) { + fn sub_trader_balance(&mut self, who:i32, cur:i32, delta: FiNum) { self.traders[who as usize].sub_balance(cur,delta); *self.money_supply.get_mut(&cur).unwrap()-=delta; } @@ -109,7 +109,7 @@ impl Market { 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.money_supply.insert(self.asset_count,FiNum::new(0)); self.asset_count } fn name_to_number(&self, name:&str) -> i32 { @@ -123,7 +123,7 @@ impl Market { for t in &self.traders { println!(" Trader {}: {}",t.id,t.name); for (cur,bal) in t.balances.iter() { - println!(" {}: {}",self.number_to_name(*cur),fxp_to_fp(*bal)) + println!(" {}: {}",self.number_to_name(*cur),*bal) } } for (key,value) in &self.offers { @@ -131,61 +131,61 @@ impl Market { value.dump(); } } - fn make_offer(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:u64, buy_qty_initial:u64) // Dollars, Bitcoin, 64000, 1 + fn make_offer(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:FiNum, buy_qty_initial:FiNum) // Dollars, Bitcoin, 64000, 1 { // 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)); + println!("Subtracting {} currency type {}",sell_qty_initial,sell_type); + println!("Making offer to sell {} {} and get {} {}",sell_qty_initial,self.number_to_name(sell_type),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 - let sell_rate=fxp_div(sell_qty,buy_qty); + let sell_rate=sell_qty/buy_qty; let ap=(buy_type,sell_type); let mut lc=3; - while sell_qty>0 && lc>0 { + while sell_qty>0.into() && lc>0 { if let Some(asks)=self.offers.get_mut(&ap) { if asks.v.len()>0 { let elt=&mut asks.v[0]; let t1=&mut self.traders[elt.owner as usize]; - let ask_rate=fxp_div(elt.buy_qty,elt.sell_qty); - let elt_buy_remain=fxp_mult(elt.buy_qty,fxp_div(elt.sell_remain,elt.sell_qty)); + let ask_rate=elt.buy_qty/elt.sell_qty; + let elt_buy_remain=elt.buy_qty*elt.sell_remain/elt.sell_qty; 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 d=elt.sell_remain/buy_qty_initial; + let m=sell_qty*d; let nsq=sell_qty-m; - sell_qty-=fxp_mult(sell_qty,fxp_div(elt.sell_remain,buy_qty_initial)); + sell_qty-=sell_qty*elt.sell_remain/buy_qty_initial; buy_qty-=elt.sell_remain; - println!("Adding {} {}",fxp_to_fp(elt_buy_remain),sell_type); + println!("Adding {} {}",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); + println!("Adding {} {}",sell_qty,sell_type); t1.add_balance(sell_type,sell_qty); - sell_qty=0; - buy_qty=0; + sell_qty=0.into(); + buy_qty=0.into(); } else { - let transact=std::cmp::min(sell_qty,fxp_mult(elt.buy_qty,fxp_div(elt.sell_remain,elt.sell_qty))); - 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); + let transact=std::cmp::min(sell_qty,elt.buy_qty*elt.sell_remain/elt.sell_qty); + elt.sell_remain-=transact/ask_rate; + buy_qty-=transact/ask_rate; + let mut sell_delta=transact*sell_rate/ask_rate; + println!("Adding {} {}",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(); } + if elt.sell_remain==0.into() { println!("Popping asks"); asks.pop(); } } } else { break; } } else { break; } } else { break; } lc-=1; } - if sell_qty>0 { + if sell_qty>0.into() { let ap=(sell_type,buy_type); if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); } let mut bids=self.offers.get_mut(&ap).unwrap(); - println!("Inserting new offer with sell_qty {}, buy_qty {}",fxp_to_fp(sell_qty),fxp_to_fp(buy_qty)); + println!("Inserting new offer with sell_qty {}, buy_qty {}",sell_qty,buy_qty); bids.insert(Offer { owner:owner, sell_qty:sell_qty, sell_remain:sell_qty, buy_qty:buy_qty } ); } } @@ -294,18 +294,22 @@ fn fxp_mult(a: u64, b: u64) -> u64 { } fn main() { + let a=FiNum::new_i32(2); + let b=FiNum::new_i32(3); + let c=b+a; + println!("C is {}, C.value() is {}",c,c.value()); 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"); let luni =m.register_trader("Luni"); let usd =m.register_asset("USD"); let btc =m.register_asset("BTC"); - m.add_trader_balance(teppy,btc,fxp_from_int(5)); - m.add_trader_balance(luni ,usd,fxp_from_int(300000)); + m.add_trader_balance(teppy,btc,5.into()); + m.add_trader_balance(luni ,usd,300000.into()); m.dump(); - m.make_offer(teppy,btc,usd,fxp_from_int(1),fxp_from_int(60000)); + m.make_offer(teppy,btc,usd,1.into(),60000.into()); + m.dump(); + m.sanity_check(); + m.make_offer(luni ,usd,btc,128000.into(),2.into()); // 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(); }