Browse Source

add 'sat' as base unit option

3.2.x
SomberNight 7 years ago
parent
commit
c03d68d758
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 9
      gui/kivy/uix/dialogs/choice_dialog.py
  2. 5
      gui/kivy/uix/dialogs/settings.py
  3. 11
      gui/qt/amountedit.py
  4. 26
      gui/qt/main_window.py
  5. 21
      lib/util.py

9
gui/kivy/uix/dialogs/choice_dialog.py

@ -44,14 +44,19 @@ Builder.load_string('''
class ChoiceDialog(Factory.Popup):
def __init__(self, title, choices, key, callback):
def __init__(self, title, choices, key, callback, keep_choice_order=False):
Factory.Popup.__init__(self)
print(choices, type(choices))
if keep_choice_order:
orig_index = {choice: i for (i, choice) in enumerate(choices)}
sort_key = lambda x: orig_index[x[0]]
else:
sort_key = lambda x: x
if type(choices) is list:
choices = dict(map(lambda x: (x,x), choices))
layout = self.ids.choices
layout.bind(minimum_height=layout.setter('height'))
for k, v in sorted(choices.items()):
for k, v in sorted(choices.items(), key=sort_key):
l = Label(text=v)
l.height = '48dp'
l.size_hint_x = 4

5
gui/kivy/uix/dialogs/settings.py

@ -3,7 +3,7 @@ from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from electrum.util import base_units
from electrum.util import base_units_list
from electrum.i18n import languages
from electrum_gui.kivy.i18n import _
from electrum.plugins import run_hook
@ -136,7 +136,8 @@ class SettingsDialog(Factory.Popup):
def cb(text):
self.app._set_bu(text)
item.bu = self.app.base_unit
self._unit_dialog = ChoiceDialog(_('Denomination'), list(base_units.keys()), self.app.base_unit, cb)
self._unit_dialog = ChoiceDialog(_('Denomination'), base_units_list,
self.app.base_unit, cb, keep_choice_order=True)
self._unit_dialog.open()
def coinselect_status(self):

11
gui/qt/amountedit.py

@ -5,7 +5,7 @@ from PyQt5.QtGui import *
from PyQt5.QtWidgets import (QLineEdit, QStyle, QStyleOptionFrame)
from decimal import Decimal
from electrum.util import format_satoshis_plain
from electrum.util import format_satoshis_plain, decimal_point_to_base_unit_name
class MyLineEdit(QLineEdit):
@ -80,14 +80,7 @@ class BTCAmountEdit(AmountEdit):
self.decimal_point = decimal_point
def _base_unit(self):
p = self.decimal_point()
if p == 8:
return 'BTC'
if p == 5:
return 'mBTC'
if p == 2:
return 'bits'
raise Exception('Unknown base unit')
return decimal_point_to_base_unit_name(self.decimal_point())
def get_amount(self):
try:

26
gui/qt/main_window.py

@ -47,7 +47,9 @@ from electrum.i18n import _
from electrum.util import (format_time, format_satoshis, format_fee_satoshis,
format_satoshis_plain, NotEnoughFunds, PrintError,
UserCancelled, NoDynamicFeeEstimates, profiler,
export_meta, import_meta, bh2u, bfh, InvalidPassword)
export_meta, import_meta, bh2u, bfh, InvalidPassword,
base_units, base_units_list, base_unit_name_to_decimal_point,
decimal_point_to_base_unit_name)
from electrum import Transaction
from electrum import util, bitcoin, commands, coinchooser
from electrum import paymentrequest
@ -655,14 +657,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return self.decimal_point
def base_unit(self):
assert self.decimal_point in [2, 5, 8]
if self.decimal_point == 2:
return 'bits'
if self.decimal_point == 5:
return 'mBTC'
if self.decimal_point == 8:
return 'BTC'
raise Exception('Unknown base unit')
return decimal_point_to_base_unit_name(self.decimal_point)
def connect_fields(self, window, btc_e, fiat_e, fee_e):
@ -2727,9 +2722,9 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
SSL_id_e.setReadOnly(True)
id_widgets.append((SSL_id_label, SSL_id_e))
units = ['BTC', 'mBTC', 'bits']
units = base_units_list
msg = (_('Base unit of your wallet.')
+ '\n1 BTC = 1000 mBTC. 1 mBTC = 1000 bits.\n'
+ '\n1 BTC = 1000 mBTC. 1 mBTC = 1000 bits. 1 bit = 100 sat.\n'
+ _('This setting affects the Send tab, and all balance related fields.'))
unit_label = HelpLabel(_('Base unit') + ':', msg)
unit_combo = QComboBox()
@ -2741,14 +2736,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return
edits = self.amount_e, self.fee_e, self.receive_amount_e
amounts = [edit.get_amount() for edit in edits]
if unit_result == 'BTC':
self.decimal_point = 8
elif unit_result == 'mBTC':
self.decimal_point = 5
elif unit_result == 'bits':
self.decimal_point = 2
else:
raise Exception('Unknown base unit')
self.decimal_point = base_unit_name_to_decimal_point(unit_result)
self.config.set_key('decimal_point', self.decimal_point, True)
nz.setMaximum(self.decimal_point)
self.history_list.update()

21
lib/util.py

@ -40,7 +40,26 @@ def inv_dict(d):
return {v: k for k, v in d.items()}
base_units = {'BTC':8, 'mBTC':5, 'uBTC':2}
base_units = {'BTC':8, 'mBTC':5, 'bits':2, 'sat':0}
base_units_inverse = inv_dict(base_units)
base_units_list = ['BTC', 'mBTC', 'bits', 'sat'] # list(dict) does not guarantee order
def decimal_point_to_base_unit_name(dp: int) -> str:
# e.g. 8 -> "BTC"
try:
return base_units_inverse[dp]
except KeyError:
raise Exception('Unknown base unit')
def base_unit_name_to_decimal_point(unit_name: str) -> int:
# e.g. "BTC" -> 8
try:
return base_units[unit_name]
except KeyError:
raise Exception('Unknown base unit')
def normalize_version(v):
return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]

Loading…
Cancel
Save