commit c03d68d758f8ee40d3b61a64605ead779e6f5eb3
parent a459eea0186fd87a319022a306a91f00a5a95878
Author: SomberNight <somber.night@protonmail.com>
Date: Sat, 5 May 2018 12:42:17 +0200
add 'sat' as base unit option
Diffstat:
5 files changed, 39 insertions(+), 33 deletions(-)
diff --git a/gui/kivy/uix/dialogs/choice_dialog.py b/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
diff --git a/gui/kivy/uix/dialogs/settings.py b/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):
diff --git a/gui/qt/amountedit.py b/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:
diff --git a/gui/qt/main_window.py b/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()
diff --git a/lib/util.py b/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(".")]