init code
This commit is contained in:
36
internal/data/market/spot_price.go
Normal file
36
internal/data/market/spot_price.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user