Track prediction-market parity and expose outcome display quotes
This commit is contained in:
@@ -141,6 +141,10 @@ impl Listing {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
|
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
|
||||||
pub(crate) enum Query {
|
pub(crate) enum Query {
|
||||||
|
Quote {
|
||||||
|
market: String,
|
||||||
|
outcome: String,
|
||||||
|
},
|
||||||
Catalog {
|
Catalog {
|
||||||
event: String,
|
event: String,
|
||||||
},
|
},
|
||||||
@@ -157,6 +161,34 @@ pub(crate) enum Query {
|
|||||||
impl Query {
|
impl Query {
|
||||||
pub fn read(self, db: &Connection) -> Result<Value> {
|
pub fn read(self, db: &Connection) -> Result<Value> {
|
||||||
match self {
|
match self {
|
||||||
|
Self::Quote { market, outcome } => {
|
||||||
|
ensure!(matches!(outcome.as_str(), "YES" | "NO"), "invalid outcome");
|
||||||
|
let phase: String =
|
||||||
|
db.query_row("SELECT phase FROM markets WHERE id=?", [&market], |r| {
|
||||||
|
r.get(0)
|
||||||
|
})?;
|
||||||
|
let (bid,ask):(Option<i64>,Option<i64>)=db.query_row(
|
||||||
|
"SELECT MAX(CASE WHEN side='buy' THEN price END), MIN(CASE WHEN side='sell' THEN price END)
|
||||||
|
FROM orders WHERE market=? AND outcome=? AND status='live'",
|
||||||
|
params![market,outcome], |r|Ok((r.get(0)?,r.get(1)?)))?;
|
||||||
|
// Fills also cover databases created before timestamped history was introduced.
|
||||||
|
let last: Option<i64> = db.query_row(
|
||||||
|
"SELECT (SELECT f.price FROM fills f JOIN orders o ON o.seq=f.bid
|
||||||
|
WHERE o.market=? AND o.outcome=? ORDER BY f.seq DESC LIMIT 1)",
|
||||||
|
params![market, outcome],
|
||||||
|
|r| r.get(0),
|
||||||
|
)?;
|
||||||
|
let (display, source) = match (phase.as_str(), bid, ask, last) {
|
||||||
|
("continuous", Some(b), Some(a), _) if (0..=10).contains(&(a - b)) => {
|
||||||
|
(Some((a + b) * 5000), Some("midpoint"))
|
||||||
|
}
|
||||||
|
("continuous", _, _, Some(p)) => (Some(p * 10000), Some("last_trade")),
|
||||||
|
_ => (None, None),
|
||||||
|
};
|
||||||
|
Ok(json!({"market":market,"outcome":outcome,"phase":phase,
|
||||||
|
"best_bid_micros":bid.map(|p|p*10000),"best_ask_micros":ask.map(|p|p*10000),
|
||||||
|
"last_trade_micros":last.map(|p|p*10000),"display_price_micros":display,"source":source}))
|
||||||
|
}
|
||||||
Self::Catalog { event } => {
|
Self::Catalog { event } => {
|
||||||
let detail = db.query_row(
|
let detail = db.query_row(
|
||||||
"SELECT title,sport,competition,home,away,scheduled_at_ms FROM events WHERE id=?",
|
"SELECT title,sport,competition,home,away,scheduled_at_ms FROM events WHERE id=?",
|
||||||
|
|||||||
@@ -30,7 +30,10 @@ impl Exchange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute(&mut self, payload: Value) -> Result<Value> {
|
pub fn execute(&mut self, payload: Value) -> Result<Value> {
|
||||||
if matches!(payload["op"].as_str(), Some("catalog" | "history")) {
|
if matches!(
|
||||||
|
payload["op"].as_str(),
|
||||||
|
Some("catalog" | "history" | "quote")
|
||||||
|
) {
|
||||||
let query: catalog::Query = serde_json::from_value(payload)?;
|
let query: catalog::Query = serde_json::from_value(payload)?;
|
||||||
let tx = self.db.transaction()?;
|
let tx = self.db.transaction()?;
|
||||||
let value = query.read(&tx)?;
|
let value = query.read(&tx)?;
|
||||||
|
|||||||
@@ -245,3 +245,27 @@ fn sports_catalog_and_trade_curve_survive_retries() {
|
|||||||
next["after"] = json!([p["at_ms"], p["fill"]]);
|
next["after"] = json!([p["at_ms"], p["fill"]]);
|
||||||
assert_eq!(e.execute(next).unwrap()["points"], json!([]));
|
assert_eq!(e.execute(next).unwrap()["points"], json!([]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn displayed_quote_uses_midpoint_through_ten_cents_then_last_fill() {
|
||||||
|
let mut e = Exchange::open(":memory:").unwrap();
|
||||||
|
setup(&mut e);
|
||||||
|
let quote = json!({"op":"quote","market":"e","outcome":"YES"});
|
||||||
|
e.execute(json!({"id":"open","op":"open","market":"e"}))
|
||||||
|
.unwrap();
|
||||||
|
assert!(e.execute(quote.clone()).unwrap()["display_price_micros"].is_null());
|
||||||
|
e.execute(order("maker", "alice", "sell", 40, 1)).unwrap();
|
||||||
|
e.execute(order("trade", "bob", "buy", 40, 1)).unwrap();
|
||||||
|
e.execute(order("bid", "bob", "buy", 34, 1)).unwrap();
|
||||||
|
let ask = e.execute(order("ask", "alice", "sell", 44, 1)).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
e.execute(quote.clone()).unwrap()["display_price_micros"],
|
||||||
|
390000
|
||||||
|
);
|
||||||
|
e.execute(json!({"op":"cancel","id":"withdraw","account":"alice","order":ask["order"]}))
|
||||||
|
.unwrap();
|
||||||
|
e.execute(order("wider", "alice", "sell", 45, 1)).unwrap();
|
||||||
|
let display = e.execute(quote).unwrap();
|
||||||
|
assert_eq!(display["display_price_micros"], 400000);
|
||||||
|
assert_eq!(display["source"], "last_trade");
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Polymarket 预测市场行为对照
|
||||||
|
|
||||||
|
验证单位为一个用户可观察场景。每个场景记录官方依据、输入、可检查的输出、Eventbook 测试和实际运行证据。现有 Rust 分支的格式、元数据及独立 SQLite 检查不能替代 Rust 集成测试或真实场所对照。首轮关注 Predictions;自定义集合竞价、外场路由与篮子作为 Eventbook 扩展单独标识。
|
||||||
|
|
||||||
|
| 场景 | 已有能力 | 尚缺的对照 |
|
||||||
|
|---|---|---|
|
||||||
|
| 事件、市场和结果 | 体育事件下有多个二元合约、规则和结果标签 | 稳定的结果资产标识、非体育事件、多结果组合和版本映射 |
|
||||||
|
| 报价与走势 | 本地成交曲线、连续交易展示价 | 盘口深度、细分报价单位、互补结果深度、实际历史样本 |
|
||||||
|
| 委托与成交 | 单簿价格时间优先、GTC、IOC | GTD、FOK、post-only、互补单及延迟和失败回报 |
|
||||||
|
| 持仓与抵押 | 模拟资产全额拆出 YES/NO | 合并成套份额、赎回、费用与多结果转换 |
|
||||||
|
| 裁决与交割 | 本地人工封盘、裁决、兑付 | 提案、争议、最终性,以及撮合后链上交割的独立状态 |
|
||||||
|
| 客户端 | CLI 查询规则、价格曲线与报价 | 用户可见的活动、挂单、持仓及断线回补的完整流程 |
|
||||||
|
|
||||||
|
## 展示价样例
|
||||||
|
|
||||||
|
使用只读命令 `{"op":"quote","market":"e","outcome":"YES"}`。一美元为 1000000 微单位。连续交易时,买一 0.34、卖一 0.40 得到 370000。卖一改为 0.44 时显示 390000;再改为 0.45 则显示最近成交价。价格来源随结果返回。没有历史成交且没有可用中点时返回 null。单边且已有成交时采用最近成交;盘前和停止交易后的展示价为空。这两项是拟定的本地边界,待用外部样本核对。quote 读取一致的数据库快照,不触发撮合。[官方价格口径](https://docs.polymarket.com/concepts/prices-orderbook.md)
|
||||||
|
|
||||||
|
关联测试为 exchange 的 displayed_quote_uses_midpoint_through_ten_cents_then_last_fill。更广的验收依据见[事件与市场](https://docs.polymarket.com/concepts/markets-events.md)、[订单生命周期](https://docs.polymarket.com/concepts/order-lifecycle.md)和[多结果市场](https://docs.polymarket.com/concepts/negative-risk.md)。
|
||||||
Reference in New Issue
Block a user