Publish settlement clarifications and atomically clear affected orders
This commit is contained in:
@@ -222,3 +222,23 @@ sequenceDiagram
|
||||
E-->>H: 按观测时刻排序的价格点
|
||||
H-->>C: history 数组,每点包含 t 和 p
|
||||
```
|
||||
|
||||
### 发布后的裁决说明
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
H["market_http.rs"] --> Q["exchange/clarifications.rs"]
|
||||
```
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant C as 客户端
|
||||
participant H as market_http
|
||||
participant E as Exchange
|
||||
C->>H: GET /market-clarifications?market_id=1
|
||||
H->>E: 用目录 ID、limit、offset 和排序方向查询
|
||||
E-->>H: 规则说明、清盘标记和本地发布时间
|
||||
H-->>C: 已发布的公告数组
|
||||
```
|
||||
|
||||
market_id 使用 assets 查询中的数字目录 ID,可重复传入。默认按公告 ID 降序,每页 20 条;支持 order=id、ascending、offset 和 0 到 100 的 limit。当前查询对其他筛选参数返回 400。
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user