Match complementary binary orders and settle paired issuance or merge

This commit is contained in:
2026-12-03 03:49:42 +08:00
parent 4eb80fa458
commit 47d49cb8c9
10 changed files with 238 additions and 38 deletions
+74
View File
@@ -419,3 +419,77 @@ fn rejected_sell_fok_releases_inventory() {
assert!(e.execute(sell).is_err());
assert_eq!(state(&mut e), before);
}
#[test]
fn complementary_buyers_create_inventory_then_sellers_release_collateral() {
let dir = tempdir().unwrap();
let path = dir.path().join("paired.db");
let mut e = Exchange::open(&path).unwrap();
for account in ["alice", "bob"] {
e.execute(json!({"op":"fund","id":account,"account":account,"cents":1000}))
.unwrap();
}
e.execute(json!({"op":"list","id":"market","market":"e","rules":"official result"}))
.unwrap();
let mut no = order("first", "bob", "buy", 40, 10);
no["outcome"] = json!("NO");
e.execute(no).unwrap();
e.execute(order("second", "alice", "buy", 60, 10)).unwrap();
e.execute(json!({"op":"open","id":"open","market":"e"}))
.unwrap();
let s = state(&mut e);
assert_eq!(s["paired_fills"].as_array().unwrap().len(), 1);
assert_eq!(s["paired_fills"][0]["kind"], "mint");
assert_eq!(s["markets"][0]["collateral"], 1000);
assert_eq!(s["accounts"][0]["cash"], 400);
assert_eq!(s["accounts"][1]["cash"], 600);
e.execute(order("rest", "alice", "sell", 55, 10)).unwrap();
drop(e);
let mut e = Exchange::open(path).unwrap();
let quote = e
.execute(json!({"op":"quote","market":"e","outcome":"NO"}))
.unwrap();
assert_eq!(quote["best_bid_micros"], 450000);
let mut sell = order("exit", "bob", "sell", 41, 10);
sell["outcome"] = json!("NO");
sell["tif"] = json!("FOK");
let reply = e.execute(sell.clone()).unwrap();
assert_eq!(reply["filled"], 10);
assert_eq!(e.execute(sell).unwrap(), reply);
let s = state(&mut e);
assert_eq!(s["paired_fills"][1]["kind"], "merge");
assert_eq!(s["markets"][0]["collateral"], 0);
assert_eq!(s["accounts"][0]["cash"], 950);
assert_eq!(s["accounts"][1]["cash"], 1050);
assert!(s["positions"]
.as_array()
.unwrap()
.iter()
.all(|p| p["quantity"] == 0));
let history=e.execute(json!({"op":"history","market":"e","outcome":"NO","from_ms":0,"to_ms":i64::MAX,"limit":3})).unwrap();
assert_eq!(history["points"][0]["price_micros"], 400000);
assert_eq!(history["points"][1]["price_micros"], 450000);
}
#[test]
fn direct_and_complementary_liquidity_share_price_time_priority() {
let mut e = Exchange::open(":memory:").unwrap();
setup(&mut e);
e.execute(json!({"op":"open","id":"open","market":"e"}))
.unwrap();
let mut no = order("rest-no", "bob", "buy", 40, 2);
no["outcome"] = json!("NO");
e.execute(no).unwrap(); // projected ask 60, before direct ask at 60
e.execute(order("equal", "alice", "sell", 60, 3)).unwrap();
e.execute(order("better", "alice", "sell", 59, 1)).unwrap();
let mut buy = order("sweep", "alice", "buy", 63, 4);
buy["tif"] = json!("FOK");
assert_eq!(e.execute(buy).unwrap()["filled"], 4);
let s = state(&mut e);
assert_eq!(s["fills"][0]["seq"], 1);
assert_eq!(s["fills"][0]["price"], 59);
assert_eq!(s["paired_fills"][0]["seq"], 2);
assert_eq!(s["paired_fills"][0]["quantity"], 2);
assert_eq!(s["fills"][1]["seq"], 3);
assert_eq!(s["fills"][1]["quantity"], 1);
}