Skip to content

Commit

Permalink
fixup! Kucoin: Simplify GenerateDefaultSubs
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 27, 2023
1 parent 24468d9 commit 62be3e8
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions exchanges/kucoin/kucoin_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,21 @@ const (
marketOrderbookLevel2Channels = "/market/level2:%s" // /market/level2:{symbol},{symbol}...
marketOrderbookLevel2to5Channel = "/spotMarket/level2Depth5:%s" // /spotMarket/level2Depth5:{symbol},{symbol}...
marketOrderbokLevel2To50Channel = "/spotMarket/level2Depth50:%s" // /spotMarket/level2Depth50:{symbol},{symbol}...
marketCandlesChannel = "/market/candles:%s_%s" // /market/candles:{symbol}_{type}
marketCandlesChannel = "/market/candles:%s_%s" // /market/candles:{symbol}_{interval}
marketMatchChannel = "/market/match:%s" // /market/match:{symbol},{symbol}...
indexPriceIndicatorChannel = "/indicator/index:%s" // /indicator/index:{symbol0},{symbol1}..
markPriceIndicatorChannel = "/indicator/markPrice:%s" // /indicator/markPrice:{symbol0},{symbol1}...
marginFundingbookChangeChannel = "/margin/fundingBook:%s" // /margin/fundingBook:{currency0},{currency1}...

// Private channel

// Private channels
privateSpotTradeOrders = "/spotMarket/tradeOrders"
accountBalanceChannel = "/account/balance"
marginPositionChannel = "/margin/position"
marginLoanChannel = "/margin/loan:%s" // /margin/loan:{currency}
spotMarketAdvancedChannel = "/spotMarket/advancedOrders"

// futures channels

futuresChannelPrefix = "/contract"
futuresTickerV2Channel = "/contractMarket/tickerV2:%s" // /contractMarket/tickerV2:{symbol}
futuresTickerChannel = "/contractMarket/ticker:%s" // /contractMarket/ticker:{symbol}
futuresOrderbookLevel2Channel = "/contractMarket/level2:%s" // /contractMarket/level2:{symbol}
Expand All @@ -68,7 +67,6 @@ const (
futuresTrasactionStatisticsTimerEventChannel = "/contractMarket/snapshot:%s" // /contractMarket/snapshot:{symbol}

// futures private channels

futuresTradeOrdersBySymbolChannel = "/contractMarket/tradeOrders:%s" // /contractMarket/tradeOrders:{symbol}
futuresTradeOrderChannel = "/contractMarket/tradeOrders"
futuresStopOrdersLifecycleEventChannel = "/contractMarket/advancedOrders"
Expand Down Expand Up @@ -1056,26 +1054,18 @@ func (ku *Kucoin) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
s.Params = map[string]any{}
}

switch s.Channel {
case marketTickerSnapshotChannel, marketOrderbookLevel2Channels, marketTickerSnapshotForCurrencyChannel, marketOrderbookLevel2to5Channel,
marketOrderbokLevel2To50Channel, marketTickerChannel, indexPriceIndicatorChannel, markPriceIndicatorChannel, marketMatchChannel, marketCandlesChannel:
// Subscriptions which can use a single comma-separated sub per asset
subs := subConcatSymbols(assetPairs, s)
subscriptions = append(subscriptions, subs...)
case marginLoanChannel:
switch {
case s.Channel == marginLoanChannel:
s.Asset = asset.Margin
for _, c := range assetPairs[asset.Margin].GetCurrencies() {
s.Currency = currency.Pair{Base: c}
subscriptions = append(subscriptions, s)
}
case marginFundingbookChangeChannel:
case s.Channel == marginFundingbookChangeChannel:
s.Asset = asset.Margin
s.Params["currencies"] = assetPairs[asset.Margin].GetCurrencies().Join()
subscriptions = append(subscriptions, s)
case futuresTickerV2Channel, futuresTickerChannel, futuresExecutionDataChannel, futuresOrderbookLevel2Channel, futuresOrderbookLevel2Depth5Channel,
futuresOrderbookLevel2Depth50Channel, futuresContractMarketDataChannel, futuresTradeOrdersBySymbolChannel, futuresPositionChangeEventChannel,
futuresTrasactionStatisticsTimerEventChannel:

case isFuturesChannel(s.Channel) && isSymbolChannel(s.Channel):
s.Asset = asset.Futures
for _, p := range assetPairs[asset.Futures] {
var err error
Expand All @@ -1084,13 +1074,27 @@ func (ku *Kucoin) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
}
subscriptions = append(subscriptions, s)
}
case isSymbolChannel(s.Channel):
// Subscriptions which can use a single comma-separated sub per asset
subs := subConcatSymbols(assetPairs, s)
subscriptions = append(subscriptions, subs...)
default:
subscriptions = append(subscriptions, s)
}
}
return subscriptions, nil
}

// isFuturesChannel returns true it this channel path starts with the common futures prefix '/contract'
func isFuturesChannel(c string) bool {
return strings.HasPrefix(c, futuresChannelPrefix)
}

// isSymbolChannel returns true it this channel path ends in a formatting %s to accept a Symbol
func isSymbolChannel(c string) bool {
return strings.HasSuffix(c, "%s")
}

// channelName converts global channel Names used in config of channel input into kucoin channel names
// returns the name unchanged if no match is found
func channelName(name string) string {
Expand Down

0 comments on commit 62be3e8

Please sign in to comment.