Match public CLOB side prices and batch responses to observed books
This commit is contained in:
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user