Browse Source

util: small clean-up for format_satoshis

bip39-recovery
SomberNight 5 years ago
parent
commit
b6db201570
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 14
      electrum/gui/kivy/main_window.py
  2. 2
      electrum/gui/qt/amountedit.py
  3. 3
      electrum/gui/qt/main_window.py
  4. 8
      electrum/simple_config.py
  5. 18
      electrum/tests/test_util.py
  6. 12
      electrum/util.py

14
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

2
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):

3
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,

8
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()

18
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)

12
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 == '!':

Loading…
Cancel
Save