add 'sat' as base unit option

This commit is contained in:
SomberNight 2018-05-05 12:42:17 +02:00
parent a459eea018
commit c03d68d758
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
5 changed files with 39 additions and 33 deletions

View file

@ -44,14 +44,19 @@ Builder.load_string('''
class ChoiceDialog(Factory.Popup): 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) Factory.Popup.__init__(self)
print(choices, type(choices)) 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: if type(choices) is list:
choices = dict(map(lambda x: (x,x), choices)) choices = dict(map(lambda x: (x,x), choices))
layout = self.ids.choices layout = self.ids.choices
layout.bind(minimum_height=layout.setter('height')) 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 = Label(text=v)
l.height = '48dp' l.height = '48dp'
l.size_hint_x = 4 l.size_hint_x = 4

View file

@ -3,7 +3,7 @@ from kivy.factory import Factory
from kivy.properties import ObjectProperty from kivy.properties import ObjectProperty
from kivy.lang import Builder 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.i18n import languages
from electrum_gui.kivy.i18n import _ from electrum_gui.kivy.i18n import _
from electrum.plugins import run_hook from electrum.plugins import run_hook
@ -136,7 +136,8 @@ class SettingsDialog(Factory.Popup):
def cb(text): def cb(text):
self.app._set_bu(text) self.app._set_bu(text)
item.bu = self.app.base_unit 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() self._unit_dialog.open()
def coinselect_status(self): def coinselect_status(self):

View file

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

View file

@ -47,7 +47,9 @@ from electrum.i18n import _
from electrum.util import (format_time, format_satoshis, format_fee_satoshis, from electrum.util import (format_time, format_satoshis, format_fee_satoshis,
format_satoshis_plain, NotEnoughFunds, PrintError, format_satoshis_plain, NotEnoughFunds, PrintError,
UserCancelled, NoDynamicFeeEstimates, profiler, 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 Transaction
from electrum import util, bitcoin, commands, coinchooser from electrum import util, bitcoin, commands, coinchooser
from electrum import paymentrequest from electrum import paymentrequest
@ -655,14 +657,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return self.decimal_point return self.decimal_point
def base_unit(self): def base_unit(self):
assert self.decimal_point in [2, 5, 8] return decimal_point_to_base_unit_name(self.decimal_point)
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')
def connect_fields(self, window, btc_e, fiat_e, fee_e): 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) SSL_id_e.setReadOnly(True)
id_widgets.append((SSL_id_label, SSL_id_e)) id_widgets.append((SSL_id_label, SSL_id_e))
units = ['BTC', 'mBTC', 'bits'] units = base_units_list
msg = (_('Base unit of your wallet.') 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.')) + _('This setting affects the Send tab, and all balance related fields.'))
unit_label = HelpLabel(_('Base unit') + ':', msg) unit_label = HelpLabel(_('Base unit') + ':', msg)
unit_combo = QComboBox() unit_combo = QComboBox()
@ -2741,14 +2736,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return return
edits = self.amount_e, self.fee_e, self.receive_amount_e edits = self.amount_e, self.fee_e, self.receive_amount_e
amounts = [edit.get_amount() for edit in edits] amounts = [edit.get_amount() for edit in edits]
if unit_result == 'BTC': self.decimal_point = base_unit_name_to_decimal_point(unit_result)
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.config.set_key('decimal_point', self.decimal_point, True) self.config.set_key('decimal_point', self.decimal_point, True)
nz.setMaximum(self.decimal_point) nz.setMaximum(self.decimal_point)
self.history_list.update() self.history_list.update()

View file

@ -40,7 +40,26 @@ def inv_dict(d):
return {v: k for k, v in d.items()} 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): def normalize_version(v):
return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")] return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]