use crate::{
assets, inventory, lots, tick_size::TickSize, validation::identifier, Exchange, MarketDataError,
};
use rusqlite::OptionalExtension;
use serde_json::{json, Value};
impl Exchange {
/// Resolve either outcome to the same market and its stable YES/NO pair.
pub fn market_by_token(&self, token: &str) -> Result, MarketDataError> {
let (_, market) = assets::parse_id(token)
.map_err(|_| MarketDataError::Invalid("Invalid token id".into()))?;
let exists: bool = self
.db
.prepare_cached("SELECT EXISTS(SELECT 1 FROM markets WHERE id=?)")?
.query_row([market], |r| r.get(0))?;
Ok(exists.then(|| json!({"condition_id":market,
"primary_token_id":assets::id(market,"YES"),"secondary_token_id":assets::id(market,"NO")})))
}
/// Report the configuration used by local execution in a single read snapshot.
pub fn clob_market_info(&self, market: &str) -> Result , MarketDataError> {
identifier(market).map_err(|_| MarketDataError::Invalid("Invalid condition id".into()))?;
let mut stmt = self.db.prepare_cached(include_str!("clob_market.sql"))?;
let info = stmt
.query_row([market], |r| {
let yes: String = r.get(0)?;
let no: String = r.get(1)?;
let tick = r
.get::<_, Option>(2)?
.map(TickSize)
.unwrap_or(TickSize::WHOLE_CENT);
let minimum = r.get::<_, Option>(3)?.unwrap_or(lots::MICROS);
let fee: i64 = r.get(4)?;
let neg_risk: bool = r.get(5)?;
let kickoff: Option = r.get(6)?;
let mut info = json!({"gst":kickoff,"t":[
{"t":assets::id(market,"YES"),"o":yes},
{"t":assets::id(market,"NO"),"o":no}],
"mts":tick.number(),"mos":inventory::decimal(minimum.into(),inventory::UNIT),
"nr":neg_risk,"rfqe":false,"ibce":false,"oas":0});
if fee != 0 {
info["fd"] =
json!({"r":inventory::decimal(fee.into(),inventory::UNIT),"e":1,"to":true});
}
Ok(info)
})
.optional()?;
Ok(info)
}
}