changes
This commit is contained in:
54
src/main.rs
54
src/main.rs
@@ -27,6 +27,25 @@ struct Trader {
|
|||||||
balances: HashMap<usize,FiNum>, // Maps Currency to Amount
|
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 {
|
impl Trader {
|
||||||
fn new(name:&str,id:usize) -> Self {
|
fn new(name:&str,id:usize) -> Self {
|
||||||
Trader {
|
Trader {
|
||||||
@@ -195,32 +214,32 @@ impl Dumpable for Order {
|
|||||||
|
|
||||||
struct Market {
|
struct Market {
|
||||||
asset_name2num: HashMap<String,usize>,
|
asset_name2num: HashMap<String,usize>,
|
||||||
asset_num2name: HashMap<usize,String>,
|
assets: Vec<Asset>,
|
||||||
asset_count:usize,
|
|
||||||
money_supply: HashMap<usize,FiNum>,
|
money_supply: HashMap<usize,FiNum>,
|
||||||
traders: Vec<Trader>,
|
traders: Vec<Trader>,
|
||||||
trader_name2num: HashMap<String,usize>,
|
trader_name2num: HashMap<String,usize>,
|
||||||
orders: HashMap<(usize,usize),PQueue<Rc<RefCell<Order>>>>,
|
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
|
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 {
|
impl Market {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
let mut rval=Market {
|
let mut rval=Market {
|
||||||
asset_name2num: HashMap::new(),
|
asset_name2num: HashMap::new(),
|
||||||
asset_num2name: HashMap::new(),
|
assets: Vec::new(),
|
||||||
asset_count:0,
|
|
||||||
money_supply: HashMap::new(),
|
money_supply: HashMap::new(),
|
||||||
orders: HashMap::new(),
|
|
||||||
royalties: HashMap::new(),
|
|
||||||
traders: Vec::new(),
|
traders: Vec::new(),
|
||||||
trader_name2num: HashMap::new(),
|
trader_name2num: HashMap::new(),
|
||||||
|
orders: HashMap::new(),
|
||||||
|
royalties: HashMap::new(),
|
||||||
|
royalty_rate: HashMap::new(),
|
||||||
};
|
};
|
||||||
rval.register_trader("*NONE*");
|
rval.register_trader("*NONE*");
|
||||||
rval
|
rval
|
||||||
}
|
}
|
||||||
fn distribute_royalty(&self, amount:FiNum) {
|
fn number_to_name(&self, n: usize) -> String {
|
||||||
|
self.assets[n].name.clone()
|
||||||
}
|
}
|
||||||
fn sanity_check(&self) {
|
fn sanity_check(&self) {
|
||||||
println!("Sanity Checking Market...");
|
println!("Sanity Checking Market...");
|
||||||
@@ -252,18 +271,14 @@ impl Market {
|
|||||||
*self.money_supply.get_mut(&cur).unwrap()-=delta;
|
*self.money_supply.get_mut(&cur).unwrap()-=delta;
|
||||||
}
|
}
|
||||||
fn register_asset(&mut self, name:&str) -> usize {
|
fn register_asset(&mut self, name:&str) -> usize {
|
||||||
self.asset_count+=1;
|
self.assets.push(Asset::new(name));
|
||||||
self.asset_name2num.insert(String::from(name),self.asset_count);
|
self.asset_name2num.insert(String::from(name),self.assets.len()-1);
|
||||||
self.asset_num2name.insert(self.asset_count,String::from(name));
|
self.money_supply.insert(self.assets.len()-1,FiNum::new(0));
|
||||||
self.money_supply.insert(self.asset_count,FiNum::new(0));
|
self.assets.len()-1
|
||||||
self.asset_count
|
|
||||||
}
|
}
|
||||||
fn name_to_number(&self, name:&str) -> usize {
|
fn name_to_number(&self, name:&str) -> usize {
|
||||||
*self.asset_name2num.get(name).unwrap()
|
*self.asset_name2num.get(name).unwrap()
|
||||||
}
|
}
|
||||||
fn number_to_name(&self, num:usize) -> &str {
|
|
||||||
&*self.asset_num2name.get(&num).unwrap()
|
|
||||||
}
|
|
||||||
fn dump(&self) {
|
fn dump(&self) {
|
||||||
println!("Dumping Market:");
|
println!("Dumping Market:");
|
||||||
for t in &self.traders {
|
for t in &self.traders {
|
||||||
@@ -312,10 +327,11 @@ impl Market {
|
|||||||
let bids=self.orders.get_mut(&ap).unwrap();
|
let bids=self.orders.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() {
|
if sell_qty_remain>0.into() {
|
||||||
// self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).insert(sell_qty_remain);
|
let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new());
|
||||||
// let rt_loc=self.royalties.get(&sell_type).unwrap().tree.len();
|
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(
|
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);
|
bids.insert(neworder);
|
||||||
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
self.traders[owner as usize].sub_balance(sell_type,sell_qty_remain);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user