Resolve outcome tokens to CLOB markets and expose their execution parameters

This commit is contained in:
2026-12-03 09:40:34 +08:00
parent 81595a89ee
commit 7aa3280e5b
11 changed files with 230 additions and 2 deletions
+48
View File
@@ -115,6 +115,29 @@ fn tick_size(engine: &Mutex<Exchange>, params: &str, segment: Option<&str>) -> R
Ok(json!({"minimum_tick_size":size}))
}
fn market_lookup(engine: &Mutex<Exchange>, prefix: &str, raw: &str) -> Reply {
let is_token = prefix == "markets-by-token";
let invalid = if is_token {
"Invalid token id"
} else {
"Invalid condition id"
};
let id = percent_decode_str(raw)
.decode_utf8()
.map_err(|_| (400, invalid))?;
let exchange = engine.lock().map_err(|_| (500, "Internal server error"))?;
let info = if is_token {
exchange.market_by_token(&id)
} else {
exchange.clob_market_info(&id)
};
info.map_err(|e| match e {
exchange::MarketDataError::Invalid(_) => (400, invalid),
exchange::MarketDataError::Unavailable => (500, "Internal server error"),
})?
.ok_or((if is_token { 404 } else { 400 }, "market not found"))
}
fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
let (path, params) = req.url().split_once('?').unwrap_or((req.url(), ""));
let mut allow = None;
@@ -126,6 +149,22 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
.strip_prefix("/tick-size/")
.filter(|s| !s.contains('/'));
let is_tick_route = path == "/tick-size" || tick_segment.is_some();
let lookup = path
.strip_prefix('/')
.and_then(|p| p.split_once('/'))
.filter(|(prefix, id)| {
matches!(*prefix, "markets-by-token" | "clob-markets") && !id.contains('/')
});
if let Some((prefix, id)) = lookup {
if req.method() != &Method::Get {
return send(req, 405, json!({"error":"Method not allowed"}), Some("GET"));
}
let (status, body) = match market_lookup(engine, prefix, id) {
Ok(value) => (200, value),
Err((status, msg)) => (status, json!({"error":msg})),
};
return send(req, status, body, None);
}
let reply = match (req.method(), path) {
(&Method::Get, _) if is_tick_route => tick_size(engine, params, tick_segment),
(_, _) if is_tick_route => {
@@ -261,7 +300,16 @@ mod tests {
let last_batch = format!("/last-trades-prices?{csv}");
let last_single = encoded.replacen("/book?", "/last-trade-price?", 1);
let tick_query = encoded.replacen("/book?", "/tick-size?", 1);
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"}]});
let cases = [
("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),
("GET","/markets-by-token/garbage",String::new(),400,None),
("GET","/clob-markets/missing",String::new(),400,None),
("POST","/clob-markets/map+2%2F1","{}".into(),405,None),
("GET",tick_query.as_str(),String::new(),200,Some(json!({"minimum_tick_size":0.01}))),
("GET","/tick-size/eb%3ANO%3Amap%202%3A%E8%83%9C%E8%80%85",String::new(),200,Some(json!({"minimum_tick_size":0.01}))),
("GET","/tick-size/eb:YES:map+2%2F1",String::new(),200,Some(json!({"minimum_tick_size":0.0025}))),