changes
This commit is contained in:
56
src/main.rs
56
src/main.rs
@@ -27,6 +27,25 @@ struct Trader {
|
||||
balances: HashMap<usize,FiNum>, // Maps Currency to Amount
|
||||
}
|
||||
|
||||
struct Asset {
|
||||
name: String,
|
||||
royalty0_rate: FiNum, // Traders get this when entered
|
||||
royalty1_rate: FiNum, // Traders get this when executed
|
||||
commission0_rate: FiNum, // House gets this when entered
|
||||
commission1_rate: FiNum, // House gets this when executed
|
||||
}
|
||||
|
||||
impl Asset {
|
||||
fn new(name: &str) -> Self {
|
||||
Asset {
|
||||
name: String::from(name),
|
||||
royalty0_rate:FiNum::zero(),
|
||||
royalty1_rate:FiNum::zero(),
|
||||
commission0_rate:FiNum::zero(),
|
||||
commission1_rate:FiNum::zero(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Trader {
|
||||
fn new(name:&str,id:usize) -> Self {
|
||||
Trader {
|
||||
@@ -195,33 +214,33 @@ impl Dumpable for Order {
|
||||
|
||||
struct Market {
|
||||
asset_name2num: HashMap<String,usize>,
|
||||
asset_num2name: HashMap<usize,String>,
|
||||
asset_count:usize,
|
||||
assets: Vec<Asset>,
|
||||
money_supply: HashMap<usize,FiNum>,
|
||||
traders: Vec<Trader>,
|
||||
trader_name2num: HashMap<String,usize>,
|
||||
orders: HashMap<(usize,usize),PQueue<Rc<RefCell<Order>>>>,
|
||||
royalties: HashMap<usize,RoyaltyTree>, // Active orders that are accepting asset X. They receive royalties when someone makes an order to sell X
|
||||
royalty_rate: HashMap<usize,FiNum>,
|
||||
}
|
||||
|
||||
impl Market {
|
||||
fn new() -> Self {
|
||||
let mut rval=Market {
|
||||
asset_name2num: HashMap::new(),
|
||||
asset_num2name: HashMap::new(),
|
||||
asset_count:0,
|
||||
assets: Vec::new(),
|
||||
money_supply: HashMap::new(),
|
||||
orders: HashMap::new(),
|
||||
royalties: HashMap::new(),
|
||||
traders: Vec::new(),
|
||||
trader_name2num: HashMap::new(),
|
||||
orders: HashMap::new(),
|
||||
royalties: HashMap::new(),
|
||||
royalty_rate: HashMap::new(),
|
||||
};
|
||||
rval.register_trader("*NONE*");
|
||||
rval
|
||||
}
|
||||
fn distribute_royalty(&self, amount:FiNum) {
|
||||
|
||||
}
|
||||
fn number_to_name(&self, n: usize) -> String {
|
||||
self.assets[n].name.clone()
|
||||
}
|
||||
fn sanity_check(&self) {
|
||||
println!("Sanity Checking Market...");
|
||||
for (cur,amt) in self.money_supply.iter() {
|
||||
@@ -252,18 +271,14 @@ impl Market {
|
||||
*self.money_supply.get_mut(&cur).unwrap()-=delta;
|
||||
}
|
||||
fn register_asset(&mut self, name:&str) -> usize {
|
||||
self.asset_count+=1;
|
||||
self.asset_name2num.insert(String::from(name),self.asset_count);
|
||||
self.asset_num2name.insert(self.asset_count,String::from(name));
|
||||
self.money_supply.insert(self.asset_count,FiNum::new(0));
|
||||
self.asset_count
|
||||
self.assets.push(Asset::new(name));
|
||||
self.asset_name2num.insert(String::from(name),self.assets.len()-1);
|
||||
self.money_supply.insert(self.assets.len()-1,FiNum::new(0));
|
||||
self.assets.len()-1
|
||||
}
|
||||
fn name_to_number(&self, name:&str) -> usize {
|
||||
*self.asset_name2num.get(name).unwrap()
|
||||
}
|
||||
fn number_to_name(&self, num:usize) -> &str {
|
||||
&*self.asset_num2name.get(&num).unwrap()
|
||||
}
|
||||
fn dump(&self) {
|
||||
println!("Dumping Market:");
|
||||
for t in &self.traders {
|
||||
@@ -312,10 +327,11 @@ impl Market {
|
||||
let bids=self.orders.get_mut(&ap).unwrap();
|
||||
let sell_qty_remain=sell_qty_initial*buy_qty/buy_qty_initial;
|
||||
if sell_qty_remain>0.into() {
|
||||
// self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).insert(sell_qty_remain);
|
||||
// let rt_loc=self.royalties.get(&sell_type).unwrap().tree.len();
|
||||
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.
|
||||
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: 0 } ));
|
||||
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;
|
||||
bids.insert(neworder);
|
||||
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user