87e69ec8d0
Build Docker Image / build (amd64) (push) Successful in 2m21s
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 <noreply@anthropic.com>
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package market
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/adshao/go-binance/v2"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (ms *MarketData) GetSpotPrice(symbol string) (float64, bool) {
|
|
ms.mu.RLock()
|
|
p, ok := ms.spotPrice[symbol]
|
|
ms.mu.RUnlock()
|
|
if ok {
|
|
return p, true
|
|
}
|
|
|
|
// If not found, check if it's an Alpha token
|
|
if ms.IsAlphaToken(symbol) {
|
|
if alphaToken, exists := ms.GetAlphaToken(symbol); exists {
|
|
if price := alphaToken.GetPrice(); price > 0 {
|
|
return price, true
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0, false
|
|
}
|
|
|
|
func (ms *MarketData) StartSpotWsMarkPrice() error {
|
|
_, _, err := binance.WsAllMarketsStatServe(ms.spotWsAllMarketsStatHandler, ms.spotWsErrHandler) //.WsAllMarkPriceServe(ms.futureWsMarkPriceHandler, ms.futureWsErrHandler)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ms *MarketData) spotWsAllMarketsStatHandler(event binance.WsAllMarketsStatEvent) {
|
|
ms.mu.Lock()
|
|
defer ms.mu.Unlock()
|
|
for _, priceEvent := range event {
|
|
price, err := strconv.ParseFloat(priceEvent.LastPrice, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
ms.spotPrice[priceEvent.Symbol] = price
|
|
}
|
|
}
|
|
|
|
func (ms *MarketData) spotWsErrHandler(err error) {
|
|
log.Debug().Err(err).Msg("Spot Ws Error. Restart socket")
|
|
_ = ms.StartSpotWsMarkPrice()
|
|
}
|