init code
This commit is contained in:
50
internal/data/market/future_price.go
Normal file
50
internal/data/market/future_price.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"github.com/adshao/go-binance/v2/futures"
|
||||
"github.com/rs/zerolog/log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (ms MarketData) GetFuturePrice(symbol string) (float64, float64, int64, bool) {
|
||||
p, ok := ms.futureMarkPrice[symbol]
|
||||
if !ok {
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
return p, ms.futureFundingRate[symbol], ms.futureNextFundingTime[symbol], true
|
||||
}
|
||||
|
||||
func (ms MarketData) StartFutureWsMarkPrice() error {
|
||||
_, _, err := futures.WsAllMarkPriceServe(ms.futureWsMarkPriceHandler, ms.futureWsErrHandler)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms MarketData) futureWsMarkPriceHandler(event futures.WsAllMarkPriceEvent) {
|
||||
for _, priceEvent := range event {
|
||||
price, err := strconv.ParseFloat(priceEvent.MarkPrice, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
fundingRate, err := strconv.ParseFloat(priceEvent.FundingRate, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ms.futureMarkPrice[priceEvent.Symbol] = price
|
||||
ms.futureFundingRate[priceEvent.Symbol] = fundingRate
|
||||
ms.futureNextFundingTime[priceEvent.Symbol] = priceEvent.NextFundingTime
|
||||
}
|
||||
}
|
||||
|
||||
func (ms MarketData) futureWsErrHandler(err error) {
|
||||
log.Debug().Err(err).Msg("Ws Error. Restart socket")
|
||||
_ = ms.StartFutureWsMarkPrice()
|
||||
}
|
||||
|
||||
func (ms MarketData) GetAllFundRate() (map[string]float64, map[string]int64) {
|
||||
return ms.futureFundingRate, ms.futureNextFundingTime
|
||||
}
|
||||
25
internal/data/market/main.go
Normal file
25
internal/data/market/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package market
|
||||
|
||||
import "github.com/rs/zerolog/log"
|
||||
|
||||
type MarketData struct {
|
||||
futureMarkPrice map[string]float64
|
||||
futureFundingRate map[string]float64
|
||||
futureNextFundingTime map[string]int64
|
||||
|
||||
spotPrice map[string]float64
|
||||
}
|
||||
|
||||
func NewMarketData() *MarketData {
|
||||
log.Info().Msg("Start market service")
|
||||
ms := &MarketData{
|
||||
futureMarkPrice: make(map[string]float64),
|
||||
futureFundingRate: make(map[string]float64),
|
||||
futureNextFundingTime: make(map[string]int64),
|
||||
|
||||
spotPrice: make(map[string]float64),
|
||||
}
|
||||
_ = ms.StartFutureWsMarkPrice()
|
||||
_ = ms.StartSpotWsMarkPrice()
|
||||
return ms
|
||||
}
|
||||
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