Archived
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:
33 files changed
+8150
-1846
No files matched your search
+91
-33
@@ -1,25 +1,21 @@
|
||||
//! Polymarket: which markets are live (Gamma), the orchestrator that places
|
||||
//! them on connections, the connections, and the book threads they feed
|
||||
//! Polymarket: its market channel (the connection's side of it), Gamma (which
|
||||
//! markets are live), and the hash rule its book threads apply
|
||||
//! (docs/adr/0003-first-one-wins.md).
|
||||
|
||||
pub mod feed;
|
||||
pub mod gamma;
|
||||
pub mod orchestrator;
|
||||
pub mod socket;
|
||||
pub mod wire;
|
||||
|
||||
/// How many book threads own the books. One handled ~30,000 frames a second
|
||||
/// in sprint 02.
|
||||
pub const BOOK_THREADS: usize = 16;
|
||||
use crossbeam::channel::{Receiver, Sender};
|
||||
use serde_json::json;
|
||||
use tungstenite::client::IntoClientRequest;
|
||||
|
||||
/// The book thread that owns a market's books, from its `conditionId` (`0x`
|
||||
/// and 64 hex digits, a hash: any 16 of them spread evenly). Computed, so the
|
||||
/// orchestrator and every connection agree without a shared table.
|
||||
pub fn book_thread(condition_id: &str) -> usize {
|
||||
let digits = condition_id.get(2..18).unwrap_or_default();
|
||||
// An id of another shape still gets a thread, always the same one.
|
||||
u64::from_str_radix(digits, 16).unwrap_or(0) as usize % BOOK_THREADS
|
||||
}
|
||||
use crate::core::{self, Finding, Request, Undecoded};
|
||||
|
||||
pub const MARKET_CHANNEL_URL: &str = "wss://ws-subscriptions-clob.polymarket.com/ws/market";
|
||||
/// Tokens per subscribe or unsubscribe message: a first message of more than
|
||||
/// 500 gets no opening books (docs/notes.md).
|
||||
const CHUNK: usize = 500;
|
||||
|
||||
/// A live market: its symbols and the two inputs of their book hash that the
|
||||
/// websocket doesn't carry (docs/notes.md).
|
||||
@@ -34,27 +30,89 @@ pub struct Market {
|
||||
pub neg_risk: bool,
|
||||
}
|
||||
|
||||
impl core::Market for Market {
|
||||
fn id(&self) -> &str {
|
||||
&self.condition_id
|
||||
}
|
||||
|
||||
fn symbols(&self) -> &[String] {
|
||||
&self.tokens
|
||||
}
|
||||
}
|
||||
|
||||
/// The exchange, at its market channel's URL (the tests' local one in tests).
|
||||
pub struct Polymarket {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl core::Exchange for Polymarket {
|
||||
type Market = Market;
|
||||
type Event = wire::Event;
|
||||
type Session = Session;
|
||||
type Rule = feed::Chain;
|
||||
|
||||
const NAME: &'static str = "polymarket";
|
||||
|
||||
fn request(&self) -> anyhow::Result<tungstenite::handshake::client::Request> {
|
||||
Ok(self.url.as_str().into_client_request()?)
|
||||
}
|
||||
|
||||
fn session(&self) -> Session {
|
||||
Session
|
||||
}
|
||||
|
||||
fn rule(&self) -> feed::Chain {
|
||||
feed::Chain::default()
|
||||
}
|
||||
|
||||
fn fetch(&self, requests: Receiver<Request>, findings: Sender<Finding<Market>>) {
|
||||
gamma::run(requests, findings)
|
||||
}
|
||||
}
|
||||
|
||||
/// The market channel keeps no state of its own per connection.
|
||||
pub struct Session;
|
||||
|
||||
impl Session {
|
||||
/// The messages that subscribe or unsubscribe `tokens` on an open
|
||||
/// connection.
|
||||
fn operations(operation: &str, tokens: &[&String]) -> Vec<String> {
|
||||
tokens
|
||||
.chunks(CHUNK)
|
||||
.map(|chunk| json!({"assets_ids": chunk, "operation": operation}).to_string())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl core::Session for Session {
|
||||
type Event = wire::Event;
|
||||
|
||||
/// Opened empty, then subscribed in chunks: one rule for every token.
|
||||
fn opening(&mut self) -> Vec<String> {
|
||||
vec![json!({"type": "market", "assets_ids": []}).to_string()]
|
||||
}
|
||||
|
||||
fn subscribe(&mut self, tokens: &[&String]) -> Vec<String> {
|
||||
Self::operations("subscribe", tokens)
|
||||
}
|
||||
|
||||
fn unsubscribe(&mut self, tokens: &[&String]) -> Vec<String> {
|
||||
Self::operations("unsubscribe", tokens)
|
||||
}
|
||||
|
||||
fn keepalive(&self) -> Option<&'static str> {
|
||||
Some("PING")
|
||||
}
|
||||
|
||||
fn decode(&mut self, text: &str) -> Result<Vec<wire::Event>, Undecoded> {
|
||||
wire::decode(text).map_err(|error| Undecoded::Unreadable(error.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fixture_markets() -> Vec<Market> {
|
||||
gamma::parse_markets(include_str!("../../tests/fixtures/gamma-markets.json"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_market_always_gets_the_same_book_thread_and_markets_spread() {
|
||||
let markets = fixture_markets();
|
||||
let threads: std::collections::BTreeSet<usize> = markets
|
||||
.iter()
|
||||
.map(|market| book_thread(&market.condition_id))
|
||||
.collect();
|
||||
assert!(threads.iter().all(|&thread| thread < BOOK_THREADS));
|
||||
// 20 markets over 16 threads: several threads, not one.
|
||||
assert!(threads.len() > 5, "{threads:?}");
|
||||
let id = &markets[0].condition_id;
|
||||
assert_eq!(book_thread(id), book_thread(&id.clone()));
|
||||
assert_eq!(book_thread("not an id"), 0);
|
||||
}
|
||||
}
|
||||
mod socket_tests;
|
||||
Reference in new issue
Block a user