-
Notifications
You must be signed in to change notification settings - Fork 3.4k
millisat precision #10586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
accumulator
wants to merge
14
commits into
spesmilo:master
Choose a base branch
from
accumulator:millisat_precision
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+722
−330
Open
millisat precision #10586
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b2cebd9
qt: fix weird inheritance tree FeerateEdit, move text render with dec…
accumulator 5b0949d
qml: in QEAmount, keep msat and sat synchronized, and be more strict …
accumulator 3b322b8
wallet: kwarg all parameters of wallet.create_request()
accumulator ecbda4d
invoices: add BaseInvoice.get_amount_sat_msat_precision() func
accumulator 651254f
qt: millisat precision request tab, request list, invoice list
accumulator 6e31df9
lnworker: can_{pay,receive}_invoice millisat precision
accumulator 657465d
lnurl, pi: use msat precision
accumulator fb17752
qml: millisat precision ReceiveDetailsDialog, QERequestDetails, QEInv…
accumulator 6f567a0
qml: refactor QEConfig.unitsToSats to QEConfig.baseunitStrToAmount and
accumulator 32eadce
qml: only use QEAmount container instead of javascript/qml (q(u))int(64)
accumulator 294f931
qml: fix lnurlw
accumulator 8935c29
qml: take can-send into account for lnurlp
accumulator 2c1dbe8
qt: BTCAmountEdit allow dynamically changing precision, disable msat …
accumulator 6bc0bcd
util: format_satoshis_plain: remove TODO, hard-fail on float
accumulator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from decimal import Decimal | ||
|
|
||
| from PyQt6.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject | ||
|
|
||
| from electrum.logging import get_logger | ||
|
|
@@ -7,7 +9,6 @@ | |
| class QEAmount(QObject): | ||
| """Container for bitcoin amounts that can be passed around more | ||
| easily between python, QML-property and QML-javascript contexts. | ||
| Note: millisat and sat amounts are not synchronized! | ||
|
|
||
| QML type 'int' in property definitions is 32 bit signed, so will overflow easily | ||
| on (milli)satoshi amounts! 'int' in QML-javascript seems to be larger than 32 bit, and | ||
|
|
@@ -20,32 +21,50 @@ class QEAmount(QObject): | |
|
|
||
| _logger = get_logger(__name__) | ||
|
|
||
| def __init__(self, *, amount_sat: int = 0, amount_msat: int = 0, is_max: bool = False, from_invoice=None, parent=None): | ||
| valueChanged = pyqtSignal() | ||
|
|
||
| def __init__(self, *, amount_sat: int = None, amount_msat: int = None, is_max: bool = False, from_invoice=None, parent=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default argument change from E.g. when opening a swap view: |
||
| super().__init__(parent) | ||
| self._amount_sat = int(amount_sat) if amount_sat is not None else None | ||
| self._amount_msat = int(amount_msat) if amount_msat is not None else None | ||
|
|
||
| self._amount_msat = None | ||
| if amount_sat is not None: | ||
| assert isinstance(amount_sat, int) | ||
| self._amount_msat = self._sat_to_msat(amount_sat) | ||
| if amount_msat is not None: | ||
| assert isinstance(amount_msat, int) | ||
| if amount_sat is not None: | ||
| assert amount_sat == self._msat_to_sat(amount_msat) # if both defined, assert conversion is as expected | ||
| self._amount_msat = amount_msat | ||
| if is_max: | ||
| assert amount_sat is None and amount_msat is None | ||
|
|
||
| self._is_max = is_max | ||
| if from_invoice: | ||
| assert amount_sat is None and amount_msat is None, 'cannot combine from_invoice and amount_(m)sat' | ||
| inv_amt = from_invoice.get_amount_msat() | ||
| if inv_amt == '!': | ||
| self._is_max = True | ||
| elif inv_amt is not None: | ||
| self._amount_msat = int(inv_amt) | ||
| self._amount_sat = int(from_invoice.get_amount_sat()) | ||
|
|
||
| valueChanged = pyqtSignal() | ||
| def _sat_to_msat(self, amount_sat: int | None) -> int | None: | ||
| return amount_sat * 1000 if amount_sat is not None else None | ||
|
|
||
| def _msat_to_sat(self, amount_msat: int | None) -> int | None: | ||
| return int(Decimal(amount_msat) / 1000) if amount_msat is not None else None | ||
|
|
||
| @pyqtProperty('qint64', notify=valueChanged) | ||
| def satsInt(self): | ||
| if self._amount_sat is None: # should normally be defined when accessing this property | ||
| self._logger.warning('amount_sat is undefined, returning 0') | ||
| if self._amount_msat is None: # should normally be defined when accessing this property | ||
| self._logger.warning('amount_msat is undefined, returning 0') | ||
| return 0 | ||
| return self._amount_sat | ||
| return self._msat_to_sat(self._amount_msat) | ||
|
|
||
| @satsInt.setter | ||
| def satsInt(self, sats): | ||
| if self._amount_sat != sats: | ||
| self._amount_sat = sats | ||
| msats = self._sat_to_msat(sats) | ||
| if self._amount_msat != msats: | ||
| self._amount_msat = msats | ||
| self.valueChanged.emit() | ||
|
|
||
| @pyqtProperty('qint64', notify=valueChanged) | ||
|
|
@@ -63,7 +82,7 @@ def msatsInt(self, msats): | |
|
|
||
| @pyqtProperty(str, notify=valueChanged) | ||
| def satsStr(self): | ||
| return str(self._amount_sat) | ||
| return str(self._msat_to_sat(self._amount_msat)) | ||
|
|
||
| @pyqtProperty(str, notify=valueChanged) | ||
| def msatsStr(self): | ||
|
|
@@ -81,11 +100,14 @@ def isMax(self, ismax): | |
|
|
||
| @pyqtProperty(bool, notify=valueChanged) | ||
| def isEmpty(self): | ||
| return not(self._is_max or self._amount_sat or self._amount_msat) | ||
| return not (self._is_max or self._amount_msat) | ||
|
|
||
| @pyqtProperty(bool, notify=valueChanged) | ||
| def hasMsatPrecision(self): | ||
| return not (self._amount_msat == self._sat_to_msat(self._msat_to_sat(self._amount_msat))) | ||
|
|
||
| @pyqtSlot() | ||
| def clear(self): | ||
| self._amount_sat = 0 | ||
| self._amount_msat = 0 | ||
| self._is_max = False | ||
| self.valueChanged.emit() | ||
|
|
@@ -95,28 +117,33 @@ def copyFrom(self, amount): | |
| if not amount: | ||
| self._logger.warning('copyFrom with None argument. assuming 0') # TODO | ||
| amount = QEAmount() | ||
| self.satsInt = amount.satsInt | ||
| self.msatsInt = amount.msatsInt | ||
| self.isMax = amount.isMax | ||
|
|
||
| changed = False | ||
| if self._amount_msat != amount._amount_msat: | ||
| self._amount_msat = amount._amount_msat | ||
| changed = True | ||
| if self._is_max != amount._is_max: | ||
| self._is_max = amount._is_max | ||
| changed = True | ||
| if changed: | ||
| self.valueChanged.emit() | ||
|
|
||
| def __eq__(self, other): | ||
| if isinstance(other, QEAmount): | ||
| return self._amount_sat == other._amount_sat and self._amount_msat == other._amount_msat and self._is_max == other._is_max | ||
| return self._amount_msat == other._amount_msat and self._is_max == other._is_max | ||
| elif isinstance(other, int): | ||
| return self._amount_sat == other | ||
| elif isinstance(other, str): | ||
| return self.satsStr == other | ||
| return self._amount_msat == other | ||
|
|
||
| return False | ||
|
|
||
| def __str__(self): | ||
| s = _('Amount') | ||
| if self._is_max: | ||
| return '%s(MAX)' % s | ||
| return '%s(sats=%d, msats=%d)' % (s, self._amount_sat, self._amount_msat) | ||
| return '%s(sats=%s, msats=%s)' % (s, str(self._msat_to_sat(self._amount_msat)), str(self._amount_msat)) | ||
|
|
||
| def __repr__(self): | ||
| return f"<QEAmount max={self._is_max} sats={self._amount_sat} msats={self._amount_msat} empty={self.isEmpty}>" | ||
| return f"<QEAmount max={self._is_max} sats={self._msat_to_sat(self._amount_msat)} msats={self._amount_msat} empty={self.isEmpty}>" | ||
|
|
||
|
|
||
| class QEBytes(QObject): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This passes
NonetoConfig.formatMilliSats()and raises an Exception, e.g. when creating a lightning invoice: