Seems to work! No sanity errors.
This commit is contained in:
127
src/main.rs
127
src/main.rs
@@ -13,20 +13,20 @@ struct Trader {
|
||||
impl Trader {
|
||||
fn new(name:&str,id:i32) -> Self {
|
||||
Trader {
|
||||
name: String::from(name),
|
||||
id: id,
|
||||
balances: HashMap::new()
|
||||
}
|
||||
name: String::from(name),
|
||||
id: id,
|
||||
balances: HashMap::new()
|
||||
}
|
||||
}
|
||||
fn add_balance(&mut self, cur:i32, delta:FiNum) {
|
||||
self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta);
|
||||
}
|
||||
self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta);
|
||||
}
|
||||
fn sub_balance(&mut self, cur:i32, delta:FiNum) {
|
||||
self.balances.entry(cur).and_modify(|ent| *ent-=delta );
|
||||
}
|
||||
self.balances.entry(cur).and_modify(|ent| *ent-=delta );
|
||||
}
|
||||
fn get_balance(&self, cur:i32) -> FiNum {
|
||||
*self.balances.get(&cur).map(|bal| bal).unwrap_or(&FiNum::new(0u64))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -72,7 +72,7 @@ impl Market {
|
||||
asset_count:0,
|
||||
money_supply: HashMap::new(),
|
||||
offers: HashMap::new(),
|
||||
traders: Vec::new(),
|
||||
traders: Vec::new(),
|
||||
trader_name2num: HashMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -93,9 +93,9 @@ impl Market {
|
||||
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));
|
||||
self.traders.push(Trader::new(name,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: FiNum) {
|
||||
self.traders[who as usize].add_balance(cur,delta);
|
||||
@@ -131,63 +131,33 @@ impl Market {
|
||||
value.dump();
|
||||
}
|
||||
}
|
||||
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 {}",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=sell_qty/buy_qty;
|
||||
fn make_offer(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1
|
||||
{
|
||||
if self.traders[owner as usize].get_balance(sell_type)<sell_qty_initial { return false; }
|
||||
let mut buy_qty=buy_qty_initial;
|
||||
let ap=(buy_type,sell_type);
|
||||
let mut lc=3;
|
||||
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=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=elt.sell_remain/buy_qty_initial;
|
||||
let m=sell_qty*d;
|
||||
let nsq=sell_qty-m;
|
||||
sell_qty-=sell_qty*elt.sell_remain/buy_qty_initial;
|
||||
buy_qty-=elt.sell_remain;
|
||||
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 {} {}",sell_qty,sell_type);
|
||||
t1.add_balance(sell_type,sell_qty);
|
||||
sell_qty=0.into();
|
||||
buy_qty=0.into();
|
||||
} else {
|
||||
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.into() { println!("Popping asks"); asks.pop(); }
|
||||
}
|
||||
} else { break; }
|
||||
} else { break; }
|
||||
} else { break; }
|
||||
lc-=1;
|
||||
}
|
||||
if sell_qty>0.into() {
|
||||
while buy_qty>FiNum::new(0) && self.offers.contains_key(&ap) && self.offers.get(&ap).unwrap().v.len()>0 {
|
||||
let elt=&mut self.offers.get_mut(&ap).unwrap().v[0];
|
||||
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);
|
||||
elt.sell_remain-=qty;
|
||||
buy_qty-=qty;
|
||||
let pay_qty=qty*elt.buy_qty/elt.sell_qty;
|
||||
self.traders[owner as usize].sub_balance(sell_type,pay_qty);
|
||||
self.traders[owner as usize].add_balance(buy_type ,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 buy_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 {}",sell_qty,buy_qty);
|
||||
bids.insert(Offer { owner:owner, sell_qty:sell_qty, sell_remain:sell_qty, buy_qty:buy_qty } );
|
||||
}
|
||||
let bids=self.offers.get_mut(&ap).unwrap();
|
||||
let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial;
|
||||
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);
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,30 +238,7 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
|
||||
}
|
||||
}
|
||||
|
||||
//fn fxp_mult(n0:u64, n1:u64) ->u64 { (((n0>>32)*(n1>>32))<<64)+(((n0>>32)*(n1&0xFFFFFFFF))<<32)+(((n0&0xFFFFFFFF)*(n1>>32))<<32)+(n0&0xFFFFFFFF)*(n1&0xFFFFFFFF) }
|
||||
fn fxp_div(n0:u64, n1:u64) -> u64 { fxp_mult(n0,fxp_recip(n1)) }
|
||||
fn fxp_from_int(n:u32) -> u64 { (n as u64)<<32 }
|
||||
fn fxp_recip(n:u64) -> u64 { 0xFFFFFFFFFFFFFFFFu64/n }
|
||||
fn fxp_to_fp(n:u64) -> f64 { (n as f64)/(1u64<<32) as f64 }
|
||||
fn fxp_mult(a: u64, b: u64) -> u64 {
|
||||
let a_high = a >> 32;
|
||||
let a_low = a & 0xFFFFFFFF;
|
||||
let b_high = b >> 32;
|
||||
let b_low = b & 0xFFFFFFFF;
|
||||
|
||||
let high_high = a_high * b_high;
|
||||
let high_low = a_high * b_low;
|
||||
let low_high = a_low * b_high;
|
||||
let low_low = a_low * b_low;
|
||||
|
||||
let mid_term = high_low + low_high;
|
||||
let carry = mid_term >> 32;
|
||||
|
||||
let result_high = high_high + carry;
|
||||
let result_low = (mid_term << 32) + low_low;
|
||||
|
||||
(result_high << 32) | (result_low >> 32)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a=FiNum::new_i32(2);
|
||||
@@ -305,9 +252,7 @@ fn main() {
|
||||
let btc =m.register_asset("BTC");
|
||||
m.add_trader_balance(teppy,btc,5.into());
|
||||
m.add_trader_balance(luni ,usd,300000.into());
|
||||
m.dump();
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user