URI: 
       tlightning: display forwarded payments as a single history item - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 8b12f481dadae4a1bd52a9de8b3f52db061cc5f8
   DIR parent 3e443535a27e121d428ee184734be3bb28129100
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Wed, 27 Feb 2019 11:42:09 +0100
       
       lightning: display forwarded payments as a single history item
       
       Diffstat:
         M electrum/gui/qt/history_list.py     |       2 +-
         M electrum/lnutil.py                  |       4 ++--
         M electrum/lnworker.py                |      47 ++++++++++++++++++++-----------
       
       3 files changed, 33 insertions(+), 20 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py
       t@@ -281,7 +281,7 @@ class HistoryModel(QAbstractItemModel, Logger):
                    transactions[txid] = tx_item
                for tx_item in lightning_history:
                    txid = tx_item.get('txid')
       -            ln_value = tx_item['amount_msat']/1000 * (-1 if tx_item['direction'] =='sent' else 1)
       +            ln_value = tx_item['amount_msat']/1000.
                    if txid and txid in transactions:
                        item = transactions[txid]
                        item['label'] = tx_item['label'] + ' (%s)'%self.parent.format_amount_and_units(tx_item['amount_msat']/1000)
   DIR diff --git a/electrum/lnutil.py b/electrum/lnutil.py
       t@@ -408,8 +408,8 @@ class HTLCOwner(IntFlag):
                return HTLCOwner(-self)
        
        class Direction(IntFlag):
       -    SENT = 3
       -    RECEIVED = 4
       +    SENT = -1
       +    RECEIVED = 1
        
        SENT = Direction.SENT
        RECEIVED = Direction.RECEIVED
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -13,6 +13,7 @@ import socket
        import json
        from datetime import datetime, timezone
        from functools import partial
       +from collections import defaultdict
        
        import dns.resolver
        import dns.exception
       t@@ -140,28 +141,40 @@ class LNWorker(PrintError):
                return PR_PAID
        
            def get_payments(self):
       +        # return one item per payment_hash
                # note: with AMP we will have several channels per payment
       -        out = {}
       +        out = defaultdict(list)
                for chan in self.channels.values():
       -            out.update(chan.get_payments())
       +            d = chan.get_payments()
       +            for k, v in d.items():
       +                out[k].append(v)
                return out
        
            def get_history(self):
                out = []
       -        for chan_id, htlc, direction, status in self.get_payments().values():
       -            key = bh2u(htlc.payment_hash)
       -            timestamp = self.preimages[key][1] if key in self.preimages else None
       +        for payment_hash, plist in self.get_payments().items():
       +            if len(plist) == 1:
       +                chan_id, htlc, _direction, status = plist[0]
       +                direction = 'sent' if _direction == SENT else 'received'
       +                amount_msat= int(_direction) * htlc.amount_msat
       +                label = ''
       +            else:
       +                # assume forwarding
       +                direction = 'forwarding'
       +                amount_msat = sum([int(_direction) * htlc.amount_msat for chan_id, htlc, _direction, status in plist])
       +                status = ''
       +                label = _('Forwarding')
       +
       +            timestamp = self.preimages[payment_hash][1] if payment_hash in self.preimages else None
                    item = {
       -                'type':'payment',
       +                'type': 'payment',
       +                'label': label,
                        'timestamp':timestamp or 0,
       -                'date':timestamp_to_datetime(timestamp),
       -                'direction': 'sent' if direction == SENT else 'received',
       -                'status':status,
       -                'amount_msat':htlc.amount_msat,
       -                'payment_hash':bh2u(htlc.payment_hash),
       -                'channel_id':bh2u(chan_id),
       -                'htlc_id':htlc.htlc_id,
       -                'cltv_expiry':htlc.cltv_expiry,
       +                'date': timestamp_to_datetime(timestamp),
       +                'direction': direction,
       +                'status': status,
       +                'amount_msat': amount_msat,
       +                'payment_hash': payment_hash
                    }
                    out.append(item)
                # add funding events
       t@@ -170,7 +183,7 @@ class LNWorker(PrintError):
                    item = {
                        'channel_id': bh2u(chan.channel_id),
                        'type': 'channel_opening',
       -                'label': _('Channel opening'),
       +                'label': _('Open channel'),
                        'txid': funding_txid,
                        'amount_msat': chan.balance(LOCAL, ctn=0),
                        'direction': 'received',
       t@@ -182,7 +195,7 @@ class LNWorker(PrintError):
                    item = {
                        'channel_id': bh2u(chan.channel_id),
                        'txid':closing_txid,
       -                'label': _('Channel closure'),
       +                'label': _('Close channel'),
                        'type': 'channel_closure',
                        'amount_msat': chan.balance(LOCAL),
                        'direction': 'sent',
       t@@ -193,7 +206,7 @@ class LNWorker(PrintError):
                out.sort(key=lambda x: (x.get('timestamp') or float("inf")))
                balance_msat = 0
                for item in out:
       -            balance_msat += item['amount_msat'] * (1 if item['direction']=='received' else -1)
       +            balance_msat += item['amount_msat']
                    item['balance_msat'] = balance_msat
                return out