Serve side-specific execution quotes over the public market HTTP API
This commit is contained in:
@@ -1096,3 +1096,18 @@ sequenceDiagram
|
||||
E->>E: 按结果换算两侧,计算价差或精确到半个价格步长的中点
|
||||
E-->>C: 按结果资产索引,未上市资产无条目
|
||||
```
|
||||
|
||||
交易方向查询也读取 quote_events。execution_price 和 execution_prices 通过 quotes.rs 查询请求时的最优对手价。批量调用在一条 SQL 中读取所有请求的市场,YES 和 NO 分别换算后再按方向取价。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant H as HTTP 行情服务
|
||||
participant E as Exchange
|
||||
participant Q as quotes.rs
|
||||
participant D as SQLite
|
||||
H->>E: execution_prices,传入资产和交易方向
|
||||
E->>Q: 读取批量报价
|
||||
Q->>D: 单条查询读取每个市场最近一次最优档
|
||||
D-->>Q: 返回买一与卖一
|
||||
Q-->>H: 按资产和方向分组的可用价格
|
||||
```
|
||||
|
||||
@@ -48,6 +48,7 @@ use std::{path::Path, time::Duration};
|
||||
pub use assets::parse_id as parse_asset_id;
|
||||
pub use depth_feed::MarketPage;
|
||||
pub use prices_history::http::HistoryQuery;
|
||||
pub use quotes::{PriceQueryError, PriceRequest, PriceSide};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MarketDataError {
|
||||
|
||||
@@ -136,23 +136,42 @@ impl Filter {
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "UPPERCASE")]
|
||||
enum Side {
|
||||
pub enum PriceSide {
|
||||
Buy,
|
||||
Sell,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct PriceRequest {
|
||||
token_id: String,
|
||||
side: Side,
|
||||
pub struct PriceRequest {
|
||||
pub token_id: String,
|
||||
pub side: PriceSide,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PriceQueryError {
|
||||
RequestCount,
|
||||
InvalidToken,
|
||||
MissingBook,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PriceQueryError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter.write_str(match self {
|
||||
Self::RequestCount => "expected 1 to 500 price requests",
|
||||
Self::InvalidToken => "invalid Eventbook asset_id",
|
||||
Self::MissingBook => "unknown Eventbook asset_id",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for PriceQueryError {}
|
||||
|
||||
impl PriceRequest {
|
||||
fn label(&self) -> &'static str {
|
||||
match self.side {
|
||||
Side::Buy => "BUY",
|
||||
Side::Sell => "SELL",
|
||||
PriceSide::Buy => "BUY",
|
||||
PriceSide::Sell => "SELL",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,14 +184,14 @@ 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>>> {
|
||||
ensure!(
|
||||
(1..=500).contains(&requests.len()),
|
||||
"expected 1 to 500 price requests"
|
||||
);
|
||||
if !(1..=500).contains(&requests.len()) {
|
||||
return Err(PriceQueryError::RequestCount.into());
|
||||
}
|
||||
let mut markets = Vec::with_capacity(requests.len());
|
||||
let mut outcomes = Vec::with_capacity(requests.len());
|
||||
for request in requests {
|
||||
let (outcome, market) = assets::parse_id(&request.token_id)?;
|
||||
let (outcome, market) =
|
||||
assets::parse_id(&request.token_id).map_err(|_| PriceQueryError::InvalidToken)?;
|
||||
markets.push(market);
|
||||
outcomes.push(outcome);
|
||||
}
|
||||
@@ -185,15 +204,14 @@ fn price_rows(db: &Connection, requests: &[PriceRequest]) -> Result<Vec<Option<S
|
||||
let mut rows = stmt.query([serde_json::to_string(&markets)?])?;
|
||||
let mut prices = Vec::with_capacity(requests.len());
|
||||
while let Some(row) = rows.next()? {
|
||||
ensure!(
|
||||
row.get::<_, Option<String>>(4)?.is_some(),
|
||||
"unknown Eventbook asset_id"
|
||||
);
|
||||
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 {
|
||||
Side::Buy => quote.ask,
|
||||
Side::Sell => quote.bid,
|
||||
PriceSide::Buy => quote.ask,
|
||||
PriceSide::Sell => quote.bid,
|
||||
};
|
||||
prices.push(price.map(|p| {
|
||||
let mut text = format!("{}.{:04}", p / 10000, p % 10000);
|
||||
@@ -207,8 +225,15 @@ fn price_rows(db: &Connection, requests: &[PriceRequest]) -> Result<Vec<Option<S
|
||||
}
|
||||
|
||||
pub(crate) fn prices(db: &Connection, requests: &[PriceRequest]) -> Result<Value> {
|
||||
price_map(db, requests, true)
|
||||
}
|
||||
|
||||
fn price_map(db: &Connection, requests: &[PriceRequest], include_empty: bool) -> Result<Value> {
|
||||
let mut result = json!({});
|
||||
for (request, price) in requests.iter().zip(price_rows(db, requests)?) {
|
||||
if price.is_none() && !include_empty {
|
||||
continue;
|
||||
}
|
||||
let number = price
|
||||
.as_deref()
|
||||
.map(str::parse::<serde_json::Number>)
|
||||
@@ -219,6 +244,14 @@ pub(crate) fn prices(db: &Connection, requests: &[PriceRequest]) -> Result<Value
|
||||
}
|
||||
|
||||
impl crate::Exchange {
|
||||
pub fn execution_price(&self, request: &PriceRequest) -> Result<Value> {
|
||||
request.single(&self.db)
|
||||
}
|
||||
|
||||
pub fn execution_prices(&self, requests: &[PriceRequest]) -> Result<Value> {
|
||||
price_map(&self.db, requests, false)
|
||||
}
|
||||
|
||||
pub fn spreads(
|
||||
&self,
|
||||
tokens: &[String],
|
||||
|
||||
Reference in New Issue
Block a user