Account for fractional outcome sets from split through redemption
This commit is contained in:
@@ -2392,3 +2392,83 @@ fn kickoff_clears_three_books_once_and_returns_only_remaining_reserves() {
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_pairs_share_reservations_and_redeem_after_restart() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("sets.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
for line in include_str!("../../../docs/examples/fractional-inventory.jsonl").lines() {
|
||||
let cmd: Value = serde_json::from_str(line).unwrap();
|
||||
if cmd["id"] == "merge-free" {
|
||||
let mut too_many = cmd.clone();
|
||||
too_many["qty_micros"] = json!(500002);
|
||||
let before = state(&mut e);
|
||||
assert!(e.execute(too_many).is_err());
|
||||
assert_eq!(state(&mut e), before);
|
||||
}
|
||||
if cmd["id"] == "payout" {
|
||||
drop(e);
|
||||
e = Exchange::open(&path).unwrap();
|
||||
}
|
||||
let r = e.execute(cmd.clone()).unwrap();
|
||||
match cmd["op"].as_str().unwrap() {
|
||||
"positions" => {
|
||||
let yes = r["positions"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|p| p["outcome"] == "YES")
|
||||
.unwrap();
|
||||
for (threshold, expected) in
|
||||
[("1.500001", 2), ("1.500001000000001", 0), ("1500001e-6", 2)]
|
||||
{
|
||||
let minimum: serde_json::Number = threshold.parse().unwrap();
|
||||
let holders=e.execute(json!({"op":"holders","market":"match","basis":"gross","min_balance":minimum})).unwrap();
|
||||
assert_eq!(holders["data"].as_array().unwrap().len(), expected);
|
||||
}
|
||||
assert_eq!(yes["size"].to_string(), "1.500001");
|
||||
assert_eq!(yes["available_pairs"].to_string(), "0.500001");
|
||||
assert_eq!(yes["reserved_size"], 1);
|
||||
}
|
||||
"redeem" => {
|
||||
assert_eq!(r["paid"].to_string(), "100.0001");
|
||||
assert_eq!(e.execute(cmd).unwrap(), r);
|
||||
}
|
||||
"balance" => assert_eq!(r["available_micros"], 3_000_000),
|
||||
"activity" => {
|
||||
let rows = r["activity"].as_array().unwrap();
|
||||
assert_eq!(rows[0]["shares"].to_string(), "1.500001");
|
||||
assert_eq!(rows[1]["net_cash_delta_micros"], 500001);
|
||||
assert_eq!(rows.last().unwrap()["shares"].to_string(), "2.000002");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
assert!(state(&mut e)["position_remainders"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn half_payout_keeps_unpayable_base_unit_in_market_backing() {
|
||||
let mut e = Exchange::open(":memory:").unwrap();
|
||||
for cmd in [
|
||||
json!({"op":"fund","id":"a","account":"alice","micros":3}),
|
||||
json!({"op":"list","id":"m","market":"e","rules":"void"}),
|
||||
json!({"op":"split","id":"s","account":"alice","market":"e","qty_micros":3}),
|
||||
json!({"op":"close","id":"c","market":"e"}),
|
||||
json!({"op":"resolve","id":"r","market":"e","winner":"VOID"}),
|
||||
] {
|
||||
e.execute(cmd).unwrap();
|
||||
}
|
||||
assert_eq!(
|
||||
e.execute(json!({"op":"balance","account":"alice"}))
|
||||
.unwrap()["available_micros"],
|
||||
2
|
||||
);
|
||||
let s = state(&mut e);
|
||||
assert_eq!(s["settlement_dust"][0]["micros"], 1);
|
||||
assert_eq!(s["collateral_remainders"][0]["micros"], 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user