Serve SDK-compatible order book snapshots from resident depth
This commit is contained in:
@@ -2745,3 +2745,51 @@ fn automatic_price_uses_sdk_level_precision_while_cash_execution_is_exact() {
|
||||
assert_eq!(r["spent_cents"], 80);
|
||||
assert_eq!(r["filled"], 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sdk_book_groups_opposite_outcomes_and_preserves_snapshot_through_restart() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("snapshot.db");
|
||||
let mut writer = Exchange::open(&path).unwrap();
|
||||
let mut reader = Exchange::open(&path).unwrap();
|
||||
let mut snapshots = Vec::new();
|
||||
for line in include_str!("../../../docs/examples/clob-book.jsonl").lines() {
|
||||
let cmd: Value = serde_json::from_str(line).unwrap();
|
||||
if cmd["op"] == "clob_book" {
|
||||
snapshots.push(reader.execute(cmd).unwrap());
|
||||
} else {
|
||||
writer.execute(cmd).unwrap();
|
||||
}
|
||||
}
|
||||
let snapshot = &snapshots[1];
|
||||
assert_eq!(snapshots[0]["bids"], json!([]));
|
||||
assert_ne!(snapshots[0]["hash"], snapshot["hash"]);
|
||||
assert_eq!(snapshot["last_trade_price"], "");
|
||||
assert_eq!(snapshot["min_order_size"], "1");
|
||||
assert_eq!(snapshot["tick_size"], "0.01");
|
||||
assert_eq!(snapshot["neg_risk"], false);
|
||||
assert_eq!(
|
||||
snapshot["bids"],
|
||||
json!([{"price":"0.2","size":"1"},{"price":"0.3","size":"3.5"}])
|
||||
);
|
||||
assert_eq!(
|
||||
snapshot["asks"],
|
||||
json!([{"price":"0.8","size":"2"},{"price":"0.6","size":"1"}])
|
||||
);
|
||||
drop(reader);
|
||||
let mut reader = Exchange::open(&path).unwrap();
|
||||
let request = json!({"op":"clob_book","token_id":"eb:YES:demo"});
|
||||
assert_eq!(&reader.execute(request.clone()).unwrap(), snapshot);
|
||||
let local = reader
|
||||
.execute(json!({"op":"book","asset_id":"eb:YES:demo"}))
|
||||
.unwrap();
|
||||
assert_eq!(local["bids"][0], json!({"price_micros":300000,"size":3.5}));
|
||||
writer
|
||||
.execute(json!({"op":"fund","id":"buyer","account":"buyer","cents":100}))
|
||||
.unwrap();
|
||||
writer.execute(json!({"op":"market_buy","id":"take","account":"buyer","asset_id":"eb:YES:demo","amount_cents":60})).unwrap();
|
||||
let changed = reader.execute(request).unwrap();
|
||||
assert_ne!(changed["hash"], snapshot["hash"]);
|
||||
assert_eq!(changed["last_trade_price"], "0.6");
|
||||
assert_eq!(changed["asks"], json!([{"price":"0.8","size":"2"}]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user