Refuse a transfer that overdraws the kroner balance
This commit is contained in:
1 parent
8339118230
commit
a844d7b421
1 file changed
+21
@@ -393,6 +393,25 @@ def _picks(conn, tx_id):
|
|||||||
return [dict(row) for row in rows]
|
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):
|
def rebuild(conn=None):
|
||||||
"""Recompute every lot and every consumption from the transactions."""
|
"""Recompute every lot and every consumption from the transactions."""
|
||||||
own = conn is None
|
own = conn is None
|
||||||
@@ -402,6 +421,7 @@ def rebuild(conn=None):
|
|||||||
conn.execute("DELETE FROM lot_consumptions")
|
conn.execute("DELETE FROM lot_consumptions")
|
||||||
conn.execute("DELETE FROM lots")
|
conn.execute("DELETE FROM lots")
|
||||||
rows = conn.execute("SELECT * FROM transactions ORDER BY date, id").fetchall()
|
rows = conn.execute("SELECT * FROM transactions ORDER BY date, id").fetchall()
|
||||||
|
balances = {}
|
||||||
for row in rows:
|
for row in rows:
|
||||||
tx = dict(row)
|
tx = dict(row)
|
||||||
if tx["type"] == "transfer_in" and tx["generated"]:
|
if tx["type"] == "transfer_in" and tx["generated"]:
|
||||||
@@ -409,6 +429,7 @@ def rebuild(conn=None):
|
|||||||
else:
|
else:
|
||||||
create_lots_for(tx, conn)
|
create_lots_for(tx, conn)
|
||||||
consume(tx, _picks(conn, tx["id"]), conn)
|
consume(tx, _picks(conn, tx["id"]), conn)
|
||||||
|
_move_kroner(balances, tx)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
if own:
|
if own:
|
||||||
|
|||||||
Reference in new issue
Block a user