mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
lightning: display forwarded payments as a single history item
This commit is contained in:
parent
3e443535a2
commit
8b12f481da
3 changed files with 33 additions and 20 deletions
|
@ -281,7 +281,7 @@ class HistoryModel(QAbstractItemModel, Logger):
|
||||||
transactions[txid] = tx_item
|
transactions[txid] = tx_item
|
||||||
for tx_item in lightning_history:
|
for tx_item in lightning_history:
|
||||||
txid = tx_item.get('txid')
|
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:
|
if txid and txid in transactions:
|
||||||
item = transactions[txid]
|
item = transactions[txid]
|
||||||
item['label'] = tx_item['label'] + ' (%s)'%self.parent.format_amount_and_units(tx_item['amount_msat']/1000)
|
item['label'] = tx_item['label'] + ' (%s)'%self.parent.format_amount_and_units(tx_item['amount_msat']/1000)
|
||||||
|
|
|
@ -408,8 +408,8 @@ class HTLCOwner(IntFlag):
|
||||||
return HTLCOwner(-self)
|
return HTLCOwner(-self)
|
||||||
|
|
||||||
class Direction(IntFlag):
|
class Direction(IntFlag):
|
||||||
SENT = 3
|
SENT = -1
|
||||||
RECEIVED = 4
|
RECEIVED = 1
|
||||||
|
|
||||||
SENT = Direction.SENT
|
SENT = Direction.SENT
|
||||||
RECEIVED = Direction.RECEIVED
|
RECEIVED = Direction.RECEIVED
|
||||||
|
|
|
@ -13,6 +13,7 @@ import socket
|
||||||
import json
|
import json
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
import dns.resolver
|
import dns.resolver
|
||||||
import dns.exception
|
import dns.exception
|
||||||
|
@ -140,28 +141,40 @@ class LNWorker(PrintError):
|
||||||
return PR_PAID
|
return PR_PAID
|
||||||
|
|
||||||
def get_payments(self):
|
def get_payments(self):
|
||||||
|
# return one item per payment_hash
|
||||||
# note: with AMP we will have several channels per payment
|
# note: with AMP we will have several channels per payment
|
||||||
out = {}
|
out = defaultdict(list)
|
||||||
for chan in self.channels.values():
|
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
|
return out
|
||||||
|
|
||||||
def get_history(self):
|
def get_history(self):
|
||||||
out = []
|
out = []
|
||||||
for chan_id, htlc, direction, status in self.get_payments().values():
|
for payment_hash, plist in self.get_payments().items():
|
||||||
key = bh2u(htlc.payment_hash)
|
if len(plist) == 1:
|
||||||
timestamp = self.preimages[key][1] if key in self.preimages else None
|
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 = {
|
item = {
|
||||||
'type':'payment',
|
'type': 'payment',
|
||||||
|
'label': label,
|
||||||
'timestamp':timestamp or 0,
|
'timestamp':timestamp or 0,
|
||||||
'date':timestamp_to_datetime(timestamp),
|
'date': timestamp_to_datetime(timestamp),
|
||||||
'direction': 'sent' if direction == SENT else 'received',
|
'direction': direction,
|
||||||
'status':status,
|
'status': status,
|
||||||
'amount_msat':htlc.amount_msat,
|
'amount_msat': amount_msat,
|
||||||
'payment_hash':bh2u(htlc.payment_hash),
|
'payment_hash': payment_hash
|
||||||
'channel_id':bh2u(chan_id),
|
|
||||||
'htlc_id':htlc.htlc_id,
|
|
||||||
'cltv_expiry':htlc.cltv_expiry,
|
|
||||||
}
|
}
|
||||||
out.append(item)
|
out.append(item)
|
||||||
# add funding events
|
# add funding events
|
||||||
|
@ -170,7 +183,7 @@ class LNWorker(PrintError):
|
||||||
item = {
|
item = {
|
||||||
'channel_id': bh2u(chan.channel_id),
|
'channel_id': bh2u(chan.channel_id),
|
||||||
'type': 'channel_opening',
|
'type': 'channel_opening',
|
||||||
'label': _('Channel opening'),
|
'label': _('Open channel'),
|
||||||
'txid': funding_txid,
|
'txid': funding_txid,
|
||||||
'amount_msat': chan.balance(LOCAL, ctn=0),
|
'amount_msat': chan.balance(LOCAL, ctn=0),
|
||||||
'direction': 'received',
|
'direction': 'received',
|
||||||
|
@ -182,7 +195,7 @@ class LNWorker(PrintError):
|
||||||
item = {
|
item = {
|
||||||
'channel_id': bh2u(chan.channel_id),
|
'channel_id': bh2u(chan.channel_id),
|
||||||
'txid':closing_txid,
|
'txid':closing_txid,
|
||||||
'label': _('Channel closure'),
|
'label': _('Close channel'),
|
||||||
'type': 'channel_closure',
|
'type': 'channel_closure',
|
||||||
'amount_msat': chan.balance(LOCAL),
|
'amount_msat': chan.balance(LOCAL),
|
||||||
'direction': 'sent',
|
'direction': 'sent',
|
||||||
|
@ -193,7 +206,7 @@ class LNWorker(PrintError):
|
||||||
out.sort(key=lambda x: (x.get('timestamp') or float("inf")))
|
out.sort(key=lambda x: (x.get('timestamp') or float("inf")))
|
||||||
balance_msat = 0
|
balance_msat = 0
|
||||||
for item in out:
|
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
|
item['balance_msat'] = balance_msat
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue