Expose account holdings across reservation and redemption states

This commit is contained in:
2026-12-03 04:01:23 +08:00
parent 4a9b696644
commit 3bff1c0bc3
7 changed files with 207 additions and 2 deletions
+70
View File
@@ -584,3 +584,73 @@ fn account_order_pages_track_partial_fills_and_terminal_orders() {
json!([])
);
}
#[test]
fn portfolio_tracks_reserved_pairs_partial_sale_and_redemption() {
let dir = tempdir().unwrap();
let path = dir.path().join("positions.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
e.execute(json!({"op":"open","id":"start","market":"e"}))
.unwrap();
let ask = e.execute(order("ask", "alice", "sell", 60, 10)).unwrap();
let list = json!({"op":"positions","account":"alice","market":"e"});
let find_yes = |page: Value| {
page["positions"]
.as_array()
.unwrap()
.iter()
.find(|p| p["outcome"] == "YES")
.unwrap()
.clone()
};
let held = find_yes(e.execute(list.clone()).unwrap());
assert_eq!(held["size"], 10);
assert_eq!(held["available_size"], 0);
assert_eq!(held["reserved_size"], 10);
assert_eq!(held["mergeable"], true);
assert_eq!(held["available_pairs"], 0);
e.execute(order("take", "bob", "buy", 60, 2)).unwrap();
let held = find_yes(e.execute(list.clone()).unwrap());
assert_eq!(held["size"], 8);
assert_eq!(held["reserved_size"], 8);
e.execute(json!({"op":"cancel","id":"free","account":"alice","order":ask["order"]}))
.unwrap();
assert_eq!(
find_yes(e.execute(list.clone()).unwrap())["available_pairs"],
8
);
e.execute(json!({"op":"close","id":"close","market":"e"}))
.unwrap();
e.execute(json!({"op":"finalize","id":"result","market":"e","winner":"YES"}))
.unwrap();
drop(e);
let mut e = Exchange::open(path).unwrap();
assert_eq!(
find_yes(e.execute(list.clone()).unwrap())["payout_cents"],
800
);
let mut page = json!({"op":"positions","account":"alice","status":"REDEEMABLE","limit":1});
let first = e.execute(page.clone()).unwrap();
assert_eq!(first["positions"][0]["outcome"], "YES");
page["after"] = first["next_after"].clone();
let second = e.execute(page).unwrap();
assert_eq!(second["positions"][0]["outcome"], "NO");
assert!(second["next_after"].is_null());
let lost = e
.execute(json!({"op":"positions","account":"alice","status":"REDEEMABLE_LOST"}))
.unwrap();
assert_eq!(lost["positions"][0]["status"], "REDEEMABLE");
assert_eq!(lost["positions"][0]["payout_cents"], 0);
e.execute(json!({"op":"redeem","id":"collect","account":"alice","market":"e"}))
.unwrap();
assert_eq!(e.execute(list).unwrap()["positions"], json!([]));
let closed = e
.execute(json!({"op":"positions","account":"alice","status":"CLOSED"}))
.unwrap();
assert_eq!(closed["positions"].as_array().unwrap().len(), 2);
let other = e
.execute(json!({"op":"positions","account":"bob"}))
.unwrap();
assert_eq!(other["positions"][0]["payout_cents"], 200);
}