Cancel account orders by ID batch, market or outcome asset

This commit is contained in:
2026-12-03 04:23:08 +08:00
parent 67426ae617
commit c27bb60fd2
8 changed files with 221 additions and 2 deletions
+89
View File
@@ -1000,3 +1000,92 @@ fn twap_market_closes_before_settlement_and_reuses_exact_observations() {
300
);
}
#[test]
fn batch_cancel_releases_only_remaining_owned_orders_and_replays_receipt() {
let dir = tempdir().unwrap();
let path = dir.path().join("cancel.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
e.execute(json!({"op":"open","id":"start","market":"e"}))
.unwrap();
let ask = e.execute(order("offer", "alice", "sell", 63, 5)).unwrap()["order"].clone();
let filled = e.execute(order("take", "bob", "buy", 65, 2)).unwrap()["order"].clone();
let foreign = e.execute(order("other", "bob", "buy", 10, 1)).unwrap()["order"].clone();
let mut no = order("no", "alice", "buy", 20, 4);
no["outcome"] = json!("NO");
let bid = e.execute(no).unwrap()["order"].clone();
let batch = json!({"op":"cancel_orders","id":"withdraw","account":"alice",
"orders":[bid,ask,bid,foreign,99999]});
let result = e.execute(batch.clone()).unwrap();
assert_eq!(result["canceled"], json!([bid, ask]));
assert_eq!(
result["not_canceled"][foreign.to_string().as_str()],
"order not found"
);
assert_eq!(result["not_canceled"]["99999"], "order not found");
let again = e
.execute(json!({"op":"cancel_orders","id":"again","account":"bob","orders":[filled]}))
.unwrap();
assert_eq!(
again["not_canceled"][filled.to_string().as_str()],
"order is filled"
);
drop(e);
let mut e = Exchange::open(path).unwrap();
assert_eq!(e.execute(batch).unwrap(), result);
let state = state(&mut e);
assert_eq!(state["accounts"][0]["cash"], 1126);
let yes = state["positions"]
.as_array()
.unwrap()
.iter()
.find(|p| p["account"] == "alice" && p["outcome"] == "YES")
.unwrap();
assert_eq!(yes["quantity"], 8);
let bob = e
.execute(json!({"op":"open_orders","account":"bob"}))
.unwrap();
assert_eq!(bob["orders"][0]["order"], foreign);
}
#[test]
fn market_and_asset_cancels_are_scoped_to_the_original_side() {
let mut e = Exchange::open(":memory:").unwrap();
setup(&mut e);
e.execute(json!({"op":"list","id":"m2","market":"other","rules":"official"}))
.unwrap();
let yes = e.execute(order("yes", "alice", "sell", 60, 1)).unwrap()["order"].clone();
let mut n = order("no", "alice", "buy", 40, 1);
n["outcome"] = json!("NO");
let no = e.execute(n).unwrap()["order"].clone();
let mut elsewhere = order("other-bid", "alice", "buy", 10, 1);
elsewhere["market"] = json!("other");
let other = e.execute(elsewhere).unwrap()["order"].clone();
let scoped = json!({"op":"cancel_market_orders","id":"yes-only","account":"alice","asset_id":"eb:YES:e"});
assert_eq!(e.execute(scoped.clone()).unwrap()["canceled"], json!([yes]));
let new = e.execute(order("later", "alice", "sell", 60, 1)).unwrap()["order"].clone();
assert_eq!(e.execute(scoped).unwrap()["canceled"], json!([yes]));
assert!(e
.execute(json!({"op":"cancel_market_orders","id":"missing","account":"alice"}))
.is_err());
assert!(e
.execute(json!({"op":"cancel_all","id":"wrong","account":"alice","market":"e"}))
.is_err());
assert!(e
.execute(json!({"op":"cancel_orders","id":"empty","account":"alice","orders":[]}))
.is_err());
assert!(e
.execute(
json!({"op":"cancel_orders","id":"oversize","account":"alice","orders":vec![1;3001]})
)
.is_err());
let market = e
.execute(json!({"op":"cancel_market_orders","id":"m-only","account":"alice","market":"e"}))
.unwrap();
assert_eq!(market["canceled"], json!([no, new]));
let all = e
.execute(json!({"op":"cancel_all","id":"everything","account":"alice"}))
.unwrap();
assert_eq!(all, json!({"canceled":[other],"not_canceled":{}}));
}