Totally working, best I can tell

This commit is contained in:
2025-01-24 20:20:37 -05:00
parent 2153a6ce96
commit 8fa172da17
3 changed files with 100153 additions and 29 deletions

100122
1.log

File diff suppressed because it is too large Load Diff

View File

@@ -8,16 +8,3 @@ SR 1 51EB851 28F5C28 7AE147A 28F5C28 ; setroyalty BTC 0.02 0.01 0.03 0.01
AF 1 1 500000000 ; addfunds Teppy 5 BTC AF 1 1 500000000 ; addfunds Teppy 5 BTC
AF 2 0 186A000000000 ; addfunds Luni 100000 USD AF 2 0 186A000000000 ; addfunds Luni 100000 USD
AF 3 0 186A000000000 ; addfunds Hamza 100000 USD AF 3 0 186A000000000 ; addfunds Hamza 100000 USD
OR 1 1 200000000 0 186A000000000 ; [login Teppy] order 2 BTC 100000 USD
NOP OR 2 0 753000000000 1 80000000 ; [login Luni] order 30000 USD 0.5 BTC
NOP OR 2 0 4E2000000000 1 80000000 ; [login Luni] order 20000 USD 0.5 BTC
NOP OR 2 0 55F000000000 1 80000000 ; [login Luni] order 22000 USD 0.5 BTC
NOP OR 2 1 40000000 0 35B600000000 ; [login Luni] order 0.25 BTC 13750 USD
OR 2 0 9C4000000000 1 100000000
OR 3 0 9C4000000000 1 100000000
NOP Error: Count not find currency
OR 3 0 9C4000000000 1 100000000
OR 3 0 C35000000000 1 100000000
AF 3 0 186A000000000
OR 3 0 C35000000000 1 100000000
OR 1 1 100000000 0 9C4000000000

View File

@@ -250,8 +250,12 @@ impl RoyaltyTree {
// We really need two methods: remove an order and redistribute, remove some and return captured amount // We really need two methods: remove an order and redistribute, remove some and return captured amount
fn remove_order_id(&mut self, id: usize) -> FiNum { // And zero out in the tree fn remove_order_id(&mut self, id: usize) -> FiNum { // And zero out in the tree
if let Some(pos)=self.order_finder.remove(&id) { if let Some(pos)=self.order_finder.remove(&id) {
self.capture0(pos);
let weight=self.weight_here(pos); let weight=self.weight_here(pos);
self.sub_weight(pos,weight) self.sub_weight(pos,weight);
let rval=self.tree[pos].acc;
self.tree[pos].acc=FiNum::zero();
rval
} }
else { FiNum::zero() } else { FiNum::zero() }
} }
@@ -271,27 +275,25 @@ impl RoyaltyTree {
} }
None None
} }
fn capture_partial(&mut self, id: usize, weight: FiNum) -> FiNum { fn capture_by_order_id(&mut self, order_id: usize) -> FiNum {
FiNum::zero() if let Some(&index)=self.order_finder.get(&order_id) {
self.capture0(index);
let rval=self.tree[index].acc;
self.tree[index].acc=FiNum::zero();
rval
} else { FiNum::zero() }
} }
fn sub_weight(&mut self, index: usize, mut weight: FiNum) -> FiNum { fn sub_weight(&mut self, index: usize, mut weight: FiNum) {
self.get_royalty(index); self.get_royalty(index);
let delta_count; let delta_count;
let wh=self.weight_here(index); let wh=self.weight_here(index);
let dist;
let redist;
if weight>=wh { if weight>=wh {
delta_count=if wh>FiNum::zero() { 1 } else { 0 }; delta_count=if wh>FiNum::zero() { 1 } else { 0 };
weight=wh; weight=wh;
dist=self.tree[index].acc;
redist=FiNum::zero();
} }
else { else {
delta_count=0; delta_count=0;
dist=self.tree[index].acc*weight/wh;
redist=self.tree[index].acc-dist;
} }
self.tree[index].acc-=redist;
let mut index=index; let mut index=index;
let ff=self.forefather(); let ff=self.forefather();
loop { loop {
@@ -300,8 +302,6 @@ impl RoyaltyTree {
if index==ff { break; } if index==ff { break; }
index=wt_parent(index); index=wt_parent(index);
} }
self.add_royalty(redist);
dist
} }
fn set_location(&mut self, id: usize, index: usize) { fn set_location(&mut self, id: usize, index: usize) {
self.tree[index].order_id=id; self.tree[index].order_id=id;
@@ -639,6 +639,20 @@ impl Market {
//println!("Transact at ask rate. Qty={} elt.sell_remain={} elt.royalty_remain={} elt.commission_remain={}",qty,elt.sell_remain,elt.royalty_remain,elt.commission_remain); //println!("Transact at ask rate. Qty={} elt.sell_remain={} elt.royalty_remain={} elt.commission_remain={}",qty,elt.sell_remain,elt.royalty_remain,elt.commission_remain);
let rq2=if !elt.sell_remain.is_zero() { elt.royalty_remain *qty/elt.sell_remain } else { FiNum::zero() }; let rq2=if !elt.sell_remain.is_zero() { elt.royalty_remain *qty/elt.sell_remain } else { FiNum::zero() };
let cq2=if !elt.sell_remain.is_zero() { elt.commission_remain*qty/elt.sell_remain } else { FiNum::zero() }; let cq2=if !elt.sell_remain.is_zero() { elt.commission_remain*qty/elt.sell_remain } else { FiNum::zero() };
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new());
self.royalties.entry(buy_type) .or_insert(RoyaltyTree::new());
let rtb=self.royalties.entry(buy_type) .or_insert(RoyaltyTree::new());
let rts=self.royalties.entry(sell_type).or_insert(RoyaltyTree::new());
if let Some(&cap_index)=rts.order_finder.get(&elt.order_id) {
let wh0=rts.weight_here(cap_index);
let acc0=rts.tree[cap_index].acc;
let cap=rts.capture_by_order_id(elt.owner);
rts.sub_weight(cap_index,pay_qty);
let wh1=rts.weight_here(cap_index);
let acc1=rts.tree[cap_index].acc;
//println!("Weight {} / {} Acc {} / {} Cap {}",wh0,wh1,acc0,acc1,cap);
self.traders[elt.owner].add_balance(sell_type,cap);
}
//elt.sell_remain-=qty; //elt.sell_remain-=qty;
royalty_acc+=rq0+rq1; royalty_acc+=rq0+rq1;
commission_acc+=cq0+cq1; commission_acc+=cq0+cq1;
@@ -659,7 +673,8 @@ impl Market {
//if !top_order.commission_remain.is_zero() { println!("Little commission? {} ",top_order.commission_remain); } //if !top_order.commission_remain.is_zero() { println!("Little commission? {} ",top_order.commission_remain); }
let rq3=top_order.royalty_remain; let rq3=top_order.royalty_remain;
let cq3=top_order.commission_remain; let cq3=top_order.commission_remain;
let dist=self.royalties.get_mut(&buy_type).unwrap().remove_order_id(top_order.order_id); let dist=self.royalties.get_mut(&buy_type).unwrap().remove_order_id(top_order.order_id);
//println!("Popping top order {} and reclaiming {}",top_order.order_id,dist);
self.traders[elt.owner].add_balance(sell_type,dist); self.traders[elt.owner].add_balance(sell_type,dist);
self.order_finder.remove(&top_order.order_id); self.order_finder.remove(&top_order.order_id);
self.traders[top_order.owner].order_finder.remove(&top_order.order_id); self.traders[top_order.owner].order_finder.remove(&top_order.order_id);
@@ -1265,13 +1280,13 @@ fn interactive(m: &mut Market, mut out: Option<File>) {
} else { println!("Could not find trader {}",username); } } else { println!("Could not find trader {}",username); }
Command::None Command::None
}, },
["seedrandom", seed] => { ["seedrandom"|"sr", seed] => {
let seed=seed.parse().unwrap(); let seed=seed.parse().unwrap();
m.seed_random(seed); m.seed_random(seed);
println!("Seeded RNG with {}",seed); println!("Seeded RNG with {}",seed);
Command::None Command::None
} }
["randomcommands", qty] => { ["randomcommands"|"rc", qty] => {
let qty:u32=qty.parse().unwrap(); let qty:u32=qty.parse().unwrap();
let start = Instant::now(); let start = Instant::now();
for i in 0..qty { for i in 0..qty {