Derive market order protection from visible opposing depth

This commit is contained in:
2026-12-03 07:18:25 +08:00
parent f90605bfc7
commit 5d82be0091
10 changed files with 201 additions and 33 deletions
+59
View File
@@ -2686,3 +2686,62 @@ fn unsafe_encoded_buy_is_rejected_by_preview_estimate_and_submission() {
}
assert_eq!(state(&mut e), before);
}
#[test]
fn derives_protection_from_current_depth_and_replays_the_original_receipt() {
let dir = tempdir().unwrap();
let path = dir.path().join("book.db");
let mut writer = Exchange::open(&path).unwrap();
let mut client = Exchange::open(&path).unwrap();
for line in include_str!("../../../docs/examples/depth-protection.jsonl").lines() {
let cmd: Value = serde_json::from_str(line).unwrap();
match cmd["op"].as_str().unwrap() {
"estimate" => {
let result = client.execute(cmd.clone()).unwrap();
assert_eq!(result["filled_shares"], 5);
if cmd["tif"] == "FOK" {
assert!(result["protection_price_micros"].is_null());
assert_eq!(result["executable"], false);
let before = state(&mut client);
assert!(client.execute(json!({"op":"market_buy","id":"short","asset_id":"eb:YES:fixture","account":"taker","amount_cents":231,"tif":"FOK"})).is_err());
assert_eq!(state(&mut client), before);
} else {
assert_eq!(result["protection_price_micros"], 500000);
assert_eq!(result["executable"], true);
}
}
"market_buy" => {
let r = client.execute(cmd.clone()).unwrap();
assert_eq!(r["protection_price_micros"], 500000);
assert_eq!(r["filled"], 4);
assert_eq!(r["spent_cents"], 180);
drop(client);
client = Exchange::open(&path).unwrap();
assert_eq!(client.execute(cmd).unwrap(), r);
}
"market_sell" => {
let r = client.execute(cmd).unwrap();
assert_eq!(r["protection_price_micros"], 200000);
assert_eq!(r["proceeds_cents"], 80);
}
_ => {
writer.execute(cmd).unwrap();
}
}
}
}
#[test]
fn automatic_price_uses_sdk_level_precision_while_cash_execution_is_exact() {
let mut e = Exchange::open(":memory:").unwrap();
setup(&mut e);
e.execute(json!({"op":"open","id":"open","market":"e"}))
.unwrap();
for (id, price) in [("a10", 10), ("a70", 70), ("a90", 90)] {
e.execute(order(id, "alice", "sell", price, 1)).unwrap();
}
let r = e.execute(json!({"op":"market_buy","id":"depth","market":"e","outcome":"YES","account":"bob","amount_cents":80,"tif":"FOK"})).unwrap();
assert_eq!(r["protection_price_micros"], 900000);
assert_eq!(r["spent_cents"], 80);
assert_eq!(r["filled"], 2);
}