Refuse a transfer that overdraws the kroner balance

This commit is contained in:
ai-bot committed 2026-11-15 18:30:34 +00:00
1 parent 8339118230
commit a844d7b421
1 file changed
+21
+21
View File
@@ -393,6 +393,25 @@ def _picks(conn, tx_id):
return [dict(row) for row in rows]
def _move_kroner(balances, tx):
"""Track the derived kroner balance and refuse an over-drawn transfer.
Kroner are never stored, so the balance is carried along the replay. Only a
transfer is refused: a buy that overdraws is allowed (section 3.11).
"""
market_id = tx["market_id"]
if market_id is None:
return
balance = balances.get(market_id, 0)
out_ore = tx["leg_out_ore"] or 0
if out_ore and tx["type"] == "transfer_out" and balance < out_ore:
raise LedgerError(
"Kontoen har ikkje nok kroner: det manglar %s kr."
% rates.kroner_whole(out_ore - balance)
)
balances[market_id] = balance + (tx["leg_in_ore"] or 0) - out_ore
def rebuild(conn=None):
"""Recompute every lot and every consumption from the transactions."""
own = conn is None
@@ -402,6 +421,7 @@ def rebuild(conn=None):
conn.execute("DELETE FROM lot_consumptions")
conn.execute("DELETE FROM lots")
rows = conn.execute("SELECT * FROM transactions ORDER BY date, id").fetchall()
balances = {}
for row in rows:
tx = dict(row)
if tx["type"] == "transfer_in" and tx["generated"]:
@@ -409,6 +429,7 @@ def rebuild(conn=None):
else:
create_lots_for(tx, conn)
consume(tx, _picks(conn, tx["id"]), conn)
_move_kroner(balances, tx)
conn.commit()
finally:
if own: