From 8339118230f71e333520519f6e2841723cba6c14 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sun, 15 Nov 2026 18:30:29 +0000 Subject: [PATCH] Derive the kroner balance of an account --- ledger.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ledger.py b/ledger.py index 3434fc9..4d927a7 100644 --- a/ledger.py +++ b/ledger.py @@ -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