Match immediate buys against a cash budget and price cap

This commit is contained in:
2026-12-03 04:27:01 +08:00
parent c27bb60fd2
commit 763b46f43e
11 changed files with 244 additions and 9 deletions
+54
View File
@@ -1089,3 +1089,57 @@ fn market_and_asset_cancels_are_scoped_to_the_original_side() {
.unwrap();
assert_eq!(all, json!({"canceled":[other],"not_canceled":{}}));
}
#[test]
fn cash_buy_reconciles_direct_and_complementary_depth_with_an_exact_budget() {
let dir = tempdir().unwrap();
let path = dir.path().join("budget.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
e.execute(json!({"op":"fund","id":"c","account":"buyer","cents":170}))
.unwrap();
e.execute(json!({"op":"open","id":"start","market":"e"}))
.unwrap();
e.execute(order("ask", "alice", "sell", 35, 2)).unwrap();
let mut no = order("other-side", "alice", "buy", 55, 3);
no["outcome"] = json!("NO");
e.execute(no).unwrap();
let request = json!({"op":"market_buy","id":"budget","asset_id":"eb:YES:e","account":"buyer",
"amount_cents":170,"max_price":50});
let before = state(&mut e);
for (field, value) in [
("tif", json!("FOK")),
("amount_cents", json!(171)),
("max_price", json!(34)),
] {
let mut invalid = request.clone();
invalid[field] = value;
assert!(e.execute(invalid).is_err());
assert_eq!(state(&mut e), before);
}
let receipt = e.execute(request.clone()).unwrap();
assert_eq!(receipt["spent_cents"], 160);
assert_eq!(receipt["unspent_cents"], 10);
assert_eq!(receipt["filled"], 4);
assert_eq!(receipt["status"], "cancelled");
drop(e);
let mut e = Exchange::open(path).unwrap();
assert_eq!(e.execute(request).unwrap(), receipt);
let s = state(&mut e);
assert_eq!(s["accounts"][2]["cash"], 10);
assert_eq!(s["paired_fills"][0]["kind"], "mint");
assert_eq!(s["markets"][0]["collateral"], 1200);
let lookup = e
.execute(json!({"op":"open_orders","account":"buyer","order":receipt["order"]}))
.unwrap();
assert_eq!(lookup["orders"][0]["spent_cents"], 160);
assert_eq!(lookup["orders"][0]["open_size"], 0);
let fok = e
.execute(
json!({"op":"market_buy","id":"exact","market":"e","outcome":"YES",
"account":"bob","amount_cents":45,"max_price":45,"tif":"FOK"}),
)
.unwrap();
assert_eq!(fok["spent_cents"], 45);
assert_eq!(fok["status"], "filled");
}