Account for fractional outcome sets from split through redemption

This commit is contained in:
2026-12-03 06:53:37 +08:00
parent 7b26767a97
commit 2c4179a64e
17 changed files with 492 additions and 108 deletions
+44 -12
View File
@@ -1,4 +1,7 @@
use crate::validation::identifier;
use crate::{
inventory::{decimal, UNIT},
validation::identifier,
};
use anyhow::{ensure, Result};
use rusqlite::{params, Connection};
use serde::Deserialize;
@@ -10,10 +13,29 @@ pub(crate) struct Filter {
account: String,
market: Option<String>,
status: Option<String>,
after: Option<(i64, String, String)>,
after: Option<(SortKey, String, String)>,
limit: Option<u32>,
}
#[derive(serde::Serialize, Deserialize)]
#[serde(untagged)]
enum SortKey {
Legacy(i64),
Fraction { micros: i64 },
}
impl SortKey {
fn units(&self, status: &str) -> Result<i64> {
match self {
Self::Legacy(key) if status == "MERGEABLE" => key
.checked_mul(UNIT)
.ok_or_else(|| anyhow::anyhow!("position cursor overflow")),
Self::Legacy(key) => Ok(*key),
Self::Fraction { micros } if status == "MERGEABLE" => Ok(*micros),
_ => anyhow::bail!("invalid position cursor"),
}
}
}
impl Filter {
pub(crate) fn read(self, db: &Connection) -> Result<Value> {
identifier(&self.account)?;
@@ -36,7 +58,7 @@ impl Filter {
self.account,
self.market,
status,
cursor.map(|c| c.0),
cursor.map(|c| c.0.units(status)).transpose()?,
cursor.map(|c| c.1.as_str()),
cursor.map(|c| c.2.as_str()),
limit + 1
@@ -52,21 +74,29 @@ impl Filter {
let market: String = row.get(0)?;
let outcome: String = row.get(1)?;
let size: i64 = row.get(2)?;
let status: String = row.get(5)?;
let row_status: String = row.get(5)?;
let payout: Option<i64> = row.get(6)?;
let pairs: i64 = row.get(7)?;
let phase: String = row.get(13)?;
let (mark, source) = price(db, &market, &outcome, &phase, payout)?.unzip();
positions.push(json!({
"current_price_micros":mark,"price_source":source,"current_value_micros":holding_value(size,mark),
"asset_id":crate::assets::id(&market,&outcome),"market":market, "outcome":outcome, "status":status, "size":size,
"asset_id":crate::assets::id(&market,&outcome),"market":market, "outcome":outcome, "status":row_status, "size":decimal(size.into(),UNIT),
"neg_risk_group":row.get::<_,Option<String>>(10)?,
"augmented_role":row.get::<_,Option<String>>(11)?,"outcome_name":row.get::<_,Option<String>>(12)?,
"available_size":row.get::<_,i64>(3)?, "reserved_size":row.get::<_,i64>(4)?,
"redeemable":status=="REDEEMABLE", "payout_cents":payout.map(|p|i128::from(p)*i128::from(size)),
"mergeable":pairs>0, "available_pairs":row.get::<_,i64>(8)?
"available_size":decimal(row.get::<_,i64>(3)?.into(),UNIT), "reserved_size":decimal(row.get::<_,i64>(4)?.into(),UNIT),
"redeemable":row_status=="REDEEMABLE", "payout_cents":payout.map(|p|decimal(i128::from(p)*i128::from(size)/100,10000)),
"mergeable":pairs>0, "available_pairs":decimal(row.get::<_,i64>(8)?.into(),UNIT)
}));
last = Some((row.get::<_, i64>(9)?, market, outcome));
let key: i64 = row.get(9)?;
let sort = if status != "MERGEABLE" {
SortKey::Legacy(key)
} else if key % UNIT == 0 {
SortKey::Legacy(key / UNIT)
} else {
SortKey::Fraction { micros: key }
};
last = Some((sort, market, outcome));
}
Ok(json!({"positions":positions,"next_after":next_after}))
}
@@ -89,14 +119,14 @@ pub(crate) fn holding_value(size: i64, mark: Option<i64>) -> Option<i128> {
if size == 0 {
return Some(0);
}
mark.map(|p| i128::from(p) * i128::from(size))
mark.map(|p| i128::from(p) * i128::from(size) / i128::from(UNIT))
}
pub(crate) fn value(db: &Connection, account: &str) -> Result<Value> {
identifier(account)?;
let mut stmt = db.prepare(
"SELECT market,outcome,available+reserved AS size,phase,payout_per_share
FROM position_sizes WHERE account=? AND available+reserved>0",
FROM position_units WHERE account=? AND available+reserved>0",
)?;
let mut rows = stmt.query([account])?;
let mut priced = 0i128;
@@ -106,7 +136,8 @@ pub(crate) fn value(db: &Connection, account: &str) -> Result<Value> {
let outcome: String = row.get(1)?;
let phase: String = row.get(3)?;
let mark = price(db, &market, &outcome, &phase, row.get(4)?)?.map(|(p, _)| p);
match holding_value(row.get(2)?, mark) {
let size: i64 = row.get(2)?;
match mark.map(|price| i128::from(price) * i128::from(size)) {
Some(amount) => {
priced = priced
.checked_add(amount)
@@ -115,6 +146,7 @@ pub(crate) fn value(db: &Connection, account: &str) -> Result<Value> {
None => missing += 1,
}
}
let priced = priced / i128::from(UNIT);
Ok(
json!({"account":account,"value_micros":(missing==0).then_some(priced),
"priced_value_micros":priced,"unpriced_positions":missing}),