Support amount-specific negative-risk redemptions and wallet receipts
This commit is contained in:
@@ -380,13 +380,20 @@ pub(crate) fn apply(
|
||||
json!({"phase":"resolved","winner":winner,"outstanding_cents":crate::inventory::decimal(i128::from(backing),10000)}),
|
||||
)
|
||||
}
|
||||
Command::Redeem { market, account } => {
|
||||
Command::Redeem {
|
||||
market,
|
||||
account,
|
||||
amounts_micros,
|
||||
} => {
|
||||
identifier(&account)?;
|
||||
phase(tx, &market, &["resolved"])?;
|
||||
let paid = redeem(tx, &market, &account, request_id)?;
|
||||
Ok(
|
||||
json!({"market":market,"account":account,"paid":crate::inventory::decimal(i128::from(paid),10000)}),
|
||||
)
|
||||
let paid = redeem(tx, &market, &account, request_id, amounts_micros)?;
|
||||
let mut receipt = json!({"market":market,"account":account,"paid":crate::inventory::decimal(i128::from(paid),10000)});
|
||||
if let Some(amounts) = amounts_micros {
|
||||
receipt["amounts_micros"] = json!(amounts);
|
||||
receipt["paid_micros"] = json!(paid);
|
||||
}
|
||||
Ok(receipt)
|
||||
}
|
||||
Command::Resolve { market, winner } => {
|
||||
let backing = finalize(tx, &market, &winner)?;
|
||||
@@ -398,7 +405,7 @@ pub(crate) fn apply(
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
let mut paid = 0;
|
||||
for account in accounts {
|
||||
paid += redeem(tx, &market, &account, request_id)?;
|
||||
paid += redeem(tx, &market, &account, request_id, None)?;
|
||||
}
|
||||
let dust: i64 = tx.query_row(
|
||||
"SELECT COALESCE((SELECT micros FROM settlement_dust WHERE market=?),0)",
|
||||
@@ -465,20 +472,30 @@ pub(crate) fn finalize(tx: &Transaction, market: &str, winner: &str) -> Result<i
|
||||
Ok(backing)
|
||||
}
|
||||
|
||||
fn redeem(tx: &Transaction, market: &str, account: &str, request_id: &str) -> Result<i64> {
|
||||
fn redeem(
|
||||
tx: &Transaction,
|
||||
market: &str,
|
||||
account: &str,
|
||||
request_id: &str,
|
||||
amounts: Option<[i64; 2]>,
|
||||
) -> Result<i64> {
|
||||
let (shares, paid) = match amounts {
|
||||
Some(amounts) => crate::neg_risk::burn_for_redemption(tx, market, account, amounts)?,
|
||||
None => burn_full_balance(tx, market, account)?,
|
||||
};
|
||||
crate::inventory::movement(tx, request_id, account, market, "REDEEM", shares, paid)?;
|
||||
if paid > 0 {
|
||||
crate::inventory::backing(tx, market, -paid)?;
|
||||
ledger::cash_micros(tx, account, paid)?;
|
||||
}
|
||||
Ok(paid)
|
||||
}
|
||||
|
||||
fn burn_full_balance(tx: &Transaction, market: &str, account: &str) -> Result<(i64, i64)> {
|
||||
let (shares,paid):(Option<i64>,Option<i64>)=tx.query_row(
|
||||
"SELECT SUM(available),SUM(CASE payout_per_share WHEN 50 THEN available/2 WHEN 100 THEN available ELSE 0 END)
|
||||
FROM position_units WHERE market=? AND account=? AND available>0",params![market,account],|r|Ok((r.get(0)?,r.get(1)?)))?;
|
||||
let paid = paid.ok_or_else(|| anyhow::anyhow!("no positions to redeem"))?;
|
||||
crate::inventory::movement(
|
||||
tx,
|
||||
request_id,
|
||||
account,
|
||||
market,
|
||||
"REDEEM",
|
||||
shares.unwrap_or(0),
|
||||
paid,
|
||||
)?;
|
||||
tx.execute(
|
||||
"DELETE FROM position_remainders WHERE market=? AND account=?",
|
||||
params![market, account],
|
||||
@@ -487,7 +504,5 @@ fn redeem(tx: &Transaction, market: &str, account: &str, request_id: &str) -> Re
|
||||
"UPDATE positions SET quantity=0 WHERE market=? AND account=? AND quantity>0",
|
||||
params![market, account],
|
||||
)?;
|
||||
crate::inventory::backing(tx, market, -paid)?;
|
||||
ledger::cash_micros(tx, account, paid)?;
|
||||
Ok(paid)
|
||||
Ok((shares.unwrap_or(0), paid))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user