Files
crypto-price-bot/internal/services/controllers/market.go
thuanle c7128ff516 Address PR #15 review: batch API calls and admin-guard /refresh
1. Add GetAllPremiumIndex() to fetch all futures data in one call,
   used by GetTopPrices instead of per-symbol sequential calls.
2. Add ADMIN_CHAT_ID env check to /refresh command to restrict
   access to authorized users only.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 15:40:00 +07:00

57 lines
1.3 KiB
Go

package controllers
import (
"me.thuanle/bbot/internal/configs/strategy"
"me.thuanle/bbot/internal/data"
"sort"
)
// GetTopPrices returns the top symbol, price and rate
func GetTopPrices() ([]string, []float64, []float64) {
n := len(strategy.TopPriceSymbols)
topSym := make([]string, n)
topPrice := make([]float64, n)
topRate := make([]float64, n)
all, err := data.Market.GetAllPremiumIndex()
if err != nil {
return topSym, topPrice, topRate
}
for i, sym := range strategy.TopPriceSymbols {
p, ok := all[sym]
if !ok {
continue
}
topSym[i] = sym
topPrice[i] = p.MarkPrice
topRate[i] = p.FundingRate
}
return topSym, topPrice, topRate
}
// GetTopFundingFee returns the top funding fee: symbol, rate and cd time
func GetTopFundingFee() ([]string, []float64, []int64) {
allRates, allCd := data.Market.GetAllFundRate()
var resSym []string
for sym, rate := range allRates {
if strategy.FundRateBuyThreshold < rate && rate < strategy.FundRateSellThreshold {
continue
}
resSym = append(resSym, sym)
}
sort.Slice(resSym, func(i, j int) bool {
return allRates[resSym[i]] < allRates[resSym[j]]
})
resRate := make([]float64, len(resSym))
resCd := make([]int64, len(resSym))
for i, sym := range resSym {
resRate[i] = allRates[sym]
resCd[i] = allCd[sym]
}
return resSym, resRate, resCd
}