Serve single and batch spreads from committed book quotes

This commit is contained in:
2026-12-03 09:48:26 +08:00
parent 7aa3280e5b
commit d26c801888
6 changed files with 141 additions and 2 deletions
+54 -2
View File
@@ -51,6 +51,32 @@ fn query(engine: &Mutex<Exchange>, params: Vec<BookParam>, single: bool) -> Repl
}
}
fn spreads(engine: &Mutex<Exchange>, params: Vec<BookParam>, single: bool) -> Reply {
let tokens: Vec<_> = params.into_iter().map(|p| p.token_id).collect();
let rows = engine
.lock()
.map_err(|_| (500, "error getting the spread"))?
.spreads(&tokens)
.map_err(|error| match error {
exchange::MarketDataError::Invalid(_) if single => (400, "Invalid token id"),
exchange::MarketDataError::Invalid(_) => (400, "Invalid payload"),
exchange::MarketDataError::Unavailable => (500, "error getting the spread"),
})?;
if single {
let spread = rows
.into_values()
.next()
.ok_or((404, MISSING))?
.ok_or((422, "Two-sided quote unavailable"))?;
return Ok(json!({"spread":spread}));
}
let result = rows
.into_iter()
.filter_map(|(id, spread)| spread.map(|s| (id, Value::String(s))))
.collect();
Ok(Value::Object(result))
}
fn batch_params(req: &mut Request) -> std::result::Result<Vec<BookParam>, (u16, &'static str)> {
if req.body_length().is_some_and(|n| n as u64 > MAX_BODY) {
return Err((413, "Request body too large"));
@@ -171,6 +197,12 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
allow = Some("GET");
Err((405, "Method not allowed"))
}
(&Method::Get, "/spread") => serde_urlencoded::from_str::<BookParam>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| spreads(engine, vec![p], true)),
(&Method::Post, "/spreads") => {
batch_params(&mut req).and_then(|p| spreads(engine, p, false))
}
(&Method::Get, "/book") => serde_urlencoded::from_str::<BookParam>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| query(engine, vec![p], true)),
@@ -198,11 +230,11 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
allow = Some("GET, POST");
Err((405, "Method not allowed"))
}
(_, "/book" | "/last-trade-price" | "/v2/prices-history") => {
(_, "/spread" | "/book" | "/last-trade-price" | "/v2/prices-history") => {
allow = Some("GET");
Err((405, "Method not allowed"))
}
(_, "/books") => {
(_, "/books" | "/spreads") => {
allow = Some("POST");
Err((405, "Method not allowed"))
}
@@ -303,7 +335,27 @@ mod tests {
let lookup_pair = json!({"condition_id":"map+2/1","primary_token_id":"eb:YES:map+2/1","secondary_token_id":"eb:NO:map+2/1"});
let lookup_info = json!({"gst":null,"nr":false,"rfqe":false,"ibce":false,"oas":0,"mos":0.01,"mts":0.0025,
"t":[{"o":"YES","t":"eb:YES:map+2/1"},{"o":"NO","t":"eb:NO:map+2/1"}]});
for command in [
json!({"op":"list","id":"s-list","market":"s","rules":"spread fixture"}),
json!({"op":"open","id":"s-open","market":"s"}),
json!({"op":"fund","id":"s-f1","account":"a","cents":100}),
json!({"op":"fund","id":"s-f2","account":"b","cents":100}),
json!({"op":"order","id":"s-bid","asset_id":"eb:YES:s","account":"a","side":"buy","price":39,"qty":1}),
json!({"op":"list","id":"one-list","market":"one","rules":"one-sided fixture"}),
json!({"op":"open","id":"one-open","market":"one"}),
json!({"op":"order","id":"one-bid","asset_id":"eb:YES:one","account":"b","side":"buy","price":20,"qty":1}),
json!({"op":"order","id":"s-comp","asset_id":"eb:NO:s","account":"b","side":"buy","price":59,"qty":1}),
] {
db.execute(command).unwrap();
}
let cases = [
("GET","/spread?token_id=eb:YES:s",String::new(),200,Some(json!({"spread":"0.02"}))),
("POST","/spreads",json!([{"token_id":"eb:YES:s"},{"token_id":"eb:NO:s","side":"SELL"},{"token_id":"eb:YES:s"},{"token_id":yes}]).to_string(),200,Some(json!({"eb:YES:s":"0.02","eb:NO:s":"0.02"}))),
("GET","/spread?token_id=eb:NO:one",String::new(),422,Some(json!({"error":"Two-sided quote unavailable"}))),
("GET","/spread?token_id=eb:YES:missing",String::new(),404,Some(json!({"error":MISSING}))),
("GET","/spread?token_id=eb:YES:map%2B2%2F1",String::new(),422,Some(json!({"error":"Two-sided quote unavailable"}))),
("POST","/spreads",json!(too_many).to_string(),400,None),
("GET","/spreads",String::new(),405,None),
("GET","/markets-by-token/eb:NO:map+2%2F1",String::new(),200,Some(lookup_pair)),
("GET","/clob-markets/map+2%2F1",String::new(),200,Some(lookup_info)),
("GET","/markets-by-token/eb:YES:missing",String::new(),404,None),