This commit is contained in:
2024-05-19 11:24:59 -04:00
parent 86a7fcd698
commit da9e907932

View File

@@ -128,7 +128,7 @@ impl Market {
sorted.sort_by(|a,b| (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty))); sorted.sort_by(|a,b| (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)));
for off in sorted.iter() { for off in sorted.iter() {
println!(" {} @ {} ({})", println!(" {} @ {} ({})",
off.buy_qty*(off.sell_remain/off.sell_qty), off.buy_qty*off.sell_remain/off.sell_qty,
off.sell_qty/off.buy_qty, off.sell_qty/off.buy_qty,
self.traders[off.owner as usize].name); self.traders[off.owner as usize].name);
} }
@@ -151,16 +151,18 @@ impl Market {
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.offers.get_mut(&ap).unwrap().pop(); } if elt.sell_remain==0.into() { self.offers.get_mut(&ap).unwrap().pop(); }
} } else { break; }
} }
if buy_qty>0.into() { if buy_qty>0.into() {
let ap=(sell_type,buy_type); let ap=(sell_type,buy_type);
if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); } if let None=self.offers.get_mut(&ap) { self.offers.insert(ap,PQueue::new()); }
let bids=self.offers.get_mut(&ap).unwrap(); let bids=self.offers.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() {
bids.insert(Offer { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty } ); bids.insert(Offer { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty } );
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain); self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
} }
}
true true
} }
} }
@@ -253,7 +255,7 @@ fn main() {
let btc =m.register_asset("BTC"); let btc =m.register_asset("BTC");
m.add_trader_balance(teppy,btc,100.into()); m.add_trader_balance(teppy,btc,100.into());
m.add_trader_balance(luni ,usd,1000000.into()); m.add_trader_balance(luni ,usd,1000000.into());
for i in 1..=5 { for i in 1..=100 {
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) {
@@ -267,8 +269,16 @@ fn main() {
buy_qty=rng.gen_range(2..=5).into(); buy_qty=rng.gen_range(2..=5).into();
sell_qty=buy_qty*rng.gen_range(60000..=70000).into(); sell_qty=buy_qty*rng.gen_range(60000..=70000).into();
} }
println!("Sell {} {} to buy {} {} ({})",sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),m.traders[seller as usize].name); println!("Index {} Sell {} {} to buy {} {} ({})",i,sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),m.traders[seller as usize].name);
if i==3 {
println!("Fifty-two!");
m.dump();
}
m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty); m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty);
if i==3 {
println!("Fifty-two!");
m.dump();
}
} }
m.dump(); m.dump();
m.sanity_check(); m.sanity_check();