Skip to content

Commit

Permalink
fix stats
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Dec 3, 2023
1 parent 5c3fcfc commit 1b082fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
41 changes: 23 additions & 18 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"decred.org/dcrdex/dex/config"
dexdcr "decred.org/dcrdex/dex/networks/dcr"
walletjson "decred.org/dcrwallet/v3/rpc/jsonrpc/types"
"decred.org/dcrwallet/v3/wallet"
_ "decred.org/dcrwallet/v3/wallet/drivers/bdb"
"github.com/decred/dcrd/blockchain/stake/v5"
blockchain "github.com/decred/dcrd/blockchain/standalone/v2"
Expand Down Expand Up @@ -5003,16 +5004,21 @@ func (dcr *ExchangeWallet) StakeStatus() (*asset.TicketStakingStatus, error) {
TreasurySpends: tSpends,
TreasuryKeys: treasuryPolicy,
},
Stats: asset.TicketStats{
TotalRewards: uint64(sinfo.TotalSubsidy),
TicketCount: sinfo.OwnMempoolTix + sinfo.Unspent + sinfo.Immature + sinfo.Voted + sinfo.Revoked,
Votes: sinfo.Voted,
Revokes: sinfo.Revoked,
Queued: uint32(dcr.ticketBuyer.remaining.Load()),
},
Stats: dcr.ticketStatsFromStakeInfo(sinfo),
}, nil
}

func (dcr *ExchangeWallet) ticketStatsFromStakeInfo(sinfo *wallet.StakeInfoData) asset.TicketStats {
return asset.TicketStats{
TotalRewards: uint64(sinfo.TotalSubsidy),
TicketCount: sinfo.OwnMempoolTix + sinfo.Unspent + sinfo.Immature + sinfo.Voted + sinfo.Revoked,
Votes: sinfo.Voted,
Revokes: sinfo.Revoked,
Mempool: sinfo.OwnMempoolTix,
Queued: uint32(dcr.ticketBuyer.remaining.Load()),
}
}

func (dcr *ExchangeWallet) voteSubsidy(tipHeight int64) uint64 {
// Chance of a given ticket voting in a block is
// p = chainParams.TicketsPerBlock / (chainParams.TicketPoolSize * chainParams.TicketsPerBlock)
Expand Down Expand Up @@ -5152,9 +5158,10 @@ const ticketDataRoute = "ticketPurchaseUpdate"
// TicketPurchaseUpdate is an update from the asynchronous ticket purchasing
// loop.
type TicketPurchaseUpdate struct {
Err string `json:"err,omitempty"`
Remaining uint32 `json:"remaining"`
Tickets []*asset.Ticket `json:"tickets"`
Err string `json:"err,omitempty"`
Remaining uint32 `json:"remaining"`
Tickets []*asset.Ticket `json:"tickets"`
Stats *asset.TicketStats `json:"stats,omitempty"`
}

// runTicketBuyer attempts to buy requested tickets. Because of a dcrwallet bug,
Expand Down Expand Up @@ -5246,9 +5253,13 @@ func (dcr *ExchangeWallet) runTicketBuyer() {
remain = 0
tb.remaining.Store(remain)
}
stats := dcr.ticketStatsFromStakeInfo(sinfo)
stats.Mempool += uint32(len(tickets))
stats.Queued = uint32(remain)
dcr.emit.Data(ticketDataRoute, &TicketPurchaseUpdate{
Tickets: tickets,
Remaining: uint32(remain),
Stats: &stats,
})
for _, ticket := range tickets {
txHash, err := chainhash.NewHashFromStr(ticket.Tx.Hash)
Expand Down Expand Up @@ -5447,14 +5458,8 @@ func (dcr *ExchangeWallet) emitTipChange(height int64) {
VotingSubsidy uint64 `json:"votingSubsidy"`
Stats asset.TicketStats `json:"stats"`
}{
TicketPrice: uint64(sinfo.Sdiff),
Stats: asset.TicketStats{
TotalRewards: uint64(sinfo.TotalSubsidy),
TicketCount: sinfo.OwnMempoolTix + sinfo.Unspent + sinfo.Immature + sinfo.Voted + sinfo.Revoked,
Votes: sinfo.Voted,
Revokes: sinfo.Revoked,
Queued: uint32(dcr.ticketBuyer.remaining.Load()),
},
TicketPrice: uint64(sinfo.Sdiff),
Stats: dcr.ticketStatsFromStakeInfo(sinfo),
VotingSubsidy: dcr.voteSubsidy(height),
}
}
Expand Down
1 change: 1 addition & 0 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ type TicketStats struct {
Votes uint32 `json:"votes"`
Revokes uint32 `json:"revokes"`
Queued uint32 `json:"queued"`
Mempool uint32 `json:"mempool"`
}

// TicketStakingStatus holds various stake information from the wallet.
Expand Down
1 change: 1 addition & 0 deletions client/webserver/site/src/js/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ export interface TicketStats{
ticketCount: number
votes: number
revokes: number
mempool: number
queued: number
}

Expand Down
24 changes: 10 additions & 14 deletions client/webserver/site/src/js/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface TicketPurchaseUpdate extends BaseWalletNote {
err?: string
remaining:number
tickets?: Ticket[]
stats?: TicketStats
}

const animationLength = 300
Expand Down Expand Up @@ -946,9 +947,8 @@ export default class WalletsPage extends BasePage {
if (votingSubsidy) stakeStatus.votingSubsidy = votingSubsidy
const liveTicketCount = stakeStatus.tickets.filter((tkt: Ticket) => tkt.status <= ticketStatusLive && tkt.status >= ticketStatusUnmined).length
page.stakingTicketCount.textContent = String(liveTicketCount)
const immatureTicketCount = stakeStatus.tickets.filter((tkt: Ticket) => tkt.status === ticketStatusUnmined).length
page.immatureTicketCount.textContent = String(immatureTicketCount)
Doc.setVis(immatureTicketCount > 0, page.immatureTicketCountBox)
page.immatureTicketCount.textContent = String(stats.mempool)
Doc.setVis(stats.mempool > 0, page.immatureTicketCountBox)
page.queuedTicketCount.textContent = String(stats.queued)
page.formQueuedTix.textContent = String(stats.queued)
Doc.setVis(stats.queued > 0, page.formQueueTixBox, page.queuedTicketCountBox)
Expand Down Expand Up @@ -1032,23 +1032,19 @@ export default class WalletsPage extends BasePage {
processTicketPurchaseUpdate (walletNote: CustomWalletNote) {
const { stakeStatus, selectedAssetID, page } = this
const { assetID } = walletNote
const { err, remaining, tickets } = walletNote.payload as TicketPurchaseUpdate
const { err, remaining, tickets, stats } = walletNote.payload as TicketPurchaseUpdate
if (assetID !== selectedAssetID) return
if (tickets) {
stakeStatus.stats.ticketCount += tickets.length
stakeStatus.tickets = tickets.concat(stakeStatus.tickets)
if (err) {
Doc.show(page.purchaseTicketsErrBox)
page.purchaseTicketsErr.textContent = err
return
}
if (tickets) stakeStatus.tickets = tickets.concat(stakeStatus.tickets)
if (stats) this.updateTicketStats(stats, app().assets[assetID].unitInfo)
stakeStatus.stats.queued = remaining
page.queuedTicketCount.textContent = String(remaining)
page.formQueuedTix.textContent = String(remaining)
Doc.setVis(remaining > 0, page.queuedTicketCountBox)
const immature = stakeStatus.tickets.filter((tkt: Ticket) => tkt.status === ticketStatusUnmined).length
page.immatureTicketCount.textContent = String(immature)
Doc.setVis(immature > 0, page.immatureTicketCountBox)
if (err) {
Doc.show(page.purchaseTicketsErrBox)
page.purchaseTicketsErr.textContent = err
}
}

async setVSP (assetID: number, vsp: VotingServiceProvider) {
Expand Down

0 comments on commit 1b082fc

Please sign in to comment.