Keep negative-risk conversions collateralized after verdicts and redemption

This commit is contained in:
2026-12-03 13:10:44 +08:00
parent 0f608db47f
commit 98eece1c50
5 changed files with 128 additions and 29 deletions
+70
View File
@@ -3876,3 +3876,73 @@ fn multi_leg_cash_fees_round_before_the_multiplier() {
);
}
}
#[test]
fn converted_winning_shares_can_be_claimed_again_after_the_first_payout() {
let dir = tempdir().unwrap();
let path = dir.path().join("reported.sqlite");
let mut e = Exchange::open(&path).unwrap();
for line in include_str!("../../../docs/examples/neg-risk-fee.jsonl").lines() {
e.execute(serde_json::from_str(line).unwrap()).unwrap();
}
for line in include_str!("../../../docs/examples/neg-risk-after-verdict.jsonl").lines() {
let cmd: Value = serde_json::from_str(line).unwrap();
let r = e.execute(cmd.clone()).unwrap();
match cmd["id"].as_str().unwrap() {
"after-a" => {
assert_eq!(state(&mut e)["neg_risk_groups"][0]["reserve_cents"], 50);
assert_eq!(state(&mut e)["neg_risk_reserve_remainders"][0]["micros"], 2);
}
"first-user" => assert_eq!(r["paid"], json!(49.3752)),
"first-vault" => assert_eq!(r["paid"], json!(0.625)),
"after-all" => {
drop(e);
e = Exchange::open(&path).unwrap();
let snapshot = state(&mut e);
assert_eq!(e.execute(cmd.clone()).unwrap(), r);
let mut insufficient = cmd;
insufficient["id"] = json!("spent-no");
assert!(e.execute(insufficient).is_err());
assert_eq!(state(&mut e), snapshot);
assert_eq!(snapshot["neg_risk_groups"][0]["reserve_cents"], 0);
assert_eq!(snapshot["neg_risk_reserve_remainders"], json!([]));
}
"second-user" => assert_eq!(r["paid"], json!(24.6876)),
"second-vault" => assert_eq!(r["paid"], json!(0.3125)),
_ => {}
}
}
for (account, expected) in [("user", 29981250), ("vault", 18750)] {
assert_eq!(
e.execute(json!({"op":"balance","account":account}))
.unwrap()["available_micros"],
expected
);
}
}
#[test]
fn converting_a_winners_no_preserves_collateral_for_its_yes_holders() {
let mut e = Exchange::open(":memory:").unwrap();
for line in include_str!("../../../docs/examples/neg-risk.jsonl")
.lines()
.take(8)
{
e.execute(serde_json::from_str(line).unwrap()).unwrap();
}
e.execute(json!({"op":"close","id":"close-a","market":"a"}))
.unwrap();
e.execute(json!({"op":"finalize","id":"a-wins","market":"a","winner":"YES"}))
.unwrap();
let cash=e.execute(json!({"op":"convert","id":"cv","account":"user","group":"winner","qty":2,"no_markets":["a","b"]})).unwrap();
assert_eq!(cash["released_cents"], 200);
assert_eq!(
e.execute(json!({"op":"redeem","id":"redeem-a","account":"user","market":"a"}))
.unwrap()["paid"],
200
);
assert_eq!(
e.execute(json!({"op":"balance","account":"user"})).unwrap()["available_micros"],
30000000
);
}