Skip to content

Commit 46f3beb

Browse files
authored
Move away from GetLatestExchangeRateTime for historical mint data (#72)
1 parent c1e49f4 commit 46f3beb

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

ocp/rpc/currency/server.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
const (
3535
streamPingDelay = 5 * time.Second
3636
streamInitialRecvTimeout = 250 * time.Millisecond
37+
38+
timePerHistoricalUpdate = 5 * time.Minute
3739
)
3840

3941
type currencyServer struct {
@@ -275,8 +277,8 @@ func (s *currencyServer) GetHistoricalMintData(ctx context.Context, req *currenc
275277
}
276278

277279
// Determine the time range and interval based on the predefined range.
278-
// endTime is GetLatestExchangeRateTime() which is used in cache keys to
279-
// invalidate entries when new market data is generated (every 15 minutes).
280+
// endTime is getLatestHistoricalTime, which is used in cache keys to
281+
// invalidate entries when new market data is generated.
280282
startTime, endTime, interval := getTimeRangeForPredefinedRange(req.GetPredefinedRange())
281283

282284
// Get reserve history (cached by mint + range)
@@ -364,9 +366,9 @@ func (s *currencyServer) GetHistoricalMintData(ctx context.Context, req *currenc
364366
})
365367
}
366368

367-
// Always include a latest data point based on GetLatestExchangeRateTime
369+
// Always include a latest data point based on getLatestHistoricalTime
368370
// if it's newer than the last historical point
369-
latestTime := currency_util.GetLatestExchangeRateTime()
371+
latestTime := getLatestHistoricalTime()
370372
if len(data) == 0 || data[len(data)-1].Timestamp.AsTime().Before(latestTime) {
371373
latestReserve, err := s.data.GetCurrencyReserveAtTime(ctx, mintAccount.PublicKey().ToBase58(), latestTime)
372374
if err != nil {
@@ -494,7 +496,7 @@ func calculateMarketCap(supplyFromBonding uint64, exchangeRate float64) float64
494496
// getTimeRangeForPredefinedRange returns the start time and appropriate interval
495497
// for the given predefined range.
496498
func getTimeRangeForPredefinedRange(predefinedRange currencypb.GetHistoricalMintDataRequest_PredefinedRange) (time.Time, time.Time, query.Interval) {
497-
now := currency_util.GetLatestExchangeRateTime()
499+
now := getLatestHistoricalTime()
498500

499501
switch predefinedRange {
500502
case currencypb.GetHistoricalMintDataRequest_LAST_DAY:
@@ -513,6 +515,13 @@ func getTimeRangeForPredefinedRange(predefinedRange currencypb.GetHistoricalMint
513515
}
514516
}
515517

518+
func getLatestHistoricalTime() time.Time {
519+
secondsInUpdateInterval := int64(timePerHistoricalUpdate / time.Second)
520+
queryTimeUnix := time.Now().Unix()
521+
queryTimeUnix = queryTimeUnix - (queryTimeUnix % secondsInUpdateInterval)
522+
return time.Unix(queryTimeUnix, 0)
523+
}
524+
516525
func (s *currencyServer) StreamLiveMintData(
517526
streamer currencypb.Currency_StreamLiveMintDataServer,
518527
) error {

0 commit comments

Comments
 (0)