feat!: a generic core, and Kalshi through it

The orchestrator, the connection loop and the book thread's shell move to
src/core/, generic over an Exchange and its loss rule (ADR 0004).
Polymarket runs through it unchanged. Kalshi joins: a signed handshake
(KALSHI_KEY_ID_FILE, KALSHI_PRIVATE_KEY_FILE), a reconnect on a skipped
sequence number, first one wins by clock and rank, and its market listing
with markets tracked from three hours before they open.

Breaking: the exchange is now the first argument,
`engineering-challenge <polymarket|kalshi|kalshi-demo> [OUTPUT]`.
This commit is contained in:
pascal committed 2026-11-11 20:54:17 +00:00
1 parent 480ee3ad2d
commit 1a5bbcd602
33 files changed
+8150 -1846

No files matched your search

+33 -10
View File
@@ -1,8 +1,9 @@
//! The collector (roadmap/04): an orchestrator keeps the tracked markets equal
//! to Gamma's live ones and places them on connections (read, stamp, decode,
//! route), which feed the book threads (fold, check, resync), all writing
//! through one writer (SBE).
//! Usage: `engineering-challenge [OUTPUT]`, OUTPUT defaulting to stdout; logs
//! The collector (roadmap/04, roadmap/06): an orchestrator keeps the tracked
//! markets equal to the exchange's live ones and places them on connections
//! (read, stamp, decode, route), which feed the book threads (fold, check,
//! resync), all writing through one writer (SBE).
//! Usage: `engineering-challenge EXCHANGE [OUTPUT]`, EXCHANGE one of
//! `polymarket`, `kalshi` or `kalshi-demo`, OUTPUT defaulting to stdout; logs
//! go to stderr.
use std::sync::Arc;
@@ -19,6 +20,8 @@ macro_rules! log {
};
}
mod core;
mod kalshi;
mod polymarket;
use engineering_challenge::book::Output;
@@ -26,11 +29,31 @@ use engineering_challenge::book::Output;
/// `AlignedSbeReader::open` reads nothing else.
const CHUNK: usize = 64 << 20;
fn main() -> anyhow::Result<()> {
let output = std::env::args()
.nth(1)
.unwrap_or_else(|| "/dev/stdout".to_string());
const USAGE: &str = "usage: engineering-challenge <polymarket|kalshi|kalshi-demo> [OUTPUT]";
fn main() -> anyhow::Result<()> {
let mut args = std::env::args().skip(1);
let exchange = args.next().ok_or_else(|| anyhow::anyhow!(USAGE))?;
let output = args.next().unwrap_or_else(|| "/dev/stdout".to_string());
// Before anything starts: a key that can't be read stops the run here.
match exchange.as_str() {
"polymarket" => collect(
polymarket::Polymarket {
url: polymarket::MARKET_CHANNEL_URL.to_string(),
},
output,
),
"kalshi" => collect(
kalshi::Kalshi::production(kalshi::sign::Key::from_env()?),
output,
),
"kalshi-demo" => collect(kalshi::Kalshi::demo(kalshi::sign::Key::from_env()?), output),
_ => anyhow::bail!("unknown exchange {exchange:?}; {USAGE}"),
}
}
/// Collects `exchange` into `output` until SIGINT.
fn collect<E: core::Exchange>(exchange: E, output: String) -> anyhow::Result<()> {
let stop = Arc::new(AtomicBool::new(false));
let on_sigint = Arc::clone(&stop);
ctrlc::set_handler(move || on_sigint.store(true, Ordering::Relaxed))?;
@@ -47,7 +70,7 @@ fn main() -> anyhow::Result<()> {
// connections; the book threads stop once every connection has, and the
// writer flushes once they all have, even after a panic upstream (the run
// still fails then).
let failed = polymarket::orchestrator::run(outputs_tx, &stop);
let failed = core::orchestrator::run(Arc::new(exchange), outputs_tx, &stop);
let written = writer
.join()
.map_err(|_| anyhow::anyhow!("the writer thread panicked"))?;