changes
This commit is contained in:
52
src/main.rs
52
src/main.rs
@@ -11,6 +11,7 @@
|
|||||||
#![allow(unsafe_code)]
|
#![allow(unsafe_code)]
|
||||||
#![allow(unused_variables)]
|
#![allow(unused_variables)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
use std::io::{self, BufRead};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
@@ -23,6 +24,7 @@ use finum::FiNum;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Trader {
|
struct Trader {
|
||||||
name: String,
|
name: String,
|
||||||
|
password: String, // hash(name..salt..password)
|
||||||
id: usize,
|
id: usize,
|
||||||
balances: HashMap<usize,FiNum>, // Maps Currency to Amount
|
balances: HashMap<usize,FiNum>, // Maps Currency to Amount
|
||||||
}
|
}
|
||||||
@@ -52,6 +54,7 @@ impl Trader {
|
|||||||
fn new(name:&str,id:usize) -> Self {
|
fn new(name:&str,id:usize) -> Self {
|
||||||
Trader {
|
Trader {
|
||||||
name: String::from(name),
|
name: String::from(name),
|
||||||
|
password: String::from(""),
|
||||||
id: id,
|
id: id,
|
||||||
balances: HashMap::new()
|
balances: HashMap::new()
|
||||||
}
|
}
|
||||||
@@ -98,7 +101,7 @@ impl Royalty {
|
|||||||
|
|
||||||
impl RoyaltyTree {
|
impl RoyaltyTree {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
RoyaltyTree { tree:Vec::new(), next_entry:0, spare_change:FiNum::new() }
|
RoyaltyTree { tree:Vec::new(), next_entry:0, spare_change:FiNum::zero() }
|
||||||
}
|
}
|
||||||
fn weight_here_below(&self, index:usize) -> FiNum {
|
fn weight_here_below(&self, index:usize) -> FiNum {
|
||||||
self.tree[index].weight
|
self.tree[index].weight
|
||||||
@@ -156,7 +159,7 @@ impl RoyaltyTree {
|
|||||||
self.tree[index].acc
|
self.tree[index].acc
|
||||||
}
|
}
|
||||||
fn add_royalty(&mut self, amount: FiNum) {
|
fn add_royalty(&mut self, amount: FiNum) {
|
||||||
if self.tree.next_entry==0 { self.spare_change+=amount }
|
if self.next_entry==0 { self.spare_change+=amount }
|
||||||
else {
|
else {
|
||||||
let ff=self.forefather();
|
let ff=self.forefather();
|
||||||
if self.weight_here_below(ff).is_zero() { self.spare_change+=amount } else { self.tree[ff].lazy+=amount };
|
if self.weight_here_below(ff).is_zero() { self.spare_change+=amount } else { self.tree[ff].lazy+=amount };
|
||||||
@@ -320,16 +323,13 @@ impl Market {
|
|||||||
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_qty_plus=sell_qty_initial+royalty0+royalty1+commission0+commission1; // This is the maximum amount we are going to sell
|
let sell_qty_plus=sell_qty_initial+royalty0_qty+royalty1_qty+commission0_qty+commission1_qty; // This is the maximum amount we are going to sell
|
||||||
let mut sell_qty=sell_qty_initial;
|
let sell_qty=sell_qty_initial;
|
||||||
if initial_balance<sell_qty_plus { return false; }
|
if initial_balance<sell_qty_plus { return false; }
|
||||||
let mut buy_qty=buy_qty_initial;
|
let mut buy_qty=buy_qty_initial;
|
||||||
let ap=(buy_type,sell_type);
|
let ap=(buy_type,sell_type);
|
||||||
let mut royalty_acc=FiNum::zero();
|
let mut royalty_acc=FiNum::zero();
|
||||||
let mut commission_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_qty/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
|
||||||
@@ -344,17 +344,23 @@ impl Market {
|
|||||||
let cq0=commission0_qty*pay_qty/sell_qty_initial;
|
let cq0=commission0_qty*pay_qty/sell_qty_initial;
|
||||||
let rq1=royalty1_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 cq1=commission1_qty*pay_qty/sell_qty_initial;
|
||||||
let rq2=elt.royalty_remain*elt.qty/elt.sell_remain;
|
let rq2=elt.royalty_remain*qty/elt.sell_remain;
|
||||||
let cq2=elt.commission_remain*elt.qty/elt.sell_remain;
|
let cq2=elt.commission_remain*qty/elt.sell_remain;
|
||||||
royalty_acc+=rq0+rq1;
|
royalty_acc+=rq0+rq1;
|
||||||
commission_acc+=cq0+cq1;
|
commission_acc+=cq0+cq1;
|
||||||
|
royalty0_qty-=rq0;
|
||||||
|
royalty1_qty-=rq1;
|
||||||
|
commission0_qty-=cq0;
|
||||||
|
commission1_qty-=cq1;
|
||||||
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(rq0+rq1);
|
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(rq0+rq1);
|
||||||
|
self.royalties.entry(buy_type) .or_insert(RoyaltyTree::new()).add_royalty(rq2);
|
||||||
self.orders.get(&ap).unwrap().v[0].borrow_mut().royalty_remain-=rq2;
|
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.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(buy_type,cq2);
|
||||||
self.traders[0].add_balance(cq2); // ...and this
|
|
||||||
self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()).add_royalty(rq2);
|
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
|
if elt.sell_remain==0.into() { // deal with pennies stored in royalty_remain and commission_remain
|
||||||
|
self.traders[0].add_balance(buy_type,self.orders.get(&ap).unwrap().v[0].borrow_mut().commission_remain);
|
||||||
|
self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()).add_royalty(self.orders.get(&ap).unwrap().v[0].borrow_mut().royalty_remain);
|
||||||
self.orders.get_mut(&ap).unwrap().pop();
|
self.orders.get_mut(&ap).unwrap().pop();
|
||||||
} else {
|
} else {
|
||||||
self.orders.get(&ap).unwrap().v[0].borrow_mut().sell_remain-=qty;
|
self.orders.get(&ap).unwrap().v[0].borrow_mut().sell_remain-=qty;
|
||||||
@@ -369,9 +375,9 @@ impl Market {
|
|||||||
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
|
// Still to do: add to royalty tree the remaining royalty0 amount, and add to the house the remaining commission0 amounts, and then add royalty1 and commission1 to the Order
|
||||||
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, royalty_remain:royalty0_qty, commission_remain:commission0_qty } ));
|
||||||
rt.next_entry+=1;
|
rt.next_entry+=1;
|
||||||
bids.insert(neworder);
|
bids.insert(neworder);
|
||||||
self.traders[owner].sub_balance(sell_type,sell_qty_remain);
|
self.traders[owner].sub_balance(sell_type,sell_qty_remain);
|
||||||
@@ -567,12 +573,30 @@ fn royalty_stuff() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn process_file(fname: &str) {
|
||||||
|
println!("Processing file: {}",fname)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn interactive() {
|
||||||
|
let stdin = io::stdin();
|
||||||
|
for line in stdin.lock().lines() {
|
||||||
|
if let Ok(input) = line {
|
||||||
|
let words: Vec<&str> = input.split_whitespace().collect();
|
||||||
|
match line {
|
||||||
|
"quit" => break,
|
||||||
|
_ => println!("Unknown Command"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
let mode=if args.len()<=1 { "--exercise" } else { args[1].as_str() };
|
let mode=if args.len()<=1 { "--interactive" } else { args[1].as_str() };
|
||||||
let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, zKN6FBdD SOL type is 12
|
let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, zKN6FBdD SOL type is 12
|
||||||
match mode {
|
match mode {
|
||||||
|
"--interactive" => interactive(),
|
||||||
"--exercise" => m.exercise(),
|
"--exercise" => m.exercise(),
|
||||||
"--treestuff" => tree_stuff(),
|
"--treestuff" => tree_stuff(),
|
||||||
"--royaltystuff" => royalty_stuff(),
|
"--royaltystuff" => royalty_stuff(),
|
||||||
|
|||||||
Reference in New Issue
Block a user