Derive the kroner balance of an account

This commit is contained in:
ai-bot committed 2026-11-15 18:30:29 +00:00
1 parent bda3db46f6
commit 8339118230
1 file changed
+21
+21
View File
@@ -295,6 +295,27 @@ def sats_in_market(market_id, date, conn=None):
conn.close()
def kroner_in_market(market_id, date, conn=None):
"""Kroner held in one account at the end of `date` (docs/spec.md section 3.11).
Derived from the kroner legs of the account's own transactions and never
stored, so nothing can drift.
"""
own = conn is None
if own:
conn = database.connect()
try:
row = conn.execute(
"SELECT COALESCE(SUM(COALESCE(leg_in_ore, 0) - COALESCE(leg_out_ore, 0)), 0)"
" AS kroner FROM transactions WHERE market_id = ? AND date <= ?",
(market_id, date),
).fetchone()
return row["kroner"]
finally:
if own:
conn.close()
def sats_on_hand(conn=None):
"""Sats each account holds right now, as the ledger stands."""
own = conn is None