From 41f22df26b09c7a5c03baaeb3aa7a09e742ac998 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Fri, 26 Mar 2021 16:27:32 +0100 Subject: [PATCH] submarine swaps: use num_sats_can_receive_no_mpp, to prevent funds being locked --- electrum/gui/kivy/uix/dialogs/lightning_channels.py | 2 +- electrum/gui/qt/swap_dialog.py | 6 +++--- electrum/lnworker.py | 9 +++++++++ electrum/submarine_swaps.py | 5 +++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/electrum/gui/kivy/uix/dialogs/lightning_channels.py b/electrum/gui/kivy/uix/dialogs/lightning_channels.py index 082542a0d..aa28c95bc 100644 --- a/electrum/gui/kivy/uix/dialogs/lightning_channels.py +++ b/electrum/gui/kivy/uix/dialogs/lightning_channels.py @@ -726,7 +726,7 @@ class SwapDialog(Factory.Popup): max_onchain_spend = 0 reverse = int(min(self.lnworker.num_sats_can_send(), self.swap_manager.get_max_amount())) - forward = int(min(self.lnworker.num_sats_can_receive(), + forward = int(min(self.swap_manager.num_sats_can_receive(), # maximally supported swap amount by provider self.swap_manager.get_max_amount(), max_onchain_spend)) diff --git a/electrum/gui/qt/swap_dialog.py b/electrum/gui/qt/swap_dialog.py index e08419d64..c5fa16e71 100644 --- a/electrum/gui/qt/swap_dialog.py +++ b/electrum/gui/qt/swap_dialog.py @@ -126,7 +126,7 @@ class SwapDialog(WindowModalDialog): if self.tx: amount = self.tx.output_value_for_address(ln_dummy_address()) max_swap_amt = self.swap_manager.get_max_amount() - max_recv_amt = int(self.lnworker.num_sats_can_receive()) + max_recv_amt = int(self.swap_manager.num_sats_can_receive()) max_amt = min(max_swap_amt, max_recv_amt) if amount > max_amt: amount = max_amt @@ -149,7 +149,7 @@ class SwapDialog(WindowModalDialog): if self.is_reverse and send_amount and send_amount > self.lnworker.num_sats_can_send(): # cannot send this much on lightning recv_amount = None - if (not self.is_reverse) and recv_amount and recv_amount > self.lnworker.num_sats_can_receive(): + if (not self.is_reverse) and recv_amount and recv_amount > self.swap_manager.num_sats_can_receive(): # cannot receive this much on lightning recv_amount = None self.recv_amount_e.follows = True @@ -228,7 +228,7 @@ class SwapDialog(WindowModalDialog): onchain_amount = self.send_amount_e.get_amount() if lightning_amount is None or onchain_amount is None: return - if lightning_amount > self.lnworker.num_sats_can_receive(): + if lightning_amount > self.swap_manager.num_sats_can_receive(): if not self.window.question(CANNOT_RECEIVE_WARNING): return self.window.protect(self.do_normal_swap, (lightning_amount, onchain_amount)) diff --git a/electrum/lnworker.py b/electrum/lnworker.py index e5b8fc1cd..a36f31213 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -1951,6 +1951,15 @@ class LNWallet(LNWorker): can_receive += c.available_to_spend(REMOTE) return Decimal(can_receive) / 1000 + def num_sats_can_receive_no_mpp(self) -> Decimal: + with self.lock: + if self.channels: + can_receive = max([ + c.available_to_spend(REMOTE) for c in self.channels.values() + if c.is_active() and not c.is_frozen_for_receiving() + ]) + return Decimal(can_receive) / 1000 + def can_pay_invoice(self, invoice: LNInvoice) -> bool: return invoice.get_amount_sat() <= self.num_sats_can_send() diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index 24fb4e248..5b469ab2a 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -225,6 +225,11 @@ class SwapManager(Logger): callback = lambda: self._claim_swap(swap) self.lnwatcher.add_callback(swap.lockup_address, callback) + def num_sats_can_receive(self): + # finding how to do MPP is too hard for sender, + # might result in our coins being locked + return self.lnworker.num_sats_can_receive_no_mpp() + async def normal_swap( self, *,