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>
This commit is contained in:
@@ -4,37 +4,39 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/adshao/go-binance/v2"
|
||||
"github.com/adshao/go-binance/v2/futures"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type MarketData struct {
|
||||
mu sync.RWMutex
|
||||
futureMarkPrice map[string]float64
|
||||
futureFundingRate map[string]float64
|
||||
futureNextFundingTime map[string]int64
|
||||
|
||||
spotPrice map[string]float64
|
||||
// Trading pair caches
|
||||
spotPairs map[string]bool
|
||||
futuresPairs map[string]bool
|
||||
pairCacheMutex sync.RWMutex
|
||||
lastPairCacheUpdate time.Time
|
||||
|
||||
// Alpha token cache
|
||||
alphaTokens map[string]AlphaTokenInfo
|
||||
alphaCacheMutex sync.RWMutex
|
||||
lastAlphaCacheUpdate time.Time
|
||||
|
||||
// Binance REST clients
|
||||
spotClient *binance.Client
|
||||
futuresClient *futures.Client
|
||||
}
|
||||
|
||||
func NewMarketData() *MarketData {
|
||||
log.Info().Msg("Start market service")
|
||||
ms := &MarketData{
|
||||
futureMarkPrice: make(map[string]float64),
|
||||
futureFundingRate: make(map[string]float64),
|
||||
futureNextFundingTime: make(map[string]int64),
|
||||
|
||||
spotPrice: make(map[string]float64),
|
||||
alphaTokens: make(map[string]AlphaTokenInfo),
|
||||
spotPairs: make(map[string]bool),
|
||||
futuresPairs: make(map[string]bool),
|
||||
alphaTokens: make(map[string]AlphaTokenInfo),
|
||||
spotClient: binance.NewClient("", ""),
|
||||
futuresClient: futures.NewClient("", ""),
|
||||
}
|
||||
_ = ms.StartFutureWsMarkPrice()
|
||||
_ = ms.StartSpotWsMarkPrice()
|
||||
|
||||
// Initialize Alpha token cache and refresh every hour
|
||||
go ms.pairCacheRefreshLoop()
|
||||
go ms.alphaCacheRefreshLoop()
|
||||
|
||||
return ms
|
||||
|
||||
Reference in New Issue
Block a user