Match observed public last-trade batch semantics

This commit is contained in:
2026-12-03 16:14:25 +08:00
parent 9cc3921e3c
commit d7a77c734d
8 changed files with 53 additions and 25 deletions
+4 -15
View File
@@ -161,11 +161,6 @@ fn batch_params<T: serde::de::DeserializeOwned>(
serde_json::from_slice(&data).map_err(|_| (400, "Invalid payload"))
}
#[derive(Deserialize)]
struct TokenList {
token_ids: String,
}
fn last_prices(engine: &Mutex<Exchange>, tokens: Vec<String>, single: bool) -> Reply {
let mut rows = engine
.lock()
@@ -451,15 +446,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
(&Method::Get, "/last-trade-price") => serde_urlencoded::from_str::<BookParam>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| last_prices(engine, vec![p.token_id], true)),
(&Method::Get, "/last-trades-prices") => serde_urlencoded::from_str::<TokenList>(params)
.map_err(|_| (400, "Invalid payload"))
.and_then(|p| {
last_prices(
engine,
p.token_ids.split(',').map(str::to_owned).collect(),
false,
)
}),
(&Method::Get, "/last-trades-prices") => Err((400, "Invalid payload")),
(&Method::Post, "/last-trades-prices") => {
batch_params::<BookParam>(&mut req).and_then(|p| {
last_prices(
@@ -811,8 +798,10 @@ mod tests {
("GET","/tick-size",String::new(),400,Some(json!({"error":"Invalid token id"}))),
("POST","/tick-size/eb:YES:e",String::new(),405,None),
("GET",last_single.as_str(),String::new(),200,Some(no_fills)),
("GET",last_batch.as_str(),String::new(),200,Some(batch_no_fills.clone())),
("GET",last_batch.as_str(),String::new(),400,Some(json!({"error":"Invalid payload"}))),
("POST","/last-trades-prices",json!([{"token_id":yes,"side":"SELL"},{"token_id":no}]).to_string(),200,Some(batch_no_fills)),
("POST","/last-trades-prices","[]".into(),200,Some(json!([]))),
("GET","/last-trade-price?token_id=eb:YES:missing",String::new(),200,Some(json!({"price":"0.5","side":""}))),
("GET","/last-trade-price?token_id=",String::new(),400,Some(json!({"error":"Invalid token id"}))),
("POST","/last-trades-prices",json!(too_many).to_string(),400,Some(json!({"error":"Invalid payload"}))),
("DELETE","/last-trades-prices",String::new(),405,None),