confirm_tx_dialog: simplify messages and warnings. reduce the use of side effects
This commit is contained in:
@@ -67,8 +67,7 @@ class TxEditor(WindowModalDialog):
|
|||||||
self.make_tx = make_tx
|
self.make_tx = make_tx
|
||||||
self.output_value = output_value
|
self.output_value = output_value
|
||||||
self.tx = None # type: Optional[PartialTransaction]
|
self.tx = None # type: Optional[PartialTransaction]
|
||||||
self.message = '' # set by side effect in RBF dialogs
|
self.messages = []
|
||||||
self.warning = '' # set by side effect
|
|
||||||
self.error = '' # set by side effect
|
self.error = '' # set by side effect
|
||||||
|
|
||||||
self.config = window.config
|
self.config = window.config
|
||||||
@@ -95,11 +94,9 @@ class TxEditor(WindowModalDialog):
|
|||||||
|
|
||||||
vbox.addLayout(top)
|
vbox.addLayout(top)
|
||||||
vbox.addLayout(grid)
|
vbox.addLayout(grid)
|
||||||
|
vbox.addWidget(self.io_widget)
|
||||||
self.message_label = WWLabel('')
|
self.message_label = WWLabel('')
|
||||||
vbox.addWidget(self.message_label)
|
vbox.addWidget(self.message_label)
|
||||||
vbox.addWidget(self.io_widget)
|
|
||||||
self.warning_label = WWLabel('')
|
|
||||||
vbox.addWidget(self.warning_label)
|
|
||||||
|
|
||||||
buttons = self.create_buttons_bar()
|
buttons = self.create_buttons_bar()
|
||||||
vbox.addStretch(1)
|
vbox.addStretch(1)
|
||||||
@@ -227,7 +224,7 @@ class TxEditor(WindowModalDialog):
|
|||||||
def trigger_update(self):
|
def trigger_update(self):
|
||||||
# set tx to None so that the ok button is disabled while we compute the new tx
|
# set tx to None so that the ok button is disabled while we compute the new tx
|
||||||
self.tx = None
|
self.tx = None
|
||||||
self.message = ''
|
self.messages = []
|
||||||
self.error = ''
|
self.error = ''
|
||||||
self._update_widgets()
|
self._update_widgets()
|
||||||
self.needs_update = True
|
self.needs_update = True
|
||||||
@@ -523,6 +520,7 @@ class TxEditor(WindowModalDialog):
|
|||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
def _update_widgets(self):
|
def _update_widgets(self):
|
||||||
|
# side effect: self.error
|
||||||
self._update_amount_label()
|
self._update_amount_label()
|
||||||
if self.not_enough_funds:
|
if self.not_enough_funds:
|
||||||
self.error = _('Not enough funds.')
|
self.error = _('Not enough funds.')
|
||||||
@@ -533,15 +531,13 @@ class TxEditor(WindowModalDialog):
|
|||||||
self.error += ' ' + _('You need to set a lower fee.')
|
self.error += ' ' + _('You need to set a lower fee.')
|
||||||
else:
|
else:
|
||||||
self.error += ''
|
self.error += ''
|
||||||
else:
|
|
||||||
self.error = ''
|
|
||||||
if not self.tx:
|
if not self.tx:
|
||||||
if self.not_enough_funds:
|
if self.not_enough_funds:
|
||||||
self.io_widget.update(None)
|
self.io_widget.update(None)
|
||||||
self.set_feerounding_visibility(False)
|
self.set_feerounding_visibility(False)
|
||||||
self.warning = ''
|
self.messages = []
|
||||||
else:
|
else:
|
||||||
self.check_warnings()
|
self.messages = self.get_messages()
|
||||||
self.update_fee_fields()
|
self.update_fee_fields()
|
||||||
if self.locktime_e.get_locktime() is None:
|
if self.locktime_e.get_locktime() is None:
|
||||||
self.locktime_e.set_locktime(self.tx.locktime)
|
self.locktime_e.set_locktime(self.tx.locktime)
|
||||||
@@ -551,11 +547,10 @@ class TxEditor(WindowModalDialog):
|
|||||||
|
|
||||||
self._update_send_button()
|
self._update_send_button()
|
||||||
self._update_message()
|
self._update_message()
|
||||||
self._update_warning()
|
|
||||||
|
|
||||||
def check_warnings(self):
|
def get_messages(self):
|
||||||
# side effects: self.error, self.warning
|
# side effect: self.error
|
||||||
warnings = []
|
messages = []
|
||||||
fee = self.tx.get_fee()
|
fee = self.tx.get_fee()
|
||||||
assert fee is not None
|
assert fee is not None
|
||||||
amount = self.tx.output_value() if self.output_value == '!' else self.output_value
|
amount = self.tx.output_value() if self.output_value == '!' else self.output_value
|
||||||
@@ -567,19 +562,19 @@ class TxEditor(WindowModalDialog):
|
|||||||
if not allow_send:
|
if not allow_send:
|
||||||
self.error = long_warning
|
self.error = long_warning
|
||||||
else:
|
else:
|
||||||
warnings.append(long_warning)
|
messages.append(long_warning)
|
||||||
# warn if spending unconf
|
# warn if spending unconf
|
||||||
if any((txin.block_height is not None and txin.block_height<=0) for txin in self.tx.inputs()):
|
if any((txin.block_height is not None and txin.block_height<=0) for txin in self.tx.inputs()):
|
||||||
warnings.append(_('This transaction will spend unconfirmed coins.'))
|
messages.append(_('This transaction will spend unconfirmed coins.'))
|
||||||
# warn if we merge from mempool
|
# warn if we merge from mempool
|
||||||
if self.tx.rbf_merge_txid:
|
if self.tx.rbf_merge_txid:
|
||||||
warnings.append(_('This payment will be merged with another existing transaction.'))
|
messages.append(_('This payment will be merged with another existing transaction.'))
|
||||||
# warn if we use multiple change outputs
|
# warn if we use multiple change outputs
|
||||||
num_change = sum(int(o.is_change) for o in self.tx.outputs())
|
num_change = sum(int(o.is_change) for o in self.tx.outputs())
|
||||||
if num_change > 1:
|
if num_change > 1:
|
||||||
warnings.append(_('This transaction has {} change outputs.'.format(num_change)))
|
messages.append(_('This transaction has {} change outputs.'.format(num_change)))
|
||||||
# TODO: warn if we send change back to input address
|
# TODO: warn if we send change back to input address
|
||||||
self.warning = _('Warning') + ': ' + '\n'.join(warnings) if warnings else ''
|
return messages
|
||||||
|
|
||||||
def set_locktime(self):
|
def set_locktime(self):
|
||||||
if not self.tx:
|
if not self.tx:
|
||||||
@@ -595,14 +590,10 @@ class TxEditor(WindowModalDialog):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def _update_message(self):
|
def _update_message(self):
|
||||||
style = ColorScheme.BLUE
|
|
||||||
self.message_label.setStyleSheet(style.as_stylesheet())
|
|
||||||
self.message_label.setText(self.message)
|
|
||||||
|
|
||||||
def _update_warning(self):
|
|
||||||
style = ColorScheme.RED if self.error else ColorScheme.BLUE
|
style = ColorScheme.RED if self.error else ColorScheme.BLUE
|
||||||
self.warning_label.setStyleSheet(style.as_stylesheet())
|
message_str = '\n'.join(self.messages) if self.messages else ''
|
||||||
self.warning_label.setText(self.error or self.warning)
|
self.message_label.setStyleSheet(style.as_stylesheet())
|
||||||
|
self.message_label.setText(self.error or message_str)
|
||||||
|
|
||||||
def _update_send_button(self):
|
def _update_send_button(self):
|
||||||
enabled = bool(self.tx) and not self.error
|
enabled = bool(self.tx) and not self.error
|
||||||
@@ -645,8 +636,6 @@ class ConfirmTxDialog(TxEditor):
|
|||||||
self.tx = self.make_tx(fee_estimator, confirmed_only=confirmed_only)
|
self.tx = self.make_tx(fee_estimator, confirmed_only=confirmed_only)
|
||||||
self.not_enough_funds = False
|
self.not_enough_funds = False
|
||||||
self.no_dynfee_estimates = False
|
self.no_dynfee_estimates = False
|
||||||
error = ''
|
|
||||||
message = ''
|
|
||||||
except NotEnoughFunds:
|
except NotEnoughFunds:
|
||||||
self.not_enough_funds = True
|
self.not_enough_funds = True
|
||||||
self.tx = None
|
self.tx = None
|
||||||
|
|||||||
@@ -126,14 +126,18 @@ class _BaseRBFDialog(TxEditor):
|
|||||||
except CannotBumpFee as e:
|
except CannotBumpFee as e:
|
||||||
self.tx = None
|
self.tx = None
|
||||||
self.error = str(e)
|
self.error = str(e)
|
||||||
|
|
||||||
|
def get_messages(self):
|
||||||
|
messages = super().get_messages()
|
||||||
if not self.tx:
|
if not self.tx:
|
||||||
return
|
return
|
||||||
delta = self.tx.get_fee() - self.old_tx.get_fee()
|
delta = self.tx.get_fee() - self.old_tx.get_fee()
|
||||||
if not self.is_decrease_payment():
|
if not self.is_decrease_payment():
|
||||||
self.message = _("You will pay {} more.").format(self.main_window.format_amount_and_units(delta))
|
msg = _("You will pay {} more.").format(self.main_window.format_amount_and_units(delta))
|
||||||
else:
|
else:
|
||||||
self.message = _("The recipient will receive {} less.").format(self.main_window.format_amount_and_units(delta))
|
msg = _("The recipient will receive {} less.").format(self.main_window.format_amount_and_units(delta))
|
||||||
|
messages.insert(0, msg)
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
class BumpFeeDialog(_BaseRBFDialog):
|
class BumpFeeDialog(_BaseRBFDialog):
|
||||||
|
|||||||
Reference in New Issue
Block a user