37 lines
901 B
Go
37 lines
901 B
Go
package market
|
|
|
|
import (
|
|
"github.com/adshao/go-binance/v2"
|
|
"github.com/rs/zerolog/log"
|
|
"strconv"
|
|
)
|
|
|
|
func (ms MarketData) GetSpotPrice(symbol string) (float64, bool) {
|
|
p, ok := ms.spotPrice[symbol]
|
|
return p, ok
|
|
}
|
|
|
|
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) {
|
|
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()
|
|
}
|