Track prediction-market parity and expose outcome display quotes
This commit is contained in:
@@ -141,6 +141,10 @@ impl Listing {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub(crate) enum Query {
|
||||
Quote {
|
||||
market: String,
|
||||
outcome: String,
|
||||
},
|
||||
Catalog {
|
||||
event: String,
|
||||
},
|
||||
@@ -157,6 +161,34 @@ pub(crate) enum Query {
|
||||
impl Query {
|
||||
pub fn read(self, db: &Connection) -> Result<Value> {
|
||||
match self {
|
||||
Self::Quote { market, outcome } => {
|
||||
ensure!(matches!(outcome.as_str(), "YES" | "NO"), "invalid outcome");
|
||||
let phase: String =
|
||||
db.query_row("SELECT phase FROM markets WHERE id=?", [&market], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
let (bid,ask):(Option<i64>,Option<i64>)=db.query_row(
|
||||
"SELECT MAX(CASE WHEN side='buy' THEN price END), MIN(CASE WHEN side='sell' THEN price END)
|
||||
FROM orders WHERE market=? AND outcome=? AND status='live'",
|
||||
params![market,outcome], |r|Ok((r.get(0)?,r.get(1)?)))?;
|
||||
// Fills also cover databases created before timestamped history was introduced.
|
||||
let last: Option<i64> = db.query_row(
|
||||
"SELECT (SELECT f.price FROM fills f JOIN orders o ON o.seq=f.bid
|
||||
WHERE o.market=? AND o.outcome=? ORDER BY f.seq DESC LIMIT 1)",
|
||||
params![market, outcome],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let (display, source) = match (phase.as_str(), bid, ask, last) {
|
||||
("continuous", Some(b), Some(a), _) if (0..=10).contains(&(a - b)) => {
|
||||
(Some((a + b) * 5000), Some("midpoint"))
|
||||
}
|
||||
("continuous", _, _, Some(p)) => (Some(p * 10000), Some("last_trade")),
|
||||
_ => (None, None),
|
||||
};
|
||||
Ok(json!({"market":market,"outcome":outcome,"phase":phase,
|
||||
"best_bid_micros":bid.map(|p|p*10000),"best_ask_micros":ask.map(|p|p*10000),
|
||||
"last_trade_micros":last.map(|p|p*10000),"display_price_micros":display,"source":source}))
|
||||
}
|
||||
Self::Catalog { event } => {
|
||||
let detail = db.query_row(
|
||||
"SELECT title,sport,competition,home,away,scheduled_at_ms FROM events WHERE id=?",
|
||||
|
||||
Reference in New Issue
Block a user