Files
crypto-price-bot/internal/data/market/main.go
T
thuanle 914beea5ce Address PR #15 round 2: fail-closed admin guard, sync init, error reporting
1. /refresh now fail-closed: rejects all if ADMIN_CHAT_ID unset or invalid
2. Initial pair cache fill is synchronous — bot waits before accepting queries
3. /refresh reports failure when API fetch fails instead of always saying success

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 16:03:22 +07:00

56 lines
1.3 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("", ""),
}
if err := ms.refreshTradingPairCache(); err != nil {
log.Error().Err(err).Msg("Failed initial trading pair cache load")
}
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()
}
}