103 lines
3.7 KiB
Rust
103 lines
3.7 KiB
Rust
use crate::validation::identifier;
|
|
use anyhow::{ensure, Context, Result};
|
|
use rusqlite::Connection;
|
|
use serde_json::{json, Value};
|
|
|
|
// The outcome precedes the market so every valid market string, including
|
|
// those containing colons, round-trips without escaping or a database migration.
|
|
pub(crate) fn id(market: &str, outcome: &str) -> String {
|
|
format!("eb:{outcome}:{market}")
|
|
}
|
|
|
|
pub fn parse_id(asset: &str) -> Result<(&str, &str)> {
|
|
let (outcome, market) = asset
|
|
.strip_prefix("eb:")
|
|
.and_then(|s| s.split_once(':'))
|
|
.context("invalid Eventbook asset_id")?;
|
|
ensure!(matches!(outcome, "YES" | "NO"), "invalid asset outcome");
|
|
identifier(market)?;
|
|
Ok((outcome, market))
|
|
}
|
|
|
|
pub(crate) fn outcomes(market: &str, yes: &str, no: &str) -> Value {
|
|
json!([
|
|
{"outcome":"YES","label":yes,"asset_id":id(market,"YES")},
|
|
{"outcome":"NO","label":no,"asset_id":id(market,"NO")}
|
|
])
|
|
}
|
|
|
|
pub(crate) fn market_assets(db: &Connection, market: &str) -> Result<Value> {
|
|
identifier(market)?;
|
|
let mut detail = db.query_row(
|
|
"SELECT w.yes_label,w.no_label,m.phase,a.role,a.name,w.id,w.slug,w.question
|
|
FROM market_wire_metadata w JOIN markets m ON m.id=w.market
|
|
LEFT JOIN augmented_outcomes a ON a.market=w.market WHERE w.market=?1",
|
|
[market],
|
|
|r| {
|
|
let yes:String=r.get(0)?;
|
|
let no:String=r.get(1)?;
|
|
let mut detail=json!({"market":market,"id":r.get::<_,String>(5)?,"slug":r.get::<_,String>(6)?,"question":r.get::<_,String>(7)?,"phase":r.get::<_,String>(2)?,"outcomes":outcomes(market,&yes,&no)});
|
|
if let Some(role)=r.get::<_,Option<String>>(3)? {
|
|
detail["augmented_role"]=json!(role);
|
|
detail["outcome_name"]=json!(r.get::<_,Option<String>>(4)?);
|
|
}
|
|
Ok(detail)
|
|
},
|
|
)?;
|
|
detail["tick_size"] =
|
|
json!(crate::tick_size::TickSize::load(db, market)?.map(|tick| tick.text()));
|
|
detail["min_order_size"] =
|
|
json!(crate::minimum_size::MinimumSize::load(db, market)?.map(|size| size.text()));
|
|
detail["taker_fee_rate_ppm"] = json!(crate::fees::rate(db, market)?);
|
|
Ok(detail)
|
|
}
|
|
|
|
// Resolve only commands that address one outcome. Keep the original request
|
|
// untouched in the idempotency ledger so historic receipts still replay exactly.
|
|
pub(crate) fn resolve(db: &Connection, request: &mut Value) -> Result<()> {
|
|
if !matches!(
|
|
request["op"].as_str(),
|
|
Some(
|
|
"order"
|
|
| "book"
|
|
| "quote"
|
|
| "quote_history"
|
|
| "history"
|
|
| "prices_history"
|
|
| "open_orders"
|
|
| "trades"
|
|
| "market_sell"
|
|
| "estimate"
|
|
| "limit_amounts"
|
|
| "market_amounts"
|
|
| "tick_history"
|
|
| "tick_size_change"
|
|
| "market_buy"
|
|
| "cancel_market_orders"
|
|
)
|
|
) {
|
|
return Ok(());
|
|
}
|
|
let fields = request.as_object_mut().context("expected object")?;
|
|
let Some(asset) = fields.remove("asset_id") else {
|
|
return Ok(());
|
|
};
|
|
let (outcome, market) = parse_id(asset.as_str().context("invalid Eventbook asset_id")?)?;
|
|
for (field, value) in [("market", market), ("outcome", outcome)] {
|
|
if let Some(existing) = fields.get(field) {
|
|
ensure!(
|
|
existing.as_str() == Some(value),
|
|
"asset_id conflicts with {field}"
|
|
);
|
|
}
|
|
fields.insert(field.to_owned(), json!(value));
|
|
}
|
|
let listed: bool = db.query_row(
|
|
"SELECT EXISTS(SELECT 1 FROM markets WHERE id=?1)",
|
|
[market],
|
|
|r| r.get(0),
|
|
)?;
|
|
ensure!(listed, "unknown Eventbook asset_id");
|
|
Ok(())
|
|
}
|