Publish market listings and final resolution to opted-in subscribers

This commit is contained in:
2026-12-03 07:45:02 +08:00
parent 063fcb57a2
commit 5892772671
11 changed files with 188 additions and 6 deletions
+4
View File
@@ -69,6 +69,10 @@ sequenceDiagram
opt 成交或价格步长变化
S-->>C: 按同一游标发送 last_trade_price 或 tick_size_change
end
opt 已启用扩展功能
S->>E: 从本连接的发现游标读取后续上架公告
S-->>C: new_market;已订阅市场最终裁决时另发 market_resolved
end
C->>S: operation=unsubscribe,移除 YES
C->>W: 文本 PING
W-->>C: 文本 PONG
+60
View File
@@ -31,6 +31,7 @@ struct Market {
pub(crate) struct Session {
opened: bool,
custom: bool,
discovery_cursor: i64,
markets: BTreeMap<String, Market>,
}
@@ -55,6 +56,9 @@ impl Session {
next.opened = true;
next.custom = c.custom_feature_enabled.unwrap_or(false);
}
if subscribe && next.custom && next.markets.is_empty() {
next.discovery_cursor = engine.market_discovery_page(None)?.cursor;
}
let mut snapshots = Vec::new();
for (market, mut tokens) in groups {
if !subscribe {
@@ -108,6 +112,11 @@ impl Session {
pub fn poll(&mut self, engine: &mut Exchange) -> Result<Vec<Value>> {
let mut frames = Vec::new();
if self.custom && !self.markets.is_empty() {
let page = engine.market_discovery_page(Some(self.discovery_cursor))?;
self.discovery_cursor = page.cursor;
frames.extend(page.frames.into_iter().map(|(_, f)| f));
}
for (market, watch) in &mut self.markets {
let page = engine.market_stream_page(market, Some(watch.cursor))?;
watch.project(market, page, self.custom, &mut frames)?;
@@ -125,6 +134,12 @@ impl Market {
frames: &mut Vec<Value>,
) -> Result<()> {
for (seq, mut event) in page.frames {
if event["event_type"] == "market_resolved" {
if custom && self.assets.values().any(|asset| seq > asset.since) {
frames.push(event);
}
continue;
}
if event["event_type"] != "price_change" {
let token = event["asset_id"]
.as_str()
@@ -247,4 +262,49 @@ mod tests {
assert_eq!(batch[0]["event_type"], "tick_size_change");
assert_eq!(batch[0]["asset_id"], "eb:YES:demo");
}
#[test]
fn opted_in_sessions_discover_new_listings_and_receive_one_final_result() {
let mut db = Exchange::open(":memory:").unwrap();
let mut inputs = include_str!("../../../docs/examples/market-lifecycle.jsonl")
.lines()
.map(|line| serde_json::from_str::<Value>(line).unwrap());
db.execute(inputs.next().unwrap()).unwrap();
let mut enabled = Session::default();
let mut standard = Session::default();
for (s, custom) in [(&mut enabled, true), (&mut standard, false)] {
s.control(&mut db,&json!({"type":"market","assets_ids":["eb:YES:lobby"],"custom_feature_enabled":custom}).to_string()).unwrap();
assert!(s.poll(&mut db).unwrap().is_empty());
}
db.execute(inputs.next().unwrap()).unwrap();
let listing = inputs.next().unwrap();
db.execute(listing.clone()).unwrap();
db.execute(listing).unwrap();
let news = enabled.poll(&mut db).unwrap();
assert_eq!(news.len(), 1);
assert_eq!(news[0]["event_type"], "new_market");
assert_eq!(news[0]["slug"], "fixture-winner");
assert_eq!(news[0]["question"], "Who wins the series?");
assert_eq!(news[0]["outcomes"], json!(["Froglings", "1win"]));
assert!(standard.poll(&mut db).unwrap().is_empty());
for s in [&mut enabled, &mut standard] {
s.control(
&mut db,
r#"{"operation":"subscribe","assets_ids":["eb:YES:match-1","eb:NO:match-1"]}"#,
)
.unwrap();
}
db.execute(inputs.next().unwrap()).unwrap();
db.execute(inputs.next().unwrap()).unwrap();
assert!(enabled.poll(&mut db).unwrap().is_empty());
let resolve = inputs.next().unwrap();
db.execute(resolve.clone()).unwrap();
db.execute(resolve).unwrap();
let result = enabled.poll(&mut db).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0]["event_type"], "market_resolved");
assert_eq!(result[0]["id"], news[0]["id"]);
assert_eq!(result[0]["winning_outcome"], "1win");
assert_eq!(result[0]["winning_asset_id"], "eb:NO:match-1");
assert!(standard.poll(&mut db).unwrap().is_empty());
}
}