Serve single and batch CLOB order books over HTTP
This commit is contained in:
@@ -7,9 +7,9 @@ use crate::{
|
||||
resident::Books,
|
||||
tick_size::{TickSize, PAR},
|
||||
};
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use matching::Side;
|
||||
use rusqlite::Transaction;
|
||||
use rusqlite::{OptionalExtension, Transaction};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use sha1::{Digest, Sha1};
|
||||
@@ -45,12 +45,28 @@ impl Snapshot {
|
||||
|
||||
fn build(tx: &Transaction, books: &Books, token: &str) -> Result<Snapshot> {
|
||||
let (outcome, market) = assets::parse_id(token)?;
|
||||
let (at_ms, neg_risk): (i64, bool) = tx.query_row(
|
||||
"SELECT at_ms,EXISTS(SELECT 1 FROM neg_risk_members WHERE market=?1)
|
||||
find(tx, books, token, outcome, market)?
|
||||
.context("No orderbook exists for the requested token id")
|
||||
}
|
||||
|
||||
fn find(
|
||||
tx: &Transaction,
|
||||
books: &Books,
|
||||
token: &str,
|
||||
outcome: &str,
|
||||
market: &str,
|
||||
) -> Result<Option<Snapshot>> {
|
||||
let row: Option<(i64, bool)> = tx
|
||||
.query_row(
|
||||
"SELECT at_ms,EXISTS(SELECT 1 FROM neg_risk_members WHERE market=?1)
|
||||
FROM book_snapshot_times WHERE market=?1",
|
||||
[market],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
[market],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let Some((at_ms, neg_risk)) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
let book = books.get(market, outcome);
|
||||
let levels = |taker| {
|
||||
let mut result = Vec::new();
|
||||
@@ -85,7 +101,25 @@ fn build(tx: &Transaction, books: &Books, token: &str) -> Result<Snapshot> {
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
snapshot.set_hash()?;
|
||||
Ok(snapshot)
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
|
||||
pub(crate) fn read_many(
|
||||
tx: &Transaction,
|
||||
books: &mut Books,
|
||||
tokens: &[String],
|
||||
) -> Result<Vec<Value>> {
|
||||
let changes = books.changes(tx)?;
|
||||
books.apply(changes);
|
||||
let mut result = Vec::with_capacity(tokens.len());
|
||||
for token in tokens {
|
||||
if let Ok((outcome, market)) = assets::parse_id(token) {
|
||||
if let Some(book) = find(tx, books, token, outcome, market)? {
|
||||
result.push(serde_json::to_value(book)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn read(tx: &Transaction, books: &mut Books, token: &str) -> Result<Value> {
|
||||
|
||||
@@ -119,6 +119,14 @@ impl Exchange {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read requested outcome books in one consistent database and resident-book snapshot.
|
||||
pub fn public_books(&mut self, tokens: &[String]) -> Result<Vec<Value>> {
|
||||
let tx = expiry::transaction(&self.db, TransactionBehavior::Deferred)?;
|
||||
let books = book_snapshot::read_many(&tx, &mut self.books, tokens)?;
|
||||
tx.commit()?;
|
||||
Ok(books)
|
||||
}
|
||||
|
||||
pub fn market_stream_page(&mut self, market: &str, after: Option<i64>) -> Result<MarketPage> {
|
||||
let tx = expiry::transaction(&self.db, TransactionBehavior::Deferred)?;
|
||||
let page = market_stream::page(&tx, &mut self.books, market, after)?;
|
||||
|
||||
Reference in New Issue
Block a user