Distribute negative-risk conversion fees across cash and outcome shares

This commit is contained in:
2026-12-03 13:05:09 +08:00
parent 351de6187e
commit 0f608db47f
10 changed files with 196 additions and 17 deletions
+69 -5
View File
@@ -15,11 +15,19 @@ impl Exchange {
}
}
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ConversionFee {
bps: i64,
vault: String,
}
pub(crate) fn define(
tx: &Transaction,
group: &str,
event: &str,
markets: &[String],
fee: Option<ConversionFee>,
) -> Result<Value> {
identifier(group)?;
identifier(event)?;
@@ -44,7 +52,24 @@ pub(crate) fn define(
"members must be unused preopen markets of this event with manual resolution"
);
}
Ok(json!({"group":group,"markets":markets}))
let mut receipt = json!({"group":group,"markets":markets});
if let Some(fee) = fee {
identifier(&fee.vault)?;
ensure!(
(0..=10000).contains(&fee.bps),
"conversion fee must be 0 to 10000 bps"
);
tx.execute(
"INSERT INTO accounts(id,cash) VALUES(?,0) ON CONFLICT(id) DO NOTHING",
[&fee.vault],
)?;
tx.execute(
"INSERT INTO neg_risk_conversion_fees(group_id,bps,vault) VALUES(?,?,?)",
params![group, fee.bps, fee.vault],
)?;
receipt["conversion_fee"] = json!(fee);
}
Ok(receipt)
}
pub(crate) fn convert(
@@ -58,7 +83,10 @@ pub(crate) fn convert(
) -> Result<Value> {
identifier(group)?;
identifier(account)?;
let units = inventory::quantity_units(qty, qty_micros)?;
let units = match (qty, qty_micros) {
(Some(0), None) | (None, Some(0)) => 0,
_ => inventory::quantity_units(qty, qty_micros)?,
};
let selected: BTreeSet<&str> = no_markets.iter().map(String::as_str).collect();
ensure!(
!selected.is_empty() && selected.len() == no_markets.len(),
@@ -79,21 +107,50 @@ pub(crate) fn convert(
.all(|id| members.iter().any(|(market, _)| market.as_str() == *id)),
"NO market outside group"
);
if units == 0 {
return Ok(
json!({"group":group,"no_markets":no_markets,"yes_markets":[],
"qty":0,"qty_micros":0,"released_cents":0,"released_micros":0,"noop":true}),
);
}
ensure!(
members.iter().all(|(_, phase)| phase != "resolved"),
"group already resolving"
);
let policy = tx
.query_row(
"SELECT bps,vault FROM neg_risk_conversion_fees WHERE group_id=?",
[group],
|row| {
Ok(ConversionFee {
bps: row.get(0)?,
vault: row.get(1)?,
})
},
)
.optional()?;
// The adapter rounds each output before multiplying by the number of cash legs.
let bps = policy.as_ref().map_or(0, |fee| fee.bps);
let fee_units = i64::try_from(i128::from(units) * i128::from(bps) / 10_000)?;
let net_units = units - fee_units;
let mut yes_markets = Vec::new();
for (market, _) in &members {
if selected.contains(market.as_str()) {
inventory::position(tx, account, market, "NO", -units)?;
inventory::backing(tx, market, -units)?;
} else {
inventory::position(tx, account, market, "YES", units)?;
if net_units > 0 {
inventory::position(tx, account, market, "YES", net_units)?;
}
if let Some(fee) = policy.as_ref().filter(|_| fee_units > 0) {
inventory::position(tx, &fee.vault, market, "YES", fee_units)?;
}
yes_markets.push(market);
}
}
let released = (selected.len() as i64 - 1) * units;
let cash_legs = selected.len() as i64 - 1;
let released = cash_legs * net_units;
let cash_fee = cash_legs * fee_units;
let reserve: i64 = tx.query_row(
"SELECT micros FROM neg_risk_reserves WHERE id=?",
[group],
@@ -110,7 +167,14 @@ pub(crate) fn convert(
ledger::cash_micros(tx, account, released)?;
let mut receipt = json!({"group":group,"qty":inventory::decimal(units.into(),inventory::UNIT),
"no_markets":no_markets,"yes_markets":yes_markets,"released_cents":inventory::decimal(released.into(),10000)});
if qty_micros.is_some() {
if let Some(fee) = &policy {
if cash_fee > 0 {
ledger::cash_micros(tx, &fee.vault, cash_fee)?;
}
receipt["conversion_fee"] = json!({"bps":bps,"vault":fee.vault,
"cash_micros":cash_fee,"yes_micros_per_market":fee_units});
}
if qty_micros.is_some() || policy.is_some() {
inventory::movement(tx, request, account, group, "CONVERSION", units, released)?;
receipt["qty_micros"] = json!(units);
receipt["released_micros"] = json!(released);