Reconcile precise collateral funding, fee debits and available cash

This commit is contained in:
2026-12-03 06:30:11 +08:00
parent 2fcdac61d5
commit 14ccc97e09
14 changed files with 207 additions and 41 deletions
+17 -27
View File
@@ -1,4 +1,3 @@
use crate::ledger;
use anyhow::{ensure, Result};
use matching::Fill;
use rusqlite::{params, Connection, Transaction};
@@ -7,21 +6,24 @@ pub(crate) fn rate(db: &Connection, market: &str) -> Result<i64> {
Ok(db.query_row("SELECT COALESCE(f.rate_ppm,0) FROM markets m LEFT JOIN market_fee_rates f ON f.market=m.id WHERE m.id=?", [market], |r|r.get(0))?)
}
// rate is parts per million; prices are cents. Round each fill to units of
// ten micros, suppressing amounts below the published minimum.
pub(crate) fn micros(rate: i64, price: i64, shares: i64) -> i64 {
let numerator =
i128::from(shares) * i128::from(rate) * i128::from(price) * i128::from(100 - price);
if numerator < 100_000 {
// Price and rate both have six decimals. Round the fee only after the
// complete fill, to the nearest ten micros, with the published minimum.
pub(crate) fn micros(rate: i64, price_micros: i64, shares: i64) -> i64 {
let numerator = i128::from(shares)
* i128::from(rate)
* i128::from(price_micros)
* i128::from(1_000_000 - price_micros);
let denominator = 10_000_000_000_000;
if numerator < denominator {
return 0;
}
(((numerator + 50_000) / 100_000) * 10) as i64
(((numerator + denominator / 2) / denominator) * 10) as i64
}
pub(crate) fn total(rate: i64, fills: &[Fill]) -> i64 {
fills
.iter()
.map(|f| micros(rate, f.price, f.quantity))
.map(|f| micros(rate, f.price * 10000, f.quantity))
.sum()
}
@@ -35,7 +37,8 @@ pub(crate) fn fit_budget(rate: i64, fills: &mut Vec<Fill>, budget: i64) {
let mut count = 0;
for f in fills.iter_mut() {
let cost = |qty: i64| {
i128::from(qty) * i128::from(f.price) * 10000 + i128::from(micros(rate, f.price, qty))
i128::from(qty) * i128::from(f.price) * 10000
+ i128::from(micros(rate, f.price * 10000, qty))
};
if cost(f.quantity) <= i128::from(remaining) {
remaining -= cost(f.quantity) as i64;
@@ -60,21 +63,6 @@ pub(crate) fn fit_budget(rate: i64, fills: &mut Vec<Fill>, budget: i64) {
fills.truncate(count);
}
pub(crate) fn debit(tx: &Transaction, account: &str, fee: i64) -> Result<()> {
if fee == 0 {
return Ok(());
}
let old: i64 = tx.query_row(
"SELECT COALESCE((SELECT micros FROM cash_remainders WHERE account=?),0)",
[account],
|r| r.get(0),
)?;
let delta = old - fee;
ledger::cash(tx, account, delta.div_euclid(10000))?;
tx.execute("INSERT INTO cash_remainders(account,micros) VALUES(?,?) ON CONFLICT(account) DO UPDATE SET micros=excluded.micros",params![account,delta.rem_euclid(10000)])?;
Ok(())
}
pub(crate) fn configure(tx: &Transaction, market: &str, rate: i64) -> Result<()> {
ensure!((0..=1_000_000).contains(&rate), "invalid taker fee rate");
tx.execute(
@@ -90,6 +78,8 @@ mod tests {
#[test]
fn symmetric_taker_curve_matches_published_examples_and_minimum() {
assert_eq!(micros(50000, 13000, 5), 3210);
assert_eq!(micros(50000, 987000, 5), 3210);
for (rate, price, size, expected) in [
(50000, 50, 100, 1250000),
(50000, 30, 100, 1050000),
@@ -101,8 +91,8 @@ mod tests {
(1, 50, 60, 20),
(50000, 0, 1, 0),
] {
assert_eq!(micros(rate, price, size), expected);
assert_eq!(micros(rate, 100 - price, size), expected);
assert_eq!(micros(rate, price * 10000, size), expected);
assert_eq!(micros(rate, (100 - price) * 10000, size), expected);
}
}
}