Reconcile precise collateral funding, fee debits and available cash
This commit is contained in:
@@ -2117,3 +2117,70 @@ fn single_and_batch_execution_prices_follow_opposing_liquidity() {
|
||||
assert!(reader.execute(invalid).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collateral_query_reconciles_subcent_funding_reservations_and_trade() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("subcent.db");
|
||||
let mut exchange = Exchange::open(&path).unwrap();
|
||||
let balance = json!({"op":"balance","account":"buyer"});
|
||||
for line in include_str!("../../../docs/examples/cash-balance.jsonl").lines() {
|
||||
let cmd: Value = serde_json::from_str(line).unwrap();
|
||||
if cmd["id"] == "trade" {
|
||||
exchange
|
||||
.execute(json!({"op":"fund","id":"short","account":"short","micros":512499}))
|
||||
.unwrap();
|
||||
let mut short = cmd.clone();
|
||||
short["id"] = json!("failed");
|
||||
short["account"] = json!("short");
|
||||
let before = state(&mut exchange);
|
||||
assert!(exchange.execute(short).is_err());
|
||||
assert_eq!(state(&mut exchange), before);
|
||||
}
|
||||
let response = exchange.execute(cmd.clone()).unwrap();
|
||||
match cmd["id"].as_str() {
|
||||
Some("seller") => assert_eq!(response, json!({"funded":100})),
|
||||
Some("buyer") => assert_eq!(response, json!({"funded":51,"funded_micros":512500})),
|
||||
Some("rest") => assert_eq!(
|
||||
exchange.execute(balance.clone()).unwrap(),
|
||||
json!({"account":"buyer","available_micros":22500,"reserved_micros":490000,"total_cash_micros":512500})
|
||||
),
|
||||
Some("withdraw") => assert_eq!(
|
||||
exchange.execute(balance.clone()).unwrap()["available_micros"],
|
||||
512500
|
||||
),
|
||||
Some("trade") => assert_eq!(response["fee_micros"], 12500),
|
||||
_ => {}
|
||||
}
|
||||
if cmd["op"] == "activity" {
|
||||
assert_eq!(response["activity"][0]["net_cash_delta_micros"], -512500);
|
||||
assert_eq!(response["activity"][1]["net_cash_delta_micros"], 512500);
|
||||
}
|
||||
}
|
||||
drop(exchange);
|
||||
let mut exchange = Exchange::open(&path).unwrap();
|
||||
assert_eq!(exchange.execute(balance).unwrap()["total_cash_micros"], 0);
|
||||
assert_eq!(
|
||||
exchange
|
||||
.execute(json!({"op":"balance","account":"seller"}))
|
||||
.unwrap()["total_cash_micros"],
|
||||
500000
|
||||
);
|
||||
let repeat = json!({"op":"fund","id":"buyer","account":"buyer","micros":512500});
|
||||
assert_eq!(exchange.execute(repeat).unwrap()["funded_micros"], 512500);
|
||||
for invalid in [
|
||||
json!({"micros":0}),
|
||||
json!({"cents":1,"micros":1}),
|
||||
json!({"micros":10000000000001_i64}),
|
||||
json!({}),
|
||||
] {
|
||||
let mut request = invalid;
|
||||
request["op"] = json!("fund");
|
||||
request["account"] = json!("invalid");
|
||||
request["id"] = json!("invalid");
|
||||
assert!(exchange.execute(request).is_err());
|
||||
}
|
||||
assert!(exchange
|
||||
.execute(json!({"op":"balance","account":"invalid"}))
|
||||
.is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user