Validate market tick sizes and encode precise limit order amounts
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
use crate::{assets, minimum_size::MinimumSize};
|
||||
use anyhow::{ensure, Result};
|
||||
use rusqlite::{params, Connection, OptionalExtension, Transaction};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
pub(crate) struct TickSize(pub i64);
|
||||
|
||||
impl TickSize {
|
||||
pub fn configure(tx: &Transaction, market: &str, value: &str) -> Result<()> {
|
||||
let micros = match value.trim_end_matches('0') {
|
||||
"0.1" => 100_000,
|
||||
"0.01" => 10_000,
|
||||
"0.005" => 5_000,
|
||||
"0.0025" => 2_500,
|
||||
"0.001" => 1_000,
|
||||
"0.0001" => 100,
|
||||
_ => anyhow::bail!("unsupported tick_size"),
|
||||
};
|
||||
tx.execute(
|
||||
"INSERT INTO market_tick_sizes(market,step_micros) VALUES(?,?)",
|
||||
params![market, micros],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(db: &Connection, market: &str) -> Result<Option<Self>> {
|
||||
Ok(db
|
||||
.prepare_cached("SELECT step_micros FROM market_tick_sizes WHERE market=?")?
|
||||
.query_row([market], |r| Ok(Self(r.get(0)?)))
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
pub fn text(&self) -> String {
|
||||
format!("0.{:06}", self.0).trim_end_matches('0').to_owned()
|
||||
}
|
||||
|
||||
pub fn check(&self, price: i64) -> Result<()> {
|
||||
ensure!(
|
||||
(self.0..=1_000_000 - self.0).contains(&price) && price % self.0 == 0,
|
||||
"price outside trading range or off tick_size {}",
|
||||
self.text()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn local_step(&self) -> NonZeroU32 {
|
||||
// Each supported sub-cent step divides one cent exactly.
|
||||
NonZeroU32::new((self.0 / 10_000).max(1) as u32).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum Side {
|
||||
Buy,
|
||||
Sell,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct LimitAmounts {
|
||||
market: String,
|
||||
outcome: String,
|
||||
side: Side,
|
||||
price_micros: i64,
|
||||
size_micros: i64,
|
||||
}
|
||||
|
||||
impl LimitAmounts {
|
||||
pub fn read(self, db: &Connection) -> Result<Value> {
|
||||
ensure!(
|
||||
matches!(self.outcome.as_str(), "YES" | "NO"),
|
||||
"invalid outcome"
|
||||
);
|
||||
let tick = TickSize::load(db, &self.market)?
|
||||
.ok_or_else(|| anyhow::anyhow!("listed market with configured tick_size required"))?;
|
||||
tick.check(self.price_micros)?;
|
||||
ensure!(
|
||||
(10_000..=1_000_000_000_000_000).contains(&self.size_micros),
|
||||
"invalid share size"
|
||||
);
|
||||
let shares = self.size_micros / 10_000 * 10_000;
|
||||
if let Some(minimum) = MinimumSize::load(db, &self.market)? {
|
||||
minimum.check_microshares(shares)?;
|
||||
}
|
||||
// Every supported step times 0.01 shares is an integer number of micros.
|
||||
let collateral = i128::from(self.price_micros) * i128::from(shares) / 1_000_000;
|
||||
let (maker, taker) = match self.side {
|
||||
Side::Buy => (collateral, i128::from(shares)),
|
||||
Side::Sell => (i128::from(shares), collateral),
|
||||
};
|
||||
Ok(
|
||||
json!({"asset_id":assets::id(&self.market,&self.outcome),"tick_size":tick.text(),
|
||||
"price_micros":self.price_micros,"size_micros":shares,"collateral_micros":collateral,
|
||||
"makerAmount":maker.to_string(),"takerAmount":taker.to_string(),
|
||||
"local_precision_supported":self.price_micros%10000==0 && shares%1_000_000==0}),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user