Files
eventbook-exchange/crates/cli/README.md
T

428 lines
15 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# cli
可信操作员的 JSON 行命令入口。启动参数指定数据库文件,程序复用同一个 Exchange 连接。标准输入的每一行对应标准输出的一条 JSON 回执。输入结束时进程退出。输入线程和交易线程之间使用有界队列,交易线程按最近一笔 GTD 的有效截止时间唤醒,最长每五秒检查一次其他到期任务。
## 文件依赖
箭头从调用文件指向提供功能的文件或 crate。
```mermaid
flowchart LR
main["src/main.rs"] -->|把标准输入输出交给 serve| stream["src/stream.rs"]
main -.->|打开数据库| api["exchange crate"]
stream -.->|执行命令| api
```
## 一次命令会话
```mermaid
sequenceDiagram
actor U as 操作员
participant C as main.rs
participant S as stream.rs
participant E as Exchange
U->>C: 启动并指定数据库路径
C->>E: 打开数据库
C->>S: 传入连接和输入输出流
S->>E: 查询最近一笔待到期 GTD 剩余时间
opt 达到 GTD 截止点或五秒检查周期
S->>E: maintain 检查到期注册、释放余单并同步盘口
S->>E: 重新计算下次唤醒时间
end
loop 输入一行
U->>S: JSON 命令
alt JSON 有效
S->>E: 执行命令
E-->>S: 业务回执或错误
else JSON 解析失败
S->>S: 构造错误回执
end
S-->>U: 输出一行 JSON 并刷新
end
```
## 市场 WebSocket
已有可执行文件可用 `cli DATABASE --market-ws 127.0.0.1:9001` 启动行情监听,客户端连接 `ws://127.0.0.1:9001/ws/market`。同一数据库可以由另一条 JSON 行命令会话写入。监听模式只接收市场订阅与心跳。每个连接独立维护订阅,所有连接共享一个 Exchange。当前服务提供明文 WebSocket,接入 TLS 需要相应的服务入口。
```mermaid
flowchart LR
main["main.rs"] --> ws["market_ws.rs"]
main --> stdin["stream.rs"]
ws --> subs["subscriptions.rs"]
subs --> api["exchange::Exchange / MarketPage"]
stdin --> api
```
```mermaid
sequenceDiagram
participant C as 客户端
participant W as market_ws
participant S as 连接订阅
participant E as Exchange
C->>W: Upgrade /ws/market
C->>S: type=market,订阅 YES
S->>E: 同一快照内读取 YES 盘口和批次号
S-->>C: 初始 book 数组,可用 initial_dump=false 省略
C->>S: operation=subscribe,增加 NO
S-->>C: 新增 NO 的 book 数组
S->>E: 从该市场的已读批次号读取后续变化
S-->>C: price_change,只含已订阅且晚于其起点的资产
opt 成交或价格步长变化
S-->>C: 按同一游标发送 last_trade_price 或 tick_size_change
end
opt 已启用扩展功能
S->>E: 从本连接的发现游标读取后续上架公告
S-->>C: new_market;已订阅市场最终裁决时另发 market_resolved
end
C->>S: operation=unsubscribe,移除 YES
C->>W: 文本 PING
W-->>C: 文本 PONG
```
[订阅报文样例](../../docs/examples/market-ws.txt)逐行发送为文本帧。客户端可每 10 秒发送一次 `PING`。本地按 100 毫秒间隔检查新批次,单页最多 64 个批次,每个进程最多接纳 128 个同时连接的会话。这些是本地实现参数。慢连接的网络写入在释放 Exchange 锁后进行。
## 盘口 HTTP
`cli DATABASE --market-http 127.0.0.1:9002` 启动公开盘口读取服务。可与写入会话和 WebSocket 服务共用数据库文件。[HTTP 请求样例](../../docs/examples/clob-http.http)
```mermaid
flowchart LR
main["main.rs"] --> http["market_http.rs"]
http --> api["exchange::Exchange::public_books"]
```
```mermaid
sequenceDiagram
participant C as SDK
participant H as market_http
participant E as Exchange
C->>H: GET /book?token_id=... 或 POST /books
H->>E: 提交本次请求的资产 ID
E-->>H: 一个数据库快照中的盘口列表
H-->>C: 单盘 JSON 对象或批量 JSON 数组
```
同一个 HTTP 监听器提供 `GET /v2/prices-history`。客户端按资产查询历史,接续页可以只发同一资产和上一页的游标。[请求样例](../../docs/examples/clob-http.http)
```mermaid
flowchart LR
main["main.rs"] --> http["market_http.rs"]
http --> history["exchange::HistoryQuery"]
```
```mermaid
sequenceDiagram
participant C as 图表客户端
participant H as market_http
participant E as Exchange
C->>H: GET /v2/prices-history 含资产与窗口
H->>E: 查询历史点与分页状态
E-->>H: data 与 pagination
H-->>C: JSON 中的 next_cursor
C->>H: 同一 token_id 和 cursor
H->>E: 读取该窗口下一页
E-->>C: 后续点及是否还有数据
```
行情客户端通过 `GET /last-trade-price` 查询单个结果,通过 `POST /last-trades-prices` 发送资产数组。只带 token_ids 查询参数的批量 GET 在生产样本中返回 400。
```mermaid
flowchart LR
H["market_http.rs"] -->|"token_id 或资产列表"| L["exchange/last_trades.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http
participant E as Exchange
C->>H: 查询一个或多个资产的最后一笔成交
H->>E: last_trade_prices
E-->>H: 仅返回有成交记录的资产及其方向
H-->>C: 单查无成交时使用占位值,批查按资产 ID 排序并去重
```
客户端可通过 `GET /tick-size?token_id=…` 或 `GET /tick-size/{token_id}` 读取当前报价步长。URL 路径中的空格编码为 %20,加号表示加号本身。
```mermaid
flowchart LR
H["market_http.rs"] -->|解码 token_id| T["exchange/tick_size.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http
participant E as Exchange
C->>H: GET /tick-size 或 /tick-size/{token_id}
H->>E: 根据结果资产查询当前已提交步长
E-->>H: 精确十进制数或查无市场
H-->>C: minimum_tick_size 或 404 错误
```
### 从结果资产查交易规格
```mermaid
flowchart LR
H["market_http.rs"] -->|解码单个路径参数| E["exchange/clob_market.rs"]
```
```mermaid
sequenceDiagram
participant C as 行情客户端
participant H as market_http
participant E as Exchange
C->>H: GET /markets-by-token/{token_id}
H->>E: market_by_token
E-->>C: condition_id、YES 和 NO 资产
C->>H: GET /clob-markets/{condition_id}
H->>E: clob_market_info
E-->>C: 两个结果的标签、报价步长、最小量和费用参数
```
路径参数使用百分号编码。即使查询从 NO 开始,primary_token_id 仍对应 YES。可重放请求见[行情 HTTP 示例](../../docs/examples/clob-http.http)。
### 查询盘口价差与中点
```mermaid
sequenceDiagram
participant C as 行情客户端
participant H as market_http.rs
participant Q as exchange/quotes.rs
C->>H: 单查 /spread、/midpoint,批查 /spreads、/midpoints
H->>Q: 传入结果资产 ID
Q-->>H: 按买侧缺档取 0、卖侧缺档取 1 计算中点或价差
H-->>C: 单查用 spread 或 mid 字段,POST 批查以资产 ID 为键
```
### 查询盘口买一和卖一
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as exchange/quotes.rs
C->>H: GET /price 或 POST /prices
H->>H: 校验资产与方向一一配对
H->>E: 查询盘口同侧最优档
E-->>H: 单笔价格或按资产与方向分组的报价
H-->>C: 单查和批查的价格均为字符串
```
[可重放请求](../../docs/examples/clob-http.http)同时展示相同盘口下的买价、卖价与中点。
### CLOB 客户端历史曲线
```mermaid
sequenceDiagram
participant C as CLOB 客户端
participant H as market_http.rs
participant E as prices_history/legacy.rs
C->>H: GET /prices-history,market 与 interval 或 startTs、endTs
H->>E: clob_price_history,fidelity 以分钟计
E-->>H: 返回窗口内盘口中点样本,末尾追加最近可用点
H-->>C: history 数组,每点包含 t 和 p
```
### 发布后的裁决说明
```mermaid
flowchart LR
H["market_http.rs"] --> Q["exchange/clarifications.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http
participant E as Exchange
C->>H: GET /market-clarifications?event_id=1&market_id=3
H->>E: 按事件和市场目录 ID 筛选,再排序和翻页
E-->>H: 规则说明、清盘标记和本地发布时间
H-->>C: 已发布的公告数组
```
event_id 取自本地 events 或 catalog 查询,market_id 取自 assets 查询。两个参数均可重复,同类 ID 取并集,同时指定事件与市场时取交集。独立市场的公告也能通过 market_id 查询。默认按公告 ID 降序,每页 20 条;支持 order=id、ascending、offset 和 0 到 100 的 limit。当前查询对其他筛选参数返回 400。
### 从事件页进入订单簿
```mermaid
flowchart LR
H["market_http.rs"] --> G["exchange/gamma_directory.rs"]
H --> B["exchange/book_snapshot.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market-http
participant E as Exchange
C->>H: GET /events/slug/nvda-closing-price-2027
H->>E: gamma_event
E-->>C: markets 数组,每项含该门槛的目录 ID
C->>H: GET /markets/{id}
H->>E: gamma_market
E-->>C: outcomes 与 clobTokenIds,均为 JSON 数组编码的字符串
C->>H: GET /book?token_id=选中的资产
H-->>C: 该结果的买卖盘
```
加载 [主题事件样例](../../docs/examples/topic.jsonl) 后即可按上图读取。事件与市场均支持按数字 ID 和按 slug 查询,缺失资源返回 404。路径段按 UTF-8 百分号编码,路径中的加号表示加号。当前详情入口支持无查询参数的 GET。
### 浏览目录
`GET /markets` 和 `GET /events` 返回数组。支持 limit、offset、ascending、order、active、closed,以及可重复的 id 和 slug。市场列表也支持重复的 condition_ids 和 clob_token_ids。同类标识取并集,提供多类标识时取交集。查询字符串中的加号需要编码成 %2B。
```mermaid
flowchart LR
H["market_http.rs · GET /markets 与 /events"] --> P["exchange/gamma_list.rs"] --> D["exchange/gamma_directory.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market-http
participant E as Exchange
C->>H: GET /events?limit=1&order=id&ascending=true
H->>E: gamma_list(Events),保留重复参数
E-->>C: 一个事件及其可见子盘
C->>H: GET /markets?slug=nvda-300&closed=any
H->>E: gamma_list(Markets),按 slug 查询开放或已关闭的盘口
E-->>C: 匹配市场数组
```
### 查询公开成交
`GET /v2/trades?user=bob&limit=1` 按成交时间倒序返回 Bob 作为吃单方的成交。查询双方时加 `taker_only=false`。下一页传回 `pagination.next_cursor` 并重送筛选条件。condition 支持本地市场 ID,event_id 接受 Gamma 目录里的数字事件 ID。条件列表以逗号分隔。钱包、市场和资产字段使用本地账户和目录标识。
```mermaid
flowchart LR
H["market_http.rs"] --> T["exchange/public_trades/http.rs"] --> S["exchange/public_trades.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market-http
participant E as Exchange
C->>H: GET /v2/trades,user=bob,limit=1
H->>E: 解析参数并查询该账户的成交
E-->>H: 一条成交和下一页签名游标
H-->>C: data、pagination
C->>H: 重送 user 和 cursor
H->>E: 验证签名,从上一笔订单之后继续
E-->>C: 剩余成交,末页 next_cursor 为空
```
### 裁决状态
`GET /v2/resolutions?condition=m` 读取单盘状态,`?event_id=1` 读取事件的各个盘口。已登记问题标识的乐观裁决市场也支持 question_id 查询。多个条件或多个事件用逗号分隔,每次选择一种查询方式。
```mermaid
flowchart LR
H["market_http.rs · GET 参数"] --> Q["exchange/resolution/http.rs"]
Q --> D["exchange/resolution/state.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /v2/resolutions?event_id=1
H->>E: 校验选择器,读取事件下的各盘
E-->>H: 每个可见市场一行
H-->>C: 200 data 数组,未找到返回空数组
```
### 查询持仓价值
`GET /v2/value?user=alice&condition=e` 返回该账户在市场 e 的持仓价值,单位为模拟美元。使用已登记的本地账户名或 20 字节钱包地址。省略 condition 查询所有已持有市场。无持仓的钱包返回零,仅有现金的账户也返回零。
```mermaid
flowchart LR
H["market_http.rs · GET /v2/value"] --> V["exchange/portfolio/http.rs"]
V --> A["exchange/portfolio.rs"] --> S["exchange/portfolio/value.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /v2/value?user=alice&condition=e
H->>E: 读取 e 的所有结果资产价值
alt 持仓均可估值
H-->>C: 200,data 含 proxy_wallet 和 value
else 非零持仓缺价
H-->>C: 503,Retry-After 提示重试
end
```
### 查询未平仓量
`GET /v2/oi` 返回单行 GLOBAL。指定 `GET /v2/oi?condition=cup` 可查 cup 的金额,空盘返回 0,归档市场从数组中省略。筛选支持逗号分隔及 condition_id、conditionId 两个别名。已有份额却无法估值时,本地返回可重试的 503。
```mermaid
flowchart LR
H["market_http.rs · GET /v2/oi"] --> E["exchange/open_interest.rs"]
E --> D["exchange/open_interest.sql"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /v2/oi?condition=cup
H->>E: 验证条件,按市场汇总
E-->>H: condition_id 和 value
H-->>C: 200,data 数组
```
### 按结果资产查询基础费
`GET /fee-rate?token_id=eb%3AYES%3Acup` 和 `GET /fee-rate/eb%3ANO%3Acup` 均返回 base_fee,单位为整数基点。费用样例返回 30。计算吃单费用所需的曲线系数在该市场详情的 fd.r 中。
```mermaid
flowchart LR
H["market_http.rs"] --> F["exchange/fees.rs"] --> D["exchange/schema.sql · market_fee_terms"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /fee-rate/{token_id}
H->>E: 解码资产 ID 并查询基础费
alt 有整数基点配置
H-->>C: 200,base_fee
else 市场或基础费不存在
H-->>C: 404
end
```
### 在下单前判断互斥资产
官方 Python SDK 可以按 token_id 请求 `GET /neg-risk`。本地的 YES 和 NO 共用所属市场的分组。没有分组的已上架资产返回 false,无法匹配市场则返回 404。
```mermaid
flowchart LR
H["market_http.rs · GET /neg-risk"] --> E["exchange/neg_risk.rs"] --> S["exchange/schema.sql · neg_risk_members"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market_http.rs
participant E as Exchange
C->>H: GET /neg-risk?token_id=eb%3ANO%3Aa
H->>E: 查询市场 a 的成员关系
E-->>H: true
H-->>C: 200,neg_risk 为真
```
批量盘口 POST /books 的响应使用 asset_id 对应请求。重复资产只返回一份,单批最多 500 个不同标识。