move tx grouping code from lnworker to submarine_swaps
This better encapsulates the swaps logic. If we turn submarine_swaps into a plugin, these methods can be replaced by hooks.
This commit is contained in:
@@ -1062,14 +1062,7 @@ class LNWallet(LNWorker):
|
|||||||
'payment_hash': key,
|
'payment_hash': key,
|
||||||
'preimage': preimage,
|
'preimage': preimage,
|
||||||
}
|
}
|
||||||
# add group_id to swap transactions
|
item['group_id'] = self.swap_manager.get_group_id_for_payment_hash(payment_hash)
|
||||||
swap = self.swap_manager.get_swap(payment_hash)
|
|
||||||
if swap:
|
|
||||||
if swap.is_reverse:
|
|
||||||
item['group_id'] = swap.spending_txid
|
|
||||||
else:
|
|
||||||
item['group_id'] = swap.funding_txid
|
|
||||||
# done
|
|
||||||
out[payment_hash] = item
|
out[payment_hash] = item
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -1077,7 +1070,6 @@ class LNWallet(LNWorker):
|
|||||||
return self._labels_cache.get(txid)
|
return self._labels_cache.get(txid)
|
||||||
|
|
||||||
def get_onchain_history(self):
|
def get_onchain_history(self):
|
||||||
current_height = self.wallet.adb.get_local_height()
|
|
||||||
out = {}
|
out = {}
|
||||||
# add funding events
|
# add funding events
|
||||||
for chan in itertools.chain(self.channels.values(), self.channel_backups.values()): # type: AbstractChannel
|
for chan in itertools.chain(self.channels.values(), self.channel_backups.values()): # type: AbstractChannel
|
||||||
@@ -1127,49 +1119,14 @@ class LNWallet(LNWorker):
|
|||||||
'txpos_in_block': tx_height.txpos,
|
'txpos_in_block': tx_height.txpos,
|
||||||
} # FIXME this data structure needs to be kept in ~sync with wallet.get_onchain_history
|
} # FIXME this data structure needs to be kept in ~sync with wallet.get_onchain_history
|
||||||
out[closing_txid] = item
|
out[closing_txid] = item
|
||||||
# add info about submarine swaps
|
|
||||||
settled_payments = self.get_payments(status='settled')
|
|
||||||
for payment_hash_hex, swap in self.swap_manager.swaps.items():
|
|
||||||
txid = swap.spending_txid if swap.is_reverse else swap.funding_txid
|
|
||||||
if txid is None:
|
|
||||||
continue
|
|
||||||
payment_hash = bytes.fromhex(payment_hash_hex)
|
|
||||||
if payment_hash in settled_payments:
|
|
||||||
plist = settled_payments[payment_hash]
|
|
||||||
info = self.get_payment_info(payment_hash)
|
|
||||||
direction, amount_msat, fee_msat, timestamp = self.get_payment_value(info, plist)
|
|
||||||
else:
|
|
||||||
amount_msat = 0
|
|
||||||
|
|
||||||
if swap.is_reverse:
|
d = self.swap_manager.get_groups_for_onchain_history()
|
||||||
group_label = 'Reverse swap' + ' ' + self.config.format_amount_and_units(swap.lightning_amount)
|
for k,v in d.items():
|
||||||
else:
|
group_id = v.get('group_id')
|
||||||
group_label = 'Forward swap' + ' ' + self.config.format_amount_and_units(swap.onchain_amount)
|
group_label = v.get('group_label')
|
||||||
self._labels_cache[txid] = group_label
|
if group_id and group_label:
|
||||||
|
self._labels_cache[group_id] = group_label
|
||||||
label = _('Claim transaction') if swap.is_reverse else _('Funding transaction')
|
out.update(d)
|
||||||
delta = current_height - swap.locktime
|
|
||||||
if self.wallet.adb.is_mine(swap.lockup_address):
|
|
||||||
tx_height = self.wallet.adb.get_tx_height(swap.funding_txid)
|
|
||||||
if swap.is_reverse and tx_height.height <= 0:
|
|
||||||
label += ' (%s)' % _('waiting for funding tx confirmation')
|
|
||||||
if not swap.is_reverse and not swap.is_redeemed and swap.spending_txid is None and delta < 0:
|
|
||||||
label += f' (refundable in {-delta} blocks)' # fixme: only if unspent
|
|
||||||
out[txid] = {
|
|
||||||
'group_id': txid,
|
|
||||||
'amount_msat': 0, # must be zero for onchain tx
|
|
||||||
'type': 'swap',
|
|
||||||
'label': label,
|
|
||||||
}
|
|
||||||
if not swap.is_reverse:
|
|
||||||
# if the spending_tx is in the wallet, this will add it
|
|
||||||
# to the group (see wallet.get_full_history)
|
|
||||||
out[swap.spending_txid] = {
|
|
||||||
'group_id': txid,
|
|
||||||
'amount_msat': 0, # must be zero for onchain tx
|
|
||||||
'type': 'swap',
|
|
||||||
'label': _('Refund transaction'),
|
|
||||||
}
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def get_history(self):
|
def get_history(self):
|
||||||
|
|||||||
@@ -172,8 +172,10 @@ class SwapManager(Logger):
|
|||||||
self.percentage = 0
|
self.percentage = 0
|
||||||
self._min_amount = None
|
self._min_amount = None
|
||||||
self._max_amount = None
|
self._max_amount = None
|
||||||
|
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
self.lnworker = lnworker
|
self.lnworker = lnworker
|
||||||
|
self.config = wallet.config
|
||||||
self.taskgroup = None
|
self.taskgroup = None
|
||||||
self.dummy_address = DummyAddress.SWAP
|
self.dummy_address = DummyAddress.SWAP
|
||||||
|
|
||||||
@@ -1157,6 +1159,62 @@ class SwapManager(Logger):
|
|||||||
raise Exception('unsupported request type:' + req_type)
|
raise Exception('unsupported request type:' + req_type)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def get_groups_for_onchain_history(self):
|
||||||
|
current_height = self.wallet.adb.get_local_height()
|
||||||
|
d = {}
|
||||||
|
# add info about submarine swaps
|
||||||
|
settled_payments = self.lnworker.get_payments(status='settled')
|
||||||
|
for payment_hash_hex, swap in self.swaps.items():
|
||||||
|
txid = swap.spending_txid if swap.is_reverse else swap.funding_txid
|
||||||
|
if txid is None:
|
||||||
|
continue
|
||||||
|
payment_hash = bytes.fromhex(payment_hash_hex)
|
||||||
|
if payment_hash in settled_payments:
|
||||||
|
plist = settled_payments[payment_hash]
|
||||||
|
info = self.lnworker.get_payment_info(payment_hash)
|
||||||
|
direction, amount_msat, fee_msat, timestamp = self.lnworker.get_payment_value(info, plist)
|
||||||
|
else:
|
||||||
|
amount_msat = 0
|
||||||
|
|
||||||
|
if swap.is_reverse:
|
||||||
|
group_label = 'Reverse swap' + ' ' + self.config.format_amount_and_units(swap.lightning_amount)
|
||||||
|
else:
|
||||||
|
group_label = 'Forward swap' + ' ' + self.config.format_amount_and_units(swap.onchain_amount)
|
||||||
|
|
||||||
|
label = _('Claim transaction') if swap.is_reverse else _('Funding transaction')
|
||||||
|
delta = current_height - swap.locktime
|
||||||
|
if self.wallet.adb.is_mine(swap.lockup_address):
|
||||||
|
tx_height = self.wallet.adb.get_tx_height(swap.funding_txid)
|
||||||
|
if swap.is_reverse and tx_height.height <= 0:
|
||||||
|
label += ' (%s)' % _('waiting for funding tx confirmation')
|
||||||
|
if not swap.is_reverse and not swap.is_redeemed and swap.spending_txid is None and delta < 0:
|
||||||
|
label += f' (refundable in {-delta} blocks)' # fixme: only if unspent
|
||||||
|
d[txid] = {
|
||||||
|
'group_id': txid,
|
||||||
|
'amount_msat': 0, # must be zero for onchain tx
|
||||||
|
'type': 'swap',
|
||||||
|
'label': label,
|
||||||
|
'group_label': group_label,
|
||||||
|
}
|
||||||
|
if not swap.is_reverse:
|
||||||
|
# if the spending_tx is in the wallet, this will add it
|
||||||
|
# to the group (see wallet.get_full_history)
|
||||||
|
d[swap.spending_txid] = {
|
||||||
|
'group_id': txid,
|
||||||
|
'amount_msat': 0, # must be zero for onchain tx
|
||||||
|
'type': 'swap',
|
||||||
|
'label': _('Refund transaction'),
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
|
||||||
|
def get_group_id_for_payment_hash(self, payment_hash):
|
||||||
|
# add group_id to swap transactions
|
||||||
|
swap = self.get_swap(payment_hash)
|
||||||
|
if swap:
|
||||||
|
if swap.is_reverse:
|
||||||
|
return swap.spending_txid
|
||||||
|
else:
|
||||||
|
return swap.funding_txid
|
||||||
|
|
||||||
class HttpSwapManager(SwapManager):
|
class HttpSwapManager(SwapManager):
|
||||||
async def send_request_to_server(self, method, request_data):
|
async def send_request_to_server(self, method, request_data):
|
||||||
|
|||||||
Reference in New Issue
Block a user