Compare commits

...

4 Commits

12 changed files with 697 additions and 35 deletions
+10 -1
View File
@@ -90,6 +90,7 @@ sequenceDiagram
flowchart LR
main["main.rs"] --> http["market_http.rs"]
http --> api["exchange::Exchange::public_books"]
http --> clock["exchange::Exchange::server_time"]
```
```mermaid
@@ -97,12 +98,20 @@ sequenceDiagram
participant C as SDK
participant H as market_http
participant E as Exchange
opt 需要计算 GTD 截止时间
C->>H: GET /time
H->>E: server_time
E-->>H: Unix 秒
H-->>C: JSON 整数
end
C->>H: GET /book?token_id=... 或 POST /books
H->>E: 提交本次请求的资产 ID
E-->>H: 一个数据库快照中的盘口列表
H-->>C: 单盘 JSON 对象或批量 JSON 数组
```
`GET /time` 返回一个 JSON 整数,单位为 Unix 秒。客户端可用它估算本机与交易服务的时钟差,再填写 GTD 的 `expiration`。这是读取时钟的端点,无需账户或资产 ID。
同一个 HTTP 监听器提供 `GET /v2/prices-history`。客户端按资产查询历史,接续页可以只发同一资产和上一页的游标。[请求样例](../../docs/examples/clob-http.http)
```mermaid
@@ -223,7 +232,7 @@ sequenceDiagram
participant E as prices_history/legacy.rs
C->>H: GET /prices-history,market 与 interval 或 startTs、endTs
H->>E: clob_price_history,fidelity 以分钟计
E-->>H: 返回窗口内盘口中点样本,末尾追加最近可用点
E-->>H: 返回窗口内盘口中点样本,未封盘时追加最近可用点
H-->>C: history 数组,每点包含 t 和 p
```
+29 -1
View File
@@ -404,6 +404,14 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
Err(HistoryError::Unavailable) => Err((500, "Internal server error")),
}
}
(&Method::Get, "/time") => engine
.lock()
.map_err(|_| (500, "Internal server error"))
.and_then(|db| {
db.server_time()
.map(|seconds| json!(seconds))
.map_err(|_| (500, "Internal server error"))
}),
(&Method::Get, "/price") => serde_urlencoded::from_str::<WirePrice>(params)
.map_err(|_| (400, "Invalid token id"))
.and_then(|p| book_prices(engine, vec![p], true)),
@@ -466,6 +474,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
| "/midpoint"
| "/spread"
| "/book"
| "/time"
| "/last-trade-price"
| "/prices-history"
| "/markets"
@@ -581,7 +590,11 @@ pub(crate) fn serve(engine: Exchange, listener: TcpListener) -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use std::{io::Write, net::TcpStream, time::Duration};
use std::{
io::Write,
net::TcpStream,
time::{Duration, SystemTime, UNIX_EPOCH},
};
#[test]
fn socket_serves_single_and_batch_books_and_rejects_invalid_requests() {
@@ -863,6 +876,8 @@ mod tests {
("POST","/v2/prices-history",String::new(),405,None),
("GET", "/books", String::new(), 405, None),
("POST", "/order", "{}".into(), 404, None),
("GET", "/time", String::new(), 200, None),
("POST", "/time", String::new(), 405, None),
];
let count = cases.len();
let worker = thread::spawn(move || {
@@ -876,6 +891,10 @@ mod tests {
}
});
for (method, path, body, status, expected) in cases {
let before = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let mut socket = TcpStream::connect(port).unwrap();
socket
.set_read_timeout(Some(Duration::from_secs(5)))
@@ -897,6 +916,15 @@ mod tests {
let actual: Value = serde_json::from_str(body).unwrap();
if let Some(expected) = expected {
assert_eq!(actual, expected);
} else if path == "/time" && status == 200 {
let after = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
assert!((before..=after).contains(&actual.as_u64().unwrap()));
} else if path == "/time" {
assert!(headers.to_ascii_lowercase().contains("allow: get"));
assert!(actual["error"].is_string());
} else if status == 200 {
assert_eq!(actual.as_array().unwrap().len(), MAX_BOOKS);
} else {
+11 -3
View File
@@ -34,6 +34,8 @@ flowchart LR
expiry -->|清理到期注册| beat
beat -->|释放订单尚未成交的预留| book
api -->|启动和请求时检查到期时间| expiry["src/expiry.rs"]
api -->|读取服务端 Unix 秒| expiry
book -->|使用同一时钟校验 GTD| expiry
expiry -->|释放订单剩余预留| book["src/book.rs"]
api -->|初始化数据库| schema["src/schema.sql"]
api -->|校验请求键| valid["src/validation.rs"]
@@ -231,6 +233,12 @@ sequenceDiagram
participant E as Exchange
participant D as SQLite
participant B as 常驻盘口
opt HTTP 客户端校时
C->>E: server_time
E->>D: 读取与 GTD 共用的 Unix 秒
D-->>E: 服务器时间
E-->>C: Unix 秒整数
end
C->>E: 提交 GTD,含 Unix 秒时间戳
E->>D: 校验申报时间并原子记录委托与期限
C->>E: 到达有效截止点后查询或提交新单
@@ -243,7 +251,7 @@ sequenceDiagram
E-->>C: 返回查询结果或新单回执
```
没有请求时,模拟 CLI 会等待输入。下一次查询、交易或进程重启会先结清已到期的余单。成交、手动撤单和停盘会同步移除相应的待到期项。
模拟 CLI 在等待输入时会按最近的截止点唤醒。盘口查询、交易或进程重启也会先结清已到期余单。单纯读时钟不会触发清理。成交、手动撤单和停盘会同步移除相应的待到期项。
## 用结果资产 ID 下单
@@ -1133,7 +1141,7 @@ flowchart LR
L --> A["assets.rs"]
L --> Q["prices_history/legacy.sql"]
Q --> T["quote_schema.sql 中的 quote_events"]
Q --> O["prices_history/chart_schema.sql 中的采样起点和时间索引"]
Q --> O["prices_history/chart_schema.sql 中的采样起点、封盘时间和时间索引"]
API["lib.rs"] --> O
Q --> S["schema.sql 中的 price_settlements"]
```
@@ -1145,7 +1153,7 @@ sequenceDiagram
participant D as SQLite
C->>E: clob_price_history
E->>D: 清理到期委托并取得读快照与数据库时钟
E->>D: 按 UTC 分钟网格重放各采样点当时的盘口,再读取最近可用点
E->>D: 按 UTC 分钟网格重放盘口,封盘后只取窗口内的封盘前样本
D-->>E: 观测时间和定点价格
E-->>C: t 为 Unix 秒,p 为十进制数
```
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::{command::TimeInForce, resident::Books};
use crate::{
ledger,
expiry, ledger,
validation::{amount, identifier, phase},
};
use anyhow::{ensure, Result};
@@ -109,7 +109,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
TimeInForce::Gtd => {
let expiration =
expiration.ok_or_else(|| anyhow::anyhow!("GTD requires expiration"))?;
let now: i64 = tx.query_row("SELECT unixepoch()", [], |r| r.get(0))?;
let now = expiry::unix_seconds(tx)?;
ensure!(
expiration >= now + 180,
"GTD expiration must be at least 180 seconds ahead"
+4
View File
@@ -2,6 +2,10 @@ use crate::book::{release, CancelReason};
use anyhow::Result;
use rusqlite::{Connection, Transaction, TransactionBehavior};
pub(crate) fn unix_seconds(db: &Connection) -> rusqlite::Result<i64> {
db.query_row("SELECT unixepoch()", [], |row| row.get(0))
}
/// Return a snapshot whose live book was current when the snapshot was acquired.
/// Expirations commit separately so a rejected command cannot revive an order.
pub(crate) fn transaction(
+6 -1
View File
@@ -121,7 +121,7 @@ impl Exchange {
db.execute_batch(include_str!("quote_schema.sql"))?;
}
let history_ready: bool = db.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='trigger' AND name='chart_market_listed')",
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='trigger' AND name='chart_market_closed')",
[], |r| r.get(0),
)?;
if !history_ready {
@@ -168,6 +168,11 @@ impl Exchange {
Ok(())
}
/// Unix seconds on the same database clock used to validate GTD orders.
pub fn server_time(&self) -> Result<i64, MarketDataError> {
Ok(expiry::unix_seconds(&self.db)?)
}
/// Time until the next pending GTD stops trading, measured on the database clock.
pub fn next_gtd_expiry(&self) -> Result<Option<Duration>> {
let (first, now): (Option<i64>, i64) = self.db.query_row(
@@ -12,4 +12,12 @@ END;
INSERT OR IGNORE INTO chart_origins
SELECT id,COALESCE((SELECT MIN(at_ms) FROM quote_events WHERE market=m.id),
CAST(unixepoch('subsec')*1000 AS INTEGER)) FROM markets m;
CREATE TABLE IF NOT EXISTS chart_closures (
market TEXT PRIMARY KEY REFERENCES markets,
at_ms INTEGER NOT NULL
);
CREATE TRIGGER IF NOT EXISTS chart_market_closed AFTER UPDATE OF phase ON markets
WHEN NEW.phase='closed' AND OLD.phase<>'closed' BEGIN
INSERT INTO chart_closures VALUES(NEW.id,CAST(unixepoch('subsec')*1000 AS INTEGER));
END;
COMMIT;
+12 -12
View File
@@ -2,14 +2,16 @@
-- The local sampler uses a UTC minute grid. The venue's collector has clock jitter.
WITH RECURSIVE horizon AS (
SELECT (SELECT at_ms FROM chart_origins WHERE market=?1) AS first_ms,
(SELECT at_ms FROM price_settlements WHERE market=?1) AS resolved_ms,
(SELECT winner FROM price_settlements WHERE market=?1) AS winner,
(SELECT phase FROM markets WHERE id=?1) AS phase
(SELECT at_ms FROM chart_closures WHERE market=?1) AS closed_ms,
(SELECT phase IN ('closed','resolved') FROM markets WHERE id=?1) AS closed
), bounds AS (
SELECT first_ms, resolved_ms, winner,
CASE WHEN phase='resolved' THEN COALESCE(resolved_ms,
(SELECT MAX(at_ms) FROM quote_events WHERE market=?1))
ELSE ?6/60000*60000 END AS last_ms
SELECT first_ms, closed,
CASE WHEN NOT closed THEN ?6/60000*60000
WHEN closed_ms IS NOT NULL THEN closed_ms-1
-- Old closed markets lack the close time. Use the last
-- recorded nonempty quote and exclude the closing clear.
ELSE (SELECT MAX(at_ms) FROM quote_events WHERE market=?1
AND (yes_bid IS NOT NULL OR yes_ask IS NOT NULL)) END AS last_ms
FROM horizon
), grid(ms) AS (
SELECT ((MAX(?3,first_ms)+?5-1)/?5)*?5 FROM bounds
@@ -19,14 +21,12 @@ WITH RECURSIVE horizon AS (
), points(ms,tail) AS (
SELECT ms,0 FROM grid
UNION ALL
SELECT last_ms,1 FROM bounds WHERE resolved_ms IS NOT NULL OR first_ms<=last_ms
SELECT last_ms,1 FROM bounds WHERE NOT closed AND first_ms<=last_ms
)
SELECT points.ms/1000,
CASE WHEN points.ms=b.resolved_ms THEN
CASE WHEN b.winner='VOID' THEN 500000 WHEN b.winner=?2 THEN 1000000 ELSE 0 END
WHEN ?2='YES' THEN (COALESCE(q.yes_bid,0)+COALESCE(q.yes_ask,10000))*50
CASE WHEN ?2='YES' THEN (COALESCE(q.yes_bid,0)+COALESCE(q.yes_ask,10000))*50
ELSE 1000000-(COALESCE(q.yes_bid,0)+COALESCE(q.yes_ask,10000))*50 END
FROM points CROSS JOIN bounds b
FROM points
LEFT JOIN quote_events q ON q.seq=(
SELECT seq FROM quote_events WHERE market=?1 AND at_ms<=points.ms
ORDER BY at_ms DESC,seq DESC LIMIT 1
+13 -13
View File
@@ -683,9 +683,7 @@ fn gtd_expiry_survives_rejection_and_recovery() {
e.execute(json!({"op":"open","id":"start","market":"e"}))
.unwrap();
let db = rusqlite::Connection::open(&path).unwrap();
let now: i64 = db
.query_row("SELECT unixepoch()", [], |r| r.get(0))
.unwrap();
let now = e.server_time().unwrap();
assert_eq!(e.next_gtd_expiry().unwrap(), None);
let mut ask = order("dated", "alice", "sell", 60, 5);
ask["tif"] = json!("GTD");
@@ -1553,21 +1551,23 @@ fn legacy_history_samples_the_book_without_fills_and_appends_the_latest_mark() {
exchange
.execute(json!({"op":"close","id":"close","market":"mid-example"}))
.unwrap();
db.execute("UPDATE chart_closures SET at_ms=273000", [])
.unwrap();
db.execute(
"UPDATE quote_events SET at_ms=273000 WHERE yes_bid IS NULL AND yes_ask IS NULL",
[],
)
.unwrap();
let before_close = vec![json!({"t":240,"p":0.915})];
assert_eq!(read(&exchange, "NO", 180.001, 400.0), before_close);
exchange
.execute(json!({"op":"finalize","id":"verdict","market":"mid-example","winner":"NO"}))
.unwrap();
db.execute("UPDATE price_settlements SET at_ms=273000", [])
.unwrap();
drop(exchange);
let reopened = Exchange::open(path).unwrap();
assert_eq!(
read(&reopened, "NO", 180.001, 240.0),
vec![json!({"t":240,"p":0.915}), json!({"t":273,"p":1})]
);
assert_eq!(
read(&reopened, "YES", 5.0, 5.0),
vec![json!({"t":273,"p":0})]
);
assert_eq!(read(&reopened, "NO", 180.001, 400.0), before_close);
assert!(read(&reopened, "YES", 273.0, 400.0).is_empty());
assert!(read(&reopened, "YES", 5.0, 5.0).is_empty());
}
#[test]
+3
View File
@@ -90,3 +90,6 @@ GET http://127.0.0.1:9002/clob-markets/cup
### Load neg-risk.jsonl to query either token of a member of the election group.
GET http://127.0.0.1:9002/neg-risk?token_id=eb%3ANO%3Aa
### Exchange clock in Unix seconds, used to calculate GTD expiration.
GET http://127.0.0.1:9002/time
@@ -0,0 +1,597 @@
[
{
"catalog_url": "https://gamma-api.polymarket.com/markets?closed=true&limit=100&order=closedTime&ascending=false",
"catalog": {
"id": "7554899",
"slug": "cs2-bb3-lgc-2026-12-03-game1-round-total-30pt5",
"version": "v1",
"conditionId": "0xdf04a0495b0a16dbce386acdc16d9aed19784db9888be53d150354b83b7c18b1",
"closed": true,
"closedTime": "2026-12-03 08:32:03.519799+00",
"acceptingOrders": false,
"umaResolutionStatus": "resolved",
"outcomePrices": "[\"0\", \"1\"]",
"outcomes": "[\"Over\", \"Under\"]",
"positionIds": null,
"clobTokenIds": "[\"73979342585906843441145134109923710004884703083095161594360108477170435492453\", \"78188826358032258311424545486331333398838521699262460256542567713692269401385\"]"
},
"sides": [
{
"asset": "73979342585906843441145134109923710004884703083095161594360108477170435492453",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=73979342585906843441145134109923710004884703083095161594360108477170435492453&startTs=1796284923&endTs=1796288523&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:56 GMT",
"body": {
"history": [
{
"t": 1796285541,
"p": 0.1
},
{
"t": 1796285655,
"p": 0.1
},
{
"t": 1796285780,
"p": 0.1
},
{
"t": 1796285889,
"p": 0.1
},
{
"t": 1796286038,
"p": 0.1
},
{
"t": 1796286155,
"p": 0.1
},
{
"t": 1796286252,
"p": 0.005
},
{
"t": 1796286383,
"p": 0.0005
},
{
"t": 1796286495,
"p": 0.0005
},
{
"t": 1796286646,
"p": 0.0005
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=73979342585906843441145134109923710004884703083095161594360108477170435492453&startTs=1796290323&endTs=1796290623&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:56 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=73979342585906843441145134109923710004884703083095161594360108477170435492453&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:56 GMT",
"body": {
"history": []
}
}
},
{
"asset": "78188826358032258311424545486331333398838521699262460256542567713692269401385",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=78188826358032258311424545486331333398838521699262460256542567713692269401385&startTs=1796284923&endTs=1796288523&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:58 GMT",
"body": {
"history": [
{
"t": 1796285540,
"p": 0.9
},
{
"t": 1796285654,
"p": 0.9
},
{
"t": 1796285780,
"p": 0.9
},
{
"t": 1796285889,
"p": 0.9
},
{
"t": 1796286037,
"p": 0.9
},
{
"t": 1796286155,
"p": 0.9
},
{
"t": 1796286252,
"p": 0.995
},
{
"t": 1796286383,
"p": 0.9995
},
{
"t": 1796286495,
"p": 0.9995
},
{
"t": 1796286646,
"p": 0.9995
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=78188826358032258311424545486331333398838521699262460256542567713692269401385&startTs=1796290323&endTs=1796290623&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:58 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=78188826358032258311424545486331333398838521699262460256542567713692269401385&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:58 GMT",
"body": {
"history": []
}
}
}
]
},
{
"catalog_url": "https://gamma-api.polymarket.com/markets?closed=true&limit=100&order=closedTime&ascending=false",
"catalog": {
"id": "7543472",
"slug": "setkameua-anat-illia-2026-12-03",
"version": "v2",
"conditionId": "0x01ae18f1c798f53d0efe0903889e57483c000000000000000000000000000000",
"closed": true,
"closedTime": "2026-12-03 08:31:00.370625+00",
"acceptingOrders": false,
"umaResolutionStatus": null,
"outcomePrices": "[\"1\", \"0\"]",
"outcomes": "[\"Nikolenko Anatolii\", \"Fokin Illia\"]",
"positionIds": [
"759916398134244316868652908232832573045153963965173623897622651599218278400",
"759916398134244316868652908232832573045153963965173623897622651599218278401"
],
"clobTokenIds": "[\"759916398134244316868652908232832573045153963965173623897622651599218278400\", \"759916398134244316868652908232832573045153963965173623897622651599218278401\"]"
},
"sides": [
{
"asset": "759916398134244316868652908232832573045153963965173623897622651599218278400",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278400&startTs=1796284860&endTs=1796288460&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:59 GMT",
"body": {
"history": [
{
"t": 1796284969,
"p": 0.999
},
{
"t": 1796285059,
"p": 0.999
},
{
"t": 1796285176,
"p": 0.999
},
{
"t": 1796285310,
"p": 0.999
},
{
"t": 1796285335,
"p": 0.999
},
{
"t": 1796285416,
"p": 0.999
},
{
"t": 1796285540,
"p": 0.999
},
{
"t": 1796285654,
"p": 0.999
},
{
"t": 1796285779,
"p": 0.999
},
{
"t": 1796285889,
"p": 0.999
},
{
"t": 1796286037,
"p": 0.999
},
{
"t": 1796286154,
"p": 0.999
},
{
"t": 1796286252,
"p": 0.999
},
{
"t": 1796286382,
"p": 0.999
},
{
"t": 1796286494,
"p": 0.999
},
{
"t": 1796286645,
"p": 0.999
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278400&startTs=1796290260&endTs=1796290560&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:59 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278400&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:34:59 GMT",
"body": {
"history": []
}
}
},
{
"asset": "759916398134244316868652908232832573045153963965173623897622651599218278401",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278401&startTs=1796284860&endTs=1796288460&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:00 GMT",
"body": {
"history": [
{
"t": 1796284969,
"p": 0.001
},
{
"t": 1796285059,
"p": 0.001
},
{
"t": 1796285176,
"p": 0.001
},
{
"t": 1796285310,
"p": 0.001
},
{
"t": 1796285335,
"p": 0.001
},
{
"t": 1796285416,
"p": 0.001
},
{
"t": 1796285540,
"p": 0.001
},
{
"t": 1796285654,
"p": 0.001
},
{
"t": 1796285779,
"p": 0.001
},
{
"t": 1796285889,
"p": 0.001
},
{
"t": 1796286037,
"p": 0.001
},
{
"t": 1796286154,
"p": 0.001
},
{
"t": 1796286252,
"p": 0.001
},
{
"t": 1796286382,
"p": 0.001
},
{
"t": 1796286494,
"p": 0.001
},
{
"t": 1796286645,
"p": 0.001
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278401&startTs=1796290260&endTs=1796290560&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:00 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=759916398134244316868652908232832573045153963965173623897622651599218278401&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:01 GMT",
"body": {
"history": []
}
}
}
]
},
{
"catalog_url": "https://gamma-api.polymarket.com/markets?closed=true&limit=100&order=closedTime&ascending=false",
"catalog": {
"id": "7528433",
"slug": "sol-updown-15m-1796285700",
"version": "v2",
"conditionId": "0x0190f69570a8ebb0d5608f8f773ffc068b000000000000000000000000000000",
"closed": true,
"closedTime": "2026-12-03 08:30:28.231292+00",
"acceptingOrders": false,
"umaResolutionStatus": null,
"outcomePrices": "[\"1\", \"0\"]",
"outcomes": "[\"Up\", \"Down\"]",
"positionIds": [
"708440684409184214698552699176045036593002385654320616860038876849074339840",
"708440684409184214698552699176045036593002385654320616860038876849074339841"
],
"clobTokenIds": "[\"708440684409184214698552699176045036593002385654320616860038876849074339840\", \"708440684409184214698552699176045036593002385654320616860038876849074339841\"]"
},
"sides": [
{
"asset": "708440684409184214698552699176045036593002385654320616860038876849074339840",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339840&startTs=1796284828&endTs=1796288428&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:02 GMT",
"body": {
"history": [
{
"t": 1796284968,
"p": 0.5
},
{
"t": 1796285058,
"p": 0.5
},
{
"t": 1796285173,
"p": 0.5
},
{
"t": 1796285310,
"p": 0.5
},
{
"t": 1796285334,
"p": 0.5
},
{
"t": 1796285414,
"p": 0.505
},
{
"t": 1796285539,
"p": 0.505
},
{
"t": 1796285653,
"p": 0.5
},
{
"t": 1796285778,
"p": 0.395
},
{
"t": 1796285888,
"p": 0.55
},
{
"t": 1796286036,
"p": 0.675
},
{
"t": 1796286153,
"p": 0.8
},
{
"t": 1796286251,
"p": 0.83
},
{
"t": 1796286382,
"p": 0.83
},
{
"t": 1796286494,
"p": 0.96
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339840&startTs=1796290228&endTs=1796290528&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:02 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339840&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:02 GMT",
"body": {
"history": []
}
}
},
{
"asset": "708440684409184214698552699176045036593002385654320616860038876849074339841",
"around": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339841&startTs=1796284828&endTs=1796288428&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:03 GMT",
"body": {
"history": [
{
"t": 1796284969,
"p": 0.5
},
{
"t": 1796285059,
"p": 0.5
},
{
"t": 1796285176,
"p": 0.5
},
{
"t": 1796285310,
"p": 0.5
},
{
"t": 1796285335,
"p": 0.5
},
{
"t": 1796285416,
"p": 0.495
},
{
"t": 1796285540,
"p": 0.495
},
{
"t": 1796285654,
"p": 0.5
},
{
"t": 1796285779,
"p": 0.605
},
{
"t": 1796285888,
"p": 0.455
},
{
"t": 1796286036,
"p": 0.335
},
{
"t": 1796286154,
"p": 0.205
},
{
"t": 1796286252,
"p": 0.17
},
{
"t": 1796286382,
"p": 0.175
},
{
"t": 1796286494,
"p": 0.08
}
]
}
},
"after": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339841&startTs=1796290228&endTs=1796290528&fidelity=2",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:03 GMT",
"body": {
"history": []
}
},
"ancient": {
"method": "GET",
"url": "https://clob.polymarket.com/prices-history?market=708440684409184214698552699176045036593002385654320616860038876849074339841&startTs=120&endTs=245",
"payload": null,
"status": 200,
"date": "Thu, 03 Dec 2026 08:35:04 GMT",
"body": {
"history": []
}
}
}
]
}
]
+2 -2
View File
@@ -65,7 +65,7 @@ Polymarket 的查询使用认证身份,部分凭证有会话级可见范围。
连续交易的 order 支持 `"tif":"GTD"` 及整数秒 `expiration`,也可与 `"post_only":true` 合用。省略 tif 且提供正的到期时间时,本地采用 GTD。若数据库时钟为 T,申报 T+179 会拒绝;申报 T+180 时,有效截止点是 T+120,达到该秒就释放剩余预留。使用原请求键重试仍返回原受理回执,可通过 open_orders 指定订单号查看当前状态和申报时间。JSONL 输入空闲时用数据库时钟计算距离最近一笔待到期 GTD 的毫秒数,到点唤醒并释放余单。进程重启时先清理已到期订单,再恢复盘口。操作系统调度或 SQLite 写锁会影响实际清理延迟。[官方限价委托说明](https://docs.polymarket.com/trading/place-orders.md)
模拟器以每次请求取得数据库快照的时间判断是否到期。无人请求时的独立定时通知、外场终态字符串及逐秒延迟仍待验证。回归用例 gtd_expiry_survives_rejection_and_recovery 覆盖部分成交后到期、FOK 拒单、幂等重试和重启时解冻。
通过 `GET /time` 可读取与 GTD 受理校验共用的 SQLite 时钟。成功响应是单个 Unix 秒整数。客户端若以服务器时间 `T` 计划有效挂单 600 秒,可填写 `T+660`;服务按实际受理时刻校验最短申报期限。读时钟本身不会更改挂单。回归用例 gtd_expiry_survives_rejection_and_recovery 从公开时钟取值,覆盖过早申报、部分成交后到期、FOK 拒单、幂等重试和重启时解冻。生产 `/time` 直连未取得可用响应,本轮依据官方接口定义。外场终态字符串及逐秒延迟仍待验证。[官方时钟接口](https://docs.polymarket.com/api-reference/data/get-server-time)
## 结果资产与订单路由
@@ -376,7 +376,7 @@ Polymarket 的单资产最近成交接口规定,无成交时返回价格字符
旧版 CLOB 客户端用 market 指定结果资产。单侧盘口的生产曲线末点为 0.0005,同批中点为 0.0005、最近成交为 0.001。一个近端成交记录为空的资产也有连续的 0.5 历史点。取早于上市的绝对区间时,有历史的资产仍返回最近可用点;新资产在中点已经可查时也可能返回空曲线。[生产请求和对照样本](examples/public-clob-history.json) · [CLOB OpenAPI](https://docs.polymarket.com/api-reference/markets/get-prices-history)
本地用已经提交的盘口变更重建历史中点。到每个 UTC 采样边界,取该时刻之前(含该时刻)最近一次盘口;缺买侧用 0,缺卖侧用 1。没有挂单变化时延续上次观测。窗口之外也追加最近一分钟的可用点;若与窗口最后一点重合,可出现重复时间戳。模拟裁决后在裁决时刻以兑付值收尾。已裁决的旧盘若缺少裁决时间,只使用最后有时间记录的报价。升级旧库时从第一条可信报价开始,没有报价记录的资产从迁移时刻开始。生产采集存在秒级抖动,新盘首个采样的延迟、长期历史保留和裁决后取点尚未逐点验证。V2 的成交与兑付历史保留其独立数据源。
本地用已经提交的盘口变更重建历史中点。到每个 UTC 采样边界,取该时刻之前(含该时刻)最近一次盘口;缺买侧用 0,缺卖侧用 1。没有挂单变化时延续上次观测。窗口之外也追加最近一分钟的可用点;若与窗口最后一点重合,可出现重复时间戳。封盘后,曲线只返回请求窗口内、封盘之前的采样点。电竞、乒乓球和 SOL 涨跌盘的六个生产结果均符合这一规则。电竞负侧、胜侧的末点分别为 0.0005 和 0.9995,目录最终兑付为 0 和 1。旧库没有封盘记录时,最多采样到最后一笔非空盘口记录。生产采样时钟有秒级偏移,新盘首个采样延迟和长期保留仍待核验。V2 曲线使用成交与兑付数据。[已裁决生产样本](examples/public-closed-chart-history.json)
fidelity 以分钟为单位;省略或不大于零时按 1 分钟处理。相对窗口 1w 至少需要 5 分钟、1m 至少需要 10 分钟。显式起止时间最多相隔 15 天,多一秒即拒绝。单给 startTs 时读到当前;两个时间相等时仍可返回末点。合法但未上市的本地资产返回空数组。[HTTP 样例](examples/clob-http.http)