From d03c77837fd0b07d2c0a9b8ca4c70297872bfc5e Mon Sep 17 00:00:00 2001 From: ThomasV Date: Thu, 14 Sep 2023 15:24:17 +0200 Subject: [PATCH] Let the GUI compute the balance displayed in history. Since Qt groups swap transactions, the displayed balance was sometimes incorrect. --- electrum/gui/qml/qetransactionlistmodel.py | 4 +--- electrum/gui/qt/history_list.py | 11 ++++++++--- electrum/gui/stdio.py | 6 ++++-- electrum/gui/text.py | 3 ++- electrum/wallet.py | 5 +---- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/electrum/gui/qml/qetransactionlistmodel.py b/electrum/gui/qml/qetransactionlistmodel.py index 9ae055970..eaa773372 100644 --- a/electrum/gui/qml/qetransactionlistmodel.py +++ b/electrum/gui/qml/qetransactionlistmodel.py @@ -20,7 +20,7 @@ class QETransactionListModel(QAbstractListModel, QtEventListener): # define listmodel rolemap _ROLE_NAMES=('txid','fee_sat','height','confirmations','timestamp','monotonic_timestamp', - 'incoming','value','balance','date','label','txpos_in_block','fee', + 'incoming','value','date','label','txpos_in_block','fee', 'inputs','outputs','section','type','lightning','payment_hash','key','complete') _ROLE_KEYS = range(Qt.UserRole, Qt.UserRole + len(_ROLE_NAMES)) _ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES])) @@ -127,13 +127,11 @@ class QETransactionListModel(QAbstractListModel, QtEventListener): if item['lightning']: item['value'] = QEAmount(amount_sat=item['value'].value, amount_msat=item['amount_msat']) - item['balance'] = QEAmount(amount_sat=item['balance'].value, amount_msat=item['amount_msat']) if item['type'] == 'payment': item['incoming'] = True if item['direction'] == 'received' else False item['confirmations'] = 0 else: item['value'] = QEAmount(amount_sat=item['value'].value) - item['balance'] = QEAmount(amount_sat=item['balance'].value) if 'txid' in item: tx = self.wallet.db.get_transaction(item['txid']) diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py index 5751ebe94..a4c133474 100644 --- a/electrum/gui/qt/history_list.py +++ b/electrum/gui/qt/history_list.py @@ -206,8 +206,8 @@ class HistoryNode(CustomNode): v_str = window.format_amount(value, is_diff=True, whitespaces=whitespaces, add_thousands_sep=add_thousands_sep) return QVariant(v_str) elif col == HistoryColumns.BALANCE: - balance = tx_item['balance'].value - balance_str = window.format_amount(balance, whitespaces=whitespaces, add_thousands_sep=add_thousands_sep) + balance = tx_item['balance'].value if 'balance' in tx_item else None + balance_str = window.format_amount(balance, whitespaces=whitespaces, add_thousands_sep=add_thousands_sep) if balance is not None else '' return QVariant(balance_str) elif col == HistoryColumns.FIAT_VALUE and 'fiat_value' in tx_item: value_str = window.fx.format_fiat(tx_item['fiat_value'].value, add_thousands_sep=add_thousands_sep) @@ -322,7 +322,6 @@ class HistoryModel(CustomModel, Logger): # add child to parent parent.addChild(node) # update parent data - parent._data['balance'] = tx_item['balance'] parent._data['value'] += tx_item['value'] if 'group_label' in tx_item: parent._data['label'] = tx_item['group_label'] @@ -339,6 +338,12 @@ class HistoryModel(CustomModel, Logger): parent._data['height'] = tx_item['height'] parent._data['confirmations'] = tx_item['confirmations'] + # compute balance once all children have beed added + balance = 0 + for node in self._root._children: + balance += node._data['value'].value + node._data['balance'] = Satoshis(balance + new_length = self._root.childCount() self.beginInsertRows(QModelIndex(), 0, new_length-1) self.transactions = transactions diff --git a/electrum/gui/stdio.py b/electrum/gui/stdio.py index f3133cd5c..42d55f4df 100644 --- a/electrum/gui/stdio.py +++ b/electrum/gui/stdio.py @@ -115,8 +115,10 @@ class ElectrumGui(BaseElectrumGui, EventListener): time_str = 'unconfirmed' label = self.wallet.get_label_for_txid(hist_item.txid) - messages.append(format_str % (time_str, label, format_satoshis(delta, whitespaces=True), - format_satoshis(hist_item.balance, whitespaces=True))) + messages.append(format_str % ( + time_str, label, + format_satoshis(hist_item.delta, whitespaces=True), + format_satoshis(hist_item.balance, whitespaces=True))) self.print_list(messages[::-1], format_str%(_("Date"), _("Description"), _("Amount"), _("Balance"))) diff --git a/electrum/gui/text.py b/electrum/gui/text.py index f18e604be..5c271dc85 100644 --- a/electrum/gui/text.py +++ b/electrum/gui/text.py @@ -164,9 +164,10 @@ class ElectrumGui(BaseElectrumGui, EventListener): domain = self.wallet.get_addresses() self.history = [] self.txid = [] + balance_sat = 0 for item in self.wallet.get_full_history().values(): amount_sat = item['value'].value - balance_sat = item['balance'].value + balance_sat += amount_sat if item.get('lightning'): timestamp = item['timestamp'] label = self.wallet.get_label_for_rhash(item['payment_hash']) diff --git a/electrum/wallet.py b/electrum/wallet.py index f75dab978..61f9762a3 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -1280,7 +1280,6 @@ class Abstract_Wallet(ABC, Logger, EventListener): for k, v in sorted(list(transactions_tmp.items()), key=sort_key): transactions[k] = v now = time.time() - balance = 0 for item in transactions.values(): # add on-chain and lightning values value = Decimal(0) @@ -1288,10 +1287,8 @@ class Abstract_Wallet(ABC, Logger, EventListener): value += item['bc_value'].value if item.get('ln_value'): value += item.get('ln_value').value - # note: 'value' and 'balance' has msat precision (as LN has msat precision) + # note: 'value' has msat precision (as LN has msat precision) item['value'] = Satoshis(value) - balance += value - item['balance'] = Satoshis(balance) if include_fiat: txid = item.get('txid') if not item.get('lightning') and txid: