diff --git a/tax.py b/tax.py index 57de894..16c0578 100644 --- a/tax.py +++ b/tax.py @@ -46,6 +46,38 @@ def _settings(conn): return {row["key"]: row["value"] for row in rows} +# The types that carry both a kroner amount and a sats amount, and so have a +# bitcoin price (docs/spec.md section 7.3). The rest give None. +PRICE_TYPES = ("trade", "income", "spend", "loss", "gift_received") + + +def btc_price_ore(tx): + """The bitcoin price of docs/spec.md section 7.3, in ore per whole BTC. + + The kroner amount is the leg that carries it, or `value_ore` where no leg + does; the fee is left out. Returns None when the type has no price. + """ + tx_type = tx["type"] + if tx_type not in PRICE_TYPES: + return None + if tx_type in ("income", "gift_received"): + nok_ore = tx["value_ore"] + sats = tx["leg_in_sats"] + elif tx_type in ("spend", "loss"): + nok_ore = tx["value_ore"] + sats = tx["leg_out_sats"] + else: + nok_ore = tx["leg_out_ore"] + if nok_ore is None: + nok_ore = tx["leg_in_ore"] + sats = tx["leg_in_sats"] + if sats is None: + sats = tx["leg_out_sats"] + if not nok_ore or not sats: + return None + return _round_div(nok_ore * SATS_PER_BTC, sats) + + def report(year, conn=None): """Return the year report: per-account rows, totals, tax and reconciliation.""" year = int(year)