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>
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package market
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/adshao/go-binance/v2"
|
|
"github.com/adshao/go-binance/v2/futures"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type MarketData struct {
|
|
// 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{
|
|
spotPairs: make(map[string]bool),
|
|
futuresPairs: make(map[string]bool),
|
|
alphaTokens: make(map[string]AlphaTokenInfo),
|
|
spotClient: binance.NewClient("", ""),
|
|
futuresClient: futures.NewClient("", ""),
|
|
}
|
|
|
|
go ms.pairCacheRefreshLoop()
|
|
go ms.alphaCacheRefreshLoop()
|
|
|
|
return ms
|
|
}
|
|
|
|
func (ms *MarketData) alphaCacheRefreshLoop() {
|
|
ms.refreshAlphaTokenCache()
|
|
ticker := time.NewTicker(time.Hour)
|
|
defer ticker.Stop()
|
|
for range ticker.C {
|
|
ms.refreshAlphaTokenCache()
|
|
}
|
|
}
|