Browse Source

lightning: display forwarded payments as a single history item

regtest_lnd
ThomasV 6 years ago
committed by SomberNight
parent
commit
c6964df652
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 2
      electrum/gui/qt/history_list.py
  2. 4
      electrum/lnutil.py
  3. 47
      electrum/lnworker.py

2
electrum/gui/qt/history_list.py

@ -282,7 +282,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)

4
electrum/lnutil.py

@ -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

47
electrum/lnworker.py

@ -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
@ -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
@ -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',
@ -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',
@ -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

Loading…
Cancel
Save