Skip to content

Commit

Permalink
Kucoin: Minor refactoring in GenerateSubscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 23, 2023
1 parent 1cc0b72 commit 315148f
Showing 1 changed file with 48 additions and 89 deletions.
137 changes: 48 additions & 89 deletions exchanges/kucoin/kucoin_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,27 +1067,12 @@ func (ku *Kucoin) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
futuresAccountBalanceEventChannel)
}
}
var err error
var spotPairs currency.Pairs
if ku.CurrencyPairs.IsAssetEnabled(asset.Spot) == nil {
spotPairs, err = ku.GetEnabledPairs(asset.Spot)
if err != nil {
return nil, err
}
}
var marginPairs currency.Pairs
if ku.CurrencyPairs.IsAssetEnabled(asset.Margin) == nil {
marginPairs, err = ku.GetEnabledPairs(asset.Margin)
if err != nil {
return nil, err
}
}

var futuresPairs currency.Pairs
if ku.CurrencyPairs.IsAssetEnabled(asset.Futures) == nil {
futuresPairs, err = ku.GetEnabledPairs(asset.Futures)
if err != nil {
return nil, err
assetPairs := map[asset.Item]currency.Pairs{}
for _, a := range ku.GetAssetTypes(false) {
if p, err := ku.GetEnabledPairs(a); err == nil {
assetPairs[a] = p
} else {
assetPairs[a] = currency.Pairs{} // err is probably that Asset isn't enabled, but we don't care about any errors here
}
}
marginLoanCurrencyCheckMap := map[currency.Code]bool{}
Expand All @@ -1107,98 +1092,72 @@ func (ku *Kucoin) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
marketOrderbookLevel2to5Channel,
marketOrderbokLevel2To50Channel,
marketTickerChannel:
subscribedPairsMap := map[string]bool{}
for b := range spotPairs {
if okay := subscribedPairsMap[spotPairs[b].String()]; okay {
continue
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Spot,
Currency: spotPairs[b],
})
subscribedPairsMap[spotPairs[b].String()] = true
}
for b := range marginPairs {
if okay := subscribedPairsMap[marginPairs[b].String()]; okay {
continue
subscribedPairsMap := map[currency.Pair]bool{}
for _, a := range []asset.Item{asset.Spot, asset.Margin} {
for _, p := range assetPairs[a] {
if exists := subscribedPairsMap[p]; exists {
continue // If subscribed to any of these channels for spot, don't need to subscribe again for margin
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: a,
Currency: p,
})
subscribedPairsMap[p] = true
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Margin,
Currency: marginPairs[b],
})
subscribedPairsMap[marginPairs[b].String()] = true
}
case indexPriceIndicatorChannel,
markPriceIndicatorChannel,
marketMatchChannel:
pairs := currency.Pairs{}
for p := range spotPairs {
pairs = pairs.Add(spotPairs[p])
}
for p := range marginPairs {
pairs = pairs.Add(marginPairs[p])
for _, a := range []asset.Item{asset.Spot, asset.Margin} {
for _, p := range assetPairs[a] {
pairs = pairs.Add(p)
}
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Spot,
Params: map[string]interface{}{"symbols": pairs.Join()},
})
case marketCandlesChannel:
subscribedPairsMap := map[string]bool{}
for p := range spotPairs {
if okay := subscribedPairsMap[spotPairs[p].String()]; okay {
continue
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Spot,
Currency: spotPairs[p],
Params: map[string]interface{}{"interval": kline.FifteenMin},
})
subscribedPairsMap[spotPairs[p].String()] = true
}
for p := range marginPairs {
if okay := subscribedPairsMap[marginPairs[p].String()]; okay {
continue
subscribedPairsMap := map[currency.Pair]bool{}
for _, a := range []asset.Item{asset.Spot, asset.Margin} {
for _, p := range assetPairs[a] {
if exists := subscribedPairsMap[p]; exists {
continue // If subscribed to any of these channels for spot, don't need to subscribe again for margin
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: a,
Currency: p,
Params: map[string]interface{}{"interval": kline.FifteenMin},
})
subscribedPairsMap[p] = true
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Margin,
Currency: marginPairs[p],
Params: map[string]interface{}{"interval": kline.FifteenMin},
})
subscribedPairsMap[marginPairs[p].String()] = true
}
case marginLoanChannel:
for b := range marginPairs {
if !marginLoanCurrencyCheckMap[marginPairs[b].Quote] {
for _, p := range assetPairs[asset.Margin] {
if !marginLoanCurrencyCheckMap[p.Quote] {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Currency: currency.Pair{Base: marginPairs[b].Quote},
Currency: currency.Pair{Base: p.Quote},
})
marginLoanCurrencyCheckMap[marginPairs[b].Quote] = true
marginLoanCurrencyCheckMap[p.Quote] = true
}
if !marginLoanCurrencyCheckMap[marginPairs[b].Base] {
if !marginLoanCurrencyCheckMap[p.Base] {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Currency: currency.Pair{Base: marginPairs[b].Base},
Currency: currency.Pair{Base: p.Base},
})
marginLoanCurrencyCheckMap[marginPairs[b].Base] = true
marginLoanCurrencyCheckMap[p.Base] = true
}
}
case marginFundingbookChangeChannel:
currencyExist := map[currency.Code]bool{}
for b := range marginPairs {
okay := currencyExist[marginPairs[b].Base]
if !okay {
currencyExist[marginPairs[b].Base] = true
}
okay = currencyExist[marginPairs[b].Quote]
if !okay {
currencyExist[marginPairs[b].Quote] = true
}
for _, p := range assetPairs[asset.Margin] {
currencyExist[p.Base] = true
currencyExist[p.Quote] = true
}
var currencies string
for b := range currencyExist {
Expand All @@ -1219,15 +1178,15 @@ func (ku *Kucoin) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
futuresTradeOrdersBySymbolChannel,
futuresPositionChangeEventChannel,
futuresTrasactionStatisticsTimerEventChannel:
for b := range futuresPairs {
futuresPairs[b], err = ku.FormatExchangeCurrency(futuresPairs[b], asset.Futures)
for _, p := range assetPairs[asset.Futures] {
pF, err := ku.FormatExchangeCurrency(p, asset.Futures)
if err != nil {
continue
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[x],
Asset: asset.Futures,
Currency: futuresPairs[b],
Currency: pF,
})
}
}
Expand Down

0 comments on commit 315148f

Please sign in to comment.