diff --git a/internal/data/market/alpha_tokens.go b/internal/data/market/alpha_tokens.go index 8bdb6ff..4676ff2 100644 --- a/internal/data/market/alpha_tokens.go +++ b/internal/data/market/alpha_tokens.go @@ -83,30 +83,30 @@ func (a *AlphaTokenInfo) GetPercentChange24h() float64 { // fetchAlphaTokens fetches Alpha tokens from Binance API func (ms *MarketData) fetchAlphaTokens() ([]AlphaTokenInfo, error) { const alphaTokenURL = "https://www.binance.com/bapi/defi/v1/public/wallet-direct/buw/wallet/cex/alpha/all/token/list" - + client := &http.Client{ Timeout: 10 * time.Second, } - + resp, err := client.Get(alphaTokenURL) if err != nil { return nil, fmt.Errorf("failed to fetch Alpha tokens: %w", err) } defer resp.Body.Close() - + if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("Alpha token API returned status: %d", resp.StatusCode) } - + var tokenResponse AlphaTokenResponse if err := json.NewDecoder(resp.Body).Decode(&tokenResponse); err != nil { return nil, fmt.Errorf("failed to decode Alpha token response: %w", err) } - + if tokenResponse.Code != "000000" { return nil, fmt.Errorf("Alpha token API returned error code: %s", tokenResponse.Code) } - + return tokenResponse.Data, nil } @@ -114,34 +114,24 @@ func (ms *MarketData) fetchAlphaTokens() ([]AlphaTokenInfo, error) { func (ms *MarketData) refreshAlphaTokenCache() { ms.alphaCacheMutex.Lock() defer ms.alphaCacheMutex.Unlock() - + log.Info().Msg("Refreshing Alpha token cache") - + tokens, err := ms.fetchAlphaTokens() if err != nil { log.Error().Err(err).Msg("Failed to refresh Alpha token cache") return } - + // Clear existing cache ms.alphaTokens = make(map[string]AlphaTokenInfo) - + // Populate cache with new data for _, token := range tokens { symbol := token.Symbol // Already uppercase from API ms.alphaTokens[symbol] = token } - // Update spot prices for Alpha tokens (separate lock to avoid holding both mutexes) - ms.mu.Lock() - for _, token := range tokens { - symbol := token.Symbol - if price := token.GetPrice(); price > 0 { - ms.spotPrice[symbol] = price - } - } - ms.mu.Unlock() - ms.lastAlphaCacheUpdate = time.Now() log.Info().Int("count", len(tokens)).Msg("Alpha token cache refreshed successfully") } @@ -150,7 +140,7 @@ func (ms *MarketData) refreshAlphaTokenCache() { func (ms *MarketData) GetAlphaToken(symbol string) (AlphaTokenInfo, bool) { ms.alphaCacheMutex.RLock() defer ms.alphaCacheMutex.RUnlock() - + token, exists := ms.alphaTokens[symbol] return token, exists } @@ -160,20 +150,3 @@ func (ms *MarketData) IsAlphaToken(symbol string) bool { _, exists := ms.GetAlphaToken(symbol) return exists } - -// shouldRefreshAlphaCache checks if the Alpha cache should be refreshed -func (ms *MarketData) shouldRefreshAlphaCache() bool { - // Refresh if cache is empty (first time) or if it's older than 30 minutes - if len(ms.alphaTokens) == 0 { - return true - } - - return time.Since(ms.lastAlphaCacheUpdate) > 30*time.Minute -} - -// ensureAlphaCacheLoaded ensures Alpha cache is loaded, refreshes if needed -func (ms *MarketData) ensureAlphaCacheLoaded() { - if ms.shouldRefreshAlphaCache() { - ms.refreshAlphaTokenCache() - } -} \ No newline at end of file diff --git a/internal/data/market/spot_price.go b/internal/data/market/spot_price.go index a56a7db..2320d4c 100644 --- a/internal/data/market/spot_price.go +++ b/internal/data/market/spot_price.go @@ -15,15 +15,10 @@ func (ms *MarketData) GetSpotPrice(symbol string) (float64, bool) { return p, true } - // If not found, check if it's an Alpha token and ensure cache is loaded - ms.ensureAlphaCacheLoaded() + // If not found, check if it's an Alpha token if ms.IsAlphaToken(symbol) { if alphaToken, exists := ms.GetAlphaToken(symbol); exists { - price := alphaToken.GetPrice() - if price > 0 { - ms.mu.Lock() - ms.spotPrice[symbol] = price - ms.mu.Unlock() + if price := alphaToken.GetPrice(); price > 0 { return price, true } }