Archived
feat(polymarket): hash-verified order books for a subset of symbols
Collects the order books of Polymarket's 20 busiest markets (40 symbols) into the template's SBE format: a Delta per level change, plus Snapshots on subscribe, every minute, and whenever a fresh book corrects ours. Every change is checked against Polymarket's own book hash; a failed check resyncs the symbol on the open socket, and the log counts losses apart from the few ms our own resync hides. Runs until SIGINT, which closes the socket and flushes the output; reconnects when the server drops it. A `reconcile` binary checks an output file against itself.
This commit is contained in:
22 files changed
+6423
-84
No files matched your search
@@ -0,0 +1,76 @@
|
||||
//! Polymarket: which markets to track (Gamma), the market-channel socket, and
|
||||
//! the feed that turns its frames into books and output.
|
||||
|
||||
pub mod feed;
|
||||
pub mod socket;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
const GAMMA_MARKETS_URL: &str = "https://gamma-api.polymarket.com/markets";
|
||||
|
||||
/// A market's symbols and the two inputs of their book hash that the
|
||||
/// websocket doesn't carry (docs/notes.md).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Market {
|
||||
/// Its token ids, one per outcome: each is a symbol.
|
||||
pub tokens: Vec<String>,
|
||||
/// Exactly as the hash expects it, e.g. `"5"`.
|
||||
pub min_order_size: String,
|
||||
pub neg_risk: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct GammaMarket {
|
||||
/// A JSON array inside a string.
|
||||
clob_token_ids: String,
|
||||
order_min_size: serde_json::Number,
|
||||
neg_risk: bool,
|
||||
enable_order_book: bool,
|
||||
}
|
||||
|
||||
/// The `limit` live markets with the most 24 h volume, keeping those with an
|
||||
/// order book. Fixed for the run: discovery is M3.
|
||||
pub fn top_markets(limit: u32) -> anyhow::Result<Vec<Market>> {
|
||||
let body = ureq::get(GAMMA_MARKETS_URL)
|
||||
.query("limit", limit.to_string())
|
||||
.query("active", "true")
|
||||
.query("closed", "false")
|
||||
.query("order", "volume24hr")
|
||||
.query("ascending", "false")
|
||||
.call()?
|
||||
.body_mut()
|
||||
.read_to_string()?;
|
||||
parse_markets(&body)
|
||||
}
|
||||
|
||||
fn parse_markets(body: &str) -> anyhow::Result<Vec<Market>> {
|
||||
let markets: Vec<GammaMarket> = serde_json::from_str(body)?;
|
||||
markets
|
||||
.into_iter()
|
||||
.filter(|market| market.enable_order_book)
|
||||
.map(|market| {
|
||||
Ok(Market {
|
||||
tokens: serde_json::from_str(&market.clob_token_ids)?,
|
||||
min_order_size: market.order_min_size.to_string(),
|
||||
neg_risk: market.neg_risk,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fixture_markets() -> Vec<Market> {
|
||||
parse_markets(include_str!("../../tests/fixtures/gamma-markets.json")).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn parses_gamma_markets() {
|
||||
let markets = super::fixture_markets();
|
||||
assert_eq!(markets.len(), 20);
|
||||
assert!(markets.iter().all(|market| market.tokens.len() == 2));
|
||||
assert_eq!(markets[0].min_order_size, "5");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user