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>
This commit is contained in:
@@ -36,27 +36,50 @@ func (ms *MarketData) GetFuturePrice(symbol string) (float64, float64, int64, bo
|
||||
return markPrice, fundingRate, p.NextFundingTime, true
|
||||
}
|
||||
|
||||
func (ms *MarketData) GetAllFundRate() (map[string]float64, map[string]int64) {
|
||||
type PremiumIndex struct {
|
||||
MarkPrice float64
|
||||
FundingRate float64
|
||||
NextFundingTime int64
|
||||
}
|
||||
|
||||
func (ms *MarketData) GetAllPremiumIndex() (map[string]PremiumIndex, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
premiums, err := ms.futuresClient.NewPremiumIndexService().Do(ctx)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to fetch all futures premium index")
|
||||
return make(map[string]float64), make(map[string]int64)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rates := make(map[string]float64, len(premiums))
|
||||
times := make(map[string]int64, len(premiums))
|
||||
|
||||
result := make(map[string]PremiumIndex, len(premiums))
|
||||
for _, p := range premiums {
|
||||
rate, err := strconv.ParseFloat(p.LastFundingRate, 64)
|
||||
markPrice, err := strconv.ParseFloat(p.MarkPrice, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
rates[p.Symbol] = rate
|
||||
times[p.Symbol] = p.NextFundingTime
|
||||
rate, _ := strconv.ParseFloat(p.LastFundingRate, 64)
|
||||
result[p.Symbol] = PremiumIndex{
|
||||
MarkPrice: markPrice,
|
||||
FundingRate: rate,
|
||||
NextFundingTime: p.NextFundingTime,
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ms *MarketData) GetAllFundRate() (map[string]float64, map[string]int64) {
|
||||
all, err := ms.GetAllPremiumIndex()
|
||||
if err != nil {
|
||||
return make(map[string]float64), make(map[string]int64)
|
||||
}
|
||||
|
||||
rates := make(map[string]float64, len(all))
|
||||
times := make(map[string]int64, len(all))
|
||||
for sym, p := range all {
|
||||
rates[sym] = p.FundingRate
|
||||
times[sym] = p.NextFundingTime
|
||||
}
|
||||
return rates, times
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user