Expose the current committed tick size by outcome ID

This commit is contained in:
2026-12-03 09:32:49 +08:00
parent b523a8890f
commit 81595a89ee
10 changed files with 149 additions and 4 deletions
+23
View File
@@ -1026,3 +1026,26 @@ sequenceDiagram
L->>L: 对互补结果反转方向;缺少吃单记录时方向留空
L-->>H: 按输入顺序返回,保留重复项
```
### 已提交的报价步长
```mermaid
flowchart LR
H["cli/market_http.rs"] --> T["tick_size.rs"]
S["book_snapshot.rs"] -->|使用相同的旧盘默认步长| T
T --> I["inventory.rs 的定点数转换"]
T --> A["assets.rs 的结果标识解析"]
```
```mermaid
sequenceDiagram
participant W as 调价命令
participant D as SQLite
participant Q as 步长查询
W->>D: 在写事务内调整步长和受影响的挂单
Q->>D: 联结市场与配置读取步长
D-->>Q: 写事务提交前返回旧步长
W->>D: 提交
Q->>D: 按 YES 或 NO 重新查询同一市场
D-->>Q: 返回新步长
```
+2 -2
View File
@@ -92,8 +92,8 @@ fn find(
.map(|s| s.text())
.unwrap_or_else(|| "0.01".into()),
tick_size: TickSize::load(tx, market)?
.map(|s| s.text())
.unwrap_or_else(|| "0.01".into()),
.unwrap_or(TickSize::WHOLE_CENT)
.text(),
neg_risk,
last_trade_price: Quote::current(tx, market, outcome)?
.last
+26 -2
View File
@@ -1,4 +1,4 @@
use crate::{assets, minimum_size::MinimumSize};
use crate::{assets, inventory, minimum_size::MinimumSize, Exchange, MarketDataError};
use anyhow::{ensure, Result};
use rusqlite::{params, Connection, OptionalExtension, Transaction};
use serde::Deserialize;
@@ -35,7 +35,31 @@ pub(crate) fn validate(tick: Option<&TickSize>, price: i64) -> Result<()> {
Ok(())
}
}
impl Exchange {
/// Current grid for a listed local outcome, including the whole-cent legacy grid.
pub fn minimum_tick_size(
&self,
token: &str,
) -> Result<Option<serde_json::Number>, MarketDataError> {
let (_, market) = assets::parse_id(token)
.map_err(|_| MarketDataError::Invalid("Invalid token id".into()))?;
let mut stmt = self.db.prepare_cached(
"SELECT s.step_micros FROM markets m LEFT JOIN market_tick_sizes s ON s.market=m.id WHERE m.id=?1"
)?;
let tick = stmt
.query_row([market], |r| r.get::<_, Option<i64>>(0))
.optional()?;
Ok(tick.map(|step| step.map(TickSize).unwrap_or(TickSize::WHOLE_CENT).number()))
}
}
impl TickSize {
pub(crate) const WHOLE_CENT: Self = Self(10_000);
fn number(&self) -> serde_json::Number {
inventory::decimal(self.0.into(), inventory::UNIT)
}
pub(crate) fn parse(value: &str) -> Result<Self> {
let micros = match value.trim_end_matches('0') {
"0.1" => 100_000,
@@ -66,7 +90,7 @@ impl TickSize {
}
pub fn text(&self) -> String {
format!("0.{:06}", self.0).trim_end_matches('0').to_owned()
self.number().to_string()
}
pub fn check(&self, price: i64) -> Result<()> {
+19
View File
@@ -2285,6 +2285,14 @@ fn configured_ticks_bind_order_amounts_execution_and_opening_price() {
e.execute(json!({"op":"list","id":"invalid","market":"legacy","rules":"r"}))
.unwrap();
assert!(e.execute(json!({"op":"assets","market":"legacy"})).unwrap()["tick_size"].is_null());
assert_eq!(
json!(e.minimum_tick_size("eb:NO:legacy").unwrap()),
json!(0.01)
);
assert!(e
.minimum_tick_size("eb:YES:no-such-market")
.unwrap()
.is_none());
e.execute(json!({"op":"order","id":"old","market":"legacy","outcome":"YES","account":"bob","side":"buy","price":0,"qty":1})).unwrap();
}
@@ -2293,11 +2301,18 @@ fn replayed_tick_changes_preserve_valid_orders_and_release_only_residuals() {
let dir = tempdir().unwrap();
let path = dir.path().join("tick-changes.db");
let mut e = Exchange::open(&path).unwrap();
let reader = Exchange::open(&path).unwrap();
let mut original = Value::Null;
let mut cursor = Value::Null;
for line in include_str!("../../../docs/examples/tick-changes.jsonl").lines() {
let cmd: Value = serde_json::from_str(line).unwrap();
let response = e.execute(cmd.clone()).unwrap();
if let Some(step) = cmd.get("new_tick_size").or(cmd.get("tick_size")) {
for token in ["eb:YES:cup", "eb:NO:cup"] {
let current = reader.minimum_tick_size(token).unwrap().unwrap();
assert_eq!(current.to_string(), step.as_str().unwrap());
}
}
match cmd["op"].as_str().unwrap() {
"tick_size_change" if cmd["id"] == "finer" => {
assert_eq!(response["canceled"], json!([]))
@@ -2325,6 +2340,10 @@ fn replayed_tick_changes_preserve_valid_orders_and_release_only_residuals() {
}
drop(e);
let mut e = Exchange::open(path).unwrap();
assert_eq!(
json!(e.minimum_tick_size("eb:YES:cup").unwrap()),
json!(0.1)
);
let before = state(&mut e);
let position = before["positions"]
.as_array()