diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py index 366a27cf2..b3493e9be 100644 --- a/electrum/gui/qt/history_list.py +++ b/electrum/gui/qt/history_list.py @@ -845,9 +845,9 @@ class HistoryList(MyTreeView, AcceptFileDragDrop): return try: self.wallet.export_history_to_file( - self.main_window.fx if self.hm.should_show_fiat() else None, - filename, - csv_button.isChecked(), + fx=self.main_window.fx if self.hm.should_show_fiat() else None, + file_path=filename, + is_csv=csv_button.isChecked(), ) except (IOError, os.error) as reason: export_error_label = _("Electrum was unable to produce a transaction export.") diff --git a/electrum/wallet.py b/electrum/wallet.py index aea8a5c75..d7ae3000a 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -3614,9 +3614,14 @@ class Abstract_Wallet(ABC, Logger, EventListener): util.trigger_callback('wallet_updated', self) self.adb.set_future_tx(tx.txid(), wanted_height=wanted_height) - def export_history_to_file(self, fx: Optional['FxThread'], file_path: str, is_csv: bool): + def export_history_to_file(self, *, fx: Optional['FxThread'], file_path: str, is_csv: bool): + """Create a file containing the wallet history in either json or csv format, e.g. for bookkeeping.""" + if run_hook('export_history_to_file', self, fx, file_path, is_csv): + return # allow for plugins to create history fancy export txns = self.get_full_history(fx=fx) - lines = [] + # remove unconfirmed/local tx as their ordering is not deterministic, and they don't seem + # useful for a wallet export (can't do accounting on a tx that hasn't happened yet) + txns = {k: v for k, v in txns.items() if v['timestamp'] not in (None, 0)} def get_all_fees_paid_by_item(h_item: dict) -> Tuple[int, Optional[Fiat]]: # gets all fees paid in an item (or group), as the outer group doesn't contain the @@ -3642,9 +3647,10 @@ class Abstract_Wallet(ABC, Logger, EventListener): return fees_sat, fees_fiat + lines = [] if is_csv: # sort by timestamp so the generated csv is more understandable on first sight - txns = dict(sorted(txns.items(), key=lambda h_item: h_item[1]['timestamp'] or 0)) + txns = dict(sorted(txns.items(), key=lambda h_item: h_item[1]['timestamp'])) for item in txns.values(): # tx groups will are shown as single element fees_sat, fees_fiat = get_all_fees_paid_by_item(item) @@ -3659,7 +3665,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): item['bc_value'], item['ln_value'], item.get('fiat_value', ''), - fees_sat, + util.format_satoshis(fees_sat), str(fees_fiat or ''), item['date'] ] @@ -3676,7 +3682,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): "amount_chain_bc", "amount_lightning_bc", "fiat_value", - "network_fee_satoshi", + "network_fee_bc", "fiat_fee", "timestamp"]) for line in lines: