Serve priced open interest for active unarchived market listings

This commit is contained in:
2026-12-03 12:39:03 +08:00
parent e26ba27a24
commit 2a62f1812a
16 changed files with 301 additions and 78 deletions
+21
View File
@@ -357,3 +357,24 @@ sequenceDiagram
H-->>C: 503,Retry-After 提示重试
end
```
### 查询未平仓量
`GET /v2/oi` 返回单行 GLOBAL。指定 `GET /v2/oi?condition=cup` 可查 cup 的金额,空盘返回 0,归档市场从数组中省略。筛选支持逗号分隔及 condition_id、conditionId 两个别名。已有份额却无法估值时,本地返回可重试的 503。
```mermaid
flowchart LR
H["market_http.rs · GET /v2/oi"] --> E["exchange/open_interest.rs"]
E --> D["exchange/open_interest.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /v2/oi?condition=cup
H->>E: 验证条件,按市场汇总
E-->>H: condition_id 和 value
H-->>C: 200,data 数组
```
+12 -1
View File
@@ -327,7 +327,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
if req.method() == &Method::Get
&& matches!(
path,
"/v2/prices-history" | "/v2/trades" | "/v2/resolutions" | "/v2/value"
"/v2/prices-history" | "/v2/trades" | "/v2/resolutions" | "/v2/value" | "/v2/oi"
)
{
let (status, body) = data_page(path, params, engine);
@@ -485,6 +485,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
| "/markets"
| "/events"
| "/market-clarifications"
| "/v2/oi"
| "/v2/value"
| "/v2/trades"
| "/v2/resolutions"
@@ -526,6 +527,7 @@ fn read_page<Q: serde::de::DeserializeOwned>(
fn data_page(path: &str, params: &str, engine: &Mutex<Exchange>) -> (u16, Value) {
let page = match path {
"/v2/oi" => read_page(params, engine, Exchange::public_open_interest),
"/v2/value" => read_page(params, engine, Exchange::public_portfolio_value),
"/v2/trades" => read_page(params, engine, Exchange::public_trade_feed),
"/v2/resolutions" => read_page(params, engine, Exchange::public_resolutions),
@@ -537,6 +539,10 @@ fn data_page(path: &str, params: &str, engine: &Mutex<Exchange>) -> (u16, Value)
400,
json!({"code":"invalid_request","error":message,"retryable":false}),
),
Err(HistoryError::Unavailable) if path == "/v2/oi" => (
503,
json!({"code":"unavailable","error":"open interest unavailable","retryable":true}),
),
Err(HistoryError::Unavailable) if path == "/v2/value" => (
503,
json!({"code":"unavailable","error":"portfolio valuation unavailable","retryable":true}),
@@ -698,6 +704,11 @@ mod tests {
assert_eq!(quoted["outcomePrices"], r#"["0.4","0.6"]"#);
let empty_trades = json!({"data":[],"pagination":{"limit":100,"offset":0,"has_more":false,"next_cursor":null}});
let cases = [
("GET","/v2/oi?conditionId=s,s",String::new(),200,Some(json!({"data":[{"condition_id":"s","value":0}]}))),
("GET","/v2/oi?condition_id=0x0000000000000000000000000000000000000000000000000000000000000001",String::new(),200,Some(json!({"data":[]}))),
("GET","/v2/oi?condition=bad-hash",String::new(),400,Some(json!({"code":"invalid_request","error":"invalid condition: bad-hash","retryable":false}))),
("GET","/v2/oi",String::new(),503,None),
("POST","/v2/oi",String::new(),405,None),
("GET","/v2/value?user=0x0000000000000000000000000000000000000001",String::new(),200,Some(json!({"data":{"proxy_wallet":"0x0000000000000000000000000000000000000001","value":0}}))),
("GET","/v2/value",String::new(),400,None),
("GET","/v2/value?user=unpriced-owner",String::new(),503,None),