Skip to content

Commit

Permalink
Add function to get time till next ticket price change (#204)
Browse files Browse the repository at this point in the history
* Remaining of next ticket price

* Change NextTicketRemaining to NextTicketPriceRemaining

* Update calculate remaining next ticket
- Return the remaining time in seconds int64 instead of a string
- Remove the netType parameter and use mw.chainParams
  • Loading branch information
song50119 authored Aug 16, 2021
1 parent f5eefa3 commit bb3af17
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v3"
"github.com/decred/dcrd/wire"
"github.com/planetdecred/dcrlibwallet/utils"
)

// StakeInfo returns information about wallet stakes, tickets and their statuses.
Expand Down Expand Up @@ -371,3 +372,24 @@ func CallVSPTicketInfoAPI(vspHost, pubKeyAddr string) (ticketPurchaseInfo *VSPTi
}
return
}

// NextTicketPriceRemaining returns the remaning time in seconds of a ticket for the next block,
// if secs equal 0 is imminent
func (mw *MultiWallet) NextTicketPriceRemaining() (secs int64, err error) {
params, er := utils.ChainParams(mw.chainParams.Name)
if er != nil {
secs, err = -1, er
return
}
bestBestBlock := mw.GetBestBlock()
idxBlockInWindow := int(int64(bestBestBlock.Height)%params.StakeDiffWindowSize) + 1
blockTime := params.TargetTimePerBlock.Nanoseconds()
windowSize := params.StakeDiffWindowSize
x := (windowSize - int64(idxBlockInWindow)) * blockTime
if x == 0 {
secs, err = 0, nil
return
}
secs, err = int64(time.Duration(x).Seconds()), nil
return
}

0 comments on commit bb3af17

Please sign in to comment.