This commit is contained in:
2024-05-26 20:37:08 -04:00
parent b3cfc9d173
commit d7e25a5ed3

View File

@@ -5,7 +5,7 @@ use std::cmp::Ordering;
use std::cmp::min; use std::cmp::min;
use std::rc::Rc; use std::rc::Rc;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::cell:RefCell; use std::cell::RefCell;
use rand::prelude::*; use rand::prelude::*;
use rand::rngs::StdRng; use rand::rngs::StdRng;
use finum::FiNum; use finum::FiNum;
@@ -74,9 +74,10 @@ struct Market {
asset_num2name: HashMap<i32,String>, asset_num2name: HashMap<i32,String>,
asset_count:i32, asset_count:i32,
money_supply: HashMap<i32,FiNum>, money_supply: HashMap<i32,FiNum>,
orders: HashMap<(i32,i32),PQueue<Rc<RefCell<Order>>>>,
traders: Vec<Trader>, traders: Vec<Trader>,
trader_name2num: HashMap<String,i32>, trader_name2num: HashMap<String,i32>,
orders: HashMap<(i32,i32),PQueue<Rc<RefCell<Order>>>>,
royalties: HashMap<i32,Vec<Rc<RefCell<Order>>>>, // Active orders that are accepting asset X. They receive royalties when someone makes an order to sell X
} }
impl Market { impl Market {
@@ -87,17 +88,20 @@ impl Market {
asset_count:0, asset_count:0,
money_supply: HashMap::new(), money_supply: HashMap::new(),
orders: HashMap::new(), orders: HashMap::new(),
royalties: HashMap::new(),
traders: Vec::new(), traders: Vec::new(),
trader_name2num: HashMap::new(), trader_name2num: HashMap::new(),
} }
} }
fn distribute_royalty(&self, amount:FiNum) {
}
fn sanity_check(&self) { fn sanity_check(&self) {
println!("Sanity Checking Market..."); println!("Sanity Checking Market...");
for (cur,amt) in self.money_supply.iter() { for (cur,amt) in self.money_supply.iter() {
println!("Money Supply {}: {}",self.number_to_name(*cur),*amt); println!("Money Supply {}: {}",self.number_to_name(*cur),*amt);
let mut acc_orders=FiNum::new(0); let mut acc_orders=FiNum::new(0);
for (ac,pq) in &self.orders { if ac.0==*cur { for (ac,pq) in &self.orders { if ac.0==*cur {
for off in &*pq.v { acc_orders+=off.sell_remain; } for off in &*pq.v { acc_orders+=off.borrow().sell_remain; }
} } } }
let mut acc_traders=FiNum::new(0); let mut acc_traders=FiNum::new(0);
for t in &self.traders { acc_traders+=t.get_balance(*cur); } for t in &self.traders { acc_traders+=t.get_balance(*cur); }
@@ -144,8 +148,9 @@ impl Market {
for (ap,pq) in &self.orders { for (ap,pq) in &self.orders {
println!("Orders selling {} to buy {}:",self.number_to_name(ap.0),self.number_to_name(ap.1)); println!("Orders selling {} to buy {}:",self.number_to_name(ap.0),self.number_to_name(ap.1));
let mut sorted=pq.v.clone(); let mut sorted=pq.v.clone();
sorted.sort_by(|a,b| (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty))); sorted.sort_by(|a,b| { let a=a.borrow(); let b=b.borrow(); (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)) });
for off in sorted.iter() { for off in sorted.iter() {
let off=off.borrow();
println!(" {} @ {} ({})", println!(" {} @ {} ({})",
off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty, off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty,
off.buy_qty/off.sell_qty, off.buy_qty/off.sell_qty,
@@ -158,24 +163,20 @@ impl Market {
{ {
let initial_balance=self.traders[owner as usize].get_balance(sell_type); let initial_balance=self.traders[owner as usize].get_balance(sell_type);
if initial_balance<sell_qty_initial { return false; } if initial_balance<sell_qty_initial { return false; }
// println!("INITIAL_BALANCE {} Selling {} {} to buy {} {}",initial_balance,sell_qty_initial,self.number_to_name(sell_type),buy_qty_initial,self.number_to_name(buy_type));
let mut buy_qty=buy_qty_initial; let mut buy_qty=buy_qty_initial;
let ap=(buy_type,sell_type); let ap=(buy_type,sell_type);
while buy_qty>FiNum::new(0) && self.orders.contains_key(&ap) && self.orders.get(&ap).unwrap().v.len()>0 { while buy_qty>FiNum::new(0) && self.orders.contains_key(&ap) && self.orders.get(&ap).unwrap().v.len()>0 {
let elt=&mut self.orders.get_mut(&ap).unwrap().v[0]; let mut elt=(*(self.orders.get(&ap).unwrap().v[0].borrow())).clone();
if sell_qty_initial/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate if sell_qty_initial/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate
let qty=std::cmp::min(elt.sell_remain,buy_qty); 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; elt.sell_remain-=qty;
buy_qty-=qty; buy_qty-=qty;
let pay_qty=qty*elt.buy_qty/elt.sell_qty; // 1.8499*194623/2.9744 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].sub_balance(sell_type,pay_qty);
self.traders[owner as usize].add_balance(buy_type ,qty); self.traders[owner as usize].add_balance(buy_type ,qty);
self.traders[elt.owner as usize].add_balance(sell_type,pay_qty); self.traders[elt.owner as usize].add_balance(sell_type,pay_qty);
if elt.sell_remain==0.into() { self.orders.get_mut(&ap).unwrap().pop(); } if elt.sell_remain==0.into() { self.orders.get_mut(&ap).unwrap().pop(); }
else { self.orders.get(&ap).unwrap().v[0].borrow_mut().sell_remain-=qty; }
} else { break; } } else { break; }
} }
if buy_qty>0.into() { if buy_qty>0.into() {
@@ -184,7 +185,8 @@ impl Market {
let bids=self.orders.get_mut(&ap).unwrap(); let bids=self.orders.get_mut(&ap).unwrap();
let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial; let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial;
if sell_qty_remain>0.into() { if sell_qty_remain>0.into() {
bids.insert(Rc::new(RefCell::new(Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty } ))); let neworder=Rc::new(RefCell::new(Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty } ));
bids.insert(neworder);
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain); self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
} }
} }
@@ -272,7 +274,6 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
impl Market { impl Market {
fn exercise(&mut self) { fn exercise(&mut self) {
let mut rng: StdRng=StdRng::seed_from_u64(13u64); let mut rng: StdRng=StdRng::seed_from_u64(13u64);
let debug_index=100000000;
let teppy=self.register_trader("Teppy"); let teppy=self.register_trader("Teppy");
let luni =self.register_trader("Luni"); let luni =self.register_trader("Luni");
let usd =self.register_asset("USD"); let usd =self.register_asset("USD");
@@ -281,7 +282,7 @@ impl Market {
self.add_trader_balance(luni ,usd,650000000.into()); self.add_trader_balance(luni ,usd,650000000.into());
let mut count=0; let mut count=0;
let mut tries=0; let mut tries=0;
for i in 1..=10000000 { for _i in 1..=10000000 {
let seller=if rng.gen_bool(0.5) { teppy } else { luni }; let seller=if rng.gen_bool(0.5) { teppy } else { luni };
let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum); let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum);
if rng.gen_bool(0.5) { if rng.gen_bool(0.5) {
@@ -295,8 +296,8 @@ impl Market {
buy_qty=rng.gen_range(1..=5).into(); buy_qty=rng.gen_range(1..=5).into();
sell_qty=buy_qty*rng.gen_range(60..=70).into(); sell_qty=buy_qty*rng.gen_range(60..=70).into();
} }
let mut success=false; let mut _success=false;
if self.make_order(seller,sell_type,buy_type,sell_qty,buy_qty) { count+=1; success=true; }; if self.make_order(seller,sell_type,buy_type,sell_qty,buy_qty) { count+=1; _success=true; };
if count>=1000000 { break; } if count>=1000000 { break; }
tries+=1; tries+=1;
} }
@@ -330,16 +331,28 @@ fn wt_parent(index:u64) -> u64 {
first_in_parent_row+nth_in_parent_row*skip_in_parent_row first_in_parent_row+nth_in_parent_row*skip_in_parent_row
} }
fn wt_forefather(max_index:u64) -> u64 {
let mut rval=max_index;
rval=rval|(rval>>1);
rval=rval|(rval>>2);
rval=rval|(rval>>4);
rval=rval|(rval>>8);
rval=rval|(rval>>16);
rval=rval|(rval>>32);
if rval>max_index { wt_left(rval).unwrap() } else { rval }
}
fn tree_stuff() { fn tree_stuff() {
for i in (0..=60).step_by(1) { for i in (0..=60).step_by(1) {
println!("Index {} Parent {}",i,wt_parent(i)); println!("Index {} Forefather {} Parent {}",i,wt_forefather(i),wt_parent(i));
} }
} }
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let mode=if args.len()<=1 { "--exercise" } else { args[1].as_str() }; let mode=if args.len()<=1 { "--exercise" } else { args[1].as_str() };
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 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
match mode { match mode {
"--exercise" => m.exercise(), "--exercise" => m.exercise(),
"--treestuff" => tree_stuff(), "--treestuff" => tree_stuff(),