Rewrite price lookup from WebSocket to REST API #15

Merged
thuanle merged 3 commits from feat/rest-price-lookup into main 2026-04-26 16:19:28 +07:00
4 changed files with 15 additions and 9 deletions
Showing only changes of commit 914beea5ce - Show all commits
+1 -1
View File
@@ -17,5 +17,5 @@ type IMarket interface {
// Trading pair methods // Trading pair methods
IsSpotPair(symbol string) bool IsSpotPair(symbol string) bool
IsFuturesPair(symbol string) bool IsFuturesPair(symbol string) bool
RefreshTradingPairCache() RefreshTradingPairCache() error
} }
+3
View File
@@ -36,6 +36,9 @@ func NewMarketData() *MarketData {
futuresClient: futures.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.pairCacheRefreshLoop()
go ms.alphaCacheRefreshLoop() go ms.alphaCacheRefreshLoop()
+6 -5
View File
@@ -7,20 +7,20 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func (ms *MarketData) refreshTradingPairCache() { func (ms *MarketData) refreshTradingPairCache() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel() defer cancel()
spotInfo, err := ms.spotClient.NewExchangeInfoService().Do(ctx) spotInfo, err := ms.spotClient.NewExchangeInfoService().Do(ctx)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to fetch spot exchange info") log.Error().Err(err).Msg("Failed to fetch spot exchange info")
return return err
} }
futuresInfo, err := ms.futuresClient.NewExchangeInfoService().Do(ctx) futuresInfo, err := ms.futuresClient.NewExchangeInfoService().Do(ctx)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to fetch futures exchange info") log.Error().Err(err).Msg("Failed to fetch futures exchange info")
return return err
} }
ms.pairCacheMutex.Lock() ms.pairCacheMutex.Lock()
@@ -45,6 +45,7 @@ func (ms *MarketData) refreshTradingPairCache() {
Int("spot", len(ms.spotPairs)). Int("spot", len(ms.spotPairs)).
Int("futures", len(ms.futuresPairs)). Int("futures", len(ms.futuresPairs)).
Msg("Trading pair cache refreshed") Msg("Trading pair cache refreshed")
return nil
} }
func (ms *MarketData) pairCacheRefreshLoop() { func (ms *MarketData) pairCacheRefreshLoop() {
@@ -68,6 +69,6 @@ func (ms *MarketData) IsFuturesPair(symbol string) bool {
return ms.futuresPairs[symbol] return ms.futuresPairs[symbol]
} }
func (ms *MarketData) RefreshTradingPairCache() { func (ms *MarketData) RefreshTradingPairCache() error {
ms.refreshTradingPairCache() return ms.refreshTradingPairCache()
} }
+5 -3
View File
@@ -23,10 +23,12 @@ func OnGetTopFundingFee(context telebot.Context) error {
} }
func OnRefreshPairCache(context telebot.Context) error { func OnRefreshPairCache(context telebot.Context) error {
adminID, _ := strconv.ParseInt(os.Getenv(key.AdminChatID), 10, 64) adminID, err := strconv.ParseInt(os.Getenv(key.AdminChatID), 10, 64)
if adminID != 0 && context.Sender().ID != adminID { if err != nil || adminID == 0 || context.Sender().ID != adminID {
return nil return nil
} }
data.Market.RefreshTradingPairCache() if err := data.Market.RefreshTradingPairCache(); err != nil {
return chat.ReplyMessage(context, "Failed to refresh trading pair cache")
}
return chat.ReplyMessage(context, "Trading pair cache refreshed") return chat.ReplyMessage(context, "Trading pair cache refreshed")
} }