Serve side-specific execution quotes over the public market HTTP API

This commit is contained in:
2026-12-03 10:07:23 +08:00
parent b47058d75e
commit 1b2376ccb4
7 changed files with 220 additions and 37 deletions
+1
View File
@@ -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 {
+50 -17
View File
@@ -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],