Stream committed trades and tick transitions to market subscribers

This commit is contained in:
2026-12-03 07:40:01 +08:00
parent e80e52275c
commit 063fcb57a2
10 changed files with 306 additions and 49 deletions
+44 -43
View File
@@ -34,20 +34,12 @@ pub struct MarketPage {
impl Request {
pub fn read(self, tx: &Transaction, books: &mut Books) -> Result<Value> {
let page = self.page(tx, books, false)?;
let page = self.page(tx, books)?;
let events: Vec<_> = page.frames.into_iter().map(|(_, event)| event).collect();
Ok(json!({"cursor":page.cursor,"events":events,"has_more":page.has_more}))
}
pub(crate) fn wire(market: &str, after: Option<i64>) -> Self {
Self {
market: market.into(),
after,
limit: Some(64),
}
}
pub(crate) fn page(self, tx: &Transaction, books: &mut Books, raw: bool) -> Result<MarketPage> {
fn page(self, tx: &Transaction, books: &mut Books) -> Result<MarketPage> {
let limit = self.limit.unwrap_or(100);
ensure!((1..=500).contains(&limit), "invalid book_feed batch limit");
let latest: i64 = tx.query_row(
@@ -59,7 +51,7 @@ impl Request {
let mut events = Vec::new();
for outcome in ["YES", "NO"] {
let asset = assets::id(&self.market, outcome);
let frame = book_snapshot::event(tx, books, &asset, raw)?;
let frame = book_snapshot::event(tx, books, &asset, false)?;
events.push((latest, frame));
}
return Ok(MarketPage {
@@ -70,45 +62,18 @@ impl Request {
};
ensure!((0..=latest).contains(&cursor), "invalid book_feed cursor");
let mut batches = tx.prepare_cached(
"SELECT seq,CASE WHEN ?4 THEN CAST(at_ms AS TEXT) ELSE strftime('%Y-%m-%dT%H:%M:%f000Z',at_ms/1000.0,'unixepoch') END,best_bid,best_ask
"SELECT seq,strftime('%Y-%m-%dT%H:%M:%f000Z',at_ms/1000.0,'unixepoch'),best_bid,best_ask
FROM depth_batches WHERE market=?1 AND seq>?2 ORDER BY seq LIMIT ?3")?;
let mut rows = batches.query(params![self.market, cursor, limit, raw])?;
let mut updates = tx.prepare_cached(
"SELECT side,price,size FROM depth_updates WHERE batch=? ORDER BY side,price",
)?;
let mut rows = batches.query(params![self.market, cursor, limit])?;
let mut events = Vec::new();
while let Some(row) = rows.next()? {
cursor = row.get(0)?;
let stamp: String = row.get(1)?;
let bid: Option<i64> = row.get(2)?;
let ask: Option<i64> = row.get(3)?;
let mut levels = updates.query([cursor])?;
let mut changes = Vec::new();
while let Some(level) = levels.next()? {
let side: String = level.get(0)?;
let p: i64 = level.get(1)?;
let size: i64 = level.get(2)?;
for (outcome, is_bid, p, bid, ask) in [
("YES", side == "buy", p, bid, ask),
(
"NO",
side == "sell",
PAR - p,
ask.map(|v| PAR - v),
bid.map(|v| PAR - v),
),
] {
changes.push(json!({"asset_id":assets::id(&self.market,outcome),"side":if is_bid {"BUY"} else {"SELL"},
"price":price(p),"size":decimal(size.into(),lots::PER_SHARE).to_string(),
"best_bid":bid.map(price),"best_ask":ask.map(price),"hash":null}));
}
}
let frame = if raw {
json!({"event_type":"price_change","market":self.market,"price_changes":changes,"timestamp":stamp})
} else {
json!({"topic":"market","type":"price_change","payload":{"condition_id":self.market,
"price_changes":changes,"timestamp":stamp}})
};
let changes = changes(tx, &self.market, cursor, bid, ask)?;
let frame = json!({"topic":"market","type":"price_change","payload":{"condition_id":self.market,
"price_changes":changes,"timestamp":stamp}});
events.push((cursor, frame));
}
Ok(MarketPage {
@@ -118,3 +83,39 @@ impl Request {
})
}
}
pub(crate) fn changes(
tx: &Transaction,
market: &str,
batch: i64,
bid: Option<i64>,
ask: Option<i64>,
) -> Result<Vec<Value>> {
let mut updates = tx.prepare_cached(
"SELECT side,price,size FROM depth_updates WHERE batch=? ORDER BY side,price",
)?;
let mut levels = updates.query([batch])?;
let mut changes = Vec::new();
while let Some(level) = levels.next()? {
let side: String = level.get(0)?;
let p: i64 = level.get(1)?;
let size: i64 = level.get(2)?;
for (outcome, is_bid, p, bid, ask) in [
("YES", side == "buy", p, bid, ask),
(
"NO",
side == "sell",
PAR - p,
ask.map(|v| PAR - v),
bid.map(|v| PAR - v),
),
] {
changes.push(
json!({"asset_id":assets::id(market,outcome),"side":if is_bid {"BUY"} else {"SELL"},
"price":price(p),"size":decimal(size.into(),lots::PER_SHARE).to_string(),
"best_bid":bid.map(price),"best_ask":ask.map(price),"hash":null}),
);
}
}
Ok(changes)
}