Apply market tick changes atomically and resume their event history
This commit is contained in:
@@ -2236,3 +2236,79 @@ fn configured_ticks_bind_order_amounts_execution_and_opening_price() {
|
||||
assert!(e.execute(json!({"op":"assets","market":"legacy"})).unwrap()["tick_size"].is_null());
|
||||
e.execute(json!({"op":"order","id":"old","market":"legacy","outcome":"YES","account":"bob","side":"buy","price":0,"qty":1})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replayed_tick_changes_preserve_valid_orders_and_release_only_residuals() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("tick-changes.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
let mut original = Value::Null;
|
||||
let mut cursor = Value::Null;
|
||||
for line in include_str!("../../../docs/examples/tick-changes.jsonl").lines() {
|
||||
let cmd: Value = serde_json::from_str(line).unwrap();
|
||||
let response = e.execute(cmd.clone()).unwrap();
|
||||
match cmd["op"].as_str().unwrap() {
|
||||
"tick_size_change" if cmd["id"] == "finer" => {
|
||||
assert_eq!(response["canceled"], json!([]))
|
||||
}
|
||||
"tick_size_change" => {
|
||||
assert_eq!(response["canceled"], json!([1, 3]));
|
||||
original = response;
|
||||
}
|
||||
"limit_amounts" => assert_eq!(response["collateral_micros"], 65000),
|
||||
"book" => {
|
||||
assert_eq!(response["tick_seq"], original["tick_seq"]);
|
||||
assert_eq!(response["tick_size"], "0.1");
|
||||
assert_eq!(response["bids"], json!([{"price_micros":400000,"size":1}]));
|
||||
}
|
||||
"balance" => {
|
||||
assert_eq!(response["available_micros"], 18970000);
|
||||
assert_eq!(response["reserved_micros"], 400000);
|
||||
}
|
||||
"tick_history" => {
|
||||
assert_eq!(response["has_more"], true);
|
||||
cursor = response["cursor"].clone();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
drop(e);
|
||||
let mut e = Exchange::open(path).unwrap();
|
||||
let before = state(&mut e);
|
||||
let position = before["positions"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|p| p["account"] == "alice" && p["outcome"] == "NO")
|
||||
.unwrap();
|
||||
assert_eq!(position["quantity"], 4);
|
||||
let event = json!({"op":"tick_size_change","asset_id":"eb:YES:cup","id":"mirror","new_tick_size":"0.1000","timestamp":"1757908892400"});
|
||||
assert_eq!(e.execute(event.clone()).unwrap(), original);
|
||||
let mut stale = event.clone();
|
||||
stale["id"] = json!("stale");
|
||||
stale["timestamp"] = json!("1757908892375");
|
||||
let mut conflict = event.clone();
|
||||
conflict["id"] = json!("conflict");
|
||||
conflict["new_tick_size"] = json!("0.01");
|
||||
let mut mismatch = event;
|
||||
mismatch["id"] = json!("wrong-old");
|
||||
mismatch["timestamp"] = json!("1757908892410");
|
||||
mismatch["old_tick_size"] = json!("0.005");
|
||||
mismatch["new_tick_size"] = json!("0.001");
|
||||
for bad in [
|
||||
stale,
|
||||
conflict,
|
||||
mismatch,
|
||||
json!({"op":"order","id":"offgrid","market":"cup","outcome":"YES","side":"buy","account":"bob","price":51,"qty":1}),
|
||||
] {
|
||||
assert!(e.execute(bad).is_err());
|
||||
}
|
||||
assert_eq!(before, state(&mut e));
|
||||
let page = e
|
||||
.execute(json!({"op":"tick_history","asset_id":"eb:NO:cup","after":cursor}))
|
||||
.unwrap();
|
||||
assert_eq!(page["events"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(page["events"][0]["asset_id"], "eb:NO:cup");
|
||||
assert_eq!(page["events"][0]["seq"], original["tick_seq"]);
|
||||
assert_eq!(page["has_more"], false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user