Expose integer base fees alongside execution fee curves

This commit is contained in:
2026-12-03 12:48:10 +08:00
parent 2a62f1812a
commit f7946d2277
15 changed files with 207 additions and 26 deletions
+38 -1
View File
@@ -1,6 +1,43 @@
use crate::{assets, Exchange, MarketDataError};
use anyhow::{ensure, Result};
use matching::Fill;
use rusqlite::{params, Connection, Transaction};
use rusqlite::{params, Connection, OptionalExtension, Transaction};
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct BaseFees {
maker_bps: i64,
taker_bps: i64,
}
impl BaseFees {
pub(crate) fn save(self, tx: &Transaction, market: &str) -> Result<()> {
ensure!(
self.maker_bps >= 0 && self.taker_bps >= 0,
"negative base fee"
);
tx.execute(
"INSERT INTO market_base_fees(market,maker_bps,taker_bps) VALUES(?,?,?)",
params![market, self.maker_bps, self.taker_bps],
)?;
Ok(())
}
}
impl Exchange {
pub fn base_fee_bps(&self, token: &str) -> Result<Option<i64>, MarketDataError> {
let (_, market) = assets::parse_id(token)
.map_err(|_| MarketDataError::Invalid("Invalid token id".into()))?;
let mut stmt = self
.db
.prepare_cached("SELECT taker_bps FROM market_fee_terms WHERE market=?1")?;
Ok(stmt
.query_row([market], |r| r.get::<_, Option<i64>>(0))
.optional()?
.flatten())
}
}
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))?)