Browse Source

qt swap dialog: attempt at clearer logic (at the cost of more lines)

fixes #6853
patch-4
SomberNight 4 years ago
parent
commit
ede9b4382a
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 57
      electrum/gui/qt/swap_dialog.py

57
electrum/gui/qt/swap_dialog.py

@ -104,6 +104,7 @@ class SwapDialog(WindowModalDialog):
self.is_reverse = not self.is_reverse
self.send_amount_e.setAmount(None)
self.recv_amount_e.setAmount(None)
self.max_button.setChecked(False)
self.update()
def spend_max(self):
@ -115,19 +116,20 @@ class SwapDialog(WindowModalDialog):
else:
self.send_amount_e.setAmount(None)
self.update_fee()
self.update_ok_button()
def uncheck_max(self):
self.max_button.setChecked(False)
self.update()
def _spend_max_forward_swap(self):
self.update_tx('!')
self._update_tx('!')
if self.tx:
amount = self.tx.output_value_for_address(ln_dummy_address())
max_amt = self.swap_manager.get_max_amount()
if amount > max_amt:
amount = max_amt
self.update_tx(amount)
self._update_tx(amount)
if self.tx:
amount = self.tx.output_value_for_address(ln_dummy_address())
assert amount <= max_amt
@ -155,8 +157,9 @@ class SwapDialog(WindowModalDialog):
self.recv_amount_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
self.recv_amount_e.follows = False
self.send_follows = False
self.ok_button.setEnabled((recv_amount is not None)
and (self.tx is not None or self.is_reverse))
self._update_tx(send_amount)
self.update_fee()
self.update_ok_button()
def on_recv_edited(self):
if self.recv_amount_e.follows:
@ -171,8 +174,9 @@ class SwapDialog(WindowModalDialog):
self.send_amount_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet())
self.send_amount_e.follows = False
self.send_follows = True
self.ok_button.setEnabled((send_amount is not None)
and (self.tx is not None or self.is_reverse))
self._update_tx(send_amount)
self.update_fee()
self.update_ok_button()
def update(self):
sm = self.swap_manager
@ -186,25 +190,17 @@ class SwapDialog(WindowModalDialog):
server_fee_str = '%.2f'%sm.percentage + '% + ' + self.window.format_amount(server_mining_fee) + ' ' + self.window.base_unit()
self.server_fee_label.setText(server_fee_str)
self.server_fee_label.repaint() # macOS hack for #6269
self.update_tx()
self.update_fee()
self.update_ok_button()
def update_fee(self):
self.tx = None
is_max = self.max_button.isChecked()
"""Updates self.fee_label. No other side-effects."""
if self.is_reverse:
if is_max:
self._spend_max_reverse_swap()
sm = self.swap_manager
fee = sm.get_claim_fee()
else:
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 self.tx is None:
self.ok_button.setEnabled(False)
fee_text = self.window.format_amount(fee) + ' ' + self.window.base_unit() if fee else ''
self.fee_label.setText(fee_text)
self.fee_label.repaint() # macOS hack for #6269
@ -233,10 +229,22 @@ class SwapDialog(WindowModalDialog):
return
self.window.protect(self.do_normal_swap, (lightning_amount, onchain_amount))
def update_tx(self, onchain_amount):
def update_tx(self):
if self.is_reverse:
return
is_max = self.max_button.isChecked()
if is_max:
self._spend_max_forward_swap()
else:
onchain_amount = self.send_amount_e.get_amount()
self._update_tx(onchain_amount)
def _update_tx(self, onchain_amount):
"""Updates self.tx. No other side-effects."""
if self.is_reverse:
return
if onchain_amount is None:
self.tx = None
self.ok_button.setEnabled(False)
return
outputs = [PartialTxOutput.from_address_and_value(ln_dummy_address(), onchain_amount)]
coins = self.window.get_coins()
@ -246,7 +254,16 @@ class SwapDialog(WindowModalDialog):
outputs=outputs)
except (NotEnoughFunds, NoDynamicFeeEstimates) as e:
self.tx = None
self.ok_button.setEnabled(False)
def update_ok_button(self):
"""Updates self.ok_button. No other side-effects."""
send_amount = self.send_amount_e.get_amount()
recv_amount = self.recv_amount_e.get_amount()
self.ok_button.setEnabled(
(send_amount is not None)
and (recv_amount is not None)
and (self.tx is not None or self.is_reverse)
)
def do_normal_swap(self, lightning_amount, onchain_amount, password):
tx = self.tx

Loading…
Cancel
Save