Browse Source

qt swap_dialog: "max" button now respects max htlc value

bip39-recovery
SomberNight 5 years ago
parent
commit
7570c8c1c6
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 35
      electrum/gui/qt/swap_dialog.py
  2. 5
      electrum/submarine_swaps.py

35
electrum/gui/qt/swap_dialog.py

@ -1,10 +1,11 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
from PyQt5.QtWidgets import QLabel, QVBoxLayout, QGridLayout, QPushButton
from electrum.i18n import _
from electrum.lnutil import ln_dummy_address
from electrum.transaction import PartialTxOutput
from electrum.transaction import PartialTxOutput, PartialTransaction
from electrum.submarine_swaps import SWAP_MAX_VALUE_SAT
from .util import (WindowModalDialog, Buttons, OkButton, CancelButton,
EnterButton, ColorScheme, WWLabel, read_QIcon)
@ -24,6 +25,8 @@ Do you want to continue?
class SwapDialog(WindowModalDialog):
tx: Optional[PartialTransaction]
def __init__(self, window: 'ElectrumWindow'):
WindowModalDialog.__init__(self, window, _('Submarine Swap'))
self.window = window
@ -31,6 +34,7 @@ class SwapDialog(WindowModalDialog):
self.lnworker = self.window.wallet.lnworker
self.swap_manager = self.lnworker.swap_manager
self.network = window.network
self.tx = None
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.'))
self.send_amount_e = BTCAmountEdit(self.window.get_decimal_point)
@ -99,15 +103,24 @@ class SwapDialog(WindowModalDialog):
if self.is_reverse:
return
if self.max_button.isChecked():
self.update_tx('!')
if self.tx:
txo = self.tx.outputs()[0]
self.send_amount_e.setAmount(txo.value)
self._spend_max_forward_swap()
else:
self.tx = None
self.send_amount_e.setAmount(None)
self.update_fee()
def _spend_max_forward_swap(self):
self.update_tx('!')
if self.tx:
amount = self.tx.output_value_for_address(ln_dummy_address())
if amount > SWAP_MAX_VALUE_SAT:
amount = SWAP_MAX_VALUE_SAT
self.update_tx(amount)
if self.tx:
amount = self.tx.output_value_for_address(ln_dummy_address())
assert amount <= SWAP_MAX_VALUE_SAT
self.send_amount_e.setAmount(amount)
def on_send_edited(self):
if self.send_amount_e.follows:
return
@ -154,12 +167,12 @@ class SwapDialog(WindowModalDialog):
fee = sm.get_claim_fee()
else:
is_max = self.max_button.isChecked()
onchain_amount = '!' if is_max else self.send_amount_e.get_amount()
self.update_tx(onchain_amount)
if is_max:
self._spend_max_forward_swap()
else:
onchain_amount = self.send_amount_e.get_amount()
self.update_tx(onchain_amount)
fee = self.tx.get_fee() if self.tx else None
if is_max and self.tx:
txo = self.tx.outputs()[0]
self.send_amount_e.setAmount(txo.value)
fee_text = self.window.format_amount(fee) + ' ' + self.window.base_unit() if fee else ''
self.fee_label.setText(fee_text)

5
electrum/submarine_swaps.py

@ -11,7 +11,7 @@ from .bitcoin import address_to_script, script_to_p2wsh, redeem_script_to_addres
from .transaction import TxOutpoint, PartialTxInput, PartialTxOutput, PartialTransaction, construct_witness
from .transaction import script_GetOp, match_script_against_template, OPPushDataGeneric, OPPushDataPubkey
from .util import log_exceptions
from .lnutil import REDEEM_AFTER_DOUBLE_SPENT_DELAY, ln_dummy_address
from .lnutil import REDEEM_AFTER_DOUBLE_SPENT_DELAY, ln_dummy_address, LN_MAX_HTLC_VALUE_MSAT
from .bitcoin import dust_threshold
from .logging import Logger
from .lnutil import hex_to_bytes
@ -25,6 +25,9 @@ if TYPE_CHECKING:
API_URL = 'https://lightning.electrum.org/api'
SWAP_MAX_VALUE_SAT = LN_MAX_HTLC_VALUE_MSAT // 1000
WITNESS_TEMPLATE_SWAP = [
opcodes.OP_HASH160,
OPPushDataGeneric(lambda x: x == 20),

Loading…
Cancel
Save