Match public CLOB side prices and batch responses to observed books
This commit is contained in:
@@ -196,18 +196,18 @@ sequenceDiagram
|
||||
H-->>C: 单查用 spread 或 mid 字段,批查以资产 ID 为键
|
||||
```
|
||||
|
||||
### 买入或卖出一个结果的报价
|
||||
### 查询盘口买一和卖一
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant C as 客户端
|
||||
participant H as market_http.rs
|
||||
participant E as exchange/quotes.rs
|
||||
C->>H: GET /price 或 GET、POST /prices
|
||||
C->>H: GET /price 或 POST /prices
|
||||
H->>H: 校验资产与方向一一配对
|
||||
H->>E: 查询最优对手报价
|
||||
H->>E: 查询盘口同侧最优档
|
||||
E-->>H: 单笔价格或按资产与方向分组的报价
|
||||
H-->>C: 单查返回价格字符串,批查返回 JSON 数字
|
||||
H-->>C: 单查和批查的价格均为字符串
|
||||
```
|
||||
|
||||
[可重放请求](../../docs/examples/clob-http.http)同时展示相同盘口下的买价、卖价与中点。
|
||||
|
||||
@@ -108,54 +108,42 @@ impl WirePrice {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PriceList {
|
||||
token_ids: String,
|
||||
sides: String,
|
||||
}
|
||||
|
||||
impl PriceList {
|
||||
fn into_rows(self) -> std::result::Result<Vec<WirePrice>, (u16, &'static str)> {
|
||||
let tokens = self.token_ids.split(',');
|
||||
let sides = self.sides.split(',');
|
||||
let count = tokens.clone().count();
|
||||
if count > MAX_BOOKS || count != sides.clone().count() {
|
||||
return Err((400, "Invalid payload"));
|
||||
}
|
||||
Ok(tokens
|
||||
.zip(sides)
|
||||
.map(|(token, side)| WirePrice {
|
||||
token_id: token.to_owned(),
|
||||
side: Some(side.to_owned()),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
fn execution_prices(engine: &Mutex<Exchange>, rows: Vec<WirePrice>, single: bool) -> Reply {
|
||||
fn book_prices(engine: &Mutex<Exchange>, rows: Vec<WirePrice>, single: bool) -> Reply {
|
||||
if rows.is_empty() || rows.len() > MAX_BOOKS {
|
||||
return Err((400, "Invalid payload"));
|
||||
}
|
||||
let requests = rows
|
||||
.into_iter()
|
||||
.map(WirePrice::into_request)
|
||||
.map(|mut row| {
|
||||
if !single && row.side.is_none() {
|
||||
row.side = Some("SELL".into());
|
||||
}
|
||||
row.into_request()
|
||||
})
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
let exchange = engine.lock().map_err(|_| (500, "Internal server error"))?;
|
||||
let value = if single {
|
||||
exchange.execution_price(&requests[0])
|
||||
} else {
|
||||
exchange.execution_prices(&requests)
|
||||
}
|
||||
.map_err(
|
||||
|error| match error.downcast_ref::<exchange::PriceQueryError>() {
|
||||
Some(exchange::PriceQueryError::InvalidToken) if single => (400, "Invalid token id"),
|
||||
Some(exchange::PriceQueryError::MissingBook) => (404, MISSING),
|
||||
Some(_) => (400, "Invalid payload"),
|
||||
None => (500, "Internal server error"),
|
||||
},
|
||||
)?;
|
||||
if single && value["price"].is_null() {
|
||||
return Err((422, "No price available for requested side"));
|
||||
let value =
|
||||
exchange
|
||||
.public_book_prices(&requests)
|
||||
.map_err(
|
||||
|error| match error.downcast_ref::<exchange::PriceQueryError>() {
|
||||
Some(exchange::PriceQueryError::InvalidToken) if single => {
|
||||
(400, "Invalid token id")
|
||||
}
|
||||
Some(exchange::PriceQueryError::MissingBook) => (404, MISSING),
|
||||
Some(_) => (400, "Invalid payload"),
|
||||
None => (500, "Internal server error"),
|
||||
},
|
||||
)?;
|
||||
if single {
|
||||
let sides = value[requests[0].token_id.as_str()]
|
||||
.as_object()
|
||||
.ok_or((404, MISSING))?;
|
||||
let price = sides
|
||||
.values()
|
||||
.next()
|
||||
.ok_or((500, "Internal server error"))?;
|
||||
return Ok(json!({"price":price}));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
@@ -427,13 +415,11 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
|
||||
}
|
||||
(&Method::Get, "/price") => serde_urlencoded::from_str::<WirePrice>(params)
|
||||
.map_err(|_| (400, "Invalid token id"))
|
||||
.and_then(|p| execution_prices(engine, vec![p], true)),
|
||||
(&Method::Post, "/prices") => batch_params::<WirePrice>(&mut req)
|
||||
.and_then(|rows| execution_prices(engine, rows, false)),
|
||||
(&Method::Get, "/prices") => serde_urlencoded::from_str::<PriceList>(params)
|
||||
.map_err(|_| (400, "Invalid payload"))
|
||||
.and_then(PriceList::into_rows)
|
||||
.and_then(|rows| execution_prices(engine, rows, false)),
|
||||
.and_then(|p| book_prices(engine, vec![p], true)),
|
||||
(&Method::Post, "/prices") => {
|
||||
batch_params::<WirePrice>(&mut req).and_then(|rows| book_prices(engine, rows, false))
|
||||
}
|
||||
(&Method::Get, "/prices") => Err((400, "Invalid payload")),
|
||||
(&Method::Get, "/spread" | "/midpoint") => {
|
||||
let field = if path == "/spread" {
|
||||
QuoteField::Spread
|
||||
@@ -677,8 +663,7 @@ mod tests {
|
||||
}
|
||||
let sample = json!({"eb:YES:mid-example":"0.085","eb:NO:mid-example":"0.915"});
|
||||
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}});
|
||||
let price_batch = json!({"eb:YES:mid-example":{"BUY":"0.08","SELL":"0.09"},"eb:NO:mid-example":{"BUY":"0.91"},"eb:YES:one":{"BUY":"0.2"}});
|
||||
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}))
|
||||
@@ -780,18 +765,18 @@ mod tests {
|
||||
("GET","/prices-history?market=eb:YES:s&interval=1d&fidelity=0",String::new(),400,None),
|
||||
("GET","/prices-history?market=eb:YES:s&startTs=1&endTs=2&interval=max",String::new(),400,None),
|
||||
("POST","/prices-history","{}".into(),405,None),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=BUY",String::new(),200,Some(json!({"price":"0.09"}))),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=SELL",String::new(),200,Some(json!({"price":"0.08"}))),
|
||||
("GET","/price?token_id=eb:YES:one&side=SELL",String::new(),200,Some(json!({"price":"0.20"}))),
|
||||
("GET","/price?token_id=eb:YES:one&side=BUY",String::new(),422,None),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=BUY",String::new(),200,Some(json!({"price":"0.08"}))),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=SELL",String::new(),200,Some(json!({"price":"0.09"}))),
|
||||
("GET","/price?token_id=eb:YES:one&side=SELL",String::new(),200,Some(json!({"price":"1"}))),
|
||||
("GET","/price?token_id=eb:YES:one&side=BUY",String::new(),200,Some(json!({"price":"0.2"}))),
|
||||
("GET","/price?token_id=eb:YES:missing&side=BUY",String::new(),404,Some(json!({"error":MISSING}))),
|
||||
("GET","/price?token_id=eb:YES:s&side=HOLD",String::new(),400,Some(json!({"error":"Invalid side"}))),
|
||||
("GET","/price?token_id=broken&side=BUY",String::new(),400,Some(json!({"error":"Invalid token id"}))),
|
||||
("GET","/prices?token_ids=eb:YES:mid-example,eb:YES:mid-example,eb:NO:mid-example&sides=BUY,SELL,BUY",String::new(),200,Some(price_batch.clone())),
|
||||
("GET","/prices?token_ids=eb:YES:mid-example,eb:YES:mid-example,eb:NO:mid-example&sides=BUY,SELL,BUY",String::new(),400,Some(json!({"error":"Invalid payload"}))),
|
||||
("POST","/prices",json!([{"token_id":"eb:YES:mid-example","side":"BUY"},{"token_id":"eb:NO:mid-example","side":"BUY"},{"token_id":"eb:YES:mid-example","side":"SELL"},{"token_id":"eb:YES:one","side":"BUY"},{"token_id":"eb:YES:mid-example","side":"BUY"}]).to_string(),200,Some(price_batch)),
|
||||
("GET","/prices?token_ids=eb:YES:s,eb:NO:s&sides=BUY",String::new(),400,Some(json!({"error":"Invalid payload"}))),
|
||||
("POST","/prices",json!([{"token_id":"eb:YES:s","side":"BUY"},{"token_id":"eb:NO:missing","side":"SELL"}]).to_string(),404,Some(json!({"error":MISSING}))),
|
||||
("POST","/prices",json!([{"token_id":"eb:YES:s"}]).to_string(),400,Some(json!({"error":"Invalid side"}))),
|
||||
("POST","/prices",json!([{"token_id":"eb:YES:s","side":"BUY"},{"token_id":"eb:NO:missing","side":"SELL"}]).to_string(),200,Some(json!({"eb:YES:s":{"BUY":"0.39"}}))),
|
||||
("POST","/prices",json!([{"token_id":"eb:YES:s"}]).to_string(),200,Some(json!({"eb:YES:s":{"SELL":"0.41"}}))),
|
||||
("POST","/prices",json!(vec![json!({"token_id":"eb:YES:s","side":"BUY"}); MAX_BOOKS+1]).to_string(),400,None),
|
||||
("DELETE","/prices",String::new(),405,None),
|
||||
("GET","/midpoint?token_id=eb:YES:mid-example",String::new(),200,Some(json!({"mid":"0.085"}))),
|
||||
|
||||
Reference in New Issue
Block a user