Preview market sweeps and execute protected share sales

This commit is contained in:
2026-12-03 04:31:07 +08:00
parent c0b8a0f229
commit ea6383f089
10 changed files with 278 additions and 7 deletions
+59
View File
@@ -1143,3 +1143,62 @@ fn cash_buy_reconciles_direct_and_complementary_depth_with_an_exact_budget() {
assert_eq!(fok["spent_cents"], 45);
assert_eq!(fok["status"], "filled");
}
#[test]
fn market_estimate_uses_latest_committed_depth_without_reserving_it() {
let dir = tempdir().unwrap();
let path = dir.path().join("estimate.db");
let mut client = Exchange::open(&path).unwrap();
setup(&mut client);
let mut maker = Exchange::open(path).unwrap();
maker
.execute(json!({"op":"open","id":"open","market":"e"}))
.unwrap();
maker
.execute(json!({"op":"split","id":"bm","account":"bob","market":"e","qty":2}))
.unwrap();
maker.execute(order("bid", "bob", "buy", 60, 2)).unwrap();
let mut opposite = order("opposite", "bob", "sell", 45, 2);
opposite["outcome"] = json!("NO");
let resting = maker.execute(opposite).unwrap()["order"].clone();
let preview =
json!({"op":"estimate","asset_id":"eb:YES:e","side":"sell","qty":5,"min_price":50});
let before = state(&mut client);
let quote = client.execute(preview.clone()).unwrap();
assert_eq!(quote["filled_shares"], 4);
assert_eq!(quote["gross_cents"], 230);
assert_eq!(quote["worst_fill_price"], 55);
assert_eq!(quote["average_price_micros"], 575000);
assert_eq!(quote["unsold_shares"], 1);
assert_eq!(state(&mut client), before);
let mut all = preview.clone();
all["tif"] = json!("FOK");
assert_eq!(client.execute(all).unwrap()["executable"], false);
maker
.execute(json!({"op":"cancel","id":"withdraw","account":"bob","order":resting}))
.unwrap();
let fresh = client.execute(preview).unwrap();
let sale = json!({"op":"market_sell","id":"sale","asset_id":"eb:YES:e","account":"alice","qty":5,"min_price":50});
let mut guarded = sale.clone();
guarded["min_price"] = json!(61);
assert!(client.execute(guarded).is_err());
let reply = client.execute(sale.clone()).unwrap();
assert_eq!(client.execute(sale).unwrap(), reply);
assert_eq!(reply["filled"], fresh["filled_shares"]);
assert_eq!(reply["proceeds_cents"], fresh["gross_cents"]);
assert_eq!(reply["proceeds_cents"], 120);
assert_eq!(reply["unsold_shares"], 3);
let empty = client
.execute(json!({"op":"estimate","market":"e","outcome":"YES","side":"sell","qty":1}))
.unwrap();
assert!(empty["worst_fill_price"].is_null());
assert!(empty["average_price_micros"].is_null());
assert_eq!(empty["executable"], false);
maker.execute(order("ask", "alice", "sell", 35, 2)).unwrap();
let buy = client
.execute(json!({"op":"estimate","asset_id":"eb:YES:e","side":"buy","amount_cents":81}))
.unwrap();
assert_eq!(buy["worst_fill_price"], 35);
assert_eq!(buy["gross_cents"], 70);
assert_eq!(buy["unspent_cents"], 11);
}