From 87e69ec8d08b9a806504955e6f4bb29f87d0ffa9 Mon Sep 17 00:00:00 2001 From: thuanle Date: Sat, 25 Apr 2026 01:14:40 +0700 Subject: [PATCH] Remove Alpha token price caching in spotPrice map Alpha token prices are now looked up directly from Alpha data instead of being cached into spotPrice. Remove unused ensureAlphaCacheLoaded and shouldRefreshAlphaCache functions. Co-Authored-By: Claude Opus 4.7 --- internal/data/market/alpha_tokens.go | 49 +++++++--------------------- internal/data/market/spot_price.go | 9 ++--- 2 files changed, 13 insertions(+), 45 deletions(-) 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 } }