Serve V2 token directory queries for local assets

This commit is contained in:
2026-12-05 05:02:02 +08:00
parent 31af9ad9cf
commit 2888563723
9 changed files with 247 additions and 1 deletions
+2
View File
@@ -423,3 +423,5 @@ sequenceDiagram
E-->>H: true
H-->>C: 200,neg_risk 为真
```
GET /v2/tokens 支持逗号分隔的资产 ID 或条件及互斥组 ID。两个选择器择一,回执按资产关联市场、事件和裁决结果。
+16 -1
View File
@@ -329,7 +329,12 @@ 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/oi"
"/v2/tokens"
| "/v2/prices-history"
| "/v2/trades"
| "/v2/resolutions"
| "/v2/value"
| "/v2/oi"
)
{
let (status, body) = data_page(path, params, engine);
@@ -473,6 +478,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
| "/market-clarifications"
| "/v2/oi"
| "/v2/value"
| "/v2/tokens"
| "/v2/trades"
| "/v2/resolutions"
| "/v2/prices-history",
@@ -513,6 +519,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/tokens" => read_page(params, engine, Exchange::public_tokens),
"/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),
@@ -525,6 +532,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/tokens" => (
503,
json!({"code":"unavailable","error":"token directory unavailable","retryable":true}),
),
Err(HistoryError::Unavailable) if path == "/v2/oi" => (
503,
json!({"code":"unavailable","error":"open interest unavailable","retryable":true}),
@@ -689,6 +700,10 @@ 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/tokens?token_ids=eb:NO:s,00123,eb:NO:s,eb:YES:s",String::new(),200,Some(db.public_tokens(serde_json::from_value(json!({"token_id":"eb:NO:s,123,eb:YES:s"})).unwrap()).unwrap())),
("GET","/v2/tokens?condition=s&token_id=eb:YES:s",String::new(),400,None),
("GET","/v2/tokens?token_id=00123,123",String::new(),200,Some(json!({"data":[]}))),
("POST","/v2/tokens",String::new(),405,None),
("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}))),
+23
View File
@@ -1434,3 +1434,26 @@ sequenceDiagram
E-->>C: 返回本次两侧销毁量和实付金额
end
```
### Data API 资产目录
```mermaid
flowchart LR
http["cli/market_http.rs"] --> directory["token_directory.rs"]
directory --> ids["assets.rs"]
directory --> sql["token_directory.sql"]
sql --> catalog["市场、事件目录和互斥组"]
```
```mermaid
sequenceDiagram
participant H as HTTP
participant E as public_tokens
participant D as SQLite
H->>E: 结果资产、市场条件或互斥组
E->>E: 校验选择器、合并重复值
E->>D: 同一快照读取目录、结果及裁决
D-->>E: 结果标签、互补资产、事件及兑付
E-->>H: 按请求顺序返回 data
```
+2
View File
@@ -41,6 +41,7 @@ mod resident;
mod resolution;
mod tick_changes;
mod tick_size;
mod token_directory;
use resident::Books;
use anyhow::{ensure, Context, Result};
@@ -59,6 +60,7 @@ pub use prices_history::legacy::ClobHistoryQuery;
pub use public_trades::http::TradeQuery;
pub use quotes::{PriceQueryError, PriceRequest, PriceSide};
pub use resolution::http::ResolutionQuery;
pub use token_directory::TokenDirectoryQuery;
#[derive(Debug)]
pub enum MarketDataError {
+114
View File
@@ -0,0 +1,114 @@
use crate::{assets, inventory::decimal, Exchange, MarketDataError};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashSet;
const MAX_TOKEN: &str =
"115792089237316195423570985008687907853269984665640564039457584007913129639935";
#[derive(Deserialize)]
pub struct TokenDirectoryQuery {
#[serde(alias = "token_ids")]
token_id: Option<String>,
#[serde(alias = "condition_id", alias = "conditionId")]
condition: Option<String>,
}
fn invalid(message: impl Into<String>) -> MarketDataError {
MarketDataError::Invalid(message.into())
}
fn condition_hex(value: &str) -> Option<String> {
let hex = value
.strip_prefix("0x")
.or_else(|| value.strip_prefix("0X"))?;
if !matches!(hex.len(), 62 | 64) || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
Some(format!("0x{}", hex.to_ascii_lowercase()))
}
impl Exchange {
pub fn public_tokens(&self, query: TokenDirectoryQuery) -> Result<Value, MarketDataError> {
let token = query.token_id.as_deref().filter(|s| !s.trim().is_empty());
let condition = query.condition.as_deref().filter(|s| !s.trim().is_empty());
let (raw, is_token, limit, selector) = match (token, condition) {
(Some(raw), None) => (raw, true, 50, "token_id"),
(None, Some(raw)) => (raw, false, 10, "condition"),
_ => return Err(invalid("provide exactly one of 'token_id' or 'condition'")),
};
let tx = self.db.unchecked_transaction()?;
let mut seen = HashSet::new();
let mut keys = Vec::new();
for value in raw.split(',').map(str::trim) {
let (key, market, outcome) = if is_token {
if let Ok((outcome, market)) = assets::parse_id(value) {
(value.to_owned(), Some(market.to_owned()), Some(outcome))
} else {
let digits = value.trim_start_matches('0');
if value.is_empty()
|| !value.bytes().all(|b| b.is_ascii_digit())
|| digits.len() > MAX_TOKEN.len()
|| (digits.len() == MAX_TOKEN.len() && digits > MAX_TOKEN)
{
return Err(invalid(format!("invalid token id: {value}")));
}
// Numeric IDs are external. They count towards the cap but
// have no corresponding local token without a venue mapping.
(digits.to_owned(), None, None)
}
} else if let Some(hex) = condition_hex(value) {
(hex.clone(), Some(hex), None)
} else {
let listed: bool = tx.query_row(
"SELECT EXISTS(SELECT 1 FROM markets WHERE id=?1)
OR EXISTS(SELECT 1 FROM neg_risk_groups WHERE id=?1)",
[value],
|row| row.get(0),
)?;
if !listed {
return Err(invalid(format!("invalid condition id: {value}")));
}
(value.to_owned(), Some(value.to_owned()), None)
};
if seen.insert(key) {
if seen.len() > limit {
return Err(invalid(format!(
"'{selector}' accepts at most {limit} distinct values"
)));
}
if let Some(market) = market {
keys.push((market, outcome));
}
}
}
let data = {
let mut stmt = tx.prepare_cached(include_str!("token_directory.sql"))?;
let input = serde_json::to_string(&keys).map_err(|_| MarketDataError::Unavailable)?;
let rows = stmt.query_map([input], |r| {
let market: String = r.get(0)?;
let phase: String = r.get(5)?;
let winner: Option<String> = r.get(6)?;
let index: i64 = r.get(7)?;
let (outcome, other) = if index == 0 { ("YES", "NO") } else { ("NO", "YES") };
let group: Option<String> = r.get(8)?;
let price = winner.as_deref().filter(|_| phase=="resolved").map(|winner| {
if winner == "VOID" { decimal(5,10) } else { decimal(i128::from(winner == outcome),1) }
});
Ok(json!({
"token_id":assets::id(&market,outcome),"condition_id":market,
"structural_condition_id":null,"module":if group.is_some() { "local_neg_risk" } else { "local_binary" },
"outcome_index":index,"clob_index":null,"outcome":r.get::<_,String>(if index==0 {3} else {4})?,
"opposite_token_id":assets::id(&market,other),
"resolved":phase=="resolved","closed":matches!(phase.as_str(),"closed"|"resolved"),
"final_price":price,"title":r.get::<_,String>(1)?,"market_slug":r.get::<_,String>(2)?,
"neg_risk":group.is_some(),"neg_risk_market_id":group,"question_index":r.get::<_,Option<i64>>(9)?,
"event_id":r.get::<_,Option<i64>>(10)?,"event_slug":r.get::<_,Option<String>>(11)?
}))
})?;
rows.collect::<rusqlite::Result<Vec<_>>>()?
};
tx.commit()?;
Ok(json!({"data":data}))
}
}
+21
View File
@@ -0,0 +1,21 @@
WITH selectors AS (
SELECT key AS ordinal, json_extract(value,'$[0]') AS market,
json_extract(value,'$[1]') AS outcome
FROM json_each(?1)
), markets_selected AS (
SELECT s.ordinal,m.id AS market,s.outcome
FROM selectors s JOIN markets m ON m.id=s.market
UNION ALL
SELECT s.ordinal,n.market,NULL
FROM selectors s JOIN neg_risk_members n ON n.group_id=s.market
WHERE s.outcome IS NULL AND NOT EXISTS (SELECT 1 FROM markets m WHERE m.id=s.market)
), slots(outcome, outcome_index) AS (VALUES ('YES',0),('NO',1))
SELECT m.id,w.question,w.slug,w.yes_label,w.no_label,m.phase,m.winner,s.outcome_index,
n.group_id,n.ordinal,e.event_id,e.slug
FROM markets_selected x JOIN markets m ON m.id=x.market
JOIN market_wire_metadata w ON w.market=x.market
JOIN slots s ON x.outcome IS NULL OR s.outcome=x.outcome
LEFT JOIN neg_risk_members n ON n.market=x.market
LEFT JOIN listed_markets l ON l.market=x.market
LEFT JOIN numbered_events e ON e.id=l.event
ORDER BY x.ordinal,n.ordinal,m.id,s.outcome_index
+43
View File
@@ -4176,3 +4176,46 @@ fn order_submission_terms_survive_fills_cancel_retries_and_legacy_upgrade() {
assert_eq!(e.execute(cmd.clone()).unwrap()["orders"][0], expected);
}
}
#[test]
fn public_token_directory_routes_results_and_expands_negative_risk_groups() {
let mut exchange = Exchange::open(":memory:").unwrap();
for line in include_str!("../../../docs/examples/neg-risk.jsonl")
.lines()
.take(5)
{
exchange
.execute(serde_json::from_str(line).unwrap())
.unwrap();
}
let read = |e: &Exchange, value| {
e.public_tokens(serde_json::from_value(value).unwrap())
.unwrap()["data"]
.clone()
};
let group = read(&exchange, json!({"condition":"winner"}));
assert_eq!(group.as_array().unwrap().len(), 6);
assert_eq!(group[0]["condition_id"], "a");
assert_eq!(group[0]["question_index"], 0);
assert_eq!(group[1]["opposite_token_id"], group[0]["token_id"]);
assert_eq!(group[4]["condition_id"], "other");
assert!(group[0]["event_id"].is_number());
let rows = read(
&exchange,
json!({"token_ids":"eb:NO:a,123,eb:YES:a,eb:NO:a"}),
);
assert_eq!(rows, json!([group[1], group[0]]));
assert_eq!(rows[0]["final_price"], Value::Null);
setup(&mut exchange);
exchange
.execute(json!({"op":"close","id":"close-e","market":"e"}))
.unwrap();
exchange
.execute(json!({"op":"finalize","id":"void-e","market":"e","winner":"VOID"}))
.unwrap();
let voided = read(&exchange, json!({"conditionId":"e"}));
assert_eq!(voided[0]["closed"], true);
assert_eq!(voided[1]["resolved"], true);
assert_eq!(voided[0]["final_price"], 0.5);
assert_eq!(voided[1]["final_price"], 0.5);
}
+21
View File
@@ -0,0 +1,21 @@
{
"one": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=759916398134244316868652908232832573045153963965173623897622651599218278400","status":200,"date":"Fri, 04 Dec 2026 20:59:33 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"alias": {"url":"https://data-api.polymarket.com/v2/tokens?tokenId=759916398134244316868652908232832573045153963965173623897622651599218278400","status":400,"date":"Fri, 04 Dec 2026 20:59:33 GMT","body":{"error":"provide exactly one of 'token_id' or 'condition'","code":"invalid_request","retryable":false}},
"pair": {"url":"https://data-api.polymarket.com/v2/tokens?condition=0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","status":200,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":1,"clob_index":null,"outcome":"Fokin Illia","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","resolved":true,"final_price":0.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"pair_alias": {"url":"https://data-api.polymarket.com/v2/tokens?conditionId=0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","status":200,"date":"Fri, 04 Dec 2026 20:59:33 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":1,"clob_index":null,"outcome":"Fokin Illia","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","resolved":true,"final_price":0.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"v1": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=73979342585906843441145134109923710004884703083095161594360108477170435492453","status":200,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"data":[{"token_id":"73979342585906843441145134109923710004884703083095161594360108477170435492453","condition_id":"0xdf04a0495b0a16dbce386acdc16d9aed19784db9888be53d150354b83b7c18b1","structural_condition_id":null,"module":"v1_ctf","outcome_index":0,"clob_index":0,"outcome":"Over","opposite_token_id":"78188826358032258311424545486331333398838521699262460256542567713692269401385","resolved":true,"final_price":0.0,"title":"Map 1 Total Rounds: Over/Under 30.5","market_slug":"cs2-bb3-lgc-2026-12-03-game1-round-total-30pt5","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1495714,"event_slug":"cs2-bb3-lgc-2026-12-03"}]}},
"multi": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=759916398134244316868652908232832573045153963965173623897622651599218278401%2C123%2C759916398134244316868652908232832573045153963965173623897622651599218278400%2C759916398134244316868652908232832573045153963965173623897622651599218278401","status":200,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":1,"clob_index":null,"outcome":"Fokin Illia","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","resolved":true,"final_price":0.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"both": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=759916398134244316868652908232832573045153963965173623897622651599218278400&condition=0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","status":400,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"error":"provide exactly one of 'token_id' or 'condition'","code":"invalid_request","retryable":false}},
"none": {"url":"https://data-api.polymarket.com/v2/tokens?","status":400,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"error":"provide exactly one of 'token_id' or 'condition'","code":"invalid_request","retryable":false}},
"missing": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=123","status":200,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"data":[]}},
"malformed": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=abc","status":400,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"error":"invalid token id: abc","code":"invalid_request","parameter":"token_id","retryable":false}},
"bad_condition": {"url":"https://data-api.polymarket.com/v2/tokens?condition=invalid","status":400,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"error":"invalid condition id: invalid","code":"invalid_request","parameter":"condition","retryable":false}},
"unknown_condition": {"url":"https://data-api.polymarket.com/v2/tokens?condition=0x0000000000000000000000000000000000000000000000000000000000000000","status":200,"date":"Fri, 04 Dec 2026 20:59:34 GMT","body":{"data":[]}},
"multi_condition": {"url":"https://data-api.polymarket.com/v2/tokens?condition=0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000%2C0xdf04a0495b0a16dbce386acdc16d9aed19784db9888be53d150354b83b7c18b1","status":200,"date":"Fri, 04 Dec 2026 20:59:35 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":1,"clob_index":null,"outcome":"Fokin Illia","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","resolved":true,"final_price":0.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"73979342585906843441145134109923710004884703083095161594360108477170435492453","condition_id":"0xdf04a0495b0a16dbce386acdc16d9aed19784db9888be53d150354b83b7c18b1","structural_condition_id":null,"module":"v1_ctf","outcome_index":0,"clob_index":0,"outcome":"Over","opposite_token_id":"78188826358032258311424545486331333398838521699262460256542567713692269401385","resolved":true,"final_price":0.0,"title":"Map 1 Total Rounds: Over/Under 30.5","market_slug":"cs2-bb3-lgc-2026-12-03-game1-round-total-30pt5","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1495714,"event_slug":"cs2-bb3-lgc-2026-12-03"},{"token_id":"78188826358032258311424545486331333398838521699262460256542567713692269401385","condition_id":"0xdf04a0495b0a16dbce386acdc16d9aed19784db9888be53d150354b83b7c18b1","structural_condition_id":null,"module":"v1_ctf","outcome_index":1,"clob_index":1,"outcome":"Under","opposite_token_id":"73979342585906843441145134109923710004884703083095161594360108477170435492453","resolved":true,"final_price":1.0,"title":"Map 1 Total Rounds: Over/Under 30.5","market_slug":"cs2-bb3-lgc-2026-12-03-game1-round-total-30pt5","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1495714,"event_slug":"cs2-bb3-lgc-2026-12-03"}]}},
"plural": {"url":"https://data-api.polymarket.com/v2/tokens?token_ids=759916398134244316868652908232832573045153963965173623897622651599218278400","status":200,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"zero_prefix": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=000759916398134244316868652908232832573045153963965173623897622651599218278400","status":200,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"empty_token": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=","status":400,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"error":"provide exactly one of 'token_id' or 'condition'","code":"invalid_request","retryable":false}},
"huge": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999","status":400,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"error":"invalid token id: 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999","code":"invalid_request","parameter":"token_id","retryable":false}},
"short_condition": {"url":"https://data-api.polymarket.com/v2/tokens?condition=0x01ae18f1c798f53d0efe0903889e57483c0000000000000000000000000000","status":200,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"data":[{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":0,"clob_index":null,"outcome":"Nikolenko Anatolii","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","resolved":true,"final_price":1.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"},{"token_id":"759916398134244316868652908232832573045153963965173623897622651599218278401","condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","structural_condition_id":"0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000","module":"binary","outcome_index":1,"clob_index":null,"outcome":"Fokin Illia","opposite_token_id":"759916398134244316868652908232832573045153963965173623897622651599218278400","resolved":true,"final_price":0.0,"title":"Nikolenko Anatolii vs. Fokin Illia","market_slug":"setkameua-anat-illia-2026-12-03","closed":true,"neg_risk":false,"neg_risk_market_id":null,"question_index":null,"event_id":1496938,"event_slug":"setkameua-anat-illia-2026-12-03"}]}},
"cap_tokens": {"url":"https://data-api.polymarket.com/v2/tokens?token_id=0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C15%2C16%2C17%2C18%2C19%2C20%2C21%2C22%2C23%2C24%2C25%2C26%2C27%2C28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38%2C39%2C40%2C41%2C42%2C43%2C44%2C45%2C46%2C47%2C48%2C49%2C50","status":400,"date":"Fri, 04 Dec 2026 21:00:05 GMT","body":{"error":"'token_id' accepts at most 50 distinct values","code":"invalid_request","parameter":"token_id","retryable":false}}
}
+5
View File
@@ -466,3 +466,8 @@ SDK 使用 `GET /neg-risk?token_id=…`,也可以从已缓存的 CLOB 市场
用空数据库重放[原单状态样例](examples/order-status.jsonl)。卖方以 0.60 挂 5 份 post-only 卖单,买方按金额吃掉 2 份。卖方撤回余单后,按原订单号查询,累计成交仍为 2,可成交余量为 0,`order_type` 为 GTC。买方的原单类型为 FAK。按卖方订单号查询 `trades`,这笔执行的份额也是 2。重启和重发相同命令沿用原回执与受理时间。
`open_orders` 的列表和精确查询均返回 `created_at`(Unix 秒)、`order_type` 和本地扩展 `post_only`。升级已有数据库时,用已受理的命令回执和 GTD 截止记录恢复提交指令;没有原始时钟记录的受理时间为 null。缺少原始回执的历史行仍可查询,未知字段保留为 null。官方当前将累计成交和逐笔对账分别放在订单的 size_matched 和账户成交记录;其单笔订单 HTTP 接口需要账户认证。本地验证使用可信操作员的 JSONL 接口,订单号与账户是模拟环境的标识。生产私有接口尚未实测。[官方订单查询](https://docs.polymarket.com/trading/manage-orders)
### Data API 资产反查
GET /v2/tokens 接受 token_id / token_ids 或 condition / condition_id / conditionId,二选一。生产样本中,资产查询保持请求顺序并合并重复项,数字标识的前导零会被归一。合法的未知资产返回空数组。用本地市场 ID 返回 YES、NO 两侧,用本地互斥组 ID 按题目序号展开成员。已裁决市场可返回最终兑付值,VOID 有两项 0.5。回执保留稳定的本地 eb 标识并自描述本地模块。对照样本覆盖原生 V2、CTF 和短条件 ID,外场同一市场的多协议资产映射仍待接入本地目录。[生产请求和响应](examples/public-token-directory.json) · [官方 OpenAPI](https://data-api.polymarket.com/v2/openapi.json)