Separate final resolution from holder-initiated redemption

This commit is contained in:
2026-12-03 03:43:27 +08:00
parent 6736619277
commit d6eef2f07f
5 changed files with 147 additions and 26 deletions
+52
View File
@@ -314,3 +314,55 @@ fn merge_preserves_reservations_and_collateral_after_restart() {
.execute(json!({"op":"merge","id":"late","market":"e","account":"alice","qty":1}))
.is_err());
}
#[test]
fn explicit_redemption_is_account_scoped_and_restart_safe() {
for (winner, bob_payout, alice_payout) in
[("YES", 300, 700), ("NO", 0, 1000), ("VOID", 150, 850)]
{
let dir = tempdir().unwrap();
let path = dir.path().join("redeem.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
let claim = json!({"id":"claim","op":"redeem","market":"e","account":"bob"});
assert!(e.execute(claim.clone()).is_err());
e.execute(order("offer", "alice", "sell", 50, 3)).unwrap();
e.execute(order("buy", "bob", "buy", 50, 3)).unwrap();
e.execute(json!({"id":"open","op":"open","market":"e"}))
.unwrap();
e.execute(json!({"id":"close","op":"close","market":"e"}))
.unwrap();
let before = state(&mut e);
let final_result = e
.execute(json!({"id":"final","op":"finalize","market":"e","winner":winner}))
.unwrap();
assert_eq!(final_result["outstanding_cents"], 1000);
let after = state(&mut e);
assert_eq!(before["accounts"], after["accounts"]);
assert_eq!(before["positions"], after["positions"]);
assert!(e
.execute(json!({"id":"again","op":"finalize","market":"e","winner":"VOID"}))
.is_err());
let receipt = e.execute(claim.clone()).unwrap();
assert_eq!(receipt["paid"], bob_payout);
drop(e);
let mut e = Exchange::open(&path).unwrap();
assert_eq!(e.execute(claim).unwrap(), receipt);
assert!(e
.execute(json!({"id":"another","op":"redeem","market":"e","account":"bob"}))
.is_err());
assert_eq!(state(&mut e)["markets"][0]["collateral"], alice_payout);
assert_eq!(
e.execute(json!({"id":"alice-claim","op":"redeem","market":"e","account":"alice"}))
.unwrap()["paid"],
alice_payout
);
let final_state = state(&mut e);
assert_eq!(final_state["markets"][0]["collateral"], 0);
assert!(final_state["positions"]
.as_array()
.unwrap()
.iter()
.all(|p| p["quantity"] == 0));
}
}