electrum

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

commit 5afdc14913c464643efe1bf65763e8285d5ac4b6
parent 8fa6bd2aac4a289874cf29494081337796016055
Author: SomberNight <somber.night@protonmail.com>
Date:   Sat, 13 Oct 2018 04:21:07 +0200

util: small clean-up re format_satoshis

related #4771

Diffstat:
Melectrum/gui/qt/main_window.py | 2+-
Melectrum/tests/test_util.py | 58+++++++++++++++++++++-------------------------------------
Melectrum/util.py | 7+++++--
3 files changed, 27 insertions(+), 40 deletions(-)

diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py @@ -688,7 +688,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): return text def format_fee_rate(self, fee_rate): - return format_fee_satoshis(fee_rate/1000, self.num_zeros) + ' sat/byte' + return format_fee_satoshis(fee_rate/1000, num_zeros=self.num_zeros) + ' sat/byte' def get_decimal_point(self): return self.decimal_point diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py @@ -1,4 +1,6 @@ -from electrum.util import format_satoshis, parse_URI +from decimal import Decimal + +from electrum.util import format_satoshis, format_fee_satoshis, parse_URI from . import SequentialTestCase @@ -6,56 +8,38 @@ from . import SequentialTestCase class TestUtil(SequentialTestCase): def test_format_satoshis(self): - result = format_satoshis(1234) - expected = "0.00001234" - self.assertEqual(expected, result) + self.assertEqual("0.00001234", format_satoshis(1234)) def test_format_satoshis_negative(self): - result = format_satoshis(-1234) - expected = "-0.00001234" - self.assertEqual(expected, result) + self.assertEqual("-0.00001234", format_satoshis(-1234)) def test_format_fee(self): - result = format_satoshis(1700/1000, 0, 0) - expected = "1.7" - self.assertEqual(expected, result) + self.assertEqual("1.7", format_fee_satoshis(1700/1000)) def test_format_fee_precision(self): - result = format_satoshis(1666/1000, 0, 0, precision=6) - expected = "1.666" - self.assertEqual(expected, result) - - result = format_satoshis(1666/1000, 0, 0, precision=1) - expected = "1.7" - self.assertEqual(expected, result) + self.assertEqual("1.666", + format_fee_satoshis(1666/1000, precision=6)) + self.assertEqual("1.7", + format_fee_satoshis(1666/1000, precision=1)) def test_format_satoshis_whitespaces(self): - result = format_satoshis(12340, whitespaces=True) - expected = " 0.0001234 " - self.assertEqual(expected, result) - - result = format_satoshis(1234, whitespaces=True) - expected = " 0.00001234" - self.assertEqual(expected, result) + self.assertEqual(" 0.0001234 ", + format_satoshis(12340, whitespaces=True)) + self.assertEqual(" 0.00001234", + format_satoshis(1234, whitespaces=True)) def test_format_satoshis_whitespaces_negative(self): - result = format_satoshis(-12340, whitespaces=True) - expected = " -0.0001234 " - self.assertEqual(expected, result) - - result = format_satoshis(-1234, whitespaces=True) - expected = " -0.00001234" - self.assertEqual(expected, result) + self.assertEqual(" -0.0001234 ", + format_satoshis(-12340, whitespaces=True)) + self.assertEqual(" -0.00001234", + format_satoshis(-1234, whitespaces=True)) def test_format_satoshis_diff_positive(self): - result = format_satoshis(1234, is_diff=True) - expected = "+0.00001234" - self.assertEqual(expected, result) + self.assertEqual("+0.00001234", + format_satoshis(1234, is_diff=True)) def test_format_satoshis_diff_negative(self): - result = format_satoshis(-1234, is_diff=True) - expected = "-0.00001234" - self.assertEqual(expected, result) + self.assertEqual("-0.00001234", format_satoshis(-1234, is_diff=True)) def _do_test_parse_URI(self, uri, expected): result = parse_URI(uri) diff --git a/electrum/util.py b/electrum/util.py @@ -540,8 +540,11 @@ FEERATE_PRECISION = 1 # num fractional decimal places for sat/byte fee rates _feerate_quanta = Decimal(10) ** (-FEERATE_PRECISION) -def format_fee_satoshis(fee, num_zeros=0): - return format_satoshis(fee, num_zeros, 0, precision=FEERATE_PRECISION) +def format_fee_satoshis(fee, *, num_zeros=0, precision=None): + if precision is None: + precision = FEERATE_PRECISION + num_zeros = min(num_zeros, FEERATE_PRECISION) # no more zeroes than available prec + return format_satoshis(fee, num_zeros=num_zeros, decimal_point=0, precision=precision) def quantize_feerate(fee):