Files
crypto-price-bot/internal/data/market/future_price.go
T
thuanle 9c39423315 Rewrite price lookup from WebSocket to REST API
Replace unreliable WebSocket connections with on-demand REST API calls
for spot and futures prices. Add cached trading pair list (refreshed
hourly) for symbol validation, and /refresh command for manual updates.

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

63 lines
1.5 KiB
Go

package market
import (
"context"
"strconv"
"time"
"github.com/rs/zerolog/log"
)
func (ms *MarketData) GetFuturePrice(symbol string) (float64, float64, int64, bool) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
premiums, err := ms.futuresClient.NewPremiumIndexService().Symbol(symbol).Do(ctx)
if err != nil {
log.Error().Err(err).Str("symbol", symbol).Msg("Failed to fetch futures premium index")
return 0, 0, 0, false
}
if len(premiums) == 0 {
return 0, 0, 0, false
}
p := premiums[0]
markPrice, err := strconv.ParseFloat(p.MarkPrice, 64)
if err != nil {
return 0, 0, 0, false
}
fundingRate, err := strconv.ParseFloat(p.LastFundingRate, 64)
if err != nil {
fundingRate = 0
}
return markPrice, fundingRate, p.NextFundingTime, true
}
func (ms *MarketData) GetAllFundRate() (map[string]float64, map[string]int64) {
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)
}
rates := make(map[string]float64, len(premiums))
times := make(map[string]int64, len(premiums))
for _, p := range premiums {
rate, err := strconv.ParseFloat(p.LastFundingRate, 64)
if err != nil {
continue
}
rates[p.Symbol] = rate
times[p.Symbol] = p.NextFundingTime
}
return rates, times
}