Serve condition-scoped portfolio valuations from marked holdings

This commit is contained in:
2026-12-03 12:31:58 +08:00
parent 5696e3dae2
commit e26ba27a24
13 changed files with 302 additions and 16 deletions
+18 -7
View File
@@ -1,3 +1,6 @@
pub(crate) mod http;
mod protocol;
use crate::{
inventory::{decimal, UNIT},
validation::identifier,
@@ -122,13 +125,15 @@ pub(crate) fn holding_value(size: i64, mark: Option<i64>) -> Option<i128> {
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_units WHERE account=? AND available+reserved>0",
)?;
let mut rows = stmt.query([account])?;
/// Total in units of (micro-share × micro-dollar per share), before final rounding.
fn marked_holdings(
db: &Connection,
account: &str,
conditions: Option<&str>,
wallet_address: bool,
) -> Result<(i128, u64)> {
let mut stmt = db.prepare_cached(include_str!("portfolio/value.sql"))?;
let mut rows = stmt.query(params![account, conditions, wallet_address])?;
let mut priced = 0i128;
let mut missing = 0u64;
while let Some(row) = rows.next()? {
@@ -146,6 +151,12 @@ pub(crate) fn value(db: &Connection, account: &str) -> Result<Value> {
None => missing += 1,
}
}
Ok((priced, missing))
}
pub(crate) fn value(db: &Connection, account: &str) -> Result<Value> {
identifier(account)?;
let (priced, missing) = marked_holdings(db, account, None, false)?;
let priced = priced / i128::from(UNIT);
Ok(
json!({"account":account,"value_micros":(missing==0).then_some(priced),