use crate::{assets, lots, minimum_size::MinimumSize, tick_size::TickSize}; use anyhow::{ensure, Result}; use matching::Side; use rusqlite::Connection; use serde::Deserialize; use serde_json::{json, Value}; pub(crate) fn cash_cents(cents: Option, micros: Option) -> Result { let cents = match (cents, micros) { (Some(n), None) => n, (None, Some(n)) if n > 0 => n / 10_000, _ => anyhow::bail!("provide one positive amount_cents or amount_micros"), }; crate::validation::amount(cents)?; Ok(cents) } pub(crate) struct Amounts { maker: i64, taker: i64, } impl Amounts { pub fn for_order( tick: Option<&TickSize>, side: Side, requested: i64, price_units: i64, ) -> Result> { match tick { Some(tick) => Self::encode( tick, side, requested, price_units * crate::tick_size::PRICE_MICROS, ) .map(Some), None => { crate::tick_size::validate(None, price_units)?; Ok(None) } } } // requested is in whole cents for BUY, 0.01-share lots for SELL. fn encode(tick: &TickSize, side: Side, requested: i64, price: i64) -> Result { tick.check(price)?; let maker = requested .checked_mul(10_000) .ok_or_else(|| anyhow::anyhow!("order amount overflow"))?; ensure!(maker > 0, "positive order amount required"); let scale = 1_000_000_i128; let taker = match side { Side::Buy => { let quantum = match tick.0 { 100_000 => 1000, 10_000 => 100, 5_000 | 1_000 => 10, 2_500 | 100 => 1, _ => anyhow::bail!("unsupported market tick"), }; let cash = i128::from(maker) * scale; let shares = cash / i128::from(price) / quantum * quantum; ensure!( cash < shares * i128::from(price + 100), "rounded market buy exceeds price protection" ); shares } Side::Sell => i128::from(maker) * i128::from(price) / scale, }; Ok(Self { maker, taker: i64::try_from(taker)?, }) } pub fn json(&self) -> Value { json!({"makerAmount": self.maker.to_string(), "takerAmount": self.taker.to_string()}) } } #[derive(Deserialize)] #[serde(tag = "side", rename_all = "lowercase", deny_unknown_fields)] pub(crate) enum Request { Buy { market: String, outcome: String, price_micros: i64, amount_cents: Option, amount_micros: Option, }, Sell { market: String, outcome: String, price_micros: i64, qty: Option, size_micros: Option, }, } impl Request { pub fn read(self, db: &Connection) -> Result { let (market, outcome, side, requested, price) = match self { Self::Buy { market, outcome, price_micros, amount_cents, amount_micros, } => ( market, outcome, Side::Buy, cash_cents(amount_cents, amount_micros)?, price_micros, ), Self::Sell { market, outcome, price_micros, qty, size_micros, } => ( market, outcome, Side::Sell, lots::amount(qty, size_micros)?, price_micros, ), }; ensure!(matches!(outcome.as_str(), "YES" | "NO"), "invalid outcome"); let tick = TickSize::load(db, &market)? .ok_or_else(|| anyhow::anyhow!("listed market with configured tick_size required"))?; let amounts = Amounts::encode(&tick, side, requested, price)?; if let Some(minimum) = MinimumSize::load(db, &market)? { minimum.check_microshares(match side { Side::Buy => amounts.taker, Side::Sell => amounts.maker, })?; } let mut result = amounts.json(); result["asset_id"] = json!(assets::id(&market, &outcome)); result["tick_size"] = json!(tick.text()); Ok(result) } }