in the middle of adding royalties to make_order
This commit is contained in:
55
src/main.rs
55
src/main.rs
@@ -314,48 +314,71 @@ impl Market {
|
|||||||
}
|
}
|
||||||
fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1
|
fn make_order(&mut self, owner:usize, sell_type:usize, buy_type:usize, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1
|
||||||
{
|
{
|
||||||
let initial_balance=self.traders[owner as usize].get_balance(sell_type);
|
let initial_balance=self.traders[owner].get_balance(sell_type);
|
||||||
if initial_balance<sell_qty_initial { return false; }
|
|
||||||
let mut buy_qty=buy_qty_initial;
|
|
||||||
let ap=(buy_type,sell_type);
|
|
||||||
let asset=self.number_to_asset(sell_type);
|
let asset=self.number_to_asset(sell_type);
|
||||||
let mut royalty0_qty=asset.royalty0_rate*sell_qty_initial;
|
let mut royalty0_qty=asset.royalty0_rate*sell_qty_initial;
|
||||||
let mut royalty1_qty=asset.royalty1_rate*sell_qty_initial;
|
let mut royalty1_qty=asset.royalty1_rate*sell_qty_initial;
|
||||||
let mut commission0_qty=asset.commission0_rate*sell_qty_initial;
|
let mut commission0_qty=asset.commission0_rate*sell_qty_initial;
|
||||||
let mut commission1_qty=asset.commission1_rate*sell_qty_initial;
|
let mut commission1_qty=asset.commission1_rate*sell_qty_initial;
|
||||||
let sell_qty0=sell_qty_initial-royalty0_qty-royalty1_qty-commission0_qty-commission1_qty;
|
let sell_qty_plus=sell_qty_initial+royalty0+royalty1+commission0+commission1; // This is the maximum amount we are going to sell
|
||||||
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(royalty0_qty);
|
let mut sell_qty=sell_qty_initial;
|
||||||
self.traders[0].add_balance(sell_type,commission0_qty);
|
if initial_balance<sell_qty_plus { return false; }
|
||||||
self.traders[owner as usize].sub_balance(sell_type,royalty0_qty+commission0_qty);
|
let mut buy_qty=buy_qty_initial;
|
||||||
|
let ap=(buy_type,sell_type);
|
||||||
|
let mut royalty_acc=FiNum::zero();
|
||||||
|
let mut commission_acc=FiNum::zero();
|
||||||
|
// self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(royalty0_qty);
|
||||||
|
// self.traders[0].add_balance(sell_type,commission0_qty);
|
||||||
|
// self.traders[owner].sub_balance(sell_type,royalty0_qty+commission0_qty);
|
||||||
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 mut elt=(*(self.orders.get(&ap).unwrap().v[0].borrow())).clone();
|
let mut elt=(*(self.orders.get(&ap).unwrap().v[0].borrow())).clone();
|
||||||
if sell_qty0/buy_qty_initial>=elt.buy_qty/elt.sell_qty { // Transact at ask_rate
|
if sell_qty/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);
|
||||||
elt.sell_remain-=qty;
|
elt.sell_remain-=qty;
|
||||||
buy_qty-=qty;
|
buy_qty-=qty;
|
||||||
let pay_qty=qty*elt.buy_qty/elt.sell_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 ].sub_balance(sell_type,pay_qty);
|
||||||
self.traders[owner as usize].add_balance(buy_type ,qty);
|
self.traders[owner ].add_balance(buy_type ,qty);
|
||||||
self.traders[elt.owner as usize].add_balance(sell_type,pay_qty);
|
self.traders[elt.owner].add_balance(sell_type,pay_qty);
|
||||||
if elt.sell_remain==0.into() { self.orders.get_mut(&ap).unwrap().pop(); }
|
let rq0=royalty0_qty *pay_qty/sell_qty_initial;
|
||||||
else { self.orders.get(&ap).unwrap().v[0].borrow_mut().sell_remain-=qty; }
|
let cq0=commission0_qty*pay_qty/sell_qty_initial;
|
||||||
|
let rq1=royalty1_qty *pay_qty/sell_qty_initial;
|
||||||
|
let cq1=commission1_qty*pay_qty/sell_qty_initial;
|
||||||
|
let rq2=elt.royalty_remain*elt.qty/elt.sell_remain;
|
||||||
|
let cq2=elt.commission_remain*elt.qty/elt.sell_remain;
|
||||||
|
royalty_acc+=rq0+rq1;
|
||||||
|
commission_acc+=cq0+cq1;
|
||||||
|
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(rq0+rq1);
|
||||||
|
self.orders.get(&ap).unwrap().v[0].borrow_mut().royalty_remain-=rq2;
|
||||||
|
self.orders.get(&ap).unwrap().v[0].borrow_mut().commission_remain-=cq2;
|
||||||
|
self.traders[elt.owner].add_balance(rq2); // Sanity check this, too tired to think about it now...
|
||||||
|
self.traders[0].add_balance(cq2); // ...and this
|
||||||
|
self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()).add_royalty(rq2);
|
||||||
|
if elt.sell_remain==0.into() { // deal with pennies stored in royalty_remain and commission_remain
|
||||||
|
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() {
|
||||||
let ap=(sell_type,buy_type);
|
let ap=(sell_type,buy_type);
|
||||||
if let None=self.orders.get_mut(&ap) { self.orders.insert(ap,PQueue::new()); }
|
if let None=self.orders.get_mut(&ap) { self.orders.insert(ap,PQueue::new()); }
|
||||||
let bids=self.orders.get_mut(&ap).unwrap();
|
let bids=self.orders.get_mut(&ap).unwrap();
|
||||||
let sell_qty_remain=sell_qty0*buy_qty/buy_qty_initial;
|
let sell_qty_remain=sell_qty*buy_qty/buy_qty_initial;
|
||||||
if sell_qty_remain>0.into() {
|
if sell_qty_remain>0.into() {
|
||||||
let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new());
|
let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new());
|
||||||
rt.add_weight(rt.next_entry,buy_qty); // Weight should really be the quantity you would be able to immediately transact rather than how much you wish for.
|
rt.add_weight(rt.next_entry,buy_qty); // Weight should really be the quantity you would be able to immediately transact rather than how much you wish for.
|
||||||
|
// Still to do: add to royalty tree the remaining royalty0+royalty1 amount, and add to the house the remaining commission0+commission1 amounts
|
||||||
let neworder=Rc::new(RefCell::new(
|
let neworder=Rc::new(RefCell::new(
|
||||||
Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt.next_entry } ));
|
Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt.next_entry } ));
|
||||||
rt.next_entry+=1;
|
rt.next_entry+=1;
|
||||||
bids.insert(neworder);
|
bids.insert(neworder);
|
||||||
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
self.traders[owner].sub_balance(sell_type,sell_qty_remain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.traders[0].add_balance(sell_type,royalty_acc+commission_acc);
|
||||||
|
self.traders[owner].sub_balance(sell_type,royalty_acc+commission_acc);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user