From a9a8ed2eb490218bb0e10bfe12f15328a6e3139b Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 3 Feb 2024 05:13:09 +0000 Subject: [PATCH] follow-up: factor out more hardcoded "sat/byte" and "sat/b" strings - rename globals - also rm hardcoded strings from qml - use consistent unit names in qml (previously mixed sat/vB and sat/byte (latter coming from core lib)) --- electrum/commands.py | 4 ++-- electrum/gui/qml/components/ConfirmTxDialog.qml | 2 +- electrum/gui/qml/components/CpfpBumpFeeDialog.qml | 4 ++-- electrum/gui/qml/components/NetworkOverview.qml | 9 ++++++--- electrum/gui/qml/components/RbfBumpFeeDialog.qml | 4 ++-- electrum/gui/qml/components/RbfCancelDialog.qml | 4 ++-- electrum/gui/qml/qeapp.py | 7 +++++++ electrum/gui/qt/amountedit.py | 4 ++-- electrum/gui/qt/main_window.py | 4 ++-- electrum/gui/qt/transaction_dialog.py | 4 ++-- electrum/simple_config.py | 6 +++--- electrum/util.py | 6 ++++-- electrum/wallet.py | 2 +- 13 files changed, 36 insertions(+), 24 deletions(-) diff --git a/electrum/commands.py b/electrum/commands.py index adc7b3a36..a0cbce7e6 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -1428,7 +1428,7 @@ command_options = { 'nocheck': (None, "Do not verify aliases"), 'imax': (None, "Maximum number of inputs"), 'fee': ("-f", "Transaction fee (absolute, in BTC)"), - 'feerate': (None, f"Transaction fee rate (in {util.FEERATE_UI_NAME_SAT_PER_VBYTE})"), + 'feerate': (None, f"Transaction fee rate (in {util.UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE})"), 'from_addr': ("-F", "Source address (must be a wallet address; use sweep to spend from non-wallet address)."), 'from_coins': (None, "Source coins (must be in wallet; use sweep to spend from non-wallet address)."), 'change_addr': ("-c", "Change address. Default is a spare address, or the source address if it's not in the wallet"), @@ -1461,7 +1461,7 @@ command_options = { 'iknowwhatimdoing': (None, "Acknowledge that I understand the full implications of what I am about to do"), 'gossip': (None, "Apply command to gossip node instead of wallet"), 'connection_string': (None, "Lightning network node ID or network address"), - 'new_fee_rate': (None, f"The Updated/Increased Transaction fee rate (in {util.FEERATE_UI_NAME_SAT_PER_VBYTE})"), + 'new_fee_rate': (None, f"The Updated/Increased Transaction fee rate (in {util.UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE})"), 'from_amount': (None, "Amount to convert (default: 1)"), 'from_ccy': (None, "Currency to convert from"), 'to_ccy': (None, "Currency to convert to"), diff --git a/electrum/gui/qml/components/ConfirmTxDialog.qml b/electrum/gui/qml/components/ConfirmTxDialog.qml index 48cd3ed6f..aa6e02406 100644 --- a/electrum/gui/qml/components/ConfirmTxDialog.qml +++ b/electrum/gui/qml/components/ConfirmTxDialog.qml @@ -123,7 +123,7 @@ ElDialog { } Label { - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } diff --git a/electrum/gui/qml/components/CpfpBumpFeeDialog.qml b/electrum/gui/qml/components/CpfpBumpFeeDialog.qml index a7cf7a963..17ebd94cd 100644 --- a/electrum/gui/qml/components/CpfpBumpFeeDialog.qml +++ b/electrum/gui/qml/components/CpfpBumpFeeDialog.qml @@ -65,7 +65,7 @@ ElDialog { Label { Layout.preferredWidth: 1 Layout.fillWidth: true - text: qsTr('%1 bytes').arg(cpfpfeebumper.totalSize) + text: cpfpfeebumper.totalSize + ' ' + UI_UNIT_NAME.TXSIZE_VBYTES } Label { @@ -161,7 +161,7 @@ ElDialog { Label { visible: cpfpfeebumper.valid - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } diff --git a/electrum/gui/qml/components/NetworkOverview.qml b/electrum/gui/qml/components/NetworkOverview.qml index 1be1c7d5c..c6e9739eb 100644 --- a/electrum/gui/qml/components/NetworkOverview.qml +++ b/electrum/gui/qml/components/NetworkOverview.qml @@ -100,7 +100,10 @@ Pane { Layout.fillWidth: true height: parent.height color: Qt.hsva(2/3-(2/3*(Math.log(Math.min(600, modelData[0]))/Math.log(600))), 0.8, 1, 1) - ToolTip.text: modelData[0] + " sat/vB around depth " + (modelData[2]/1000000).toFixed(2) + " MB" + ToolTip.text: (qsTr("%1 around depth %2") + .arg(modelData[0] + " " + UI_UNIT_NAME.FEERATE_SAT_PER_VB) + .arg((modelData[2]/1000000).toFixed(2) + " " + UI_UNIT_NAME.MEMPOOL_MB) + ) ToolTip.visible: ma.containsMouse MouseArea { id: ma @@ -144,14 +147,14 @@ Pane { RowLayout { Layout.fillWidth: true Label { - text: '<-- ' + qsTr('%1 sat/vB').arg(Math.ceil(Network.feeHistogram.max_fee)) + text: '<-- ' + Math.ceil(Network.feeHistogram.max_fee) + " " + UI_UNIT_NAME.FEERATE_SAT_PER_VB font.pixelSize: constants.fontSizeXSmall color: Material.accentColor } Label { Layout.fillWidth: true horizontalAlignment: Text.AlignRight - text: qsTr('%1 sat/vB').arg(Math.floor(Network.feeHistogram.min_fee)) + ' -->' + text: Math.floor(Network.feeHistogram.min_fee) + " " + UI_UNIT_NAME.FEERATE_SAT_PER_VB + ' -->' font.pixelSize: constants.fontSizeXSmall color: Material.accentColor } diff --git a/electrum/gui/qml/components/RbfBumpFeeDialog.qml b/electrum/gui/qml/components/RbfBumpFeeDialog.qml index a66122d35..8c3b815c5 100644 --- a/electrum/gui/qml/components/RbfBumpFeeDialog.qml +++ b/electrum/gui/qml/components/RbfBumpFeeDialog.qml @@ -104,7 +104,7 @@ ElDialog { } Label { - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } @@ -133,7 +133,7 @@ ElDialog { Label { visible: rbffeebumper.valid - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } diff --git a/electrum/gui/qml/components/RbfCancelDialog.qml b/electrum/gui/qml/components/RbfCancelDialog.qml index 601890e73..b8b5934d4 100644 --- a/electrum/gui/qml/components/RbfCancelDialog.qml +++ b/electrum/gui/qml/components/RbfCancelDialog.qml @@ -67,7 +67,7 @@ ElDialog { } Label { - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } @@ -96,7 +96,7 @@ ElDialog { Label { visible: txcanceller.valid - text: 'sat/vB' + text: UI_UNIT_NAME.FEERATE_SAT_PER_VB color: Material.accentColor } } diff --git a/electrum/gui/qml/qeapp.py b/electrum/gui/qml/qeapp.py index c8a7e3847..158015648 100644 --- a/electrum/gui/qml/qeapp.py +++ b/electrum/gui/qml/qeapp.py @@ -12,6 +12,7 @@ from PyQt6.QtCore import (pyqtSlot, pyqtSignal, pyqtProperty, QObject, QT_VERSIO from PyQt6.QtGui import QGuiApplication, QFontDatabase, QScreen from PyQt6.QtQml import qmlRegisterType, qmlRegisterUncreatableType, QQmlApplicationEngine +import electrum from electrum import version, constants from electrum.i18n import _ from electrum.logging import Logger, get_logger @@ -417,6 +418,12 @@ class ElectrumQmlApplication(QGuiApplication): 'qt_version': QT_VERSION_STR, 'pyqt_version': PYQT_VERSION_STR }) + self.context.setContextProperty('UI_UNIT_NAME', { + "FEERATE_SAT_PER_VBYTE": electrum.util.UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE, + "FEERATE_SAT_PER_VB": electrum.util.UI_UNIT_NAME_FEERATE_SAT_PER_VB, + "TXSIZE_VBYTES": electrum.util.UI_UNIT_NAME_TXSIZE_VBYTES, + "MEMPOOL_MB": electrum.util.UI_UNIT_NAME_MEMPOOL_MB, + }) self.plugins.load_plugin('trustedcoin') diff --git a/electrum/gui/qt/amountedit.py b/electrum/gui/qt/amountedit.py index 3ada52917..e755152cf 100644 --- a/electrum/gui/qt/amountedit.py +++ b/electrum/gui/qt/amountedit.py @@ -10,7 +10,7 @@ from PyQt5.QtWidgets import (QLineEdit, QStyle, QStyleOptionFrame, QSizePolicy) from .util import char_width_in_lineedit, ColorScheme from electrum.util import (format_satoshis_plain, decimal_point_to_base_unit_name, - FEERATE_PRECISION, quantize_feerate, DECIMAL_POINT, FEERATE_UI_NAME_SAT_PER_VBYTE) + FEERATE_PRECISION, quantize_feerate, DECIMAL_POINT, UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE) from electrum.bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC _NOT_GIVEN = object() # sentinel value @@ -165,7 +165,7 @@ class FeerateEdit(BTCAmountEdit): self.extra_precision = FEERATE_PRECISION def _base_unit(self): - return FEERATE_UI_NAME_SAT_PER_VBYTE + return UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE def _get_amount_from_text(self, text): sat_per_byte_amount = super()._get_amount_from_text(text) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 3e132c9e2..311fc308c 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -56,7 +56,7 @@ from electrum.plugin import run_hook, BasePlugin from electrum.i18n import _ from electrum.util import (format_time, UserCancelled, profiler, bfh, InvalidPassword, UserFacingException, get_new_wallet_name, send_exception_to_crash_reporter, - AddTransactionException, os_chmod) + AddTransactionException, os_chmod, UI_UNIT_NAME_TXSIZE_VBYTES) from electrum.bip21 import BITCOIN_BIP21_URI_SCHEME from electrum.payment_identifier import PaymentIdentifier from electrum.invoices import PR_PAID, Invoice @@ -2566,7 +2566,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener): vbox.addWidget(WWLabel(msg2)) grid = QGridLayout() grid.addWidget(QLabel(_('Total size') + ':'), 0, 0) - grid.addWidget(QLabel('%d bytes'% total_size), 0, 1) + grid.addWidget(QLabel(f"{total_size} {UI_UNIT_NAME_TXSIZE_VBYTES}"), 0, 1) max_fee = new_tx.output_value() grid.addWidget(QLabel(_('Input amount') + ':'), 1, 0) grid.addWidget(QLabel(self.format_amount(max_fee) + ' ' + self.base_unit()), 1, 1) diff --git a/electrum/gui/qt/transaction_dialog.py b/electrum/gui/qt/transaction_dialog.py index bc492c942..304c94601 100644 --- a/electrum/gui/qt/transaction_dialog.py +++ b/electrum/gui/qt/transaction_dialog.py @@ -52,7 +52,7 @@ from electrum.plugin import run_hook from electrum import simple_config from electrum.transaction import SerializationError, Transaction, PartialTransaction, TxOutpoint, TxinDataFetchProgress from electrum.logging import get_logger -from electrum.util import ShortID, get_asyncio_loop +from electrum.util import ShortID, get_asyncio_loop, UI_UNIT_NAME_TXSIZE_VBYTES from electrum.network import Network from electrum.wallet import TxSighashRiskLevel, TxSighashDanger @@ -863,7 +863,7 @@ class TxDialog(QDialog, MessageBoxMixin): self.amount_label.setText(amount_str) else: self.amount_label.hide() - size_str = _("Size:") + ' %d bytes'% size + size_str = _("Size:") + f" {size} {UI_UNIT_NAME_TXSIZE_VBYTES}" if fee is None: if prog := self._fetch_txin_data_progress: if not prog.has_errored: diff --git a/electrum/simple_config.py b/electrum/simple_config.py index fbb034728..8b59aec35 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -590,7 +590,7 @@ class SimpleConfig(Logger): def get_depth_mb_str(self, depth: int) -> str: # e.g. 500_000 -> "0.50 MB" depth_mb = "{:.2f}".format(depth / 1_000_000) # maybe .rstrip("0") ? - return f"{depth_mb} MB" + return f"{depth_mb} {util.UI_UNIT_NAME_MEMPOOL_MB}" def depth_tooltip(self, depth: Optional[int]) -> str: """Returns text tooltip for given mempool depth (in vbytes).""" @@ -637,7 +637,7 @@ class SimpleConfig(Logger): fee_per_byte = None else: fee_per_byte = fee_per_kb/1000 - rate_str = format_fee_satoshis(fee_per_byte) + f" {util.FEERATE_UI_NAME_SAT_PER_VBYTE}" + rate_str = format_fee_satoshis(fee_per_byte) + f" {util.UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE}" if dyn: if mempool: @@ -875,7 +875,7 @@ class SimpleConfig(Logger): def format_fee_rate(self, fee_rate) -> str: """fee_rate is in sat/kvByte.""" - return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + f" {util.FEERATE_UI_NAME_SAT_PER_VBYTE}" + return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + f" {util.UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE}" def get_base_unit(self): return decimal_point_to_base_unit_name(self.decimal_point) diff --git a/electrum/util.py b/electrum/util.py index abe98c47f..ba361160c 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -792,8 +792,10 @@ def format_satoshis( FEERATE_PRECISION = 1 # num fractional decimal places for sat/byte fee rates _feerate_quanta = Decimal(10) ** (-FEERATE_PRECISION) -FEERATE_UI_NAME_SAT_PER_VBYTE = "sat/byte" -FEERATE_UI_NAME_SAT_PER_VBYTE_SHORT = "sat/b" +UI_UNIT_NAME_FEERATE_SAT_PER_VBYTE = "sat/byte" +UI_UNIT_NAME_FEERATE_SAT_PER_VB = "sat/b" +UI_UNIT_NAME_TXSIZE_VBYTES = "bytes" +UI_UNIT_NAME_MEMPOOL_MB = "MB" def format_fee_satoshis(fee, *, num_zeros=0, precision=None): diff --git a/electrum/wallet.py b/electrum/wallet.py index 45245be24..ae46929e3 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -1657,7 +1657,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): if fee is not None: size = tx.estimated_size() fee_per_byte = fee / size - extra.append(format_fee_satoshis(fee_per_byte) + f" {util.FEERATE_UI_NAME_SAT_PER_VBYTE_SHORT}") + extra.append(format_fee_satoshis(fee_per_byte) + f" {util.UI_UNIT_NAME_FEERATE_SAT_PER_VB}") if fee is not None and height in (TX_HEIGHT_UNCONF_PARENT, TX_HEIGHT_UNCONFIRMED) \ and self.config.has_fee_mempool(): exp_n = self.config.fee_to_depth(fee_per_byte)