Match fractional share lots and preserve order quantities across restart

This commit is contained in:
2026-12-03 07:01:24 +08:00
parent 2c4179a64e
commit 064b301ebd
27 changed files with 343 additions and 111 deletions
+84 -10
View File
@@ -1118,29 +1118,29 @@ fn cash_buy_reconciles_direct_and_complementary_depth_with_an_exact_budget() {
assert_eq!(state(&mut e), before);
}
let receipt = e.execute(request.clone()).unwrap();
assert_eq!(receipt["spent_cents"], 160);
assert_eq!(receipt["unspent_cents"], 10);
assert_eq!(receipt["filled"], 4);
assert_eq!(receipt["spent_cents"].to_string(), "169.9");
assert_eq!(receipt["unspent_cents"].to_string(), "0.1");
assert_eq!(receipt["filled"].to_string(), "4.22");
assert_eq!(receipt["status"], "cancelled");
drop(e);
let mut e = Exchange::open(path).unwrap();
assert_eq!(e.execute(request).unwrap(), receipt);
let s = state(&mut e);
assert_eq!(s["accounts"][2]["cash"], 10);
assert_eq!(s["accounts"][2]["cash"], 0);
assert_eq!(s["paired_fills"][0]["kind"], "mint");
assert_eq!(s["markets"][0]["collateral"], 1200);
assert_eq!(s["markets"][0]["collateral"], 1222);
let lookup = e
.execute(json!({"op":"open_orders","account":"buyer","order":receipt["order"]}))
.unwrap();
assert_eq!(lookup["orders"][0]["spent_cents"], 160);
assert_eq!(lookup["orders"][0]["spent_cents"].to_string(), "169.9");
assert_eq!(lookup["orders"][0]["open_size"], 0);
let fok = e
.execute(
json!({"op":"market_buy","id":"exact","market":"e","outcome":"YES",
"account":"bob","amount_cents":45,"max_price":45,"tif":"FOK"}),
"account":"bob","amount_cents":27,"max_price":45,"tif":"FOK"}),
)
.unwrap();
assert_eq!(fok["spent_cents"], 45);
assert_eq!(fok["spent_cents"], 27);
assert_eq!(fok["status"], "filled");
}
@@ -1878,7 +1878,9 @@ fn fees_follow_taker_cash_budget_and_net_sale_proceeds() {
}
let reply = e.execute(cmd.clone()).unwrap();
match cmd["op"].as_str().unwrap() {
"estimate" if cmd["amount_cents"] == 5000 => assert_eq!(reply["filled_shares"], 97),
"estimate" if cmd["amount_cents"] == 5000 => {
assert_eq!(reply["filled_shares"].to_string(), "97.56")
}
"estimate" => assert_eq!(reply["fee_micros"], 1250000),
"market_buy" => {
assert_eq!(reply["total_spent_micros"], 51250000);
@@ -2052,7 +2054,7 @@ fn minimum_size_checks_submitted_orders_and_preserves_partial_fills() {
);
e.execute(json!({"op":"open","id":"open-f","market":"f"}))
.unwrap();
for (budget, ok) in [(307, false), (308, true)] {
for (budget, ok) in [(262, false), (263, true)] {
let quote=e.execute(json!({"op":"estimate","market":"f","outcome":"YES","side":"buy","max_price":50,"amount_cents":budget}));
assert_eq!(quote.is_ok(), ok);
}
@@ -2472,3 +2474,75 @@ fn half_payout_keeps_unpayable_base_unit_in_market_backing() {
assert_eq!(s["settlement_dust"][0]["micros"], 1);
assert_eq!(s["collateral_remainders"][0]["micros"], 1);
}
#[test]
fn auction_fraction_survives_restart_then_trades_at_maker_price() {
let dir = tempdir().unwrap();
let path = dir.path().join("lots.db");
let mut engine = Exchange::open(&path).unwrap();
for line in include_str!("../../../docs/examples/fractional-orders.jsonl").lines() {
let request: Value = serde_json::from_str(line).unwrap();
if request["op"] == "book" {
drop(engine);
engine = Exchange::open(&path).unwrap();
}
let reply = engine.execute(request.clone()).unwrap();
match request["op"].as_str().unwrap() {
"book" => assert_eq!(reply["asks"][0]["size"].to_string(), "0.73"),
"cancel" => assert_eq!(reply["cancelled"].to_string(), "0.44"),
"balance" => assert_eq!(reply["available_micros"], 1_565_500),
"history" => {
assert_eq!(reply["points"][0]["quantity"].to_string(), "0.5");
assert_eq!(reply["points"][1]["quantity"].to_string(), "0.29");
assert_eq!(reply["points"][1]["price_micros"], 550000);
}
_ => {}
}
}
let lookup = engine
.execute(json!({"op":"open_orders","account":"maker","order":1}))
.unwrap();
assert_eq!(lookup["orders"][0]["size_matched"].to_string(), "0.79");
assert_eq!(lookup["orders"][0]["original_size"].to_string(), "1.23");
let pos = engine
.execute(json!({"op":"positions","account":"maker","market":"e"}))
.unwrap();
let yes = pos["positions"]
.as_array()
.unwrap()
.iter()
.find(|p| p["outcome"] == "YES")
.unwrap();
assert_eq!(yes["available_size"].to_string(), "1.21");
assert_eq!(yes["reserved_size"], 0);
}
#[test]
fn old_resting_order_is_scaled_once_and_keeps_fifo_after_partial_fill() {
let dir = tempdir().unwrap();
let path = dir.path().join("old.db");
let db = rusqlite::Connection::open(&path).unwrap();
db.execute_batch(include_str!("../src/schema.sql")).unwrap();
db.execute_batch(
"INSERT INTO accounts VALUES('a',0),('b',100);
INSERT INTO markets(id,rules,phase,collateral) VALUES('old','r','continuous',100);
INSERT INTO positions VALUES('a','old','YES',0),('a','old','NO',1);
INSERT INTO orders VALUES(17,'a','old','YES','sell',40,1,1,'live');",
)
.unwrap();
drop(db);
for _ in 0..2 {
let mut e = Exchange::open(&path).unwrap();
assert_eq!(
e.execute(json!({"op":"book","market":"old","outcome":"YES"}))
.unwrap()["asks"][0]["size"],
1
);
}
let mut e = Exchange::open(&path).unwrap();
e.execute(json!({"op":"order","id":"hit","account":"b","market":"old","outcome":"YES","side":"buy","price":50,"size_micros":10000})).unwrap();
let r = e
.execute(json!({"op":"cancel","id":"release","account":"a","order":17}))
.unwrap();
assert_eq!(r["cancelled"].to_string(), "0.99");
}