Publish settlement clarifications and atomically clear affected orders

This commit is contained in:
2026-12-03 10:42:35 +08:00
parent fe69e31929
commit 3495e8de3c
12 changed files with 319 additions and 3 deletions
+50 -1
View File
@@ -251,6 +251,34 @@ fn market_lookup(engine: &Mutex<Exchange>, prefix: &str, raw: &str) -> Reply {
.ok_or((if is_token { 404 } else { 400 }, "market not found"))
}
fn clarifications(engine: &Mutex<Exchange>, query: &str) -> Reply {
let invalid = (400, "Invalid clarification query");
let fields: Vec<(String, String)> = serde_urlencoded::from_str(query).map_err(|_| invalid)?;
let mut ids = Vec::new();
let mut limit = 20;
let mut offset = 0;
let mut ascending = false;
for (key, value) in fields {
match key.as_str() {
"market_id" => ids.push(value.parse().map_err(|_| invalid)?),
"limit" => limit = value.parse().map_err(|_| invalid)?,
"offset" => offset = value.parse().map_err(|_| invalid)?,
"ascending" => ascending = value.parse().map_err(|_| invalid)?,
"order" if value == "id" => {}
_ => return Err(invalid),
}
}
let rows = engine
.lock()
.map_err(|_| (500, "Internal server error"))?
.market_clarifications(&ids, limit, offset, ascending)
.map_err(|e| match e {
exchange::MarketDataError::Invalid(_) => invalid,
exchange::MarketDataError::Unavailable => (500, "Internal server error"),
})?;
Ok(Value::Array(rows))
}
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;
@@ -284,6 +312,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
allow = Some("GET");
Err((405, "Method not allowed"))
}
(&Method::Get, "/market-clarifications") => clarifications(engine, params),
(&Method::Get, "/prices-history") => {
let result = serde_urlencoded::from_str::<exchange::ClobHistoryQuery>(params)
.map_err(|_| HistoryError::Invalid("invalid query parameters".into()))
@@ -378,7 +407,13 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
}
(
_,
"/price" | "/midpoint" | "/spread" | "/book" | "/last-trade-price" | "/prices-history"
"/price"
| "/midpoint"
| "/spread"
| "/book"
| "/last-trade-price"
| "/prices-history"
| "/market-clarifications"
| "/v2/prices-history",
) => {
allow = Some("GET");
@@ -504,7 +539,21 @@ mod tests {
let pair = json!({"eb:YES:s":"0.4","eb:NO:s":"0.6"});
let price_batch =
json!({"eb:YES:mid-example":{"BUY":0.09,"SELL":0.08},"eb:NO:mid-example":{"BUY":0.92}});
db.execute(json!({"op":"publish_clarification","id":"note","market":market,"content":"Map one includes overtime.","clear_book":false})).unwrap();
let note = db
.execute(json!({"op":"resolution","market":market}))
.unwrap()["clarifications"]
.clone();
let note_id = note[0]["marketId"].as_i64().unwrap();
let note_query = format!(
"/market-clarifications?market_id=9999&market_id={note_id}&order=id&ascending=true"
);
let cases = [
("GET",note_query.as_str(),String::new(),200,Some(note)),
("GET","/market-clarifications?limit=0",String::new(),200,Some(json!([]))),
("GET","/market-clarifications?offset=1",String::new(),200,Some(json!([]))),
("GET","/market-clarifications?limit=101",String::new(),400,None),
("POST","/market-clarifications","{}".into(),405,None),
("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),