Serve the CLOB price history protocol with minute-based fidelity

This commit is contained in:
2026-12-03 10:18:43 +08:00
parent 74749bfdb8
commit fe69e31929
10 changed files with 201 additions and 1 deletions
+24 -1
View File
@@ -284,6 +284,23 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
allow = Some("GET");
Err((405, "Method not allowed"))
}
(&Method::Get, "/prices-history") => {
let result = serde_urlencoded::from_str::<exchange::ClobHistoryQuery>(params)
.map_err(|_| HistoryError::Invalid("invalid query parameters".into()))
.and_then(|query| {
engine
.lock()
.map_err(|_| HistoryError::Unavailable)?
.clob_price_history(query)
});
match result {
Ok(history) => Ok(history),
Err(HistoryError::Invalid(message)) => {
return send(req, 400, json!({"error":message}), None)
}
Err(HistoryError::Unavailable) => Err((500, "Internal server error")),
}
}
(&Method::Get, "/price") => serde_urlencoded::from_str::<WirePrice>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| execution_prices(engine, vec![p], true)),
@@ -361,7 +378,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
}
(
_,
"/price" | "/midpoint" | "/spread" | "/book" | "/last-trade-price"
"/price" | "/midpoint" | "/spread" | "/book" | "/last-trade-price" | "/prices-history"
| "/v2/prices-history",
) => {
allow = Some("GET");
@@ -488,6 +505,12 @@ mod tests {
let price_batch =
json!({"eb:YES:mid-example":{"BUY":0.09,"SELL":0.08},"eb:NO:mid-example":{"BUY":0.92}});
let cases = [
("GET","/prices-history?market=eb:YES:s&interval=1h&fidelity=10",String::new(),200,Some(json!({"history":[]}))),
("GET","/prices-history?market=eb:NO:s&startTs=1000&endTs=2000",String::new(),200,Some(json!({"history":[]}))),
("GET","/prices-history?market=eb:YES:s&startTs=1000",String::new(),400,None),
("GET","/prices-history?market=eb:YES:s&interval=1d&fidelity=0",String::new(),400,None),
("GET","/prices-history?market=eb:YES:s&startTs=1&endTs=2&interval=max",String::new(),400,None),
("POST","/prices-history","{}".into(),405,None),
("GET","/price?token_id=eb:YES:mid-example&side=BUY",String::new(),200,Some(json!({"price":"0.09"}))),
("GET","/price?token_id=eb:YES:mid-example&side=SELL",String::new(),200,Some(json!({"price":"0.08"}))),
("GET","/price?token_id=eb:YES:one&side=SELL",String::new(),200,Some(json!({"price":"0.20"}))),