验证 Polymarket 与 Kalshi 的只读目录及盘口 #4
Generated
+1096
-5
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["crates/matching", "crates/exchange", "crates/cli"]
|
members = ["crates/matching", "crates/exchange", "crates/cli", "crates/venue-probe"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "venue-probe"
|
||||||
|
version.workspace = true
|
||||||
|
edition.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls", "json"] }
|
||||||
|
serde.workspace = true
|
||||||
|
sha1 = "0.10"
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
# venue-probe
|
||||||
|
|
||||||
|
探针匿名读取生产目录及盘口。成功选定合约后每个场所输出一行 JSON,其中最优价格用每美元一百万微单位表示,数量保留场所返回的十进制字符串。Polymarket 依目录顺序返回两个资产的独立报价与快照标识;两次 GET 可能处于不同时间。Kalshi 返回一个 ticker 下 YES、NO 各自的买一和隐含卖一,卖量等于对侧买量。空盘一侧返回 null。目录请求失败时详情写入 stderr。已取得目录时,盘口失败写入该场所的 JSON,任一场所失败会使进程以非零状态退出。
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run --locked -p venue-probe
|
||||||
|
```
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart LR
|
||||||
|
main["src/main.rs"] -->|依次调用两个只读探针| impls["src/probes.rs"]
|
||||||
|
main -->|重放 Gamma 市场| catalog["src/catalog.rs"]
|
||||||
|
impls -->|识别持仓协议及两套 ID| catalog
|
||||||
|
main -->|指定资产取样或离线重放| audit["src/book_audit.rs"]
|
||||||
|
audit -->|精确比较价格| books
|
||||||
|
impls -.->|回归用例| fixtures["fixtures/gamma-559651.json · kalshi-combo.json"]
|
||||||
|
impls -->|可选买卖报价对照| check["src/price_check.rs"]
|
||||||
|
check -->|精确价格与市场标识| books
|
||||||
|
impls -->|目录标识、盘口校验和最优价| books["src/books.rs"]
|
||||||
|
```
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant P as venue-probe
|
||||||
|
participant G as Polymarket Gamma
|
||||||
|
participant C as Polymarket CLOB
|
||||||
|
participant K as Kalshi
|
||||||
|
P->>G: 查询开放市场
|
||||||
|
G-->>P: 返回版本、negRisk 和两套结果标识
|
||||||
|
P->>P: 保留各结果的 token ID 和 position ID,按版本选择盘口 ID
|
||||||
|
P->>P: CTF 按 negRisk 识别适配器,V2 从位置 ID 解出 31 字节条件并标注 Router
|
||||||
|
loop 按目录顺序读取两个结果
|
||||||
|
P->>C: 按 CTF token ID 或 V2 position ID 查询
|
||||||
|
alt 读取成功
|
||||||
|
C-->>P: condition、资产 ID、买卖盘
|
||||||
|
P->>P: 检查返回标识并提取最优价格与数量
|
||||||
|
opt --compare-prices
|
||||||
|
P->>C: 对同一资产分别查询 BUY 和 SELL
|
||||||
|
C-->>P: 返回报价或 HTTP 错误
|
||||||
|
P->>C: 再次读取该资产盘口
|
||||||
|
P->>P: 检查前后哈希、买一与卖一,再判断方向
|
||||||
|
end
|
||||||
|
else 读取失败
|
||||||
|
P->>P: 为该资产记录错误,继续读取另一个结果
|
||||||
|
end
|
||||||
|
end
|
||||||
|
P->>K: 查询开放市场
|
||||||
|
K-->>P: 返回 ticker、事件及组合盘每一条腿的方向
|
||||||
|
P->>K: 按 ticker 查询订单簿
|
||||||
|
alt 盘口读取并解析成功
|
||||||
|
K-->>P: YES 和 NO 买盘
|
||||||
|
P->>P: 按 1 减去对侧买价计算各自卖一
|
||||||
|
else 请求或解析失败
|
||||||
|
P->>P: 保留目录合约并记录 book_error
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
本次工作站用 curl 向官方端点执行匿名 GET。Polymarket Gamma 目录与 CLOB 时钟端点均在取得 HTTP 状态前连接重置;Kalshi 生产目录、备用生产状态端点与 Demo 状态端点均超时。此次没有取得实时盘口,Rust 探针按项目约定暂未编译运行。接口依据见 [Polymarket 市场目录](https://docs.polymarket.com/api-reference/markets/list-markets) 与 [Kalshi 盘口文档](https://docs.kalshi.com/getting_started/orderbook_responses)。
|
||||||
|
|
||||||
|
复查中,工作站分别向 Gamma `/markets?active=true&closed=false&limit=5` 与 Kalshi 生产 `/markets?status=open&limit=5` 发起匿名 GET,连接超时设为 6 秒。两次均返回 curl 28、HTTP 000,未建立 TCP 连接。目录未返回任何资产,因此本次没有由目录驱动的生产盘口回执。两家场所的可达性仍未验证。
|
||||||
|
|
||||||
|
官方文档把 V2 position ID 与 CTF token ID 都作为 CLOB 的交易标识,Gamma 原始字段分别为 positionIds 和 clobTokenIds。回归用例覆盖两种版本、错配、空盘和小数数量。Kalshi 示例中,NO 买价 0.5005 与数量 1.25 对应 YES 卖价 0.4995、卖量 1.25。协议与精度依据见 [Polymarket 订单簿接口](https://docs.polymarket.com/market-data/prices-order-books.md) 和 [Kalshi 订单簿结构](https://docs.kalshi.com/getting_started/orderbook_responses.md)。
|
||||||
|
|
||||||
|
## 核对生产买卖报价
|
||||||
|
|
||||||
|
`venue-probe --compare-prices` 在常规输出上增加每个结果的 price_check。它把报价响应、HTTP 状态、前后盘口和哈希放在同一条记录中。前后盘口稳定时,BUY 与买一、SELL 与卖一一致就标为 resting_side。缺失买或卖的一侧分别按生产样本的 0 或 1 对照;前后两侧均明确为空且报价为 0 和 1 时标为 empty_book_defaults,缺失深度字段的样本保持待定。
|
||||||
|
|
||||||
|
[市场 559651 的目录字段摘录](fixtures/gamma-559651.json)来自公开 Gamma JSON 的网页读取,记录自身的更新时间为 2026-12-02 21:08:08 UTC。该 v1 市场同时返回两套 ID,盘口请求使用 clobTokenIds。目录买一为 0.012、卖一为 0.013、最小价格步长为 0.001。测试以这些真实目录值构造模拟的 CLOB 响应;本次工作站直连 Gamma 超时,网页读取该资产的 CLOB book 和 price 也未取得响应。目录观测与模拟样例均不能确认线上 BUY 和 SELL 返回了什么。[Gamma 公开市场目录](https://gamma-api.polymarket.com/markets?limit=1)
|
||||||
|
|
||||||
|
## 核对单个盘口的档位顺序和哈希
|
||||||
|
|
||||||
|
`venue-probe --inspect-book TOKEN` 只请求一次指定资产的官方 `GET /book`。输出含 URL、HTTP 状态、收到的完整 JSON 对象以及核对结果。保存该行后,`venue-probe --replay-book TOKEN FILE` 可以离线重新核对,也可以直接读取原始盘口 JSON。命令成功表示核对过程正常结束,是否与本地盘口规则一致由 `supports_local_snapshot` 单独报告。
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant U as 调用者
|
||||||
|
participant P as venue-probe
|
||||||
|
participant C as Polymarket CLOB
|
||||||
|
alt 线上取样
|
||||||
|
U->>P: inspect-book TOKEN
|
||||||
|
P->>C: GET /book?token_id=TOKEN
|
||||||
|
C-->>P: 单个时间戳的盘口与哈希
|
||||||
|
else 离线重放
|
||||||
|
U->>P: replay-book TOKEN FILE
|
||||||
|
end
|
||||||
|
P->>P: 校验资产 ID,检查两侧全部相邻价位
|
||||||
|
P->>P: 保留档位与十进制字符串顺序,按 SDK 重算 SHA-1
|
||||||
|
P-->>U: 首尾价、档位数、顺序、收到和计算的哈希
|
||||||
|
```
|
||||||
|
|
||||||
|
两侧各有至少两个严格有序的不同价位、最优价均在末尾且哈希一致时,`supports_local_snapshot` 才为 true。空盘与单档记为 `insufficient_levels`。重复价格或非单调价格记为 `non_monotonic_or_repeated`。固定版 SDK 的哈希测试向量恰好买侧最优在末尾、卖侧最优在开头;重放它能核对序列化与分类,但无法判定生产端排序。仓库样例把测试期望的哈希填入原向量的空 hash 字段。[样例](fixtures/sdk-book.json) · [官方 SDK 哈希测试](https://github.com/Polymarket/py-clob-client-v2/blob/9c7c9e89cbbc11528daae90dc66ef65c9380cbdf/tests/test_utilities.py) · [官方序列化顺序](https://github.com/Polymarket/py-clob-client-v2/blob/9c7c9e89cbbc11528daae90dc66ef65c9380cbdf/py_clob_client_v2/utilities.py)
|
||||||
|
|
||||||
|
单资产取样会记录请求资产及本机完成观测的时间 `observed_at_ms`(Unix 毫秒)。连接在响应前失败时,`http_status` 为 null;收到响应但 JSON 解码失败时保留 HTTP 状态。两类失败均写出一行带 `audit_error` 的 JSON 并以非零状态退出。重放已保存的观测要求 HTTP 状态为 2xx。成功取样中的盘口时间戳仍来自场所。
|
||||||
|
|
||||||
|
|
||||||
|
## 从生产目录追到独立盘口
|
||||||
|
|
||||||
|
2026-12-03 的网页读取取得了两个官方生产目录。Polymarket 市场 559651 的两个结果分别有独立的 CLOB 资产 ID。Kalshi 通过其支持的生产域名返回了一个标记为 binary 的合约,其 mve_selected_legs 包含 30 条腿,每条都有 event_ticker、market_ticker 和 side。连接器在 catalog 字段保留这份清单和 mve_collection_ticker,盘口请求仍使用组合盘自身的 ticker。本次两个场所的盘口请求均未取得可解析的生产响应。[Gamma 目录](https://gamma-api.polymarket.com/markets?limit=1) · [Kalshi 目录](https://api.elections.kalshi.com/trade-api/v2/markets?limit=1) · [Kalshi 官方生产地址](https://docs.kalshi.com/getting_started/api_environments) · [Kalshi 样例字段摘录](fixtures/kalshi-combo.json)
|
||||||
|
|
||||||
|
`orderbooks_complete` 表示该场所的所有目标盘口均已读取且通过结构检查。有效空盘口的买卖价为 null。读取失败的盘口带 `book_error` 并省略报价字段。Polymarket 单侧请求失败时,另一资产的读取仍继续。Kalshi 目录中的 binary 说明兑付有两种结果,一个二元合约仍可依赖多条腿;组合盘与任一成分的可成交量应分别计量。
|
||||||
|
|
||||||
|
## 识别交易与持仓合约
|
||||||
|
|
||||||
|
`venue-probe --replay-market FILE` 从一条原始 Gamma 市场对象生成与实时探针相同的目录字段。该命令仅读本地文件。每个结果保留标签、原顺序、`clob_token_id`、`position_id` 和实际用于盘口请求的 `asset_id`。Gamma 的 `condition_id` 单独保留。对于 V2,两项 Position ID 的高 31 字节必须相同,低字节依次为 0 和 1;探针将高位按原字节宽度导出为 `position_condition_id`。当缺少版本时,兼容旧目录按 CTF token 读取盘口,持仓操作目标留空。版本明确为 v1 但缺少 negRisk 时,同样保留盘口标识,操作目标留空。输出中的目标是合约名称。[官方市场标识](https://docs.polymarket.com/market-data/market-details) · [官方持仓操作](https://docs.polymarket.com/trading/positions/manage)
|
||||||
|
|
||||||
|
| 重放文件 | 盘口字段 | 预期持仓结果 | 证据来源 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `fixtures/gamma-559651.json` | clobTokenIds | ConditionalTokens、CtfCollateralAdapter。两侧另有 position ID | 官方目录返回的市场 559651,本轮复核 negRisk 为 false |
|
||||||
|
| `fixtures/gamma-v2-docs.json` | positionIds | PositionManager、Router。两侧解得同一个 31 字节条件 | 取自官方 Market Details 页的两个 JSON 示例,未取得该市场同期生产盘口 |
|
||||||
|
|
||||||
|
这两份记录验证了目录所指的标识与目标。生产盘口深度、钱包授权和链上操作仍须分别验证。[Gamma 目录](https://gamma-api.polymarket.com/markets?limit=1) · [官方市场样例](https://docs.polymarket.com/market-data/market-details)
|
||||||
|
|
||||||
|
|
||||||
|
## 生产盘口取样
|
||||||
|
|
||||||
|
2026-12-03 06:46 UTC 从官方接口匿名读取了下列快照。fixture 保存了请求 URL、HTTP 状态、响应日期和完整 JSON 响应体,可离线检查。该次取样只覆盖公开行情读取。
|
||||||
|
|
||||||
|
| 场所与样例 | 当时的观测 |
|
||||||
|
| --- | --- |
|
||||||
|
| [Polymarket 两个结果](fixtures/live-polymarket-books.json) | 美联储十二月降息 25 个基点市场。一个结果只有 82 档卖单,最低卖价 0.001。另一个结果只有 82 档买单,最高买价 0.999。两侧最优档均在数组末尾,两个响应的 SHA-1 都能用 SDK 字段顺序重算。 |
|
||||||
|
| [Kalshi 空盘口](fixtures/live-kalshi-book.json) | KXPLATINUMH-26DEC0303-T1377.49 返回 HTTP 200,yes_dollars 和 no_dollars 均为空数组。此时没有可从该快照读出的买一或卖一。 |
|
||||||
|
|
||||||
|
2026-12-03 06:55 UTC 采集的[同侧报价快照](fixtures/live-side-prices.json)按请求保存前后盘口、报价、时间、方法及响应。双侧盘口最优价为 0.039 和 0.04。缺买盘的结果返回 BUY 字符串 0,缺卖盘的结果返回 SELL 字符串 1。批量 POST 返回相同字符串。文档列出的逗号分隔 GET 在这次生产取样返回 400。三个资产的前后盘口哈希各自一致。[官方单价接口](https://docs.polymarket.com/api-reference/market-data/get-market-price)
|
||||||
|
|
||||||
|
[电竞盘双空样本](fixtures/live-empty-prices.json)保留前后盘口和两次价格请求。两个深度数组均为空,公开 BUY 返回 "0",SELL 返回 "1"。探针把该结果计为已验证的空盘报价,实际可成交数量仍为零。
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"id": "559651",
|
||||||
|
"version": "v1",
|
||||||
|
"negRisk": false,
|
||||||
|
"updatedAt": "2026-12-02T21:08:08.650001Z",
|
||||||
|
"conditionId": "0xa467b14d51f01b957109d9cbb1d6c124fab2a089d52ed8f471d23c2812e743b7",
|
||||||
|
"outcomes": "[\"Yes\", \"No\"]",
|
||||||
|
"clobTokenIds": "[\"32338220190071351435772801779725302244575775216413325951443816017994629993401\", \"25659310674993675562345759665114759892400026242514633218387667107987341231962\"]",
|
||||||
|
"positionIds": ["895257453734540292493143124621755605615887197499956109032745153687078305792", "895257453734540292493143124621755605615887197499956109032745153687078305793"],
|
||||||
|
"enableOrderBook": true,
|
||||||
|
"acceptingOrders": true,
|
||||||
|
"bestBid": 0.012,
|
||||||
|
"bestAsk": 0.013,
|
||||||
|
"lastTradePrice": 0.012,
|
||||||
|
"orderPriceMinTickSize": 0.001,
|
||||||
|
"orderMinSize": 5
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"id": "703257",
|
||||||
|
"conditionId": "0x747dc809fb79e1b05be09c42d6179459a58de2ef3e40f02484a4e1260f741f75",
|
||||||
|
"version": "v2",
|
||||||
|
"outcomes": "[\"Yes\", \"No\"]",
|
||||||
|
"clobTokenIds": null,
|
||||||
|
"positionIds": [
|
||||||
|
"651150819117105875331414918119047680898421632356043490229292782433651916800",
|
||||||
|
"651150819117105875331414918119047680898421632356043490229292782433651916801"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
{
|
||||||
|
"ticker": "KXMVECROSSCATEGORY-S2026K3MRPLJCSLQSASGN-76SEMBLAB",
|
||||||
|
"event_ticker": "KXMVECROSSCATEGORY-S2026K3MRPLJCSLQSASGN",
|
||||||
|
"status": "active",
|
||||||
|
"updated_time": "2026-12-02T19:26:58.602846Z",
|
||||||
|
"market_type": "binary",
|
||||||
|
"mve_collection_ticker": "KXMVECROSSCATEGORY-R",
|
||||||
|
"mve_selected_legs": [
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02BAHATL",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02BAHATL-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02CAPSPA",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02CAPSPA-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02CRCOR",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02CRCOR-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02CRUINT",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02CRUINT-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02FLACHA",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02FLACHA-2",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02GPAMIR",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02GPAMIR-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02PALCFC",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02PALCFC-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02RBBFLU",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02RBBFLU-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXBRASILEIROTOTAL-26DEC02VDGVIT",
|
||||||
|
"market_ticker": "KXBRASILEIROTOTAL-26DEC02VDGVIT-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXCOPPAITALIAGAME-26DEC02COMCRE",
|
||||||
|
"market_ticker": "KXCOPPAITALIAGAME-26DEC02COMCRE-COM",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXDFBPOKALGAME-26DEC02BOCFCA",
|
||||||
|
"market_ticker": "KXDFBPOKALGAME-26DEC02BOCFCA-FCA",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXDFBPOKALGAME-26DEC02SGEBVB",
|
||||||
|
"market_ticker": "KXDFBPOKALGAME-26DEC02SGEBVB-SGE",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXDIMAYORGAME-26DEC02OCDIM",
|
||||||
|
"market_ticker": "KXDIMAYORGAME-26DEC02OCDIM-DIM",
|
||||||
|
"side": "no"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXDIMAYORTOTAL-26DEC02CANSFE",
|
||||||
|
"market_ticker": "KXDIMAYORTOTAL-26DEC02CANSFE-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXDIMAYORTOTAL-26DEC02OCDIM",
|
||||||
|
"market_ticker": "KXDIMAYORTOTAL-26DEC02OCDIM-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLGAME-26DEC02LFCSUN",
|
||||||
|
"market_ticker": "KXEPLGAME-26DEC02LFCSUN-LFC",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02AVLEVE",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02AVLEVE-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02BOUBRI",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02BOUBRI-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02BREARS",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02BREARS-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02CFCCRY",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02CFCCRY-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02HULNFO",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02HULNFO-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02MCILEE",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02MCILEE-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXEPLTOTAL-26DEC02NEWMUN",
|
||||||
|
"market_ticker": "KXEPLTOTAL-26DEC02NEWMUN-1",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXLALIGAGAME-26DEC02BARATM",
|
||||||
|
"market_ticker": "KXLALIGAGAME-26DEC02BARATM-BAR",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXLALIGATOTAL-26DEC02BARATM",
|
||||||
|
"market_ticker": "KXLALIGATOTAL-26DEC02BARATM-2",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXSCOTTISHPREMGAME-26DEC02DUNCEL",
|
||||||
|
"market_ticker": "KXSCOTTISHPREMGAME-26DEC02DUNCEL-CEL",
|
||||||
|
"side": "yes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXSCOTTISHPREMGAME-26DEC02FALMOT",
|
||||||
|
"market_ticker": "KXSCOTTISHPREMGAME-26DEC02FALMOT-TIE",
|
||||||
|
"side": "no"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXSCOTTISHPREMGAME-26DEC02HOMABE",
|
||||||
|
"market_ticker": "KXSCOTTISHPREMGAME-26DEC02HOMABE-ABE",
|
||||||
|
"side": "no"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXSCOTTISHPREMGAME-26DEC02JOHMIR",
|
||||||
|
"market_ticker": "KXSCOTTISHPREMGAME-26DEC02JOHMIR-TIE",
|
||||||
|
"side": "no"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"event_ticker": "KXSCOTTISHPREMGAME-26DEC02RFCDUU",
|
||||||
|
"market_ticker": "KXSCOTTISHPREMGAME-26DEC02RFCDUU-RFC",
|
||||||
|
"side": "yes"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"market": "cs2-bb3-lgc-2026-12-03-game1-round-total-27pt5",
|
||||||
|
"before": {
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://clob.polymarket.com/book?token_id=64069535502365350520464017439278800338614209934309608490621606944970108681676",
|
||||||
|
"payload": null,
|
||||||
|
"status": 200,
|
||||||
|
"date": "Thu, 03 Dec 2026 07:55:53 GMT",
|
||||||
|
"body": {
|
||||||
|
"market": "0xe5febd2f236a15ef7c1cc70a481e081bb64223d3e67aa3870677cccd72530853",
|
||||||
|
"asset_id": "64069535502365350520464017439278800338614209934309608490621606944970108681676",
|
||||||
|
"timestamp": "1796284481649",
|
||||||
|
"hash": "575e315a02461b9280427591ba1fe7e48b3b70ce",
|
||||||
|
"bids": [],
|
||||||
|
"asks": [],
|
||||||
|
"min_order_size": "5",
|
||||||
|
"tick_size": "0.01",
|
||||||
|
"neg_risk": false,
|
||||||
|
"last_trade_price": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"buy": {
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://clob.polymarket.com/price?token_id=64069535502365350520464017439278800338614209934309608490621606944970108681676&side=BUY",
|
||||||
|
"payload": null,
|
||||||
|
"status": 200,
|
||||||
|
"date": "Thu, 03 Dec 2026 07:55:55 GMT",
|
||||||
|
"body": {
|
||||||
|
"price": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sell": {
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://clob.polymarket.com/price?token_id=64069535502365350520464017439278800338614209934309608490621606944970108681676&side=SELL",
|
||||||
|
"payload": null,
|
||||||
|
"status": 200,
|
||||||
|
"date": "Thu, 03 Dec 2026 07:55:55 GMT",
|
||||||
|
"body": {
|
||||||
|
"price": "1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"after": {
|
||||||
|
"method": "GET",
|
||||||
|
"url": "https://clob.polymarket.com/book?token_id=64069535502365350520464017439278800338614209934309608490621606944970108681676",
|
||||||
|
"payload": null,
|
||||||
|
"status": 200,
|
||||||
|
"date": "Thu, 03 Dec 2026 07:55:56 GMT",
|
||||||
|
"body": {
|
||||||
|
"market": "0xe5febd2f236a15ef7c1cc70a481e081bb64223d3e67aa3870677cccd72530853",
|
||||||
|
"asset_id": "64069535502365350520464017439278800338614209934309608490621606944970108681676",
|
||||||
|
"timestamp": "1796284481649",
|
||||||
|
"hash": "575e315a02461b9280427591ba1fe7e48b3b70ce",
|
||||||
|
"bids": [],
|
||||||
|
"asks": [],
|
||||||
|
"min_order_size": "5",
|
||||||
|
"tick_size": "0.01",
|
||||||
|
"neg_risk": false,
|
||||||
|
"last_trade_price": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"venue": "kalshi",
|
||||||
|
"retrieved_at": "2026-12-03T06:46:06.218024+00:00",
|
||||||
|
"responses": [
|
||||||
|
{
|
||||||
|
"url": "https://external-api.kalshi.com/trade-api/v2/markets/KXPLATINUMH-26DEC0303-T1377.49/orderbook?depth=5",
|
||||||
|
"status": 200,
|
||||||
|
"http_date": "Thu, 03 Dec 2026 06:46:06 GMT",
|
||||||
|
"body": {
|
||||||
|
"orderbook_fp": {
|
||||||
|
"no_dollars": [],
|
||||||
|
"yes_dollars": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,698 @@
|
|||||||
|
{
|
||||||
|
"venue": "polymarket",
|
||||||
|
"retrieved_at": "2026-12-03T06:46:07.125735+00:00",
|
||||||
|
"responses": [
|
||||||
|
{
|
||||||
|
"url": "https://clob.polymarket.com/book?token_id=77367448845023382525249480243608609306658725883285195065573683809293972009019",
|
||||||
|
"status": 200,
|
||||||
|
"http_date": "Thu, 03 Dec 2026 06:46:06 GMT",
|
||||||
|
"body": {
|
||||||
|
"market": "0xf17ad24eb4ad57926495b114149c765b9b95bc286eadc91cbbfa901ceb10d0b8",
|
||||||
|
"asset_id": "77367448845023382525249480243608609306658725883285195065573683809293972009019",
|
||||||
|
"timestamp": "1796280316915",
|
||||||
|
"hash": "019ed79339d95dcaeaefa222db1474502202a87c",
|
||||||
|
"bids": [],
|
||||||
|
"asks": [
|
||||||
|
{
|
||||||
|
"price": "0.999",
|
||||||
|
"size": "21016611"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.998",
|
||||||
|
"size": "67500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.997",
|
||||||
|
"size": "13583.32"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.996",
|
||||||
|
"size": "1750"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.995",
|
||||||
|
"size": "1400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.994",
|
||||||
|
"size": "19166.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.993",
|
||||||
|
"size": "999.99"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.992",
|
||||||
|
"size": "875"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.991",
|
||||||
|
"size": "777.77"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.99",
|
||||||
|
"size": "4503.92"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.989",
|
||||||
|
"size": "66129.54"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.988",
|
||||||
|
"size": "1216.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.98",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.97",
|
||||||
|
"size": "734.07"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.969",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.95",
|
||||||
|
"size": "245.41"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.939",
|
||||||
|
"size": "5000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.925",
|
||||||
|
"size": "3000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.91",
|
||||||
|
"size": "5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.904",
|
||||||
|
"size": "4720"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.9",
|
||||||
|
"size": "10"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.899",
|
||||||
|
"size": "250"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.89",
|
||||||
|
"size": "90.9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.888",
|
||||||
|
"size": "1999.99"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.855",
|
||||||
|
"size": "4810"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.799",
|
||||||
|
"size": "250"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.798",
|
||||||
|
"size": "9000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.74",
|
||||||
|
"size": "38.46"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.54",
|
||||||
|
"size": "21.73"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.47",
|
||||||
|
"size": "15790.08"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.469",
|
||||||
|
"size": "5000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.43",
|
||||||
|
"size": "10032.89"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.39",
|
||||||
|
"size": "7500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.388",
|
||||||
|
"size": "1500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.38",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.35",
|
||||||
|
"size": "8666.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.288",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.28",
|
||||||
|
"size": "1005"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.279",
|
||||||
|
"size": "4810"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.22",
|
||||||
|
"size": "6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.21",
|
||||||
|
"size": "10"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.2",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.18",
|
||||||
|
"size": "1020.25"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.179",
|
||||||
|
"size": "365"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.175",
|
||||||
|
"size": "150"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.17",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.14",
|
||||||
|
"size": "5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.139",
|
||||||
|
"size": "400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.1",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.098",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.097",
|
||||||
|
"size": "980"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.096",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.08",
|
||||||
|
"size": "150"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.079",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.039",
|
||||||
|
"size": "499.98"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.038",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.034",
|
||||||
|
"size": "53"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.03",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.028",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.025",
|
||||||
|
"size": "20"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.024",
|
||||||
|
"size": "1248"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.023",
|
||||||
|
"size": "97"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.022",
|
||||||
|
"size": "1292.31"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.021",
|
||||||
|
"size": "74"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.02",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.019",
|
||||||
|
"size": "550"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.018",
|
||||||
|
"size": "1495"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.015",
|
||||||
|
"size": "400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.014",
|
||||||
|
"size": "857"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.013",
|
||||||
|
"size": "1087"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.012",
|
||||||
|
"size": "45"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.011",
|
||||||
|
"size": "749.29"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.01",
|
||||||
|
"size": "1203"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.009",
|
||||||
|
"size": "11287.36"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.008",
|
||||||
|
"size": "4830"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.007",
|
||||||
|
"size": "1349.03"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.006",
|
||||||
|
"size": "850"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.005",
|
||||||
|
"size": "1273"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.004",
|
||||||
|
"size": "931.25"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.003",
|
||||||
|
"size": "287998.71"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.002",
|
||||||
|
"size": "525378.36"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.001",
|
||||||
|
"size": "391881.71"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"min_order_size": "5",
|
||||||
|
"tick_size": "0.001",
|
||||||
|
"neg_risk": true,
|
||||||
|
"last_trade_price": "0.001"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://clob.polymarket.com/book?token_id=23677896678650147625251750117853328843601853501710889530409633595389256242910",
|
||||||
|
"status": 200,
|
||||||
|
"http_date": "Thu, 03 Dec 2026 06:46:06 GMT",
|
||||||
|
"body": {
|
||||||
|
"market": "0xf17ad24eb4ad57926495b114149c765b9b95bc286eadc91cbbfa901ceb10d0b8",
|
||||||
|
"asset_id": "23677896678650147625251750117853328843601853501710889530409633595389256242910",
|
||||||
|
"timestamp": "1796280316915",
|
||||||
|
"hash": "cf7a136a82ea7081f8a77202b0d8d57d6e9d7042",
|
||||||
|
"bids": [
|
||||||
|
{
|
||||||
|
"price": "0.001",
|
||||||
|
"size": "21016611"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.002",
|
||||||
|
"size": "67500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.003",
|
||||||
|
"size": "13583.32"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.004",
|
||||||
|
"size": "1750"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.005",
|
||||||
|
"size": "1400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.006",
|
||||||
|
"size": "19166.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.007",
|
||||||
|
"size": "999.99"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.008",
|
||||||
|
"size": "875"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.009",
|
||||||
|
"size": "777.77"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.01",
|
||||||
|
"size": "4503.92"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.011",
|
||||||
|
"size": "66129.54"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.012",
|
||||||
|
"size": "1216.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.02",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.03",
|
||||||
|
"size": "734.07"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.031",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.05",
|
||||||
|
"size": "245.41"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.061",
|
||||||
|
"size": "5000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.075",
|
||||||
|
"size": "3000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.09",
|
||||||
|
"size": "5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.096",
|
||||||
|
"size": "4720"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.1",
|
||||||
|
"size": "10"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.101",
|
||||||
|
"size": "250"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.11",
|
||||||
|
"size": "90.9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.112",
|
||||||
|
"size": "1999.99"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.145",
|
||||||
|
"size": "4810"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.201",
|
||||||
|
"size": "250"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.202",
|
||||||
|
"size": "9000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.26",
|
||||||
|
"size": "38.46"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.46",
|
||||||
|
"size": "21.73"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.53",
|
||||||
|
"size": "15790.08"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.531",
|
||||||
|
"size": "5000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.57",
|
||||||
|
"size": "10032.89"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.61",
|
||||||
|
"size": "7500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.612",
|
||||||
|
"size": "1500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.62",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.65",
|
||||||
|
"size": "8666.66"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.712",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.72",
|
||||||
|
"size": "1005"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.721",
|
||||||
|
"size": "4810"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.78",
|
||||||
|
"size": "6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.79",
|
||||||
|
"size": "10"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.8",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.82",
|
||||||
|
"size": "1020.25"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.821",
|
||||||
|
"size": "365"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.825",
|
||||||
|
"size": "150"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.83",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.86",
|
||||||
|
"size": "5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.861",
|
||||||
|
"size": "400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.9",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.902",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.903",
|
||||||
|
"size": "980"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.904",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.92",
|
||||||
|
"size": "150"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.921",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.961",
|
||||||
|
"size": "499.98"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.962",
|
||||||
|
"size": "500"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.966",
|
||||||
|
"size": "53"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.97",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.972",
|
||||||
|
"size": "1000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.975",
|
||||||
|
"size": "20"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.976",
|
||||||
|
"size": "1248"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.977",
|
||||||
|
"size": "97"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.978",
|
||||||
|
"size": "1292.31"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.979",
|
||||||
|
"size": "74"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.98",
|
||||||
|
"size": "50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.981",
|
||||||
|
"size": "550"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.982",
|
||||||
|
"size": "1495"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.985",
|
||||||
|
"size": "400"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.986",
|
||||||
|
"size": "857"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.987",
|
||||||
|
"size": "1087"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.988",
|
||||||
|
"size": "45"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.989",
|
||||||
|
"size": "749.29"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.99",
|
||||||
|
"size": "1203"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.991",
|
||||||
|
"size": "11287.36"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.992",
|
||||||
|
"size": "4830"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.993",
|
||||||
|
"size": "1349.03"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.994",
|
||||||
|
"size": "850"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.995",
|
||||||
|
"size": "1273"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.996",
|
||||||
|
"size": "931.25"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.997",
|
||||||
|
"size": "287998.71"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.998",
|
||||||
|
"size": "525378.36"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.999",
|
||||||
|
"size": "391881.71"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asks": [],
|
||||||
|
"min_order_size": "5",
|
||||||
|
"tick_size": "0.001",
|
||||||
|
"neg_risk": true,
|
||||||
|
"last_trade_price": "0.001"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"market": "0xaabbcc",
|
||||||
|
"asset_id": "100",
|
||||||
|
"bids": [
|
||||||
|
{
|
||||||
|
"price": "0.3",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.4",
|
||||||
|
"size": "100"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asks": [
|
||||||
|
{
|
||||||
|
"price": "0.6",
|
||||||
|
"size": "100"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"price": "0.7",
|
||||||
|
"size": "100"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hash": "0458ea5755c9f73d64a14636fa5c36ed460ec394",
|
||||||
|
"timestamp": "123456789",
|
||||||
|
"min_order_size": "100",
|
||||||
|
"neg_risk": false,
|
||||||
|
"tick_size": "0.01",
|
||||||
|
"last_trade_price": "0.5"
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
use crate::books::price;
|
||||||
|
use anyhow::{ensure, Context, Result};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use sha1::{Digest, Sha1};
|
||||||
|
|
||||||
|
// Preserve decimal spellings and SDK field order independently of the JSON map implementation.
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Snapshot {
|
||||||
|
market: String,
|
||||||
|
asset_id: String,
|
||||||
|
timestamp: String,
|
||||||
|
hash: String,
|
||||||
|
bids: Vec<Level>,
|
||||||
|
asks: Vec<Level>,
|
||||||
|
min_order_size: String,
|
||||||
|
tick_size: String,
|
||||||
|
neg_risk: bool,
|
||||||
|
last_trade_price: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Level {
|
||||||
|
price: String,
|
||||||
|
size: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn side(levels: &[Level], is_bid: bool) -> Result<Value> {
|
||||||
|
let prices = levels
|
||||||
|
.iter()
|
||||||
|
.map(|l| price(&Value::String(l.price.clone())))
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
let order = if prices.len() < 2 {
|
||||||
|
"insufficient_levels"
|
||||||
|
} else if prices.windows(2).all(|p| p[0] < p[1]) {
|
||||||
|
if is_bid {
|
||||||
|
"best_last"
|
||||||
|
} else {
|
||||||
|
"best_first"
|
||||||
|
}
|
||||||
|
} else if prices.windows(2).all(|p| p[0] > p[1]) {
|
||||||
|
if is_bid {
|
||||||
|
"best_first"
|
||||||
|
} else {
|
||||||
|
"best_last"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"non_monotonic_or_repeated"
|
||||||
|
};
|
||||||
|
Ok(
|
||||||
|
json!({"levels":levels.len(),"first_price":levels.first().map(|l| &l.price),
|
||||||
|
"last_price":levels.last().map(|l| &l.price),"order":order}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn inspect(raw: &Value, token: &str) -> Result<Value> {
|
||||||
|
let mut book: Snapshot =
|
||||||
|
serde_json::from_value(raw.clone()).context("invalid snapshot fields")?;
|
||||||
|
ensure!(
|
||||||
|
book.asset_id == token,
|
||||||
|
"book asset differs from requested token"
|
||||||
|
);
|
||||||
|
let bids = side(&book.bids, true)?;
|
||||||
|
let asks = side(&book.asks, false)?;
|
||||||
|
let received = std::mem::take(&mut book.hash);
|
||||||
|
let calculated = format!("{:x}", Sha1::digest(serde_json::to_vec(&book)?));
|
||||||
|
let matches = !received.is_empty() && received == calculated;
|
||||||
|
Ok(
|
||||||
|
json!({"bids":bids,"asks":asks,"received_hash":received,"sdk_hash":calculated,
|
||||||
|
"hash_matches":matches,"supports_local_snapshot":
|
||||||
|
matches && bids["order"]=="best_last" && asks["order"]=="best_last"}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preserves_sdk_preimage_and_reports_each_side_independently() {
|
||||||
|
let mut raw: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/sdk-book.json")).unwrap();
|
||||||
|
let audit = inspect(&raw, "100").unwrap();
|
||||||
|
assert_eq!(audit["hash_matches"], true);
|
||||||
|
assert_eq!(audit["bids"]["order"], "best_last");
|
||||||
|
assert_eq!(audit["asks"]["order"], "best_first");
|
||||||
|
assert_eq!(audit["supports_local_snapshot"], false);
|
||||||
|
assert!(inspect(&raw, "101").is_err());
|
||||||
|
raw["asks"].as_array_mut().unwrap().reverse();
|
||||||
|
let changed = inspect(&raw, "100").unwrap();
|
||||||
|
assert_eq!(changed["asks"]["order"], "best_last");
|
||||||
|
assert_eq!(changed["hash_matches"], false);
|
||||||
|
raw["hash"] = changed["sdk_hash"].clone();
|
||||||
|
assert_eq!(
|
||||||
|
inspect(&raw, "100").unwrap()["supports_local_snapshot"],
|
||||||
|
true
|
||||||
|
);
|
||||||
|
raw["bids"][1]["price"] = json!("0.30");
|
||||||
|
assert_eq!(
|
||||||
|
inspect(&raw, "100").unwrap()["bids"]["order"],
|
||||||
|
"non_monotonic_or_repeated"
|
||||||
|
);
|
||||||
|
raw["asks"] = json!([]);
|
||||||
|
assert_eq!(
|
||||||
|
inspect(&raw, "100").unwrap()["asks"]["order"],
|
||||||
|
"insufficient_levels"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
use anyhow::{ensure, Context, Result};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
/// Prices fit in millionths of a dollar. Preserve size as the venue's exact
|
||||||
|
/// decimal string; the two venues accept different fractional share sizes.
|
||||||
|
fn decimal(value: &Value) -> Result<(&str, &str)> {
|
||||||
|
let raw = value.as_str().context("expected decimal string")?;
|
||||||
|
let (whole, frac) = raw.split_once('.').unwrap_or((raw, ""));
|
||||||
|
ensure!(
|
||||||
|
!whole.is_empty()
|
||||||
|
&& whole.bytes().all(|b| b.is_ascii_digit())
|
||||||
|
&& !raw.ends_with('.')
|
||||||
|
&& frac.bytes().all(|b| b.is_ascii_digit()),
|
||||||
|
"invalid decimal"
|
||||||
|
);
|
||||||
|
Ok((whole, frac))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn price(value: &Value) -> Result<u64> {
|
||||||
|
let (whole, frac) = decimal(value)?;
|
||||||
|
let frac = frac.trim_end_matches('0');
|
||||||
|
ensure!(frac.len() <= 6, "price exceeds six decimal places");
|
||||||
|
let units: u64 = whole.parse()?;
|
||||||
|
ensure!(
|
||||||
|
units == 0 || (units == 1 && frac.is_empty()),
|
||||||
|
"price outside [0,1]"
|
||||||
|
);
|
||||||
|
let mut micros = 0;
|
||||||
|
for digit in frac.bytes() {
|
||||||
|
micros = micros * 10 + u64::from(digit - b'0');
|
||||||
|
}
|
||||||
|
Ok(units * 1_000_000 + micros * 10_u64.pow(6 - frac.len() as u32))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn best(levels: &Value, highest: bool, kalshi: bool) -> Result<Option<(u64, &str)>> {
|
||||||
|
let mut best: Option<(u64, &str)> = None;
|
||||||
|
for level in levels.as_array().context("missing depth array")? {
|
||||||
|
let (p, size) = if kalshi {
|
||||||
|
let pair = level.as_array().context("expected Kalshi level")?;
|
||||||
|
ensure!(pair.len() == 2, "invalid Kalshi level");
|
||||||
|
(&pair[0], &pair[1])
|
||||||
|
} else {
|
||||||
|
(&level["price"], &level["size"])
|
||||||
|
};
|
||||||
|
let p = price(p)?;
|
||||||
|
let (whole, frac) = decimal(size)?;
|
||||||
|
ensure!(
|
||||||
|
whole.bytes().chain(frac.bytes()).any(|b| b != b'0'),
|
||||||
|
"nonpositive resting size"
|
||||||
|
);
|
||||||
|
if best.map_or(
|
||||||
|
true,
|
||||||
|
|(prior, _)| if highest { p > prior } else { p < prior },
|
||||||
|
) {
|
||||||
|
best = Some((p, size.as_str().context("expected size string")?));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(best)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn level(value: Option<(u64, &str)>) -> Value {
|
||||||
|
value
|
||||||
|
.map(|(p, s)| json!({"price_micros":p,"size":s}))
|
||||||
|
.unwrap_or(Value::Null)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn poly_book(market: &Value, asset: &str, book: &Value) -> Result<Value> {
|
||||||
|
let condition = market["conditionId"]
|
||||||
|
.as_str()
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.context("missing condition ID")?;
|
||||||
|
ensure!(
|
||||||
|
book["asset_id"].as_str() == Some(asset) && book["market"].as_str() == Some(condition),
|
||||||
|
"book differs from requested condition or asset"
|
||||||
|
);
|
||||||
|
Ok(
|
||||||
|
json!({"asset_id":asset,"best_bid":level(best(&book["bids"],true,false)?),"best_ask":level(best(&book["asks"],false,false)?),
|
||||||
|
"venue_timestamp":book.get("timestamp"),"hash":book.get("hash")}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn kalshi_book(ticker: &str, book: &Value) -> Result<Value> {
|
||||||
|
let bids = &book["orderbook_fp"];
|
||||||
|
let yes = best(&bids["yes_dollars"], true, true)?;
|
||||||
|
let no = best(&bids["no_dollars"], true, true)?;
|
||||||
|
let complement = |p: u64| 1_000_000 - p;
|
||||||
|
Ok(json!({"venue":"kalshi","ticker":ticker,
|
||||||
|
"yes":{"best_bid":level(yes),"best_ask":level(no.map(|(p,s)|(complement(p),s)))},
|
||||||
|
"no":{"best_bid":level(no),"best_ask":level(yes.map(|(p,s)|(complement(p),s)))}}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn checks_book_identity_and_uses_exact_executable_levels() {
|
||||||
|
let m = json!({"conditionId":"condition"});
|
||||||
|
let b = json!({"market":"condition","asset_id":"123","bids":[{"price":"0.08","size":"13.25"},{"price":"0.009","size":"7"}],
|
||||||
|
"asks":[{"price":"0.2","size":"3"},{"price":"0.085","size":"0.75"}]});
|
||||||
|
let result = poly_book(&m, "123", &b).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result["best_bid"],
|
||||||
|
json!({"price_micros":80000,"size":"13.25"})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
result["best_ask"],
|
||||||
|
json!({"price_micros":85000,"size":"0.75"})
|
||||||
|
);
|
||||||
|
assert!(poly_book(&m, "999", &b).is_err());
|
||||||
|
assert!(poly_book(&json!({"conditionId":"other"}), "123", &b).is_err());
|
||||||
|
let k=kalshi_book("abc",&json!({"orderbook_fp":{"yes_dollars":[["0.4200","13.00"]],"no_dollars":[["0.5005","1.25"]]}})).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
k["yes"]["best_ask"],
|
||||||
|
json!({"price_micros":499500,"size":"1.25"})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
k["no"]["best_ask"],
|
||||||
|
json!({"price_micros":580000,"size":"13.00"})
|
||||||
|
);
|
||||||
|
let empty = kalshi_book(
|
||||||
|
"abc",
|
||||||
|
&json!({"orderbook_fp":{"yes_dollars":[],"no_dollars":[]}}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(empty["yes"]["best_ask"].is_null());
|
||||||
|
for p in ["-0.1", "1.000001", "0.0000001", "NaN", "1."] {
|
||||||
|
assert!(price(&json!(p)).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
use anyhow::{ensure, Context, Result};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
fn pair(value: &Value) -> Result<[String; 2]> {
|
||||||
|
Ok(match value.as_str() {
|
||||||
|
Some(s) => serde_json::from_str(s)?,
|
||||||
|
None => serde_json::from_value(value.clone())?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Polymarket serializes token IDs as decimal strings. Decode without f64 or u128 truncation.
|
||||||
|
fn uint256(raw: &str) -> Result<[u8; 32]> {
|
||||||
|
ensure!(
|
||||||
|
!raw.is_empty() && raw.len() <= 78 && raw.bytes().all(|b| b.is_ascii_digit()),
|
||||||
|
"invalid decimal token ID"
|
||||||
|
);
|
||||||
|
let mut bytes = [0u8; 32];
|
||||||
|
for digit in raw.bytes() {
|
||||||
|
let mut carry = u16::from(digit - b'0');
|
||||||
|
for byte in bytes.iter_mut().rev() {
|
||||||
|
let n = u16::from(*byte) * 10 + carry;
|
||||||
|
*byte = n as u8;
|
||||||
|
carry = n >> 8;
|
||||||
|
}
|
||||||
|
ensure!(carry == 0, "token ID exceeds uint256");
|
||||||
|
}
|
||||||
|
Ok(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokens(value: &Value) -> Result<[String; 2]> {
|
||||||
|
let ids = pair(value)?;
|
||||||
|
ensure!(
|
||||||
|
uint256(&ids[0])? != uint256(&ids[1])?,
|
||||||
|
"duplicate outcome identifiers"
|
||||||
|
);
|
||||||
|
Ok(ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn position_condition(ids: &[String; 2]) -> Result<String> {
|
||||||
|
let yes = uint256(&ids[0])?;
|
||||||
|
let no = uint256(&ids[1])?;
|
||||||
|
ensure!(
|
||||||
|
yes[..31] == no[..31] && yes[31] == 0 && no[31] == 1,
|
||||||
|
"position IDs must encode the same condition and YES/NO outcome indexes"
|
||||||
|
);
|
||||||
|
use std::fmt::Write;
|
||||||
|
let mut condition = String::from("0x");
|
||||||
|
for byte in &yes[..31] {
|
||||||
|
write!(condition, "{byte:02x}")?;
|
||||||
|
}
|
||||||
|
Ok(condition)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Market {
|
||||||
|
pub assets: [String; 2],
|
||||||
|
pub report: Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Market {
|
||||||
|
pub fn parse(m: &Value) -> Result<Self> {
|
||||||
|
let version = m
|
||||||
|
.get("version")
|
||||||
|
.filter(|v| !v.is_null())
|
||||||
|
.map(|v| v.as_str().context("invalid market version"))
|
||||||
|
.transpose()?;
|
||||||
|
let v2 = match version {
|
||||||
|
Some("v2") => true,
|
||||||
|
Some("v1") | None => false,
|
||||||
|
_ => anyhow::bail!("unknown Polymarket market version"),
|
||||||
|
};
|
||||||
|
let (key, aux) = if v2 {
|
||||||
|
("positionIds", "clobTokenIds")
|
||||||
|
} else {
|
||||||
|
("clobTokenIds", "positionIds")
|
||||||
|
};
|
||||||
|
let assets = tokens(&m[key]).with_context(|| format!("invalid {key}"))?;
|
||||||
|
let condition = m["conditionId"]
|
||||||
|
.as_str()
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.context("missing condition ID")?;
|
||||||
|
let labels = (!m["outcomes"].is_null())
|
||||||
|
.then(|| pair(&m["outcomes"]))
|
||||||
|
.transpose()?;
|
||||||
|
let neg_risk = m["negRisk"].as_bool();
|
||||||
|
let target = match (version, neg_risk) {
|
||||||
|
(Some("v2"), _) => Some("Router"),
|
||||||
|
(Some("v1"), Some(true)) => Some("NegRiskCtfCollateralAdapter"),
|
||||||
|
(Some("v1"), Some(false)) => Some("CtfCollateralAdapter"),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let position_condition_id = match version {
|
||||||
|
Some("v2") => Some(position_condition(&assets)?),
|
||||||
|
Some("v1") => Some(condition.to_owned()),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let mut report = json!({"venue":"polymarket","market":m["id"],"condition_id":condition,
|
||||||
|
"version":version,"neg_risk":neg_risk,"book_id_field":key,
|
||||||
|
"position_ledger":version.map(|_| if v2 {"PositionManager"} else {"ConditionalTokens"}),
|
||||||
|
"position_target":target,"position_condition_id":position_condition_id});
|
||||||
|
// An optional identifier family never prevents sampling valid books in the active family.
|
||||||
|
let alternate = if m[aux].is_null() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
match tokens(&m[aux]) {
|
||||||
|
Ok(ids) => Some(ids),
|
||||||
|
Err(e) => {
|
||||||
|
report["catalog_warning"] = json!(format!("invalid {aux}: {e}"));
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (clob, positions) = if v2 {
|
||||||
|
(alternate.as_ref(), Some(&assets))
|
||||||
|
} else {
|
||||||
|
(Some(&assets), alternate.as_ref())
|
||||||
|
};
|
||||||
|
report["outcomes"] = json!((0..2)
|
||||||
|
.map(|i| json!({"outcome_index":i,"asset_id":assets[i],
|
||||||
|
"label":labels.as_ref().map(|p| &p[i]),"clob_token_id":clob.map(|p| &p[i]),
|
||||||
|
"position_id":positions.map(|p| &p[i])}))
|
||||||
|
.collect::<Vec<_>>());
|
||||||
|
Ok(Self { assets, report })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn sample(raw: &str) -> Value {
|
||||||
|
serde_json::from_str(raw).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn live_ctf_catalog_keeps_both_id_families_and_selects_the_adapter() {
|
||||||
|
let mut m = sample(include_str!("../fixtures/gamma-559651.json"));
|
||||||
|
let parsed = Market::parse(&m).unwrap();
|
||||||
|
let r = parsed.report;
|
||||||
|
assert_eq!(r["position_target"], "CtfCollateralAdapter");
|
||||||
|
assert_eq!(r["position_condition_id"], m["conditionId"]);
|
||||||
|
assert_eq!(r["outcomes"][0]["position_id"], m["positionIds"][0]);
|
||||||
|
assert_eq!(r["outcomes"][0]["clob_token_id"], parsed.assets[0]);
|
||||||
|
assert_ne!(
|
||||||
|
r["outcomes"][0]["position_id"],
|
||||||
|
r["outcomes"][0]["asset_id"]
|
||||||
|
);
|
||||||
|
m["negRisk"] = json!(true);
|
||||||
|
assert_eq!(
|
||||||
|
Market::parse(&m).unwrap().report["position_target"],
|
||||||
|
"NegRiskCtfCollateralAdapter"
|
||||||
|
);
|
||||||
|
m["negRisk"] = Value::Null;
|
||||||
|
assert!(Market::parse(&m).unwrap().report["position_target"].is_null());
|
||||||
|
m["positionIds"] = json!(["bad", "1"]);
|
||||||
|
let r = Market::parse(&m).unwrap().report;
|
||||||
|
assert!(r["catalog_warning"].is_string());
|
||||||
|
assert!(r["outcomes"][0]["position_id"].is_null());
|
||||||
|
m["version"] = Value::Null;
|
||||||
|
assert_eq!(
|
||||||
|
Market::parse(&m).unwrap().report["book_id_field"],
|
||||||
|
"clobTokenIds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn documented_v2_pair_decodes_without_loss_and_selects_the_router() {
|
||||||
|
let mut m = sample(include_str!("../fixtures/gamma-v2-docs.json"));
|
||||||
|
let r = Market::parse(&m).unwrap().report;
|
||||||
|
assert_eq!(r["position_target"], "Router");
|
||||||
|
assert_eq!(
|
||||||
|
r["position_condition_id"],
|
||||||
|
"0x017089ce3ba22aaa0a4cba8250b8c8e1eb0000000000000000000000000000"
|
||||||
|
);
|
||||||
|
assert_eq!(r["outcomes"][1]["asset_id"], m["positionIds"][1]);
|
||||||
|
assert!(r["outcomes"][1]["clob_token_id"].is_null());
|
||||||
|
m["positionIds"][1] = json!("257");
|
||||||
|
assert!(Market::parse(&m).is_err());
|
||||||
|
m["positionIds"] = json!(["257", "256"]);
|
||||||
|
assert!(Market::parse(&m).is_err());
|
||||||
|
m["positionIds"] = Value::Null;
|
||||||
|
m["clobTokenIds"] = json!(["12", "13"]);
|
||||||
|
assert!(Market::parse(&m).is_err());
|
||||||
|
m["version"] = json!("future");
|
||||||
|
assert!(Market::parse(&m).is_err());
|
||||||
|
assert_eq!(
|
||||||
|
uint256(
|
||||||
|
"115792089237316195423570985008687907853269984665640564039457584007913129639935"
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
[255; 32]
|
||||||
|
);
|
||||||
|
assert!(uint256(
|
||||||
|
"115792089237316195423570985008687907853269984665640564039457584007913129639936"
|
||||||
|
)
|
||||||
|
.is_err());
|
||||||
|
assert!(tokens(&json!(["1", "01"])).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
mod book_audit;
|
||||||
|
mod books;
|
||||||
|
mod catalog;
|
||||||
|
mod price_check;
|
||||||
|
mod probes;
|
||||||
|
use anyhow::{ensure, Result};
|
||||||
|
use reqwest::blocking::Client;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
fn observe_book(client: &Client, url: reqwest::Url, token: &str) -> Result<()> {
|
||||||
|
let mut record = json!({"source":url.as_str(),"requested_asset":token,
|
||||||
|
"http_status":null,"book":null,"audit":null,"audit_error":null});
|
||||||
|
let result: Result<()> = (|| {
|
||||||
|
let response = client.get(url).send()?;
|
||||||
|
let status = response.status();
|
||||||
|
record["http_status"] = json!(status.as_u16());
|
||||||
|
let raw: Value = response.json()?;
|
||||||
|
record["book"] = raw;
|
||||||
|
ensure!(status.is_success(), "venue returned HTTP {status}");
|
||||||
|
record["audit"] = book_audit::inspect(&record["book"], token)?;
|
||||||
|
Ok(())
|
||||||
|
})();
|
||||||
|
record["observed_at_ms"] =
|
||||||
|
json!(SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as u64);
|
||||||
|
if let Err(err) = &result {
|
||||||
|
record["audit_error"] = json!(format!("{err:#}"));
|
||||||
|
}
|
||||||
|
println!("{record}");
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||||
|
let usage =
|
||||||
|
"usage: venue-probe [--compare-prices | --inspect-book TOKEN | --replay-book TOKEN FILE | --replay-market FILE]";
|
||||||
|
if let [mode, file] = args.as_slice() {
|
||||||
|
if mode == "--replay-market" {
|
||||||
|
let raw = serde_json::from_reader(std::fs::File::open(file)?)?;
|
||||||
|
println!("{}", catalog::Market::parse(&raw)?.report);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let [mode, token, file] = args.as_slice() {
|
||||||
|
ensure!(mode == "--replay-book", usage);
|
||||||
|
let input: serde_json::Value = serde_json::from_reader(std::fs::File::open(file)?)?;
|
||||||
|
if let Some(status) = input.get("http_status") {
|
||||||
|
ensure!(
|
||||||
|
status.as_u64().is_some_and(|n| (200..300).contains(&n)),
|
||||||
|
"saved observation has no successful HTTP response"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let raw = input.get("book").unwrap_or(&input);
|
||||||
|
let report = book_audit::inspect(raw, token)?;
|
||||||
|
println!("{report}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let compare = match args.as_slice() {
|
||||||
|
[] => false,
|
||||||
|
[flag] if flag == "--compare-prices" => true,
|
||||||
|
[flag, _] if flag == "--inspect-book" => false,
|
||||||
|
_ => anyhow::bail!(usage),
|
||||||
|
};
|
||||||
|
let client = Client::builder()
|
||||||
|
.connect_timeout(Duration::from_secs(5))
|
||||||
|
.timeout(Duration::from_secs(15))
|
||||||
|
.user_agent("eventbook-readonly-probe/0.1")
|
||||||
|
.build()?;
|
||||||
|
if let [_, token] = args.as_slice() {
|
||||||
|
let url = reqwest::Url::parse_with_params(
|
||||||
|
"https://clob.polymarket.com/book",
|
||||||
|
&[("token_id", token)],
|
||||||
|
)?;
|
||||||
|
return observe_book(&client, url, token);
|
||||||
|
}
|
||||||
|
let mut ok = true;
|
||||||
|
for (name, result) in [
|
||||||
|
("polymarket", probes::polymarket(&client, compare)),
|
||||||
|
("kalshi", probes::kalshi(&client)),
|
||||||
|
] {
|
||||||
|
match result {
|
||||||
|
Ok(snapshot) => {
|
||||||
|
println!("{snapshot}");
|
||||||
|
if snapshot["orderbooks_complete"] != true {
|
||||||
|
ok = false;
|
||||||
|
eprintln!("{name}: catalog read succeeded, one or more order books failed");
|
||||||
|
}
|
||||||
|
if name == "polymarket" && compare && snapshot["price_check_passed"] != true {
|
||||||
|
ok = false;
|
||||||
|
eprintln!("polymarket: price comparison not confirmed for both outcomes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
ok = false;
|
||||||
|
eprintln!("{name}: {e:#}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ensure!(ok, "one or more venues failed");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
use crate::books;
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use reqwest::{blocking::Client, Url};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
pub(crate) fn report(before: &Value, buy: Value, sell: Value, last: Result<Value>) -> Value {
|
||||||
|
let (after, error) = match last {
|
||||||
|
Ok(book) => (book, None),
|
||||||
|
Err(e) => (Value::Null, Some(format!("{e:#}"))),
|
||||||
|
};
|
||||||
|
json!({"status":classify(before,&after,&buy,&sell),
|
||||||
|
"BUY":buy,"SELL":sell,"book_after":after,"book_after_error":error})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn quote(client: &Client, asset: &str, side: &str) -> Value {
|
||||||
|
let read = || -> Result<Value> {
|
||||||
|
let url = Url::parse_with_params(
|
||||||
|
"https://clob.polymarket.com/price",
|
||||||
|
&[("token_id", asset), ("side", side)],
|
||||||
|
)?;
|
||||||
|
let response = client.get(url).send().context("quote request")?;
|
||||||
|
let status = response.status();
|
||||||
|
let mut result = json!({"http_status":status.as_u16()});
|
||||||
|
match response.json::<Value>() {
|
||||||
|
Ok(body) => {
|
||||||
|
if status.is_success() {
|
||||||
|
match books::price(&body["price"]) {
|
||||||
|
Ok(price) => result["price_micros"] = json!(price),
|
||||||
|
Err(e) => result["error"] = json!(e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result["response"] = body;
|
||||||
|
}
|
||||||
|
Err(e) => result["error"] = json!(e.to_string()),
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
};
|
||||||
|
read().unwrap_or_else(|e| json!({"error":format!("{e:#}")}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn classify(before: &Value, after: &Value, buy: &Value, sell: &Value) -> &'static str {
|
||||||
|
let Some(hash) = before["hash"].as_str().filter(|h| !h.is_empty()) else {
|
||||||
|
return "inconclusive";
|
||||||
|
};
|
||||||
|
if after.is_null() || after["hash"].as_str().filter(|h| !h.is_empty()).is_none() {
|
||||||
|
return "inconclusive";
|
||||||
|
}
|
||||||
|
if after["hash"] != hash
|
||||||
|
|| before["best_bid"] != after["best_bid"]
|
||||||
|
|| before["best_ask"] != after["best_ask"]
|
||||||
|
{
|
||||||
|
return "book_changed";
|
||||||
|
}
|
||||||
|
let bid = before["best_bid"]["price_micros"].as_u64();
|
||||||
|
let ask = before["best_ask"]["price_micros"].as_u64();
|
||||||
|
let (Some(buy), Some(sell)) = (buy["price_micros"].as_u64(), sell["price_micros"].as_u64())
|
||||||
|
else {
|
||||||
|
return "inconclusive";
|
||||||
|
};
|
||||||
|
match (bid, ask) {
|
||||||
|
(None, None) => {
|
||||||
|
let empty = [before, after].iter().all(|book| {
|
||||||
|
["best_bid", "best_ask"]
|
||||||
|
.iter()
|
||||||
|
.all(|key| book.get(*key) == Some(&Value::Null))
|
||||||
|
});
|
||||||
|
if !empty {
|
||||||
|
"inconclusive"
|
||||||
|
} else if (buy, sell) == (0, 1_000_000) {
|
||||||
|
"empty_book_defaults"
|
||||||
|
} else {
|
||||||
|
"different_prices"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(b), Some(a)) if b >= a => "inconclusive",
|
||||||
|
_ if (buy, sell) == (bid.unwrap_or(0), ask.unwrap_or(1_000_000)) => "resting_side",
|
||||||
|
(Some(b), Some(a)) if (buy, sell) == (a, b) => "opposing_side",
|
||||||
|
_ => "different_prices",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn observed_empty_book_defaults_require_two_explicitly_empty_sides() {
|
||||||
|
let observation: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/live-empty-prices.json")).unwrap();
|
||||||
|
let raw = &observation["before"]["body"];
|
||||||
|
let market = json!({"conditionId":raw["market"]});
|
||||||
|
let token = raw["asset_id"].as_str().unwrap();
|
||||||
|
let before = books::poly_book(&market, token, raw).unwrap();
|
||||||
|
let after = books::poly_book(&market, token, &observation["after"]["body"]).unwrap();
|
||||||
|
let price = |key: &str| json!({"price_micros":books::price(&observation[key]["body"]["price"]).unwrap()});
|
||||||
|
assert_eq!(
|
||||||
|
classify(&before, &after, &price("buy"), &price("sell")),
|
||||||
|
"empty_book_defaults"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
classify(&before, &after, &price("sell"), &price("buy")),
|
||||||
|
"different_prices"
|
||||||
|
);
|
||||||
|
let incomplete = json!({"hash":before["hash"]});
|
||||||
|
assert_eq!(
|
||||||
|
classify(&incomplete, &incomplete, &price("buy"), &price("sell")),
|
||||||
|
"inconclusive"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn public_side_prices_replay_against_bracketing_books() {
|
||||||
|
let raw: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/live-side-prices.json")).unwrap();
|
||||||
|
for case in raw["cases"].as_array().unwrap() {
|
||||||
|
let token = case["asset"].as_str().unwrap();
|
||||||
|
let market = json!({"conditionId":case["before"]["body"]["market"]});
|
||||||
|
let read = |key: &str| books::poly_book(&market, token, &case[key]["body"]).unwrap();
|
||||||
|
let price = |side: &str| {
|
||||||
|
let wire = &case[side]["body"]["price"];
|
||||||
|
assert_eq!(
|
||||||
|
&case["batch_post"]["body"][token][side.to_uppercase().as_str()],
|
||||||
|
wire
|
||||||
|
);
|
||||||
|
json!({"price_micros":books::price(wire).unwrap()})
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
classify(
|
||||||
|
&read("before"),
|
||||||
|
&read("after"),
|
||||||
|
&price("buy"),
|
||||||
|
&price("sell")
|
||||||
|
),
|
||||||
|
"resting_side"
|
||||||
|
);
|
||||||
|
assert_eq!(case["batch_get"]["http_status"], 400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stable_distinct_book_differentiates_the_two_price_conventions() {
|
||||||
|
let gamma: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/gamma-559651.json")).unwrap();
|
||||||
|
let tokens = crate::catalog::Market::parse(&gamma).unwrap().assets;
|
||||||
|
assert_ne!(tokens[0], gamma["positionIds"][0].as_str().unwrap());
|
||||||
|
let level = |p: &Value| json!({"price":p.to_string(),"size":"1"});
|
||||||
|
// Synthetic CLOB responses at the two prices observed on Gamma.
|
||||||
|
let raw = json!({"market":gamma["conditionId"],"asset_id":tokens[0],"hash":"synthetic",
|
||||||
|
"bids":[level(&gamma["bestBid"])],"asks":[level(&gamma["bestAsk"])]});
|
||||||
|
let before = books::poly_book(&gamma, &tokens[0], &raw).unwrap();
|
||||||
|
let bid = json!({"price_micros":12000});
|
||||||
|
let ask = json!({"price_micros":13000});
|
||||||
|
assert_eq!(classify(&before, &before, &ask, &bid), "opposing_side");
|
||||||
|
assert_eq!(classify(&before, &before, &bid, &ask), "resting_side");
|
||||||
|
assert_eq!(classify(&before, &before, &bid, &bid), "different_prices");
|
||||||
|
assert_eq!(
|
||||||
|
classify(&before, &before, &json!({"http_status":404}), &bid),
|
||||||
|
"inconclusive"
|
||||||
|
);
|
||||||
|
let mut after = before.clone();
|
||||||
|
after["hash"] = json!("changed");
|
||||||
|
assert_eq!(classify(&before, &after, &ask, &bid), "book_changed");
|
||||||
|
after["hash"] = Value::Null;
|
||||||
|
assert_eq!(classify(&before, &after, &ask, &bid), "inconclusive");
|
||||||
|
for (edge, expected) in [
|
||||||
|
(Value::Null, "different_prices"),
|
||||||
|
(before["best_bid"].clone(), "inconclusive"),
|
||||||
|
] {
|
||||||
|
let mut one = before.clone();
|
||||||
|
one["best_ask"] = edge;
|
||||||
|
assert_eq!(classify(&one, &one, &ask, &bid), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
use crate::{books, catalog::Market, price_check};
|
||||||
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use reqwest::{blocking::Client, Url};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
fn read(client: &Client, url: Url) -> Result<Value> {
|
||||||
|
let response = client
|
||||||
|
.get(url.clone())
|
||||||
|
.send()
|
||||||
|
.with_context(|| format!("GET {url}"))?;
|
||||||
|
response
|
||||||
|
.error_for_status()
|
||||||
|
.with_context(|| format!("HTTP {url}"))?
|
||||||
|
.json()
|
||||||
|
.with_context(|| format!("invalid JSON from {url}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn polymarket(client: &Client, compare: bool) -> Result<Value> {
|
||||||
|
let markets = read(
|
||||||
|
client,
|
||||||
|
Url::parse("https://gamma-api.polymarket.com/markets?active=true&closed=false&limit=50")?,
|
||||||
|
)
|
||||||
|
.context("Polymarket catalog")?;
|
||||||
|
let (m, market) = markets
|
||||||
|
.as_array()
|
||||||
|
.context("expected market array")?
|
||||||
|
.iter()
|
||||||
|
.filter(|m| m["enableOrderBook"] == true && m["acceptingOrders"] == true)
|
||||||
|
.find_map(|m| Market::parse(m).ok().map(|market| (m, market)))
|
||||||
|
.context("no supported tradable binary market in sample")?;
|
||||||
|
polymarket_report(market, compare, |asset| {
|
||||||
|
let url =
|
||||||
|
Url::parse_with_params("https://clob.polymarket.com/book", &[("token_id", asset)])?;
|
||||||
|
let book = read(client, url.clone()).context("Polymarket orderbook")?;
|
||||||
|
let mut snapshot = books::poly_book(m, asset, &book)?;
|
||||||
|
if compare {
|
||||||
|
let buy = price_check::quote(client, asset, "BUY");
|
||||||
|
let sell = price_check::quote(client, asset, "SELL");
|
||||||
|
let after = read(client, url).and_then(|book| books::poly_book(m, asset, &book));
|
||||||
|
snapshot["price_check"] = price_check::report(&snapshot, buy, sell, after);
|
||||||
|
}
|
||||||
|
Ok(snapshot)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn polymarket_report(
|
||||||
|
market: Market,
|
||||||
|
compare: bool,
|
||||||
|
mut observe: impl FnMut(&str) -> Result<Value>,
|
||||||
|
) -> Result<Value> {
|
||||||
|
let mut result = market.report;
|
||||||
|
let outcomes = result["outcomes"]
|
||||||
|
.as_array_mut()
|
||||||
|
.context("missing catalog outcomes")?;
|
||||||
|
for (asset, outcome) in market.assets.iter().zip(outcomes.iter_mut()) {
|
||||||
|
let snapshot =
|
||||||
|
observe(asset).unwrap_or_else(|err| json!({"book_error":format!("{err:#}")}));
|
||||||
|
let fields = snapshot.as_object().context("invalid book observation")?;
|
||||||
|
outcome
|
||||||
|
.as_object_mut()
|
||||||
|
.context("invalid catalog outcome")?
|
||||||
|
.extend(fields.clone());
|
||||||
|
}
|
||||||
|
let complete = outcomes.iter().all(|s| s.get("book_error").is_none());
|
||||||
|
let passed = compare
|
||||||
|
&& outcomes.iter().all(|s| {
|
||||||
|
matches!(
|
||||||
|
s["price_check"]["status"].as_str(),
|
||||||
|
Some("resting_side" | "empty_book_defaults")
|
||||||
|
)
|
||||||
|
});
|
||||||
|
result["orderbooks_complete"] = json!(complete);
|
||||||
|
if compare {
|
||||||
|
result["price_check_passed"] = json!(passed);
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn kalshi(client: &Client) -> Result<Value> {
|
||||||
|
let root = "https://external-api.kalshi.com/trade-api/v2/markets";
|
||||||
|
let markets = read(
|
||||||
|
client,
|
||||||
|
Url::parse_with_params(root, &[("status", "open"), ("limit", "1")])?,
|
||||||
|
)
|
||||||
|
.context("Kalshi catalog")?;
|
||||||
|
let market = markets["markets"].get(0).context("no open market")?;
|
||||||
|
kalshi_report(root, market, |url| read(client, url))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn kalshi_report(
|
||||||
|
root: &str,
|
||||||
|
market: &Value,
|
||||||
|
observe: impl FnOnce(Url) -> Result<Value>,
|
||||||
|
) -> Result<Value> {
|
||||||
|
let ticker = market["ticker"]
|
||||||
|
.as_str()
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.context("no open market ticker")?;
|
||||||
|
let mut url = Url::parse(root)?;
|
||||||
|
url.path_segments_mut()
|
||||||
|
.map_err(|_| anyhow!("invalid market URL"))?
|
||||||
|
.extend([ticker, "orderbook"]);
|
||||||
|
let mut snapshot = match observe(url).and_then(|data| books::kalshi_book(ticker, &data)) {
|
||||||
|
Ok(mut book) => {
|
||||||
|
book["orderbooks_complete"] = json!(true);
|
||||||
|
book
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
json!({"venue":"kalshi","ticker":ticker,"orderbooks_complete":false,"book_error":format!("{err:#}")})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
snapshot["catalog"] = json!({"event_ticker":market["event_ticker"],"market_type":market["market_type"],
|
||||||
|
"status":market["status"],"mve_collection_ticker":market["mve_collection_ticker"],
|
||||||
|
"mve_selected_legs":market["mve_selected_legs"],"updated_time":market["updated_time"]});
|
||||||
|
Ok(snapshot)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn captured_production_books_keep_empty_sides_and_exact_prices() {
|
||||||
|
let capture: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/live-polymarket-books.json")).unwrap();
|
||||||
|
let responses = capture["responses"].as_array().unwrap();
|
||||||
|
assert_eq!(responses.len(), 2);
|
||||||
|
let condition = json!({"conditionId":"0xf17ad24eb4ad57926495b114149c765b9b95bc286eadc91cbbfa901ceb10d0b8"});
|
||||||
|
for (i, response) in responses.iter().enumerate() {
|
||||||
|
let asset = response["url"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.split_once("token_id=")
|
||||||
|
.unwrap()
|
||||||
|
.1;
|
||||||
|
let raw = &response["body"];
|
||||||
|
let book = books::poly_book(&condition, asset, raw).unwrap();
|
||||||
|
let audit = crate::book_audit::inspect(raw, asset).unwrap();
|
||||||
|
assert_eq!(audit["hash_matches"], true);
|
||||||
|
let (active, empty, side, price) = if i == 0 {
|
||||||
|
("best_ask", "best_bid", "asks", 1_000)
|
||||||
|
} else {
|
||||||
|
("best_bid", "best_ask", "bids", 999_000)
|
||||||
|
};
|
||||||
|
assert!(book[empty].is_null());
|
||||||
|
assert_eq!(
|
||||||
|
book[active],
|
||||||
|
json!({"price_micros":price,"size":"391881.71"})
|
||||||
|
);
|
||||||
|
assert_eq!(audit[side]["levels"], 82);
|
||||||
|
assert_eq!(audit[side]["order"], "best_last");
|
||||||
|
}
|
||||||
|
|
||||||
|
let capture: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/live-kalshi-book.json")).unwrap();
|
||||||
|
let book = books::kalshi_book(
|
||||||
|
"KXPLATINUMH-26DEC0303-T1377.49",
|
||||||
|
&capture["responses"][0]["body"],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
for side in ["yes", "no"] {
|
||||||
|
assert_eq!(book[side], json!({"best_bid":null,"best_ask":null}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn failed_first_polymarket_book_preserves_the_second_observation() {
|
||||||
|
let market: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/gamma-559651.json")).unwrap();
|
||||||
|
let record = Market::parse(&market).unwrap();
|
||||||
|
let assets = record.assets.clone();
|
||||||
|
let report = polymarket_report(record, true, |id| {
|
||||||
|
if id == assets[0].as_str() {
|
||||||
|
anyhow::bail!("simulated timeout");
|
||||||
|
}
|
||||||
|
Ok(json!({"asset_id":id,"best_bid":null,"best_ask":null}))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(report["market"], market["id"]);
|
||||||
|
assert_eq!(report["orderbooks_complete"], false);
|
||||||
|
assert_eq!(report["price_check_passed"], false);
|
||||||
|
let rows = report["outcomes"].as_array().unwrap();
|
||||||
|
assert_eq!(rows[0]["asset_id"].as_str(), Some(assets[0].as_str()));
|
||||||
|
assert_eq!(rows[0]["book_error"], "simulated timeout");
|
||||||
|
assert!(rows[0].get("best_bid").is_none());
|
||||||
|
assert_eq!(rows[1]["asset_id"].as_str(), Some(assets[1].as_str()));
|
||||||
|
assert_eq!(rows[1]["label"], "No");
|
||||||
|
assert_eq!(rows[1].get("best_bid"), Some(&Value::Null));
|
||||||
|
assert!(rows[1].get("book_error").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn kalshi_combo_keeps_all_legs_on_failed_and_empty_books() {
|
||||||
|
let market: Value =
|
||||||
|
serde_json::from_str(include_str!("../fixtures/kalshi-combo.json")).unwrap();
|
||||||
|
let root = "https://external-api.kalshi.com/trade-api/v2/markets";
|
||||||
|
let failed = kalshi_report(root, &market, |url| {
|
||||||
|
assert_eq!(
|
||||||
|
url.path(),
|
||||||
|
format!(
|
||||||
|
"/trade-api/v2/markets/{}/orderbook",
|
||||||
|
market["ticker"].as_str().unwrap()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
anyhow::bail!("simulated timeout");
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(failed["orderbooks_complete"], false);
|
||||||
|
assert!(failed.get("yes").is_none());
|
||||||
|
assert_eq!(failed["catalog"]["market_type"], "binary");
|
||||||
|
assert_eq!(
|
||||||
|
failed["catalog"]["mve_selected_legs"]
|
||||||
|
.as_array()
|
||||||
|
.unwrap()
|
||||||
|
.len(),
|
||||||
|
30
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
failed["catalog"]["mve_selected_legs"],
|
||||||
|
market["mve_selected_legs"]
|
||||||
|
);
|
||||||
|
let empty = kalshi_report(root, &market, |_| {
|
||||||
|
Ok(json!({"orderbook_fp":{"yes_dollars":[],"no_dollars":[]}}))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(empty["orderbooks_complete"], true);
|
||||||
|
assert_eq!(empty["catalog"], failed["catalog"]);
|
||||||
|
assert_eq!(empty["yes"]["best_bid"], Value::Null);
|
||||||
|
assert!(empty.get("book_error").is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user