Serve single and batch CLOB order books over HTTP

This commit is contained in:
2026-12-03 07:52:06 +08:00
parent 5892772671
commit 5dde2cd28e
11 changed files with 452 additions and 11 deletions
+42 -8
View File
@@ -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> {