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.output_value = output_value
|
||||
self.tx = None # type: Optional[PartialTransaction]
|
||||
self.message = '' # set by side effect in RBF dialogs
|
||||
self.warning = '' # set by side effect
|
||||
self.messages = []
|
||||
self.error = '' # set by side effect
|
||||
|
||||
self.config = window.config
|
||||
@@ -95,11 +94,9 @@ class TxEditor(WindowModalDialog):
|
||||
|
||||
vbox.addLayout(top)
|
||||
vbox.addLayout(grid)
|
||||
vbox.addWidget(self.io_widget)
|
||||
self.message_label = WWLabel('')
|
||||
vbox.addWidget(self.message_label)
|
||||
vbox.addWidget(self.io_widget)
|
||||
self.warning_label = WWLabel('')
|
||||
vbox.addWidget(self.warning_label)
|
||||
|
||||
buttons = self.create_buttons_bar()
|
||||
vbox.addStretch(1)
|
||||
@@ -227,7 +224,7 @@ class TxEditor(WindowModalDialog):
|
||||
def trigger_update(self):
|
||||
# set tx to None so that the ok button is disabled while we compute the new tx
|
||||
self.tx = None
|
||||
self.message = ''
|
||||
self.messages = []
|
||||
self.error = ''
|
||||
self._update_widgets()
|
||||
self.needs_update = True
|
||||
@@ -523,6 +520,7 @@ class TxEditor(WindowModalDialog):
|
||||
self.accept()
|
||||
|
||||
def _update_widgets(self):
|
||||
# side effect: self.error
|
||||
self._update_amount_label()
|
||||
if self.not_enough_funds:
|
||||
self.error = _('Not enough funds.')
|
||||
@@ -533,15 +531,13 @@ class TxEditor(WindowModalDialog):
|
||||
self.error += ' ' + _('You need to set a lower fee.')
|
||||
else:
|
||||
self.error += ''
|
||||
else:
|
||||
self.error = ''
|
||||
if not self.tx:
|
||||
if self.not_enough_funds:
|
||||
self.io_widget.update(None)
|
||||
self.set_feerounding_visibility(False)
|
||||
self.warning = ''
|
||||
self.messages = []
|
||||
else:
|
||||
self.check_warnings()
|
||||
self.messages = self.get_messages()
|
||||
self.update_fee_fields()
|
||||
if self.locktime_e.get_locktime() is None:
|
||||
self.locktime_e.set_locktime(self.tx.locktime)
|
||||
@@ -551,11 +547,10 @@ class TxEditor(WindowModalDialog):
|
||||
|
||||
self._update_send_button()
|
||||
self._update_message()
|
||||
self._update_warning()
|
||||
|
||||
def check_warnings(self):
|
||||
# side effects: self.error, self.warning
|
||||
warnings = []
|
||||
def get_messages(self):
|
||||
# side effect: self.error
|
||||
messages = []
|
||||
fee = self.tx.get_fee()
|
||||
assert fee is not None
|
||||
amount = self.tx.output_value() if self.output_value == '!' else self.output_value
|
||||
@@ -567,19 +562,19 @@ class TxEditor(WindowModalDialog):
|
||||
if not allow_send:
|
||||
self.error = long_warning
|
||||
else:
|
||||
warnings.append(long_warning)
|
||||
messages.append(long_warning)
|
||||
# warn if spending unconf
|
||||
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
|
||||
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
|
||||
num_change = sum(int(o.is_change) for o in self.tx.outputs())
|
||||
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
|
||||
self.warning = _('Warning') + ': ' + '\n'.join(warnings) if warnings else ''
|
||||
return messages
|
||||
|
||||
def set_locktime(self):
|
||||
if not self.tx:
|
||||
@@ -595,14 +590,10 @@ class TxEditor(WindowModalDialog):
|
||||
pass
|
||||
|
||||
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
|
||||
self.warning_label.setStyleSheet(style.as_stylesheet())
|
||||
self.warning_label.setText(self.error or self.warning)
|
||||
message_str = '\n'.join(self.messages) if self.messages else ''
|
||||
self.message_label.setStyleSheet(style.as_stylesheet())
|
||||
self.message_label.setText(self.error or message_str)
|
||||
|
||||
def _update_send_button(self):
|
||||
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.not_enough_funds = False
|
||||
self.no_dynfee_estimates = False
|
||||
error = ''
|
||||
message = ''
|
||||
except NotEnoughFunds:
|
||||
self.not_enough_funds = True
|
||||
self.tx = None
|
||||
|
||||
@@ -126,14 +126,18 @@ class _BaseRBFDialog(TxEditor):
|
||||
except CannotBumpFee as e:
|
||||
self.tx = None
|
||||
self.error = str(e)
|
||||
|
||||
def get_messages(self):
|
||||
messages = super().get_messages()
|
||||
if not self.tx:
|
||||
return
|
||||
delta = self.tx.get_fee() - self.old_tx.get_fee()
|
||||
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:
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user