Serve condition-scoped portfolio valuations from marked holdings

This commit is contained in:
2026-12-03 12:31:58 +08:00
parent 5696e3dae2
commit e26ba27a24
13 changed files with 302 additions and 16 deletions
+24
View File
@@ -333,3 +333,27 @@ sequenceDiagram
E-->>H: 每个可见市场一行
H-->>C: 200 data 数组,未找到返回空数组
```
### 查询持仓价值
`GET /v2/value?user=alice&condition=e` 返回该账户在市场 e 的持仓价值,单位为模拟美元。使用已登记的本地账户名或 20 字节钱包地址。省略 condition 查询所有已持有市场。无持仓的钱包返回零,仅有现金的账户也返回零。
```mermaid
flowchart LR
H["market_http.rs · GET /v2/value"] --> V["exchange/portfolio/http.rs"]
V --> A["exchange/portfolio.rs"] --> S["exchange/portfolio/value.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /v2/value?user=alice&condition=e
H->>E: 读取 e 的所有结果资产价值
alt 持仓均可估值
H-->>C: 200,data 含 proxy_wallet 和 value
else 非零持仓缺价
H-->>C: 503,Retry-After 提示重试
end
```
+22 -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/prices-history" | "/v2/trades" | "/v2/resolutions" | "/v2/value"
)
{
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/value"
| "/v2/trades"
| "/v2/resolutions"
| "/v2/prices-history",
@@ -525,6 +526,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/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),
_ => read_page(params, engine, Exchange::public_price_history),
@@ -535,6 +537,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/value" => (
503,
json!({"code":"unavailable","error":"portfolio valuation unavailable","retryable":true}),
),
Err(HistoryError::Unavailable) if path == "/v2/resolutions" => (
500,
json!({"code":"internal","error":"resolution state unavailable","retryable":true}),
@@ -554,6 +560,9 @@ fn send(req: Request, status: u16, body: Value, allow: Option<&str>) -> std::io:
let mut response = Response::from_data(body.to_string().into_bytes())
.with_status_code(status)
.with_header(Header::from_bytes("Content-Type", "application/json").unwrap());
if status == 503 {
response = response.with_header(Header::from_bytes("Retry-After", "1").unwrap());
}
if let Some(method) = allow {
response.add_header(Header::from_bytes("Allow", method).unwrap());
}
@@ -616,6 +625,9 @@ mod tests {
let lookup_info = json!({"gst":null,"nr":false,"rfqe":false,"ibce":false,"oas":0,"mos":0.01,"mts":0.0025,
"t":[{"o":"YES","t":"eb:YES:map+2/1"},{"o":"NO","t":"eb:NO:map+2/1"}]});
for command in [
json!({"op":"fund","id":"v-fund","account":"unpriced-owner","cents":100}),
json!({"op":"list","id":"v-list","market":"unpriced","rules":"valuation fixture"}),
json!({"op":"split","id":"v-split","account":"unpriced-owner","market":"unpriced","qty":1}),
json!({"op":"list","id":"s-list","market":"s","rules":"spread fixture"}),
json!({"op":"open","id":"s-open","market":"s"}),
json!({"op":"fund","id":"s-f1","account":"a","cents":100}),
@@ -686,6 +698,12 @@ 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/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),
("GET","/v2/value?user=a&condition=",String::new(),400,None),
("GET","/v2/value?user=0x4d97dcd97ec945f40cf65f87097ace5ea0476045",String::new(),400,None),
("POST","/v2/value",String::new(),405,None),
("GET","/v2/trades?user=unknown&conditionId=unknown&takerOnly=false",String::new(),200,Some(empty_trades)),
("GET","/v2/trades?offset=2",String::new(),400,None),
("GET","/v2/trades?cursor=invalid",String::new(),400,None),
@@ -850,6 +868,9 @@ mod tests {
assert!(headers
.to_ascii_lowercase()
.contains("content-type: application/json"));
if status == 503 {
assert!(headers.to_ascii_lowercase().contains("retry-after: 1"));
}
let actual: Value = serde_json::from_str(body).unwrap();
if let Some(expected) = expected {
assert_eq!(actual, expected);