Resolve up-down markets from exact same-stream TWAP observations
This commit is contained in:
@@ -936,3 +936,67 @@ fn unchallenged_second_proposal_only_clears_its_own_outcome() {
|
||||
assert!(e.execute(finalize("YES")).is_err());
|
||||
assert_eq!(e.execute(finalize("NO")).unwrap()["winner"], "NO");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twap_market_closes_before_settlement_and_reuses_exact_observations() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("twap.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
let db = rusqlite::Connection::open(&path).unwrap();
|
||||
let now: i64 = db
|
||||
.query_row("SELECT unixepoch()", [], |r| r.get(0))
|
||||
.unwrap();
|
||||
let start = now - 300;
|
||||
let end = now - 1;
|
||||
e.execute(json!({"op":"fund","id":"a","account":"alice","cents":1000}))
|
||||
.unwrap();
|
||||
e.execute(
|
||||
json!({"op":"list","id":"l","market":"m","rules":"final >= opening is Up",
|
||||
"resolution":{"kind":"twap","stream":"test/btc-usd","start_at":start,"end_at":now+600}}),
|
||||
)
|
||||
.unwrap();
|
||||
e.execute(json!({"op":"split","id":"s","market":"m","account":"alice","qty":3}))
|
||||
.unwrap();
|
||||
e.execute(json!({"op":"open","id":"o","market":"m"}))
|
||||
.unwrap();
|
||||
e.execute(json!({"op":"order","id":"sell","market":"m","account":"alice","side":"sell","outcome":"YES","price":50,"qty":3})).unwrap();
|
||||
let observe = |id: &str, t: i64, price: &str| {
|
||||
json!({"op":"observe","id":id,"market":"m","stream":"test/btc-usd",
|
||||
"price":price,"observed_at":t,"evidence":"simulated-report"})
|
||||
};
|
||||
e.execute(observe("initial", start, "0100.0000000000000000010"))
|
||||
.unwrap();
|
||||
assert!(e.execute(observe("future", now + 600, "200")).is_err());
|
||||
db.execute("UPDATE twap_markets SET end_at=? WHERE market='m'", [end])
|
||||
.unwrap();
|
||||
drop(e);
|
||||
let mut e = Exchange::open(path).unwrap();
|
||||
let report = json!({"op":"resolution","market":"m"});
|
||||
assert_eq!(
|
||||
e.execute(report.clone()).unwrap()["status"],
|
||||
"awaiting_observations"
|
||||
);
|
||||
assert_eq!(state(&mut e)["orders"][0]["status"], "cancelled");
|
||||
let finish = json!({"op":"finalize","id":"f","market":"m"});
|
||||
assert!(e.execute(finish.clone()).is_err());
|
||||
assert!(e
|
||||
.execute(json!({"op":"resolve","id":"bypass","market":"m","winner":"YES"}))
|
||||
.is_err());
|
||||
assert!(e.execute(observe("wrong-time", end - 1, "100")).is_err());
|
||||
let mut wrong_stream = observe("wrong-stream", end, "100");
|
||||
wrong_stream["stream"] = json!("test/other");
|
||||
assert!(e.execute(wrong_stream).is_err());
|
||||
e.execute(observe("end", end, "100.000000000000000000"))
|
||||
.unwrap();
|
||||
e.execute(observe("same", end, "100")).unwrap();
|
||||
assert!(e.execute(observe("conflict", end, "101")).is_err());
|
||||
let detail = e.execute(report).unwrap();
|
||||
assert_eq!(detail["computed_winner"], "NO");
|
||||
assert_eq!(detail["opening"], "100.000000000000000001");
|
||||
assert_eq!(e.execute(finish).unwrap()["winner"], "NO");
|
||||
assert_eq!(
|
||||
e.execute(json!({"op":"redeem","id":"claim","market":"m","account":"alice"}))
|
||||
.unwrap()["paid"],
|
||||
300
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user