Expose paginated wallet activity from trades, position operations and redemptions
This commit is contained in:
@@ -9,7 +9,12 @@ use anyhow::{ensure, Result};
|
||||
use rusqlite::{params, OptionalExtension, Transaction};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub(crate) fn apply(tx: &Transaction, books: &Books, command: Command) -> Result<Value> {
|
||||
pub(crate) fn apply(
|
||||
tx: &Transaction,
|
||||
books: &Books,
|
||||
command: Command,
|
||||
request_id: &str,
|
||||
) -> Result<Value> {
|
||||
match command {
|
||||
Command::Fund { account, cents } => {
|
||||
identifier(&account)?;
|
||||
@@ -276,7 +281,7 @@ pub(crate) fn apply(tx: &Transaction, books: &Books, command: Command) -> Result
|
||||
Command::Redeem { market, account } => {
|
||||
identifier(&account)?;
|
||||
phase(tx, &market, &["resolved"])?;
|
||||
let paid = redeem(tx, &market, &account)?;
|
||||
let paid = redeem(tx, &market, &account, request_id)?;
|
||||
Ok(json!({"market":market,"account":account,"paid":paid}))
|
||||
}
|
||||
Command::Resolve { market, winner } => {
|
||||
@@ -288,7 +293,7 @@ pub(crate) fn apply(tx: &Transaction, books: &Books, command: Command) -> Result
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
let mut paid = 0;
|
||||
for account in accounts {
|
||||
paid += redeem(tx, &market, &account)?;
|
||||
paid += redeem(tx, &market, &account, request_id)?;
|
||||
}
|
||||
ensure!(backing == paid, "collateral mismatch");
|
||||
Ok(json!({"phase":"resolved","paid":paid}))
|
||||
@@ -331,13 +336,19 @@ pub(crate) fn finalize(tx: &Transaction, market: &str, winner: &str) -> Result<i
|
||||
)?)
|
||||
}
|
||||
|
||||
fn redeem(tx: &Transaction, market: &str, account: &str) -> Result<i64> {
|
||||
fn redeem(tx: &Transaction, market: &str, account: &str, request_id: &str) -> Result<i64> {
|
||||
let paid:Option<i64>=tx.query_row(
|
||||
"SELECT SUM(p.quantity * CASE WHEN m.winner='VOID' THEN 50 WHEN p.outcome=m.winner THEN 100 ELSE 0 END)
|
||||
FROM positions p JOIN markets m ON m.id=p.market
|
||||
WHERE p.market=? AND p.account=? AND p.quantity>0",
|
||||
params![market,account],|row|row.get(0))?;
|
||||
let paid = paid.ok_or_else(|| anyhow::anyhow!("no positions to redeem"))?;
|
||||
tx.execute(
|
||||
"INSERT INTO wallet_movements(request_id,account,kind,market,shares,cash_delta,at_ms)
|
||||
SELECT ?1,?2,'REDEEM',?3,SUM(quantity),?4,CAST(unixepoch('subsec')*1000 AS INTEGER)
|
||||
FROM positions WHERE account=?2 AND market=?3 AND quantity>0",
|
||||
params![request_id, account, market, paid],
|
||||
)?;
|
||||
tx.execute(
|
||||
"UPDATE positions SET quantity=0 WHERE market=? AND account=? AND quantity>0",
|
||||
params![market, account],
|
||||
|
||||
Reference in New Issue
Block a user