This commit is contained in:
2024-05-16 15:50:37 -04:00
parent 35f1d053fd
commit 413ef23cda
2 changed files with 34 additions and 284 deletions

View File

@@ -60,6 +60,7 @@ struct Market {
asset_count:i32,
offers: HashMap<(i32,i32),PQueue<Offer>>,
traders: Vec<Trader>,
trader_name2num: HashMap<String,i32>,
}
impl Market {
@@ -69,14 +70,22 @@ impl Market {
asset_num2name: HashMap::new(),
asset_count:0,
offers: HashMap::new(),
traders: Vec::new()
traders: Vec::new(),
trader_name2num: HashMap::new(),
}
}
fn register_trader(&mut self, name:&str) -> i32 {
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
}
fn add_trader_balance(&mut self, who:i32, cur:i32, delta: u64) {
self.traders[who as usize].add_balance(cur,delta);
}
fn sub_trader_balance(&mut self, who:i32, cur:i32, delta: u64) {
self.traders[who as usize].sub_balance(cur,delta);
}
fn register_asset(&mut self, name:&str) -> i32 {
self.asset_count+=1;
self.asset_name2num.insert(String::from(name),self.asset_count);
@@ -91,6 +100,12 @@ impl Market {
}
fn dump(&self) {
println!("Dumping 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))
}
}
for (key,value) in &self.offers {
println!("Offers selling {} to buy {}:",self.number_to_name(key.0),self.number_to_name(key.1));
value.dump();
@@ -98,6 +113,8 @@ impl Market {
}
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
{ // 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!("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
@@ -108,6 +125,7 @@ impl Market {
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));
if sell_rate>=ask_rate { // Transact at ask_rate
@@ -115,16 +133,20 @@ impl Market {
if buy_qty>=elt.sell_remain {
sell_qty-=fxp_mult(sell_qty,fxp_div(elt.sell_remain,buy_qty_initial));
buy_qty-=elt.sell_remain;
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;
t1.add_balance(sell_type,sell_qty);
sell_qty=0;
buy_qty=0;
} 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);
sell_qty-=fxp_mult(transact,fxp_div(sell_rate,ask_rate));
let mut sell_delta=fxp_mult(transact,fxp_div(sell_rate,ask_rate));
t1.add_balance(sell_type,sell_delta);
sell_qty-=sell_delta;
if elt.sell_remain==0 { println!("Popping asks"); asks.pop(); }
}
} else { break; }
@@ -245,22 +267,16 @@ fn fxp_mult(a: u64, b: u64) -> u64 {
}
fn main() {
let mut t1=Trader::new("Teppy",1);
t1.add_balance(1,100);
t1.add_balance(2,150);
t1.sub_balance(1, 25);
println!("Balances are {} and {}",t1.get_balance(1),t1.get_balance(2));
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 usd=m.register_asset("USD");
let btc=m.register_asset("BTC");
let n=64000u64<<32;
println!("This should be 64000: {} or in raw form {}",fxp_to_fp(n),n);
println!("This should be 1/64000: {} or in raw form {}",fxp_to_fp(fxp_recip(n)),fxp_recip(n));
println!("This should be 64000: {} or in raw form {}",fxp_to_fp(fxp_recip(fxp_recip(n))),fxp_recip(fxp_recip(n)));
println!("This should be 1: {} or in raw form ",fxp_to_fp(fxp_mult(fxp_recip(fxp_from_int(64000)),fxp_from_int(64000))));
println!("{}*{}={}",fxp_to_fp(fxp_from_int(3)),fxp_to_fp(fxp_from_int(5)),fxp_to_fp(fxp_mult(fxp_from_int(3),fxp_from_int(5))));
m.make_offer(0,btc,usd,fxp_from_int(1),fxp_from_int(60000));
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.dump();
m.make_offer(1,usd,btc,fxp_from_int(128000),fxp_from_int(2)); // Buy 1 BTC for 64000 USD
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();
}