Skip to content

Commit

Permalink
calc stake stats internally. more sync improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Nov 23, 2019
1 parent 7e7a203 commit b5c3731
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
17 changes: 16 additions & 1 deletion pydecred/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def __fromjson__(obj):
acct = Account.__fromjson__(obj, cls=DecredAccount)
acct.tickets = obj["tickets"]
acct.stakePools = obj["stakePools"]
acct.updateStakeStats()
return acct
def open(self, pw):
"""
Expand All @@ -139,6 +140,20 @@ def close(self):
super().close()
self._votingKey.key.zero()
self._votingKey = None
def calcBalance(self, tipHeight):
tot = 0
avail = 0
staked = 0
for utxo in self.utxoscan():
tot += utxo.satoshis
if utxo.isTicket():
staked += utxo.satoshis
if utxo.isSpendable(tipHeight):
avail += utxo.satoshis
self.balance.total = tot
self.balance.available = avail
self.balance.staked = staked
return self.balance
def updateStakeStats(self):
"""
Updates the stake stats object.
Expand Down Expand Up @@ -374,7 +389,7 @@ def sync(self, blockchain, signals):
# First, look at addresses that have been generated but not seen. Run in
# loop until the gap limit is reached.
requestedTxs = 0
addrs = self.unseenAddrs()
addrs = self.gapAddrs()
while addrs:
for addr in addrs:
for txid in blockchain.txsForAddr(addr):
Expand Down
2 changes: 1 addition & 1 deletion pydecred/dcrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ def parse(obj):
amount = obj["amount"] if "amount" in obj else 0,
satoshis = obj["satoshis"] if "satoshis" in obj else 0,
maturity = obj["maturity"] if "maturity" in obj else None,
scriptClass = obj["scriptClass"] if "scriptClass" in obj else None,
tinfo = obj["tinfo"] if "tinfo" in obj else None,
)
utxo.parseScriptClass()
return utxo
def parseScriptClass(self):
"""
Expand Down
22 changes: 6 additions & 16 deletions ui/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ def __init__(self, app):
optsLyt.addWidget(self.spinner, 0, 1, Q.ALIGN_RIGHT)

# Open staking window. Button is initally hidden until sync is complete.
self.stakeBttn = btn = app.getButton(SMALL, "Staking")
btn.setVisible(False)
btn = app.getButton(SMALL, "Staking")
btn.setMinimumWidth(110)
btn.clicked.connect(self.openStaking)
optsLyt.addWidget(btn, 0, 1, Q.ALIGN_RIGHT)
Expand Down Expand Up @@ -512,27 +511,17 @@ def balanceUpdated(self, bal):
self.totalBalance.setText("{0:,.2f}".format(dcr))
self.totalBalance.setToolTip("%.8f" % dcr)
self.availBalance.setText("%s spendable" % availStr.rstrip('0').rstrip('.'))
staked = bal.staked/bal.total if bal.total > 0 else 0
self.statsLbl.setText("%s%% staked" % helpers.formatNumber(staked*100))
self.balance = bal
if self.ticketStats:
self.setTicketStats()
def walletSynced(self):
"""
Connected to the ui.SYNC_SIGNAL. Remove loading spinner and set ticket
stats.
"""
acct = self.app.wallet.selectedAccount
self.ticketStats = acct.ticketStats()
self.setTicketStats()
self.spinner.setVisible(False)
self.stakeBttn.setVisible(True)
def setTicketStats(self):
"""
Set the staking statistics.
"""
staked = 0
if self.ticketStats and self.balance.total > 0:
staked = self.ticketStats.value/self.balance.total
self.statsLbl.setText("%s%% staked" % helpers.formatNumber(staked*100))
def spendClicked(self, e=None):
"""
Display a form to send funds to an address. A Qt Slot, but any event
Expand Down Expand Up @@ -945,10 +934,11 @@ def __init__(self, app):
self.edit = edit = QtWidgets.QTextEdit()
edit.setAcceptRichText(False)
edit.setMaximumWidth(300)
edit.setFixedHeight(225)
edit.setFixedHeight(200)
edit.setStyleSheet("QLabel{border: 1px solid #777777; padding: 10px;}")
# edit.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard)
row, lyt = Q.makeWidget(QtWidgets.QWidget, "horizontal")
row.setContentsMargins(2, 2, 2, 2)
self.layout.addWidget(row)
lyt.addStretch(1)
lyt.addWidget(edit)
Expand Down Expand Up @@ -1039,7 +1029,6 @@ def __init__(self, app):
# Register for a few key signals.
self.app.registerSignal(ui.BLOCKCHAIN_CONNECTED, self.blockchainConnected)
self.app.registerSignal(ui.BALANCE_SIGNAL, self.balanceSet)
self.app.registerSignal(ui.SYNC_SIGNAL, self.setStats)

# ticket price is a single row reading `Ticket Price: XX.YY DCR`.
lbl = Q.makeLabel("Ticket Price: ", 16)
Expand Down Expand Up @@ -1151,6 +1140,7 @@ def balanceSet(self, balance):
"""
self.balance = balance
self.setBuyStats()
self.setStats()

def setBuyStats(self):
"""
Expand Down
18 changes: 11 additions & 7 deletions wallet/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,27 @@ class Balance(object):
for this wallet. The `available` sum is the same, but without those which
appear to be from immature coinbase or stakebase transactions.
"""
def __init__(self, total=0, available=0):
def __init__(self, total=0, available=0, staked=0):
self.total = total
self.available = available
self.staked = staked
def __tojson__(self):
return {
"total": self.total,
"available": self.available,
"staked": self.staked,
}
@staticmethod
def __fromjson__(obj):
return Balance(
total = obj["total"],
available = obj["available"]
available = obj["available"],
staked = obj["staked"] if "staked" in obj else 0
)
def __repr__(self):
return (
"Balance(total=%.8f, available=%.8f)" %
(self.total*1e-8, self.available*1e-8)
"Balance(total=%.8f, available=%.8f, staked=%.8f)" %
(self.total*1e-8, self.available*1e-8, self.staked*1e-8)
)
tinyjson.register(Balance, "Balance")

Expand Down Expand Up @@ -632,10 +635,11 @@ def watchAddrs(self):
a = a.union(self.externalAddresses)
a = a.union((a for a in self.internalAddresses if a not in self.txs))
return filterCrazyAddress(a)
def unseenAddrs(self):
def gapAddrs(self):
return filterCrazyAddress(
[a for a in self.internalAddresses if a not in self.txs] +
[a for a in self.externalAddresses if a not in self.txs])
self.internalAddresses[self.lastSeenInt:] +
self.externalAddresses[self.lastSeenExt:]
)
def currentAddress(self):
"""
Get the external address at the cursor. The cursor is not moved.
Expand Down

0 comments on commit b5c3731

Please sign in to comment.