Support amount-specific negative-risk redemptions and wallet receipts

This commit is contained in:
2026-12-03 13:17:04 +08:00
parent 98eece1c50
commit 3bec895a0a
9 changed files with 199 additions and 22 deletions
+30
View File
@@ -183,6 +183,36 @@ pub(crate) fn convert(
Ok(receipt)
}
/// Burn the YES/NO amounts transferred by a negative-risk adapter caller.
/// The caller settles the returned (burned shares, collateral payout) in this transaction.
pub(crate) fn burn_for_redemption(
tx: &Transaction,
market: &str,
account: &str,
amounts: [i64; 2],
) -> Result<(i64, i64)> {
let winner: Option<String> = tx.query_row(
"SELECT m.winner FROM neg_risk_members n JOIN markets m ON m.id=n.market WHERE n.market=? AND m.phase='resolved'",
[market], |row| row.get(0),
).optional()?;
let [yes, no] = amounts;
let paid = match winner.as_deref() {
Some("YES") => yes,
Some("NO") => no,
_ => anyhow::bail!("explicit redemption amounts require a resolved negative-risk market"),
};
ensure!(yes >= 0 && no >= 0, "negative redemption amount");
let burned = yes
.checked_add(no)
.ok_or_else(|| anyhow::anyhow!("redemption share total overflow"))?;
for (outcome, units) in [("YES", yes), ("NO", no)] {
if units > 0 {
inventory::position(tx, account, market, outcome, -units)?;
}
}
Ok((burned, paid))
}
// Run in the same transaction as the binary payout check. A failing verdict
// rolls back the transfer as well as any optimistic-resolution state change.
pub(crate) fn allocate_payout(tx: &Transaction, market: &str, outcome: &str) -> Result<()> {