URI: 
       tMerge pull request #6968 from HardCorePawn/issue6664 - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 0ce6adffcc271a8e31705e16b7b24a0e93a80ca0
   DIR parent f9f49daad7a9eee80abccd49ce43bee07258fd99
  HTML Author: ghost43 <somber.night@protonmail.com>
       Date:   Sat, 27 Feb 2021 08:31:17 +0000
       
       Merge pull request #6968 from HardCorePawn/issue6664
       
       Added fiat fee estimate to Advanced Preview
       Diffstat:
         M electrum/gui/qt/main_window.py      |      26 ++++++++++++++++++--------
         M electrum/gui/qt/transaction_dialog… |      43 +++++++++++++++++++++++++++----
       
       2 files changed, 56 insertions(+), 13 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
       t@@ -873,18 +873,28 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
                self.payto_e.resolve()
                self.notify_transactions()
        
       -    def format_amount(self, x, is_diff=False, whitespaces=False):
       -        # x is in sats
       -        return self.config.format_amount(x, is_diff=is_diff, whitespaces=whitespaces)
       -
       -    def format_amount_and_units(self, amount):
       -        # amount is in sats
       -        text = self.config.format_amount_and_units(amount)
       -        x = self.fx.format_amount_and_units(amount) if self.fx else None
       +    def format_amount(self, amount_sat, is_diff=False, whitespaces=False) -> str:
       +        """Formats amount as string, converting to desired unit.
       +        E.g. 500_000 -> '0.005'
       +        """
       +        return self.config.format_amount(amount_sat, is_diff=is_diff, whitespaces=whitespaces)
       +
       +    def format_amount_and_units(self, amount_sat) -> str:
       +        """Returns string with both bitcoin and fiat amounts, in desired units.
       +        E.g. 500_000 -> '0.005 BTC (191.42 EUR)'
       +        """
       +        text = self.config.format_amount_and_units(amount_sat)
       +        x = self.fx.format_amount_and_units(amount_sat) if self.fx else None
                if text and x:
                    text += ' (%s)'%x
                return text
        
       +    def format_fiat_and_units(self, amount_sat) -> str:
       +        """Returns string of FX fiat amount, in desired units.
       +        E.g. 500_000 -> '191.42 EUR'
       +        """
       +        return self.fx.format_amount_and_units(amount_sat) if self.fx else ''
       +
            def format_fee_rate(self, fee_rate):
                return self.config.format_fee_rate(fee_rate)
        
   DIR diff --git a/electrum/gui/qt/transaction_dialog.py b/electrum/gui/qt/transaction_dialog.py
       t@@ -68,6 +68,9 @@ class TxSizeLabel(QLabel):
            def setAmount(self, byte_size):
                self.setText(('x   %s bytes   =' % byte_size) if byte_size else '')
        
       +class TxFiatLabel(QLabel):
       +    def setAmount(self, fiat_fee):
       +        self.setText(('≈  %s' % fiat_fee) if fiat_fee else '')
        
        class QTextEditWithDefaultSize(QTextEdit):
            def sizeHint(self):
       t@@ -429,12 +432,18 @@ class BaseTxDialog(QDialog, MessageBoxMixin):
                desc = self.desc
                base_unit = self.main_window.base_unit()
                format_amount = self.main_window.format_amount
       +        format_fiat_and_units = self.main_window.format_fiat_and_units
                tx_details = self.wallet.get_tx_info(self.tx)
                tx_mined_status = tx_details.tx_mined_status
                exp_n = tx_details.mempool_depth_bytes
                amount, fee = tx_details.amount, tx_details.fee
                size = self.tx.estimated_size()
                txid = self.tx.txid()
       +        fx = self.main_window.fx
       +        tx_item_fiat = None
       +        if (self.finalized  # ensures we don't use historical rates for tx being constructed *now*
       +                and txid is not None and fx.is_enabled() and amount is not None):
       +            tx_item_fiat = self.wallet.get_tx_item_fiat(txid, abs(amount), fx, fee)
                lnworker_history = self.wallet.lnworker.get_onchain_history() if self.wallet.lnworker else {}
                if txid in lnworker_history:
                    item = lnworker_history[txid]
       t@@ -491,16 +500,31 @@ class BaseTxDialog(QDialog, MessageBoxMixin):
                    amount_str = _("Transaction unrelated to your wallet")
                elif amount is None:
                    amount_str = ''
       -        elif amount > 0:
       -            amount_str = _("Amount received:") + ' %s'% format_amount(amount) + ' ' + base_unit
                else:
       -            amount_str = _("Amount sent:") + ' %s'% format_amount(-amount) + ' ' + base_unit
       +            if amount > 0:
       +                amount_str = _("Amount received:") + ' %s'% format_amount(amount) + ' ' + base_unit
       +            else:
       +                amount_str = _("Amount sent:") + ' %s' % format_amount(-amount) + ' ' + base_unit
       +            if fx.is_enabled():
       +                if tx_item_fiat:
       +                    amount_str += ' (%s)' % tx_item_fiat['fiat_value'].to_ui_string()
       +                else:
       +                    amount_str += ' (%s)' % format_fiat_and_units(abs(amount))
                if amount_str:
                    self.amount_label.setText(amount_str)
                else:
                    self.amount_label.hide()
                size_str = _("Size:") + ' %d bytes'% size
       -        fee_str = _("Fee") + ': %s' % (format_amount(fee) + ' ' + base_unit if fee is not None else _('unknown'))
       +        if fee is None:
       +            fee_str = _("Fee") + ': ' + _("unknown")
       +        else:
       +            fee_str = _("Fee") + f': {format_amount(fee)} {base_unit}'
       +            if fx.is_enabled():
       +                if tx_item_fiat:
       +                    fiat_fee_str = tx_item_fiat['fiat_fee'].to_ui_string()
       +                else:
       +                    fiat_fee_str = format_fiat_and_units(fee)
       +                fee_str += f' ({fiat_fee_str})'
                if fee is not None:
                    fee_rate = Decimal(fee) / size  # sat/byte
                    fee_str += '  ( %s ) ' % self.main_window.format_fee_rate(fee_rate * 1000)
       t@@ -529,7 +553,8 @@ class BaseTxDialog(QDialog, MessageBoxMixin):
                    ln_amount_str = ''
                elif ln_amount > 0:
                    ln_amount_str = _('Amount received in channels') + ': ' + format_amount(ln_amount) + ' ' + base_unit
       -        elif ln_amount < 0:
       +        else:
       +            assert ln_amount < 0, f"{ln_amount!r}"
                    ln_amount_str = _('Amount withdrawn from channels') + ': ' + format_amount(-ln_amount) + ' ' + base_unit
                if ln_amount_str:
                    self.ln_amount_label.setText(ln_amount_str)
       t@@ -783,6 +808,11 @@ class PreviewTxDialog(BaseTxDialog, TxEditor):
                self.size_e.setAmount(0)
                self.size_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
        
       +        self.fiat_fee_label = TxFiatLabel()
       +        self.fiat_fee_label.setAlignment(Qt.AlignCenter)
       +        self.fiat_fee_label.setAmount(0)
       +        self.fiat_fee_label.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet())
       +
                self.feerate_e = FeerateEdit(lambda: 0)
                self.feerate_e.setAmount(self.config.fee_per_byte())
                self.feerate_e.textEdited.connect(partial(self.on_fee_or_feerate, self.feerate_e, False))
       t@@ -823,6 +853,7 @@ class PreviewTxDialog(BaseTxDialog, TxEditor):
                grid.addWidget(self.size_e, 0, 2)
                grid.addWidget(self.fee_e, 0, 3)
                grid.addWidget(self.feerounding_icon, 0, 4)
       +        grid.addWidget(self.fiat_fee_label, 0, 5)
                grid.addWidget(self.fee_slider, 1, 1)
                grid.addWidget(self.fee_combo, 1, 2)
                hbox.addLayout(grid)
       t@@ -916,6 +947,8 @@ class PreviewTxDialog(BaseTxDialog, TxEditor):
                fee = tx.get_fee()
        
                self.size_e.setAmount(size)
       +        fiat_fee = self.main_window.format_fiat_and_units(fee)
       +        self.fiat_fee_label.setAmount(fiat_fee)
        
                # Displayed fee/fee_rate values are set according to user input.
                # Due to rounding or dropping dust in CoinChooser,