more QEAmount refactoring
This commit is contained in:
@@ -43,12 +43,12 @@ Pane {
|
|||||||
placeholderText: qsTr('Amount')
|
placeholderText: qsTr('Amount')
|
||||||
inputMethodHints: Qt.ImhPreferNumbers
|
inputMethodHints: Qt.ImhPreferNumbers
|
||||||
|
|
||||||
property string textAsSats
|
property Amount textAsSats
|
||||||
onTextChanged: {
|
onTextChanged: {
|
||||||
textAsSats = Config.unitsToSats(amount.text)
|
textAsSats = Config.unitsToSats(amount.text)
|
||||||
if (amountFiat.activeFocus)
|
if (amountFiat.activeFocus)
|
||||||
return
|
return
|
||||||
amountFiat.text = Daemon.fx.fiatValue(amount.textAsSats)
|
amountFiat.text = text == '' ? '' : Daemon.fx.fiatValue(amount.textAsSats)
|
||||||
}
|
}
|
||||||
|
|
||||||
Connections {
|
Connections {
|
||||||
@@ -109,7 +109,7 @@ Pane {
|
|||||||
expiresmodel.append({'text': qsTr('1 hour'), 'value': 60*60})
|
expiresmodel.append({'text': qsTr('1 hour'), 'value': 60*60})
|
||||||
expiresmodel.append({'text': qsTr('1 day'), 'value': 24*60*60})
|
expiresmodel.append({'text': qsTr('1 day'), 'value': 24*60*60})
|
||||||
expiresmodel.append({'text': qsTr('1 week'), 'value': 7*24*60*60})
|
expiresmodel.append({'text': qsTr('1 week'), 'value': 7*24*60*60})
|
||||||
expiresmodel.append({'text': qsTr('1 month'), 'value': 31*7*24*60*60})
|
expiresmodel.append({'text': qsTr('1 month'), 'value': 31*24*60*60})
|
||||||
expiresmodel.append({'text': qsTr('Never'), 'value': 0})
|
expiresmodel.append({'text': qsTr('Never'), 'value': 0})
|
||||||
expires.currentIndex = 0
|
expires.currentIndex = 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ Pane {
|
|||||||
placeholderText: qsTr('Amount')
|
placeholderText: qsTr('Amount')
|
||||||
Layout.preferredWidth: parent.width /2
|
Layout.preferredWidth: parent.width /2
|
||||||
inputMethodHints: Qt.ImhPreferNumbers
|
inputMethodHints: Qt.ImhPreferNumbers
|
||||||
property string textAsSats
|
property Amount textAsSats
|
||||||
onTextChanged: {
|
onTextChanged: {
|
||||||
textAsSats = Config.unitsToSats(amount.text)
|
textAsSats = Config.unitsToSats(amount.text)
|
||||||
if (amountFiat.activeFocus)
|
if (amountFiat.activeFocus)
|
||||||
|
|||||||
@@ -89,23 +89,25 @@ class QEConfig(QObject):
|
|||||||
def max_precision(self):
|
def max_precision(self):
|
||||||
return self.decimal_point() + 0 #self.extra_precision
|
return self.decimal_point() + 0 #self.extra_precision
|
||||||
|
|
||||||
@pyqtSlot(str, result='qint64')
|
@pyqtSlot(str, result=QEAmount)
|
||||||
def unitsToSats(self, unitAmount):
|
def unitsToSats(self, unitAmount):
|
||||||
|
self._amount = QEAmount()
|
||||||
try:
|
try:
|
||||||
x = Decimal(unitAmount)
|
x = Decimal(unitAmount)
|
||||||
except:
|
except:
|
||||||
return 0
|
return self._amount
|
||||||
|
|
||||||
# scale it to max allowed precision, make it an int
|
# scale it to max allowed precision, make it an int
|
||||||
max_prec_amount = int(pow(10, self.max_precision()) * x)
|
max_prec_amount = int(pow(10, self.max_precision()) * x)
|
||||||
# if the max precision is simply what unit conversion allows, just return
|
# if the max precision is simply what unit conversion allows, just return
|
||||||
if self.max_precision() == self.decimal_point():
|
if self.max_precision() == self.decimal_point():
|
||||||
return max_prec_amount
|
self._amount = QEAmount(amount_sat=max_prec_amount)
|
||||||
|
return self._amount
|
||||||
self._logger.debug('fallthrough')
|
self._logger.debug('fallthrough')
|
||||||
# otherwise, scale it back to the expected unit
|
# otherwise, scale it back to the expected unit
|
||||||
#amount = Decimal(max_prec_amount) / Decimal(pow(10, self.max_precision()-self.decimal_point()))
|
#amount = Decimal(max_prec_amount) / Decimal(pow(10, self.max_precision()-self.decimal_point()))
|
||||||
#return int(amount) #Decimal(amount) if not self.is_int else int(amount)
|
#return int(amount) #Decimal(amount) if not self.is_int else int(amount)
|
||||||
return 0
|
return self._amount
|
||||||
|
|
||||||
@pyqtSlot('quint64', result=float)
|
@pyqtSlot('quint64', result=float)
|
||||||
def satsToUnits(self, satoshis):
|
def satsToUnits(self, satoshis):
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from electrum.transaction import PartialTxOutput
|
|||||||
from electrum.util import NotEnoughFunds, profiler
|
from electrum.util import NotEnoughFunds, profiler
|
||||||
|
|
||||||
from .qewallet import QEWallet
|
from .qewallet import QEWallet
|
||||||
|
from .qetypes import QEAmount
|
||||||
|
|
||||||
class QETxFinalizer(QObject):
|
class QETxFinalizer(QObject):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
@@ -16,7 +17,7 @@ class QETxFinalizer(QObject):
|
|||||||
_logger = get_logger(__name__)
|
_logger = get_logger(__name__)
|
||||||
|
|
||||||
_address = ''
|
_address = ''
|
||||||
_amount = ''
|
_amount = QEAmount()
|
||||||
_fee = ''
|
_fee = ''
|
||||||
_feeRate = ''
|
_feeRate = ''
|
||||||
_wallet = None
|
_wallet = None
|
||||||
@@ -58,14 +59,14 @@ class QETxFinalizer(QObject):
|
|||||||
self.addressChanged.emit()
|
self.addressChanged.emit()
|
||||||
|
|
||||||
amountChanged = pyqtSignal()
|
amountChanged = pyqtSignal()
|
||||||
@pyqtProperty(str, notify=amountChanged)
|
@pyqtProperty(QEAmount, notify=amountChanged)
|
||||||
def amount(self):
|
def amount(self):
|
||||||
return self._amount
|
return self._amount
|
||||||
|
|
||||||
@amount.setter
|
@amount.setter
|
||||||
def amount(self, amount):
|
def amount(self, amount):
|
||||||
if self._amount != amount:
|
if self._amount != amount:
|
||||||
self._logger.info('amount = "%s"' % amount)
|
self._logger.info('amount = "%s"' % repr(amount))
|
||||||
self._amount = amount
|
self._amount = amount
|
||||||
self.amountChanged.emit()
|
self.amountChanged.emit()
|
||||||
|
|
||||||
@@ -181,7 +182,7 @@ class QETxFinalizer(QObject):
|
|||||||
@profiler
|
@profiler
|
||||||
def make_tx(self, rbf: bool):
|
def make_tx(self, rbf: bool):
|
||||||
coins = self._wallet.wallet.get_spendable_coins(None)
|
coins = self._wallet.wallet.get_spendable_coins(None)
|
||||||
outputs = [PartialTxOutput.from_address_and_value(self.address, int(self.amount))]
|
outputs = [PartialTxOutput.from_address_and_value(self.address, self._amount.satsInt)]
|
||||||
tx = self._wallet.wallet.make_unsigned_transaction(coins=coins,outputs=outputs, fee=None)
|
tx = self._wallet.wallet.make_unsigned_transaction(coins=coins,outputs=outputs, fee=None)
|
||||||
self._logger.debug('fee: %d, inputs: %d, outputs: %d' % (tx.get_fee(), len(tx.inputs()), len(tx.outputs())))
|
self._logger.debug('fee: %d, inputs: %d, outputs: %d' % (tx.get_fee(), len(tx.inputs()), len(tx.outputs())))
|
||||||
return tx
|
return tx
|
||||||
@@ -204,7 +205,8 @@ class QETxFinalizer(QObject):
|
|||||||
self.validChanged.emit()
|
self.validChanged.emit()
|
||||||
return
|
return
|
||||||
|
|
||||||
amount = int(self.amount) if self.amount != '!' else tx.output_value()
|
amount = self._amount.satsInt if not self._amount.isMax else tx.output_value()
|
||||||
|
|
||||||
tx_size = tx.estimated_size()
|
tx_size = tx.estimated_size()
|
||||||
fee = tx.get_fee()
|
fee = tx.get_fee()
|
||||||
feerate = Decimal(fee) / tx_size # sat/byte
|
feerate = Decimal(fee) / tx_size # sat/byte
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from electrum.invoices import (Invoice, InvoiceError, PR_TYPE_ONCHAIN, PR_TYPE
|
|||||||
from .qeinvoicelistmodel import QEInvoiceListModel, QERequestListModel
|
from .qeinvoicelistmodel import QEInvoiceListModel, QERequestListModel
|
||||||
from .qetransactionlistmodel import QETransactionListModel
|
from .qetransactionlistmodel import QETransactionListModel
|
||||||
from .qeaddresslistmodel import QEAddressListModel
|
from .qeaddresslistmodel import QEAddressListModel
|
||||||
|
from .qetypes import QEAmount
|
||||||
|
|
||||||
class QEWallet(QObject):
|
class QEWallet(QObject):
|
||||||
__instances = []
|
__instances = []
|
||||||
@@ -318,10 +319,10 @@ class QEWallet(QObject):
|
|||||||
self._requestModel.add_invoice(req)
|
self._requestModel.add_invoice(req)
|
||||||
return addr
|
return addr
|
||||||
|
|
||||||
@pyqtSlot('quint64', 'QString', int)
|
@pyqtSlot(QEAmount, 'QString', int)
|
||||||
@pyqtSlot('quint64', 'QString', int, bool)
|
@pyqtSlot(QEAmount, 'QString', int, bool)
|
||||||
@pyqtSlot('quint64', 'QString', int, bool, bool)
|
@pyqtSlot(QEAmount, 'QString', int, bool, bool)
|
||||||
def create_request(self, amount: int, message: str, expiration: int, is_lightning: bool = False, ignore_gap: bool = False):
|
def create_request(self, amount: QEAmount, message: str, expiration: int, is_lightning: bool = False, ignore_gap: bool = False):
|
||||||
expiry = expiration #TODO: self.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
|
expiry = expiration #TODO: self.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
|
||||||
try:
|
try:
|
||||||
if is_lightning:
|
if is_lightning:
|
||||||
@@ -329,9 +330,9 @@ class QEWallet(QObject):
|
|||||||
self.requestCreateError.emit('fatal',_("You need to open a Lightning channel first."))
|
self.requestCreateError.emit('fatal',_("You need to open a Lightning channel first."))
|
||||||
return
|
return
|
||||||
# TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
|
# TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
|
||||||
key = self.wallet.lnworker.add_request(amount, message, expiry)
|
key = self.wallet.lnworker.add_request(amount.satsInt, message, expiry)
|
||||||
else:
|
else:
|
||||||
key = self.create_bitcoin_request(amount, message, expiry, ignore_gap)
|
key = self.create_bitcoin_request(amount.satsInt, message, expiry, ignore_gap)
|
||||||
if not key:
|
if not key:
|
||||||
return
|
return
|
||||||
self._addressModel.init_model()
|
self._addressModel.init_model()
|
||||||
|
|||||||
Reference in New Issue
Block a user