URI: 
       tswaps: improve history display - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 252591832a68e7f6c38606f78d00c821a5a11c67
   DIR parent 7ec7dd07d096cbf5a9fda7f65c90b0e220629634
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Wed, 27 May 2020 09:59:53 +0200
       
       swaps: improve history display
       
       Diffstat:
         M electrum/gui/qt/transaction_dialog… |       7 +------
         M electrum/lnworker.py                |      53 ++++++++++++++++++-------------
         M electrum/wallet.py                  |      20 +++++++++++++-------
       
       3 files changed, 45 insertions(+), 35 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/transaction_dialog.py b/electrum/gui/qt/transaction_dialog.py
       t@@ -414,11 +414,6 @@ class BaseTxDialog(QDialog, MessageBoxMixin):
                        tx_mined_status = self.wallet.lnworker.lnwatcher.get_tx_height(txid)
                else:
                    ln_amount = None
       -        swap_history = self.wallet.lnworker.get_swap_history() if self.wallet.lnworker else {}
       -        if txid in swap_history:
       -            item = swap_history[txid]
       -            ln_amount = item['lightning_amount']
       -
                self.broadcast_button.setEnabled(tx_details.can_broadcast)
                can_sign = not self.tx.is_complete() and \
                    (self.wallet.can_sign(self.tx) or bool(self.external_keypairs))
       t@@ -490,7 +485,7 @@ class BaseTxDialog(QDialog, MessageBoxMixin):
                    self.fee_warning_icon.setVisible(bool(risk_of_burning_coins))
                self.fee_label.setText(fee_str)
                self.size_label.setText(size_str)
       -        if ln_amount is None:
       +        if ln_amount is None or ln_amount == 0:
                    ln_amount_str = ''
                elif ln_amount > 0:
                    ln_amount_str = _('Amount received in channels') + ': ' + format_amount(ln_amount) + ' ' + base_unit
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -594,6 +594,16 @@ class LNWallet(LNWorker):
                        out[k] += v
                return out
        
       +    def get_payment_value(self, info, plist):
       +        amount_msat = 0
       +        fee_msat = None
       +        for chan_id, htlc, _direction in plist:
       +            amount_msat += int(_direction) * htlc.amount_msat
       +            if _direction == SENT and info and info.amount:
       +                fee_msat = (fee_msat or 0) - info.amount*1000 - amount_msat
       +        timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
       +        return amount_msat, fee_msat, timestamp
       +
            def get_lightning_history(self):
                out = {}
                for key, plist in self.get_settled_payments().items():
       t@@ -601,13 +611,7 @@ class LNWallet(LNWorker):
                        continue
                    payment_hash = bytes.fromhex(key)
                    info = self.get_payment_info(payment_hash)
       -            timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
       -            amount_msat = 0
       -            fee_msat = None
       -            for chan_id, htlc, _direction in plist:
       -                amount_msat += int(_direction) * htlc.amount_msat
       -                if _direction == SENT and info and info.amount:
       -                    fee_msat = (fee_msat or 0) - info.amount*1000 - amount_msat
       +            amount_msat, fee_msat, timestamp = self.get_payment_value(info, plist)
                    if info is not None:
                        label = self.wallet.get_label(key)
                        direction = ('sent' if info.direction == SENT else 'received') if len(plist)==1 else 'self-payment'
       t@@ -642,21 +646,6 @@ class LNWallet(LNWorker):
                    out[payment_hash] = item
                return out
        
       -    def get_swap_history(self):
       -        out = {}
       -        for k, swap_info in self.swap_manager.swaps.items():
       -            is_reverse = swap_info.get('invoice')
       -            if is_reverse:
       -                txid = swap_info.get('claim_txid')
       -            else:
       -                txid = swap_info.get('funding_txid')
       -            if txid is None:
       -                continue
       -            out[txid] = {
       -                'lightning_amount': swap_info.get('lightning_amount', 0) * (-1 if is_reverse else 1)
       -            }
       -        return out
       -
            def get_onchain_history(self):
                out = {}
                # add funding events
       t@@ -691,6 +680,26 @@ class LNWallet(LNWorker):
                        'fee_msat': None,
                    }
                    out[closing_txid] = item
       +        # add submarine swaps
       +        settled_payments = self.get_settled_payments()
       +        for preimage_hex, swap_info in self.swap_manager.swaps.items():
       +            is_reverse = swap_info.get('invoice')
       +            txid = swap_info.get('claim_txid' if is_reverse else 'funding_txid')
       +            if txid is None:
       +                continue
       +            payment_hash = sha256(bytes.fromhex(preimage_hex))
       +            if payment_hash.hex() in settled_payments:
       +                plist = settled_payments[payment_hash.hex()]
       +                info = self.get_payment_info(payment_hash)
       +                amount_msat, fee_msat, timestamp = self.get_payment_value(info, plist)
       +            else:
       +                amount_msat = 0
       +            out[txid] = {
       +                'txid': txid,
       +                'amount_msat': amount_msat,
       +                'type': 'swap',
       +                'label': 'Reverse swap' if is_reverse else 'Normal swap' # TODO: show time left until we can get a refund
       +            }
                return out
        
            def get_history(self):
   DIR diff --git a/electrum/wallet.py b/electrum/wallet.py
       t@@ -820,23 +820,29 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                transactions_tmp = OrderedDictWithIndex()
                # add on-chain txns
                onchain_history = self.get_onchain_history(domain=onchain_domain)
       +        lnworker_history = self.lnworker.get_onchain_history() if self.lnworker else []
                for tx_item in onchain_history:
                    txid = tx_item['txid']
                    transactions_tmp[txid] = tx_item
       -        # add LN txns
       -        if self.lnworker and include_lightning:
       -            lightning_history = self.lnworker.get_history()
       -        else:
       -            lightning_history = []
       -        for i, tx_item in enumerate(lightning_history):
       +            # add lnworker info here
       +            if txid in lnworker_history:
       +                item = lnworker_history[txid]
       +                tx_item['label'] = item['label']
       +                tx_item['type'] = item['type']
       +                ln_value = Decimal(item['amount_msat']) / 1000
       +                tx_item['ln_value'] = Satoshis(ln_value)
       +        # add lightning transactions.
       +        lightning_history = self.lnworker.get_lightning_history() if self.lnworker and include_lightning else {}
       +        for tx_item in lightning_history.values():
                    txid = tx_item.get('txid')
                    ln_value = Decimal(tx_item['amount_msat']) / 1000
       +            # merge items that have a txid with onchain tx
                    if txid and txid in transactions_tmp:
                        item = transactions_tmp[txid]
                        item['label'] = tx_item['label']
                        item['type'] = tx_item['type']
       -                #item['channel_id'] = tx_item['channel_id']
                        item['ln_value'] = Satoshis(ln_value)
       +                item['amount_msat'] = tx_item['amount_msat']
                    else:
                        tx_item['lightning'] = True
                        tx_item['ln_value'] = Satoshis(ln_value)