Expire GTD orders before matching, reads and recovery
This commit is contained in:
@@ -654,3 +654,74 @@ fn portfolio_tracks_reserved_pairs_partial_sale_and_redemption() {
|
||||
.unwrap();
|
||||
assert_eq!(other["positions"][0]["payout_cents"], 200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gtd_expiry_survives_rejection_and_recovery() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("expiry.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
setup(&mut e);
|
||||
e.execute(json!({"op":"open","id":"start","market":"e"}))
|
||||
.unwrap();
|
||||
let db = rusqlite::Connection::open(&path).unwrap();
|
||||
let now: i64 = db
|
||||
.query_row("SELECT unixepoch()", [], |r| r.get(0))
|
||||
.unwrap();
|
||||
let mut ask = order("dated", "alice", "sell", 60, 5);
|
||||
ask["tif"] = json!("GTD");
|
||||
assert!(e.execute(ask.clone()).is_err());
|
||||
ask["expiration"] = json!(now + 179);
|
||||
assert!(e.execute(ask.clone()).is_err());
|
||||
ask["expiration"] = json!(now + 600);
|
||||
ask["post_only"] = json!(true);
|
||||
let receipt = e.execute(ask.clone()).unwrap();
|
||||
let mut bid = order("dated-buy", "bob", "buy", 20, 3);
|
||||
bid["expiration"] = json!(now + 600);
|
||||
let bid_id = e.execute(bid).unwrap()["order"].as_i64().unwrap();
|
||||
e.execute(order("take", "bob", "buy", 60, 2)).unwrap();
|
||||
let ask_id = receipt["order"].as_i64().unwrap();
|
||||
db.execute(
|
||||
"UPDATE order_deadlines SET expiration=unixepoch()+60 WHERE seq=?",
|
||||
[ask_id],
|
||||
)
|
||||
.unwrap();
|
||||
let mut sweep = order("sweep", "bob", "buy", 65, 1);
|
||||
sweep["tif"] = json!("FOK");
|
||||
assert!(e.execute(sweep).is_err());
|
||||
assert_eq!(e.execute(ask).unwrap(), receipt);
|
||||
let lookup = e
|
||||
.execute(json!({"op":"open_orders","account":"alice","order":ask_id}))
|
||||
.unwrap();
|
||||
assert_eq!(lookup["orders"][0]["status"], "expired");
|
||||
assert_eq!(lookup["orders"][0]["size_matched"], 2);
|
||||
assert_eq!(lookup["orders"][0]["open_size"], 0);
|
||||
let held = e
|
||||
.execute(json!({"op":"positions","account":"alice"}))
|
||||
.unwrap();
|
||||
let yes = held["positions"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|r| r["outcome"] == "YES")
|
||||
.unwrap();
|
||||
assert_eq!(yes["available_size"], 8);
|
||||
assert_eq!(yes["reserved_size"], 0);
|
||||
drop(e);
|
||||
db.execute(
|
||||
"UPDATE order_deadlines SET expiration=unixepoch()+59 WHERE seq=?",
|
||||
[bid_id],
|
||||
)
|
||||
.unwrap();
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
let s = state(&mut e);
|
||||
assert_eq!(s["accounts"][1]["cash"], 1880);
|
||||
assert_eq!(s["orders"][1]["status"], "expired");
|
||||
assert_eq!(
|
||||
e.execute(json!({"op":"book","market":"e","outcome":"NO"}))
|
||||
.unwrap()["asks"],
|
||||
json!([])
|
||||
);
|
||||
drop(e);
|
||||
let mut e = Exchange::open(path).unwrap();
|
||||
assert_eq!(state(&mut e)["accounts"][1]["cash"], 1880);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user