electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit b6db20157079d271c8304fa1d3e6927617babce1
parent f91674992f6982ddbc0a7bd1a1a57fb43165c9e0
Author: SomberNight <somber.night@protonmail.com>
Date:   Mon, 22 Jun 2020 02:46:16 +0200

util: small clean-up for format_satoshis

Diffstat:
Melectrum/gui/kivy/main_window.py | 14++++++++++----
Melectrum/gui/qt/amountedit.py | 2+-
Melectrum/gui/qt/main_window.py | 3+--
Melectrum/simple_config.py | 8+++++++-
Melectrum/tests/test_util.py | 18+++++++++++++++++-
Melectrum/util.py | 12++++++++++--
6 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/electrum/gui/kivy/main_window.py b/electrum/gui/kivy/main_window.py @@ -293,7 +293,7 @@ class ElectrumWindow(App): if rate.is_nan(): return '' satoshis = int(pow(10,8) * Decimal(fiat_amount) / Decimal(rate)) - return format_satoshis_plain(satoshis, self.decimal_point()) + return format_satoshis_plain(satoshis, decimal_point=self.decimal_point()) def get_amount(self, amount_str): a, u = amount_str.split() @@ -909,17 +909,23 @@ class ElectrumWindow(App): amount = tx.output_value() __, x_fee_amount = run_hook('get_tx_extra_fee', self.wallet, tx) or (None, 0) amount_after_all_fees = amount - x_fee_amount - return format_satoshis_plain(amount_after_all_fees, self.decimal_point()) + return format_satoshis_plain(amount_after_all_fees, decimal_point=self.decimal_point()) def format_amount(self, x, is_diff=False, whitespaces=False): - return format_satoshis(x, 0, self.decimal_point(), is_diff=is_diff, whitespaces=whitespaces) + return format_satoshis( + x, + num_zeros=0, + decimal_point=self.decimal_point(), + is_diff=is_diff, + whitespaces=whitespaces, + ) def format_amount_and_units(self, x) -> str: if x is None: return 'none' if x == '!': return 'max' - return format_satoshis_plain(x, self.decimal_point()) + ' ' + self.base_unit + return format_satoshis_plain(x, decimal_point=self.decimal_point()) + ' ' + self.base_unit def format_fee_rate(self, fee_rate): # fee_rate is in sat/kB diff --git a/electrum/gui/qt/amountedit.py b/electrum/gui/qt/amountedit.py @@ -110,7 +110,7 @@ class BTCAmountEdit(AmountEdit): if amount is None: self.setText(" ") # Space forces repaint in case units changed else: - self.setText(format_satoshis_plain(amount, self.decimal_point())) + self.setText(format_satoshis_plain(amount, decimal_point=self.decimal_point())) class FeerateEdit(BTCAmountEdit): diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py @@ -54,8 +54,7 @@ from electrum import (keystore, ecc, constants, util, bitcoin, commands, from electrum.bitcoin import COIN, is_address from electrum.plugin import run_hook, BasePlugin from electrum.i18n import _ -from electrum.util import (format_time, format_satoshis, format_fee_satoshis, - format_satoshis_plain, +from electrum.util import (format_time, UserCancelled, profiler, bh2u, bfh, InvalidPassword, UserFacingException, diff --git a/electrum/simple_config.py b/electrum/simple_config.py @@ -614,7 +614,13 @@ class SimpleConfig(Logger): pass def format_amount(self, x, is_diff=False, whitespaces=False): - return format_satoshis(x, self.num_zeros, self.decimal_point, is_diff=is_diff, whitespaces=whitespaces) + return format_satoshis( + x, + num_zeros=self.num_zeros, + decimal_point=self.decimal_point, + is_diff=is_diff, + whitespaces=whitespaces, + ) def format_amount_and_units(self, amount): return self.format_amount(amount) + ' '+ self.get_base_unit() diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py @@ -1,7 +1,8 @@ from decimal import Decimal from electrum.util import (format_satoshis, format_fee_satoshis, parse_URI, - is_hash256_str, chunks, is_ip_address, list_enabled_bits) + is_hash256_str, chunks, is_ip_address, list_enabled_bits, + format_satoshis_plain) from . import ElectrumTestCase @@ -14,6 +15,12 @@ class TestUtil(ElectrumTestCase): def test_format_satoshis_negative(self): self.assertEqual("-0.00001234", format_satoshis(-1234)) + def test_format_satoshis_to_mbtc(self): + self.assertEqual("0.01234", format_satoshis(1234, decimal_point=5)) + + def test_format_satoshis_decimal(self): + self.assertEqual("0.00001234", format_satoshis(Decimal(1234))) + def test_format_fee_float(self): self.assertEqual("1.7", format_fee_satoshis(1700/1000)) @@ -45,6 +52,15 @@ class TestUtil(ElectrumTestCase): def test_format_satoshis_diff_negative(self): self.assertEqual("-0.00001234", format_satoshis(-1234, is_diff=True)) + def test_format_satoshis_plain(self): + self.assertEqual("0.00001234", format_satoshis_plain(1234)) + + def test_format_satoshis_plain_decimal(self): + self.assertEqual("0.00001234", format_satoshis_plain(Decimal(1234))) + + def test_format_satoshis_plain_to_mbtc(self): + self.assertEqual("0.01234", format_satoshis_plain(1234, decimal_point=5)) + def _do_test_parse_URI(self, uri, expected): result = parse_URI(uri) self.assertEqual(expected, result) diff --git a/electrum/util.py b/electrum/util.py @@ -582,7 +582,7 @@ def chunks(items, size: int): yield items[i: i + size] -def format_satoshis_plain(x, decimal_point = 8) -> str: +def format_satoshis_plain(x, *, decimal_point=8) -> str: """Display a satoshi amount scaled. Always uses a '.' as a decimal point and has no thousands separator""" if x == '!': @@ -594,7 +594,15 @@ def format_satoshis_plain(x, decimal_point = 8) -> str: DECIMAL_POINT = localeconv()['decimal_point'] # type: str -def format_satoshis(x, num_zeros=0, decimal_point=8, precision=None, is_diff=False, whitespaces=False) -> str: +def format_satoshis( + x, # in satoshis + *, + num_zeros=0, + decimal_point=8, + precision=None, + is_diff=False, + whitespaces=False, +) -> str: if x is None: return 'unknown' if x == '!':