Replay stable production books and validate resting-side quotes

This commit is contained in:
2026-12-03 15:02:42 +08:00
parent 0aabc9fa02
commit efeeeb4e08
4 changed files with 86 additions and 24 deletions
+46 -22
View File
@@ -51,23 +51,18 @@ fn classify(before: &Value, after: &Value, buy: &Value, sell: &Value) -> &'stati
{
return "book_changed";
}
let (Some(bid), Some(ask), Some(buy), Some(sell)) = (
before["best_bid"]["price_micros"].as_u64(),
before["best_ask"]["price_micros"].as_u64(),
buy["price_micros"].as_u64(),
sell["price_micros"].as_u64(),
) else {
let bid = before["best_bid"]["price_micros"].as_u64();
let ask = before["best_ask"]["price_micros"].as_u64();
let (Some(buy), Some(sell)) = (buy["price_micros"].as_u64(), sell["price_micros"].as_u64())
else {
return "inconclusive";
};
if bid >= ask {
return "inconclusive";
}
if (buy, sell) == (ask, bid) {
"matches_documentation"
} else if (buy, sell) == (bid, ask) {
"reversed"
} else {
"different_prices"
match (bid, ask) {
(None, None) => "inconclusive",
(Some(b), Some(a)) if b >= a => "inconclusive",
_ if (buy, sell) == (bid.unwrap_or(0), ask.unwrap_or(1_000_000)) => "resting_side",
(Some(b), Some(a)) if (buy, sell) == (a, b) => "opposing_side",
_ => "different_prices",
}
}
@@ -75,6 +70,35 @@ fn classify(before: &Value, after: &Value, buy: &Value, sell: &Value) -> &'stati
mod tests {
use super::*;
#[test]
fn public_side_prices_replay_against_bracketing_books() {
let raw: Value =
serde_json::from_str(include_str!("../fixtures/live-side-prices.json")).unwrap();
for case in raw["cases"].as_array().unwrap() {
let token = case["asset"].as_str().unwrap();
let market = json!({"conditionId":case["before"]["body"]["market"]});
let read = |key: &str| books::poly_book(&market, token, &case[key]["body"]).unwrap();
let price = |side: &str| {
let wire = &case[side]["body"]["price"];
assert_eq!(
&case["batch_post"]["body"][token][side.to_uppercase().as_str()],
wire
);
json!({"price_micros":books::price(wire).unwrap()})
};
assert_eq!(
classify(
&read("before"),
&read("after"),
&price("buy"),
&price("sell")
),
"resting_side"
);
assert_eq!(case["batch_get"]["http_status"], 400);
}
}
#[test]
fn stable_distinct_book_differentiates_the_two_price_conventions() {
let gamma: Value =
@@ -88,11 +112,8 @@ mod tests {
let before = books::poly_book(&gamma, &tokens[0], &raw).unwrap();
let bid = json!({"price_micros":12000});
let ask = json!({"price_micros":13000});
assert_eq!(
classify(&before, &before, &ask, &bid),
"matches_documentation"
);
assert_eq!(classify(&before, &before, &bid, &ask), "reversed");
assert_eq!(classify(&before, &before, &ask, &bid), "opposing_side");
assert_eq!(classify(&before, &before, &bid, &ask), "resting_side");
assert_eq!(classify(&before, &before, &bid, &bid), "different_prices");
assert_eq!(
classify(&before, &before, &json!({"http_status":404}), &bid),
@@ -103,10 +124,13 @@ mod tests {
assert_eq!(classify(&before, &after, &ask, &bid), "book_changed");
after["hash"] = Value::Null;
assert_eq!(classify(&before, &after, &ask, &bid), "inconclusive");
for edge in [Value::Null, before["best_bid"].clone()] {
for (edge, expected) in [
(Value::Null, "different_prices"),
(before["best_bid"].clone(), "inconclusive"),
] {
let mut one = before.clone();
one["best_ask"] = edge;
assert_eq!(classify(&one, &one, &ask, &bid), "inconclusive");
assert_eq!(classify(&one, &one, &ask, &bid), expected);
}
}
}
+1 -1
View File
@@ -65,7 +65,7 @@ fn polymarket_report(
let passed = compare
&& outcomes
.iter()
.all(|s| s["price_check"]["status"] == "matches_documentation");
.all(|s| s["price_check"]["status"] == "resting_side");
result["orderbooks_complete"] = json!(complete);
if compare {
result["price_check_passed"] = json!(passed);