Match observed public last-trade batch semantics

This commit is contained in:
2026-12-03 16:14:25 +08:00
parent 9cc3921e3c
commit d7a77c734d
8 changed files with 53 additions and 25 deletions
+2 -2
View File
@@ -125,7 +125,7 @@ sequenceDiagram
E-->>C: 后续点及是否还有数据
```
行情客户端也可以调用 `GET /last-trade-price`、`GET /last-trades-prices` 和 `POST /last-trades-prices`。GET 批量参数 token_ids 用逗号分隔;本地资产 ID 自身包含逗号时可使用 POST 的 JSON 数组。
行情客户端通过 `GET /last-trade-price` 查询单个结果,通过 `POST /last-trades-prices` 发送资产数组。只带 token_ids 查询参数的批量 GET 在生产样本中返回 400。
```mermaid
flowchart LR
@@ -140,7 +140,7 @@ sequenceDiagram
C->>H: 查询一个或多个资产的最后一笔成交
H->>E: last_trade_prices
E-->>H: 仅返回有成交记录的资产及其方向
H-->>C: 单查无成交时使用占位值,批查直接返回数组
H-->>C: 单查无成交时使用占位值,批查按资产 ID 排序并去重
```
客户端可通过 `GET /tick-size?token_id=…` 或 `GET /tick-size/{token_id}` 读取当前报价步长。URL 路径中的空格编码为 %20,加号表示加号本身。
+4 -15
View File
@@ -161,11 +161,6 @@ fn batch_params<T: serde::de::DeserializeOwned>(
serde_json::from_slice(&data).map_err(|_| (400, "Invalid payload"))
}
#[derive(Deserialize)]
struct TokenList {
token_ids: String,
}
fn last_prices(engine: &Mutex<Exchange>, tokens: Vec<String>, single: bool) -> Reply {
let mut rows = engine
.lock()
@@ -451,15 +446,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
(&Method::Get, "/last-trade-price") => serde_urlencoded::from_str::<BookParam>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| last_prices(engine, vec![p.token_id], true)),
(&Method::Get, "/last-trades-prices") => serde_urlencoded::from_str::<TokenList>(params)
.map_err(|_| (400, "Invalid payload"))
.and_then(|p| {
last_prices(
engine,
p.token_ids.split(',').map(str::to_owned).collect(),
false,
)
}),
(&Method::Get, "/last-trades-prices") => Err((400, "Invalid payload")),
(&Method::Post, "/last-trades-prices") => {
batch_params::<BookParam>(&mut req).and_then(|p| {
last_prices(
@@ -811,8 +798,10 @@ mod tests {
("GET","/tick-size",String::new(),400,Some(json!({"error":"Invalid token id"}))),
("POST","/tick-size/eb:YES:e",String::new(),405,None),
("GET",last_single.as_str(),String::new(),200,Some(no_fills)),
("GET",last_batch.as_str(),String::new(),200,Some(batch_no_fills.clone())),
("GET",last_batch.as_str(),String::new(),400,Some(json!({"error":"Invalid payload"}))),
("POST","/last-trades-prices",json!([{"token_id":yes,"side":"SELL"},{"token_id":no}]).to_string(),200,Some(batch_no_fills)),
("POST","/last-trades-prices","[]".into(),200,Some(json!([]))),
("GET","/last-trade-price?token_id=eb:YES:missing",String::new(),200,Some(json!({"price":"0.5","side":""}))),
("GET","/last-trade-price?token_id=",String::new(),400,Some(json!({"error":"Invalid token id"}))),
("POST","/last-trades-prices",json!(too_many).to_string(),400,Some(json!({"error":"Invalid payload"}))),
("DELETE","/last-trades-prices",String::new(),405,None),
+1 -1
View File
@@ -1037,7 +1037,7 @@ sequenceDiagram
L->>D: 单条查询联结每个资产的最近成交、成交上下文和吃单委托
D-->>L: 成交价格及吃单方结果和方向
L->>L: 对互补结果反转方向;缺少吃单记录时方向留空
L-->>H: 仅返回有成交的资产,按输入顺序保留重复项
L-->>H: 每个有成交的资产返回一项,按资产 ID 字符串升序排列
```
### 已提交的报价步长
+8 -3
View File
@@ -15,11 +15,16 @@ pub(crate) fn taker_side(
}
impl Exchange {
/// Return actual last trades, in request order, from one SQLite snapshot.
/// Return one latest trade per asset, sorted by token ID, from one SQLite snapshot.
pub fn last_trade_prices(&self, tokens: &[String]) -> Result<Vec<Value>, MarketDataError> {
if !(1..=500).contains(&tokens.len()) {
return Err(MarketDataError::Invalid("expected 1 to 500 tokens".into()));
if tokens.len() > 500 {
return Err(MarketDataError::Invalid(
"expected at most 500 tokens".into(),
));
}
let mut tokens: Vec<&str> = tokens.iter().map(String::as_str).collect();
tokens.sort_unstable();
tokens.dedup();
let assets = tokens
.iter()
.map(|token| assets::parse_id(token))
+9 -1
View File
@@ -3035,7 +3035,15 @@ fn market_stream_preserves_trades_and_empty_book_tick_across_restart() {
prices[1],
json!({"token_id":yes,"price":"0.35","side":"BUY"})
);
assert_eq!(prices[0], prices[2]);
assert_eq!(prices.len(), 2);
let shuffled = [
yes.clone(),
no.clone(),
"eb:YES:unknown".to_owned(),
yes.clone(),
];
assert_eq!(reader.last_trade_prices(&shuffled).unwrap(), prices);
assert!(reader.last_trade_prices(&[]).unwrap().is_empty());
let page = reader.market_stream_page("demo", Some(0)).unwrap();
let kinds: Vec<_> = page
.frames