wallet: add struct PiechartBalance
This commit is contained in:
@@ -832,15 +832,15 @@ class QEWallet(AuthMixin, QObject, QtEventListener):
|
|||||||
|
|
||||||
@pyqtSlot(result='QVariantMap')
|
@pyqtSlot(result='QVariantMap')
|
||||||
def getBalancesForPiechart(self):
|
def getBalancesForPiechart(self):
|
||||||
confirmed, unconfirmed, unmatured, frozen, lightning, f_lightning = balances = self.wallet.get_balances_for_piechart()
|
p_bal = self.wallet.get_balances_for_piechart()
|
||||||
return {
|
return {
|
||||||
'confirmed': confirmed,
|
'confirmed': p_bal.confirmed,
|
||||||
'unconfirmed': unconfirmed,
|
'unconfirmed': p_bal.unconfirmed,
|
||||||
'unmatured': unmatured,
|
'unmatured': p_bal.unmatured,
|
||||||
'frozen': frozen,
|
'frozen': p_bal.frozen,
|
||||||
'lightning': int(lightning),
|
'lightning': int(p_bal.lightning),
|
||||||
'f_lightning': int(f_lightning),
|
'f_lightning': int(p_bal.lightning_frozen),
|
||||||
'total': sum([int(x) for x in list(balances)])
|
'total': int(p_bal.total())
|
||||||
}
|
}
|
||||||
|
|
||||||
@pyqtSlot(str, result=bool)
|
@pyqtSlot(str, result=bool)
|
||||||
|
|||||||
@@ -168,7 +168,13 @@ class BalanceDialog(WindowModalDialog):
|
|||||||
self.config = parent.config
|
self.config = parent.config
|
||||||
self.fx = parent.fx
|
self.fx = parent.fx
|
||||||
|
|
||||||
confirmed, unconfirmed, unmatured, frozen, lightning, f_lightning = self.wallet.get_balances_for_piechart()
|
p_bal = self.wallet.get_balances_for_piechart()
|
||||||
|
confirmed = p_bal.confirmed
|
||||||
|
unconfirmed = p_bal.unconfirmed
|
||||||
|
unmatured = p_bal.unmatured
|
||||||
|
frozen = p_bal.frozen
|
||||||
|
lightning = p_bal.lightning
|
||||||
|
f_lightning = p_bal.lightning_frozen
|
||||||
|
|
||||||
frozen_str = self.config.format_amount_and_units(frozen)
|
frozen_str = self.config.format_amount_and_units(frozen)
|
||||||
confirmed_str = self.config.format_amount_and_units(confirmed)
|
confirmed_str = self.config.format_amount_and_units(confirmed)
|
||||||
|
|||||||
@@ -1066,19 +1066,19 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener):
|
|||||||
icon = read_QIcon("status_lagging%s.png"%fork_str)
|
icon = read_QIcon("status_lagging%s.png"%fork_str)
|
||||||
else:
|
else:
|
||||||
network_text = _("Connected")
|
network_text = _("Connected")
|
||||||
confirmed, unconfirmed, unmatured, frozen, lightning, f_lightning = self.wallet.get_balances_for_piechart()
|
p_bal = self.wallet.get_balances_for_piechart()
|
||||||
self.balance_label.update_list(
|
self.balance_label.update_list(
|
||||||
[
|
[
|
||||||
(_('Frozen'), COLOR_FROZEN, frozen),
|
(_('Frozen'), COLOR_FROZEN, p_bal.frozen),
|
||||||
(_('Unmatured'), COLOR_UNMATURED, unmatured),
|
(_('Unmatured'), COLOR_UNMATURED, p_bal.unmatured),
|
||||||
(_('Unconfirmed'), COLOR_UNCONFIRMED, unconfirmed),
|
(_('Unconfirmed'), COLOR_UNCONFIRMED, p_bal.unconfirmed),
|
||||||
(_('On-chain'), COLOR_CONFIRMED, confirmed),
|
(_('On-chain'), COLOR_CONFIRMED, p_bal.confirmed),
|
||||||
(_('Lightning'), COLOR_LIGHTNING, lightning),
|
(_('Lightning'), COLOR_LIGHTNING, p_bal.lightning),
|
||||||
(_('Lightning frozen'), COLOR_FROZEN_LIGHTNING, f_lightning),
|
(_('Lightning frozen'), COLOR_FROZEN_LIGHTNING, p_bal.lightning_frozen),
|
||||||
],
|
],
|
||||||
warning = self.wallet.is_low_reserve(),
|
warning = self.wallet.is_low_reserve(),
|
||||||
)
|
)
|
||||||
balance = confirmed + unconfirmed + unmatured + frozen + lightning + f_lightning
|
balance = p_bal.total()
|
||||||
balance_text = _("Balance") + ": %s "%(self.format_amount_and_units(balance))
|
balance_text = _("Balance") + ": %s "%(self.format_amount_and_units(balance))
|
||||||
# append fiat balance and price
|
# append fiat balance and price
|
||||||
if self.fx.is_enabled():
|
if self.fx.is_enabled():
|
||||||
@@ -1787,7 +1787,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener):
|
|||||||
console.updateNamespace(methods)
|
console.updateNamespace(methods)
|
||||||
|
|
||||||
def show_balance_dialog(self):
|
def show_balance_dialog(self):
|
||||||
balance = sum(self.wallet.get_balances_for_piechart())
|
balance = self.wallet.get_balances_for_piechart().total()
|
||||||
if balance == 0:
|
if balance == 0:
|
||||||
return
|
return
|
||||||
from .balance_dialog import BalanceDialog
|
from .balance_dialog import BalanceDialog
|
||||||
@@ -1981,7 +1981,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener):
|
|||||||
def new_channel_dialog(self, *, amount_sat=None, min_amount_sat=None):
|
def new_channel_dialog(self, *, amount_sat=None, min_amount_sat=None):
|
||||||
from electrum.lnutil import MIN_FUNDING_SAT
|
from electrum.lnutil import MIN_FUNDING_SAT
|
||||||
from .new_channel_dialog import NewChannelDialog
|
from .new_channel_dialog import NewChannelDialog
|
||||||
confirmed, unconfirmed, unmatured, frozen, lightning, f_lightning = self.wallet.get_balances_for_piechart()
|
confirmed = self.wallet.get_spendable_balance_sat(confirmed_only=True)
|
||||||
min_amount_sat = min_amount_sat or MIN_FUNDING_SAT
|
min_amount_sat = min_amount_sat or MIN_FUNDING_SAT
|
||||||
if confirmed < min_amount_sat:
|
if confirmed < min_amount_sat:
|
||||||
msg = _('Not enough funds') + '\n\n' + _('You need at least {} to open a channel.').format(self.format_amount_and_units(min_amount_sat))
|
msg = _('Not enough funds') + '\n\n' + _('You need at least {} to open a channel.').format(self.format_amount_and_units(min_amount_sat))
|
||||||
|
|||||||
@@ -133,11 +133,11 @@ class ElectrumGui(BaseElectrumGui, EventListener):
|
|||||||
msg = _("Synchronizing...")
|
msg = _("Synchronizing...")
|
||||||
else:
|
else:
|
||||||
c, u, x = self.wallet.get_balance()
|
c, u, x = self.wallet.get_balance()
|
||||||
msg = _("Balance")+": %f "%(Decimal(c) / COIN)
|
msg = _("Balance")+": {} ".format(Decimal(c) / COIN)
|
||||||
if u:
|
if u:
|
||||||
msg += " [%f unconfirmed]"%(Decimal(u) / COIN)
|
msg += " [{} unconfirmed]".format(Decimal(u) / COIN)
|
||||||
if x:
|
if x:
|
||||||
msg += " [%f unmatured]"%(Decimal(x) / COIN)
|
msg += " [{} unmatured]".format(Decimal(x) / COIN)
|
||||||
else:
|
else:
|
||||||
msg = _("Not connected")
|
msg = _("Not connected")
|
||||||
|
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ class ElectrumGui(BaseElectrumGui, EventListener):
|
|||||||
if not self.wallet.is_up_to_date():
|
if not self.wallet.is_up_to_date():
|
||||||
msg = _("Synchronizing...")
|
msg = _("Synchronizing...")
|
||||||
else:
|
else:
|
||||||
balance = sum(self.wallet.get_balances_for_piechart())
|
balance = self.wallet.get_balances_for_piechart().total()
|
||||||
msg = _("Balance") + ': ' + self.config.format_amount_and_units(balance)
|
msg = _("Balance") + ': ' + self.config.format_amount_and_units(balance)
|
||||||
else:
|
else:
|
||||||
msg = _("Not connected")
|
msg = _("Not connected")
|
||||||
|
|||||||
@@ -2717,7 +2717,7 @@ class LNWallet(LNWorker):
|
|||||||
with self.lock:
|
with self.lock:
|
||||||
self.payment_info.pop(payment_hash_hex, None)
|
self.payment_info.pop(payment_hash_hex, None)
|
||||||
|
|
||||||
def get_balance(self, frozen=False):
|
def get_balance(self, *, frozen=False) -> Decimal:
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return Decimal(sum(
|
return Decimal(sum(
|
||||||
chan.balance(LOCAL) if not chan.is_closed() and (chan.is_frozen_for_sending() if frozen else True) else 0
|
chan.balance(LOCAL) if not chan.is_closed() and (chan.is_frozen_for_sending() if frozen else True) else 0
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import itertools
|
|||||||
import threading
|
import threading
|
||||||
import enum
|
import enum
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
import electrum_ecc as ecc
|
import electrum_ecc as ecc
|
||||||
from aiorpcx import ignore_after, run_in_thread
|
from aiorpcx import ignore_after, run_in_thread
|
||||||
@@ -370,6 +371,19 @@ class TxWalletDetails(NamedTuple):
|
|||||||
is_related_to_wallet: bool
|
is_related_to_wallet: bool
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True, slots=True, frozen=True)
|
||||||
|
class PiechartBalance:
|
||||||
|
confirmed: int # confirmed and matured and NOT frozen
|
||||||
|
unconfirmed: int # unconfirmed and NOT frozen
|
||||||
|
unmatured: int # unmatured and NOT frozen
|
||||||
|
frozen: int # on-chain
|
||||||
|
lightning: Decimal
|
||||||
|
lightning_frozen: Decimal
|
||||||
|
|
||||||
|
def total(self) -> Decimal:
|
||||||
|
return self.confirmed + self.unconfirmed + self.unmatured + self.frozen + self.lightning + self.lightning_frozen
|
||||||
|
|
||||||
|
|
||||||
class Abstract_Wallet(ABC, Logger, EventListener):
|
class Abstract_Wallet(ABC, Logger, EventListener):
|
||||||
"""
|
"""
|
||||||
Wallet classes are created to handle various address generation methods.
|
Wallet classes are created to handle various address generation methods.
|
||||||
@@ -1063,6 +1077,9 @@ class Abstract_Wallet(ABC, Logger, EventListener):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def get_balance(self, **kwargs):
|
def get_balance(self, **kwargs):
|
||||||
|
"""Note: intended for display-purposes.
|
||||||
|
Do not use for NotEnoughFunds checks. Use get_spendable_balance_sat() instead.
|
||||||
|
"""
|
||||||
domain = self.get_addresses()
|
domain = self.get_addresses()
|
||||||
return self.adb.get_balance(domain, **kwargs)
|
return self.adb.get_balance(domain, **kwargs)
|
||||||
|
|
||||||
@@ -1144,9 +1161,11 @@ class Abstract_Wallet(ABC, Logger, EventListener):
|
|||||||
)
|
)
|
||||||
return c1-c2, u1-u2, x1-x2
|
return c1-c2, u1-u2, x1-x2
|
||||||
|
|
||||||
def get_balances_for_piechart(self):
|
def get_balances_for_piechart(self) -> PiechartBalance:
|
||||||
|
"""Note: intended for display-purposes.
|
||||||
|
Do not use for NotEnoughFunds checks. Use get_spendable_balance_sat() instead.
|
||||||
|
"""
|
||||||
# return only positive values
|
# return only positive values
|
||||||
# todo: add lightning frozen
|
|
||||||
c, u, x = self.get_balance()
|
c, u, x = self.get_balance()
|
||||||
fc, fu, fx = self.get_frozen_balance()
|
fc, fu, fx = self.get_frozen_balance()
|
||||||
lightning = self.lnworker.get_balance() if self.has_lightning() else 0
|
lightning = self.lnworker.get_balance() if self.has_lightning() else 0
|
||||||
@@ -1156,7 +1175,14 @@ class Abstract_Wallet(ABC, Logger, EventListener):
|
|||||||
uu = u - fu
|
uu = u - fu
|
||||||
xx = x - fx
|
xx = x - fx
|
||||||
frozen = fc + fu + fx
|
frozen = fc + fu + fx
|
||||||
return cc, uu, xx, frozen, lightning - f_lightning, f_lightning
|
return PiechartBalance(
|
||||||
|
confirmed=cc,
|
||||||
|
unconfirmed=uu,
|
||||||
|
unmatured=xx,
|
||||||
|
frozen=frozen,
|
||||||
|
lightning=lightning - f_lightning,
|
||||||
|
lightning_frozen=f_lightning,
|
||||||
|
)
|
||||||
|
|
||||||
def balance_at_timestamp(self, domain, target_timestamp):
|
def balance_at_timestamp(self, domain, target_timestamp):
|
||||||
# we assume that get_history returns items ordered by block height
|
# we assume that get_history returns items ordered by block height
|
||||||
|
|||||||
Reference in New Issue
Block a user