feat(polymarket): skeleton and market-channel connectivity spike

Replaces the template's demo main.rs with a spike that connects to
Polymarket's public market channel, subscribes to one market, and logs every
frame with its receive time. Adds scripts/ci.sh (fmt, clippy, tests), the
project CLAUDE.md, ADR 0001 (OS threads, shared-nothing), and the market
channel's message shapes in docs/notes.md: no sequence number, a book hash on
every change, mirrored Yes/No books.
This commit is contained in:
pascal committed 2026-11-04 14:47:14 +01:00
1 parent adcf205f0b
commit 0e687e7eba
11 files changed
+713 -138

No files matched your search

+2 -63
View File
@@ -1,66 +1,5 @@
use chrono::DateTime;
use engineering_challenge::Side;
use engineering_challenge::util::dtos::challenge_orderbook::{
AnyMessage, PriceLevelUpdateDomain, SnapshotAsksEntryDomain, SnapshotBidsEntryDomain,
SnapshotDomain,
};
use engineering_challenge::util::{AlignedSbeReader, AlignedSbeWriter, OrderbookSchema};
mod polymarket;
const CHUNK_SIZE_64_MEBIBYTES: usize = 64 << 20;
/// This is a dummy example where we define a snapshot and price level update,
/// write them to a file using [`AlignedSbeWriter`], and then read them back
/// from the same file using [`AlignedSbeReader`], asserting that what we read
/// back is the exact same as what we wrote.
///
/// You should replace this main function with your actual solution, and split
/// up your code into modules in `src/` in a way that makes sense to you.
fn main() -> anyhow::Result<()> {
let timestamp = DateTime::from_timestamp_micros(1_700_000_000_000_000).unwrap();
let symbol = "EXAMPLE".to_owned();
let snapshot = SnapshotDomain {
received_at: timestamp,
exchange_timestamp: timestamp,
bids: vec![SnapshotBidsEntryDomain {
price_level: 400_000,
quantity: 2_000_000,
}],
asks: vec![SnapshotAsksEntryDomain {
price_level: 600_000,
quantity: 3_500_000,
}],
symbol: symbol.clone(),
};
let update = PriceLevelUpdateDomain {
backtest_timestamp: timestamp,
exchange_timestamp: timestamp,
price_level: 400_000,
quantity: 1_250_000,
side: Side::Buy,
symbol: symbol.clone(),
};
let file = tempfile::NamedTempFile::new()?;
let mut writer = AlignedSbeWriter::create(file.path(), CHUNK_SIZE_64_MEBIBYTES)?;
writer.write_msg(&snapshot)?;
writer.write_msg(&update)?;
writer.flush()?;
drop(writer);
let mut reader = AlignedSbeReader::<OrderbookSchema>::open(file.path())?;
let Some(AnyMessage::Snapshot(decoded)) = reader.read_msg()? else {
anyhow::bail!("expected snapshot");
};
assert_eq!(SnapshotDomain::try_from_decoder(decoded)?, snapshot);
let Some(AnyMessage::PriceLevelUpdate(decoded)) = reader.read_msg()? else {
anyhow::bail!("expected price level update");
};
assert_eq!(PriceLevelUpdateDomain::try_from_decoder(decoded)?, update);
assert!(reader.read_msg()?.is_none());
println!("Verification success!");
Ok(())
polymarket::run_spike()
}
+33
View File
@@ -0,0 +1,33 @@
//! Connectivity spike: connect to Polymarket's public market channel, subscribe
//! to one market, and log every raw frame with its receive time.
use chrono::{SecondsFormat, Utc};
use tungstenite::Message;
const MARKET_CHANNEL_URL: &str = "wss://ws-subscriptions-clob.polymarket.com/ws/market";
/// Both tokens (Yes, No) of one market, hardcoded for the spike (discovery comes
/// in M3): "2026 Balance of Power: D Senate, D House", which closes 2026-11-04,
/// so pick a fresh market to rerun this.
const SUBSCRIBE_MESSAGE: &str = r#"{"type":"market","assets_ids":["34722410608062854697106861099776685947172185964394483545370684749662285977831","24483563228660028121020430722896740519524131851526579619586176931221901842798"]}"#;
/// Connects, sends [`SUBSCRIBE_MESSAGE`], and logs frames until the server
/// closes the connection.
pub fn run_spike() -> anyhow::Result<()> {
let (mut socket, _response) = tungstenite::connect(MARKET_CHANNEL_URL)?;
socket.send(Message::text(SUBSCRIBE_MESSAGE))?;
loop {
let message = socket.read()?;
// Stamp as soon as the frame is off the wire, before any processing.
let received_at = Utc::now().to_rfc3339_opts(SecondsFormat::Micros, true);
match message {
Message::Text(text) => println!("{received_at} {text}"),
Message::Close(frame) => {
println!("{received_at} close {frame:?}");
return Ok(());
}
other => println!("{received_at} {other:?}"),
}
}
}