tswaps: add safeguards to gui - electrum - Electrum Bitcoin wallet
 (HTM) git clone https://git.parazyd.org/electrum
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
       ---
 (DIR) commit e6e6103434b26486661e61345fc35eba1544ad7f
 (DIR) parent 5fa09970b65ed0480a7040621c73afab3cabcd77
 (HTM) Author: ThomasV <thomasv@electrum.org>
       Date:   Thu, 28 May 2020 15:03:57 +0200
       
       swaps: add safeguards to gui
       
       Diffstat:
         M electrum/gui/qt/swap_dialog.py      |      34 ++++++++++++++++++++++++-------
       
       1 file changed, 27 insertions(+), 7 deletions(-)
       ---
 (DIR) diff --git a/electrum/gui/qt/swap_dialog.py b/electrum/gui/qt/swap_dialog.py
       t@@ -20,6 +20,12 @@ from .fee_slider import FeeSlider, FeeComboBox
        import asyncio
        from .util import read_QIcon
        
       +CANNOT_RECEIVE_WARNING = """
       +The requested amount is higher than what you can receive in your currently open channels.
       +If you continue, your funds will be locked until the remote server can find a path to pay you.
       +If the swap cannot be performed after 24h, you will be refunded.
       +Do you want to continue?
       +"""
        
        class SwapDialog(WindowModalDialog):
        
       t@@ -27,7 +33,8 @@ class SwapDialog(WindowModalDialog):
                WindowModalDialog.__init__(self, window, _('Submarine Swap'))
                self.window = window
                self.config = window.config
       -        self.swap_manager = self.window.wallet.lnworker.swap_manager
       +        self.lnworker = self.window.wallet.lnworker
       +        self.swap_manager = self.lnworker.swap_manager
                self.network = window.network
                vbox = QVBoxLayout(self)
                vbox.addWidget(WWLabel('Swap lightning funds for on-chain funds if you need to increase your receiving capacity. This service is powered by the Boltz backend.'))
       t@@ -92,9 +99,12 @@ class SwapDialog(WindowModalDialog):
                if self.send_amount_e.follows:
                    return
                self.send_amount_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
       -        amount = self.send_amount_e.get_amount()
       +        send_amount = self.send_amount_e.get_amount()
       +        recv_amount = self.swap_manager.get_recv_amount(send_amount, self.is_reverse)
       +        if self.is_reverse and send_amount and send_amount > self.lnworker.num_sats_can_send():
       +            recv_amount = None
                self.recv_amount_e.follows = True
       -        self.recv_amount_e.setAmount(self.swap_manager.get_recv_amount(amount, self.is_reverse))
       +        self.recv_amount_e.setAmount(recv_amount)
                self.recv_amount_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
                self.recv_amount_e.follows = False
                self.send_follows = False
       t@@ -103,9 +113,12 @@ class SwapDialog(WindowModalDialog):
                if self.recv_amount_e.follows:
                    return
                self.recv_amount_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
       -        amount = self.recv_amount_e.get_amount()
       +        recv_amount = self.recv_amount_e.get_amount()
       +        send_amount = self.swap_manager.get_send_amount(recv_amount, self.is_reverse)
       +        if self.is_reverse and send_amount and send_amount > self.lnworker.num_sats_can_send():
       +            send_amount = None
                self.send_amount_e.follows = True
       -        self.send_amount_e.setAmount(self.swap_manager.get_send_amount(amount, self.is_reverse))
       +        self.send_amount_e.setAmount(send_amount)
                self.send_amount_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
                self.send_amount_e.follows = False
                self.send_follows = True
       t@@ -124,12 +137,19 @@ class SwapDialog(WindowModalDialog):
                    return
                if self.is_reverse:
                    lightning_amount = self.send_amount_e.get_amount()
       -            onchain_amount = self.recv_amount_e.get_amount() + self.swap_manager.get_claim_fee()
       -            coro = self.swap_manager.reverse_swap(lightning_amount, onchain_amount)
       +            onchain_amount = self.recv_amount_e.get_amount()
       +            if lightning_amount is None or onchain_amount is None:
       +                return
       +            coro = self.swap_manager.reverse_swap(lightning_amount, onchain_amount + self.swap_manager.get_claim_fee())
                    self.window.run_coroutine_from_thread(coro)
                else:
                    lightning_amount = self.recv_amount_e.get_amount()
                    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 not self.window.question(CANNOT_RECEIVE_WARNING):
       +                    return
                    self.window.protect(self.do_normal_swap, (lightning_amount, onchain_amount))
        
            def do_normal_swap(self, lightning_amount, onchain_amount, password):