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
+3
View File
@@ -66,6 +66,9 @@ sequenceDiagram
S-->>C: 新增 NO 的 book 数组
S->>E: 从该市场的已读批次号读取后续变化
S-->>C: price_change,只含已订阅且晚于其起点的资产
opt 成交或价格步长变化
S-->>C: 按同一游标发送 last_trade_price 或 tick_size_change
end
C->>S: operation=unsubscribe,移除 YES
C->>W: 文本 PING
W-->>C: 文本 PONG
+35
View File
@@ -125,6 +125,19 @@ impl Market {
frames: &mut Vec<Value>,
) -> Result<()> {
for (seq, mut event) in page.frames {
if event["event_type"] != "price_change" {
let token = event["asset_id"]
.as_str()
.context("missing market event asset")?;
if self
.assets
.get(token)
.is_some_and(|asset| seq > asset.since)
{
frames.push(event);
}
continue;
}
let changes = event["price_changes"]
.as_array_mut()
.context("missing price changes")?;
@@ -212,4 +225,26 @@ mod tests {
assert_eq!(frames[0]["price_changes"][0]["asset_id"], "eb:YES:demo");
assert!(session.poll(&mut db).unwrap().is_empty());
}
#[test]
fn newly_subscribed_asset_skips_prior_tick_but_existing_asset_receives_it() {
let mut db = Exchange::open(":memory:").unwrap();
db.execute(json!({"op":"list","id":"m","market":"demo","rules":"r","tick_size":"0.01"}))
.unwrap();
let mut session = Session::default();
session
.control(&mut db, r#"{"type":"market","assets_ids":["eb:YES:demo"]}"#)
.unwrap();
db.execute(json!({"op":"tick_size_change","id":"t","asset_id":"eb:YES:demo","new_tick_size":"0.001","timestamp":"1"})).unwrap();
let snap = session
.control(
&mut db,
r#"{"operation":"subscribe","assets_ids":["eb:NO:demo"]}"#,
)
.unwrap();
assert_eq!(snap[0]["tick_size"], "0.001");
let batch = session.poll(&mut db).unwrap();
assert_eq!(batch.len(), 1);
assert_eq!(batch[0]["event_type"], "tick_size_change");
assert_eq!(batch[0]["asset_id"], "eb:YES:demo");
}
}