117 lines
3.4 KiB
Rust
117 lines
3.4 KiB
Rust
use crate::MarketDataError;
|
|
use anyhow::{ensure, Context, Result};
|
|
use rusqlite::{Connection, Transaction};
|
|
use serde_json::json;
|
|
pub(crate) fn identifier(id: &str) -> Result<()> {
|
|
ensure!(
|
|
!id.trim().is_empty() && id.trim().chars().count() <= 256,
|
|
"invalid identifier"
|
|
);
|
|
Ok(())
|
|
}
|
|
pub(crate) fn hex_id(id: &str, digits: usize) -> bool {
|
|
id.len() == 2 + digits && id.starts_with("0x") && id[2..].bytes().all(|b| b.is_ascii_hexdigit())
|
|
}
|
|
|
|
pub(crate) fn amount(n: i64) -> Result<()> {
|
|
ensure!((1..=1_000_000_000).contains(&n), "invalid amount");
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn phase(tx: &Transaction, market: &str, allowed: &[&str]) -> Result<String> {
|
|
identifier(market)?;
|
|
let p: String = tx.query_row("SELECT phase FROM markets WHERE id=?", [market], |r| {
|
|
r.get(0)
|
|
})?;
|
|
ensure!(allowed.contains(&p.as_str()), "wrong market phase");
|
|
Ok(p)
|
|
}
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
|
|
#[serde(untagged)]
|
|
pub(crate) enum IdSelector {
|
|
One(String),
|
|
Many(Vec<String>),
|
|
}
|
|
|
|
impl IdSelector {
|
|
pub(crate) fn ids(&self) -> Result<Vec<String>> {
|
|
let mut ids = match self {
|
|
Self::One(id) => vec![id.clone()],
|
|
Self::Many(ids) => ids.clone(),
|
|
};
|
|
ids.sort();
|
|
ids.dedup();
|
|
ensure!(
|
|
(1..=20).contains(&ids.len()),
|
|
"expected 1 to 20 distinct IDs"
|
|
);
|
|
for id in &ids {
|
|
identifier(id)?;
|
|
}
|
|
Ok(ids)
|
|
}
|
|
}
|
|
|
|
pub(crate) fn directory_selector(
|
|
db: &Connection,
|
|
raw: Option<&str>,
|
|
numeric: bool,
|
|
) -> Result<Option<IdSelector>> {
|
|
raw.map(|raw| {
|
|
let mut keys = Vec::new();
|
|
for id in raw.split(',').map(str::trim) {
|
|
identifier(id)?;
|
|
if numeric {
|
|
let n = id.parse::<i64>().context("invalid event_id")?;
|
|
ensure!(n > 0, "invalid event_id");
|
|
keys.push(n.to_string());
|
|
} else {
|
|
if id.starts_with("0x") && !hex_id(id, 64) {
|
|
// Locally listed condition IDs may legitimately use a short 0x prefix.
|
|
let listed: bool = db.query_row(
|
|
"SELECT EXISTS(SELECT 1 FROM markets WHERE id=?1)",
|
|
[id],
|
|
|r| r.get(0),
|
|
)?;
|
|
ensure!(listed, "invalid condition: {id}");
|
|
}
|
|
keys.push(id.to_owned());
|
|
}
|
|
}
|
|
Ok(IdSelector::Many(keys))
|
|
})
|
|
.transpose()
|
|
}
|
|
|
|
pub(crate) fn condition_filter(
|
|
raw: Option<&str>,
|
|
db: &Connection,
|
|
) -> Result<Option<String>, MarketDataError> {
|
|
let Some(raw) = raw else {
|
|
return Ok(None);
|
|
};
|
|
let selector = IdSelector::Many(
|
|
raw.split(',')
|
|
.map(str::trim)
|
|
.map(|id| {
|
|
if hex_id(id, 64) {
|
|
id.to_ascii_lowercase()
|
|
} else {
|
|
id.to_owned()
|
|
}
|
|
})
|
|
.collect(),
|
|
);
|
|
let ids = selector
|
|
.ids()
|
|
.map_err(|e| MarketDataError::Invalid(e.to_string()))?;
|
|
let mut exists = db.prepare_cached("SELECT EXISTS(SELECT 1 FROM markets WHERE id=?)")?;
|
|
for id in &ids {
|
|
if !hex_id(id, 64) && !exists.query_row([id], |r| r.get::<_, bool>(0))? {
|
|
return Err(MarketDataError::Invalid(format!("invalid condition: {id}")));
|
|
}
|
|
}
|
|
Ok(Some(json!(ids).to_string()))
|
|
}
|