9c39423315
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>
43 lines
929 B
Go
43 lines
929 B
Go
package market
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (ms *MarketData) GetSpotPrice(symbol string) (float64, bool) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
prices, err := ms.spotClient.NewListPricesService().Symbol(symbol).Do(ctx)
|
|
if err != nil {
|
|
log.Error().Err(err).Str("symbol", symbol).Msg("Failed to fetch spot price")
|
|
return ms.getAlphaPrice(symbol)
|
|
}
|
|
|
|
if len(prices) == 0 {
|
|
return ms.getAlphaPrice(symbol)
|
|
}
|
|
|
|
price, err := strconv.ParseFloat(prices[0].Price, 64)
|
|
if err != nil || price == 0 {
|
|
return ms.getAlphaPrice(symbol)
|
|
}
|
|
|
|
return price, true
|
|
}
|
|
|
|
func (ms *MarketData) getAlphaPrice(symbol string) (float64, bool) {
|
|
if ms.IsAlphaToken(symbol) {
|
|
if alphaToken, exists := ms.GetAlphaToken(symbol); exists {
|
|
if price := alphaToken.GetPrice(); price > 0 {
|
|
return price, true
|
|
}
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|