diff --git a/app.py b/app.py index 283621f..ed38384 100644 --- a/app.py +++ b/app.py @@ -235,13 +235,12 @@ def _write_transaction(conn, tx_id, tx, picks): def _sync_transfer_pair(conn, tx_id, previous, tx): """Keep the generated transfer_in in step with its transfer_out.""" group = previous["transfer_group"] if previous else None - if tx["type"] == "transfer_out": + if tx["type"] == "transfer_out" and tx["to_market_id"]: if not group: group = uuid.uuid4().hex conn.execute( "UPDATE transactions SET transfer_group = ? WHERE id = ?", (group, tx_id) ) - arrived = tx["leg_out_sats"] or 0 now = _now_stamp() row = conn.execute( "SELECT id FROM transactions WHERE type = 'transfer_in' AND transfer_group = ?", @@ -250,20 +249,23 @@ def _sync_transfer_pair(conn, tx_id, previous, tx): if row is None: conn.execute( "INSERT INTO transactions (date, type, market_id, taxable, note, leg_in_sats," - " fee_sats, fee_ore, to_market_id, transfer_group, generated, created_at," - " updated_at) VALUES (?, 'transfer_in', ?, ?, '', ?, 0, 0, ?, ?, 1, ?, ?)", - (tx["date"], tx["to_market_id"], tx["taxable"], arrived, - tx["market_id"], group, now, now), + " leg_in_ore, fee_sats, fee_ore, to_market_id, transfer_group, generated," + " created_at, updated_at)" + " VALUES (?, 'transfer_in', ?, ?, '', ?, ?, 0, 0, ?, ?, 1, ?, ?)", + (tx["date"], tx["to_market_id"], tx["taxable"], tx["leg_out_sats"], + tx["leg_out_ore"], tx["market_id"], group, now, now), ) else: conn.execute( "UPDATE transactions SET date = ?, market_id = ?, taxable = ?," - " leg_in_sats = ?, fee_sats = 0, fee_ore = 0, to_market_id = ?," - " generated = 1, updated_at = ? WHERE id = ?", - (tx["date"], tx["to_market_id"], tx["taxable"], arrived, - tx["market_id"], now, row["id"]), + " leg_in_sats = ?, leg_in_ore = ?, fee_sats = 0, fee_ore = 0," + " to_market_id = ?, generated = 1, updated_at = ? WHERE id = ?", + (tx["date"], tx["to_market_id"], tx["taxable"], tx["leg_out_sats"], + tx["leg_out_ore"], tx["market_id"], now, row["id"]), ) elif group: + # The transaction is no longer a transfer with a destination: an empty + # `Til` is a withdrawal, and it leaves no pair behind (section 3.11). _drop_ledger(conn) conn.execute( "DELETE FROM transactions WHERE type = 'transfer_in' AND transfer_group = ?",