Match public CLOB side prices and batch responses to observed books

This commit is contained in:
2026-12-03 15:02:41 +08:00
parent 4262b7adaa
commit 6e1cfb4e22
6 changed files with 101 additions and 85 deletions
+2 -2
View File
@@ -1110,7 +1110,7 @@ sequenceDiagram
E-->>C: 按结果资产索引,未上市资产无条目
```
交易方向查询也读取 quote_events。execution_price 和 execution_prices 通过 quotes.rs 查询请求时的最优对手价。批量调用在一条 SQL 中读取所有请求的市场,YES 和 NO 分别换算后再按方向取价。
HTTP 盘口查询由 public_book_prices 读取 quote_events,先清理到期任务,再在同一快照中读取各市场。BUY 取买一,SELL 取卖一。下单面板的 JSONL 命令继续查询最优对手价。
```mermaid
sequenceDiagram
@@ -1118,7 +1118,7 @@ sequenceDiagram
participant E as Exchange
participant Q as quotes.rs
participant D as SQLite
H->>E: execution_prices,传入资产和交易方向
H->>E: public_book_prices,传入资产和盘口方向
E->>Q: 读取批量报价
Q->>D: 单条查询读取每个市场最近一次最优档
D-->>Q: 返回买一与卖一
+48 -17
View File
@@ -193,6 +193,27 @@ impl PriceRequest {
// Use the last committed top of book, even when only one side has liquidity.
// All requested markets share the caller's read snapshot.
fn price_rows(db: &Connection, requests: &[PriceRequest]) -> Result<Vec<Option<String>>> {
quote_rows(db, requests)?
.into_iter()
.zip(requests)
.map(|(quote, request)| {
let quote = quote.ok_or(PriceQueryError::MissingBook)?;
let price = match request.side {
PriceSide::Buy => quote.ask,
PriceSide::Sell => quote.bid,
};
Ok(price.map(|p| {
let mut text = format!("{}.{:04}", p / 10000, p % 10000);
while text.len() > 4 && text.ends_with('0') {
text.pop();
}
text
}))
})
.collect()
}
fn quote_rows(db: &Connection, requests: &[PriceRequest]) -> Result<Vec<Option<Quote>>> {
if !(1..=500).contains(&requests.len()) {
return Err(PriceQueryError::RequestCount.into());
}
@@ -211,26 +232,17 @@ fn price_rows(db: &Connection, requests: &[PriceRequest]) -> Result<Vec<Option<S
ORDER BY j.key",
)?;
let mut rows = stmt.query([serde_json::to_string(&markets)?])?;
let mut prices = Vec::with_capacity(requests.len());
let mut quotes = Vec::with_capacity(requests.len());
while let Some(row) = rows.next()? {
if row.get::<_, Option<String>>(4)?.is_none() {
return Err(PriceQueryError::MissingBook.into());
}
let index: usize = row.get(5)?;
let quote = Quote::read(row, outcomes[index])?;
let price = match requests[index].side {
PriceSide::Buy => quote.ask,
PriceSide::Sell => quote.bid,
};
prices.push(price.map(|p| {
let mut text = format!("{}.{:04}", p / 10000, p % 10000);
while text.len() > 4 && text.ends_with('0') {
text.pop();
}
text
}));
let exists = row.get::<_, Option<String>>(4)?.is_some();
quotes.push(
exists
.then(|| Quote::read(row, outcomes[index]))
.transpose()?,
);
}
Ok(prices)
Ok(quotes)
}
pub(crate) fn prices(db: &Connection, requests: &[PriceRequest]) -> Result<Value> {
@@ -261,6 +273,25 @@ impl crate::Exchange {
price_map(&self.db, requests, false)
}
/// Public CLOB side prices refer to resting orders on the named side.
pub fn public_book_prices(&self, requests: &[PriceRequest]) -> Result<Value> {
let tx = crate::expiry::transaction(&self.db, rusqlite::TransactionBehavior::Deferred)?;
let mut result = json!({});
for (request, quote) in requests.iter().zip(quote_rows(&tx, requests)?) {
if let Some(quote) = quote {
let ticks = match request.side {
PriceSide::Buy => quote.bid.unwrap_or(0),
PriceSide::Sell => quote.ask.unwrap_or(crate::tick_size::PAR),
};
let text = format!("{}.{:04}", ticks / 10000, ticks % 10000);
result[request.token_id.as_str()][request.label()] =
json!(text.trim_end_matches('0').trim_end_matches('.'));
}
}
tx.commit()?;
Ok(result)
}
pub fn spreads(
&self,
tokens: &[String],