Skip to content

Commit

Permalink
OKx: Fix websocket subscriptions and casing (thrasher-corp#1221)
Browse files Browse the repository at this point in the history
* FIX UPPER CASE ASSET PROBLEM

* JUDAS

* FIX SUB APPENDING

* linter got me

* ran, tested, linted, fixed

* comment
  • Loading branch information
gloriousCode authored Jun 9, 2023
1 parent 9e4ea6c commit c8537c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions exchanges/okx/okx.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func (ok *Okx) GetOrderList(ctx context.Context, arg *OrderListRequestParams) ([
}
params := url.Values{}
if arg.InstrumentType != "" {
params.Set("instType", strings.ToUpper(arg.InstrumentType))
params.Set("instType", arg.InstrumentType)
}
if arg.InstrumentID != "" {
params.Set("instId", arg.InstrumentID)
Expand Down Expand Up @@ -3060,7 +3060,7 @@ func (ok *Okx) GetInstrumentTypeFromAssetItem(assetType asset.Item) string {
case asset.Options:
return okxInstTypeOption
default:
return assetType.String()
return strings.ToUpper(assetType.String())
}
}

Expand Down
72 changes: 40 additions & 32 deletions exchanges/okx/okx_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ import (
// responseStream a channel thought which the data coming from the two websocket connection will go through.
var responseStream = make(chan stream.Response)

// defaultSubscribedChannels list of channels which are subscribed by default
var defaultSubscribedChannels = []string{
okxChannelTrades,
okxChannelOrderBooks,
okxChannelCandle5m,
okxChannelTickers,
}
var (
// defaultSubscribedChannels list of channels which are subscribed by default
defaultSubscribedChannels = []string{
okxChannelTrades,
okxChannelOrderBooks,
okxChannelCandle5m,
okxChannelTickers,
}
// defaultAuthChannels list of channels which are subscribed when authenticated
defaultAuthChannels = []string{
okxChannelAccount,
okxChannelOrders,
}
)

var (
candlestickChannelsMap = map[string]bool{okxChannelCandle1Y: true, okxChannelCandle6M: true, okxChannelCandle3M: true, okxChannelCandle1M: true, okxChannelCandle1W: true, okxChannelCandle1D: true, okxChannelCandle2D: true, okxChannelCandle3D: true, okxChannelCandle5D: true, okxChannelCandle12H: true, okxChannelCandle6H: true, okxChannelCandle4H: true, okxChannelCandle2H: true, okxChannelCandle1H: true, okxChannelCandle30m: true, okxChannelCandle15m: true, okxChannelCandle5m: true, okxChannelCandle3m: true, okxChannelCandle1m: true, okxChannelCandle1Yutc: true, okxChannelCandle3Mutc: true, okxChannelCandle1Mutc: true, okxChannelCandle1Wutc: true, okxChannelCandle1Dutc: true, okxChannelCandle2Dutc: true, okxChannelCandle3Dutc: true, okxChannelCandle5Dutc: true, okxChannelCandle12Hutc: true, okxChannelCandle6Hutc: true}
candlesticksMarkPriceMap = map[string]bool{okxChannelMarkPriceCandle1Y: true, okxChannelMarkPriceCandle6M: true, okxChannelMarkPriceCandle3M: true, okxChannelMarkPriceCandle1M: true, okxChannelMarkPriceCandle1W: true, okxChannelMarkPriceCandle1D: true, okxChannelMarkPriceCandle2D: true, okxChannelMarkPriceCandle3D: true, okxChannelMarkPriceCandle5D: true, okxChannelMarkPriceCandle12H: true, okxChannelMarkPriceCandle6H: true, okxChannelMarkPriceCandle4H: true, okxChannelMarkPriceCandle2H: true, okxChannelMarkPriceCandle1H: true, okxChannelMarkPriceCandle30m: true, okxChannelMarkPriceCandle15m: true, okxChannelMarkPriceCandle5m: true, okxChannelMarkPriceCandle3m: true, okxChannelMarkPriceCandle1m: true, okxChannelMarkPriceCandle1Yutc: true, okxChannelMarkPriceCandle3Mutc: true, okxChannelMarkPriceCandle1Mutc: true, okxChannelMarkPriceCandle1Wutc: true, okxChannelMarkPriceCandle1Dutc: true, okxChannelMarkPriceCandle2Dutc: true, okxChannelMarkPriceCandle3Dutc: true, okxChannelMarkPriceCandle5Dutc: true, okxChannelMarkPriceCandle12Hutc: true, okxChannelMarkPriceCandle6Hutc: true}
Expand Down Expand Up @@ -1232,38 +1240,38 @@ func (ok *Okx) wsProcessTickers(data []byte) error {
func (ok *Okx) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
var subscriptions []stream.ChannelSubscription
assets := ok.GetAssetTypes(true)
subs := make([]string, 0, len(defaultSubscribedChannels)+len(defaultAuthChannels))
subs = append(subs, defaultSubscribedChannels...)
if ok.Websocket.CanUseAuthenticatedEndpoints() {
defaultSubscribedChannels = append(defaultSubscribedChannels,
okxChannelAccount,
okxChannelOrders,
)
subs = append(subs, defaultAuthChannels...)
}
for x := range assets {
pairs, err := ok.GetEnabledPairs(assets[x])
if err != nil {
return nil, err
}
for y := range defaultSubscribedChannels {
if defaultSubscribedChannels[y] == okxChannelCandle5m ||
defaultSubscribedChannels[y] == okxChannelTickers ||
defaultSubscribedChannels[y] == okxChannelOrders ||
defaultSubscribedChannels[y] == okxChannelOrderBooks ||
defaultSubscribedChannels[y] == okxChannelOrderBooks5 ||
defaultSubscribedChannels[y] == okxChannelOrderBooks50TBT ||
defaultSubscribedChannels[y] == okxChannelOrderBooksTBT ||
defaultSubscribedChannels[y] == okxChannelTrades {
for c := range subs {
switch subs[c] {
case okxChannelOrders:
for x := range assets {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: subs[c],
Asset: assets[x],
})
}
case okxChannelCandle5m, okxChannelTickers, okxChannelOrderBooks, okxChannelFundingRate, okxChannelOrderBooks5, okxChannelOrderBooks50TBT, okxChannelOrderBooksTBT, okxChannelTrades:
for x := range assets {
pairs, err := ok.GetEnabledPairs(assets[x])
if err != nil {
return nil, err
}
for p := range pairs {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: defaultSubscribedChannels[y],
Channel: subs[c],
Asset: assets[x],
Currency: pairs[p],
})
}
} else {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: defaultSubscribedChannels[y],
})
}
default:
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: subs[c],
})
}
}
if len(subscriptions) >= 240 {
Expand Down Expand Up @@ -1735,7 +1743,7 @@ func (ok *Okx) wsChannelSubscription(operation, channel string, assetType asset.
var format currency.PairFormat
var err error
if tInstrumentType {
instrumentType = strings.ToLower(ok.GetInstrumentTypeFromAssetItem(assetType))
instrumentType = ok.GetInstrumentTypeFromAssetItem(assetType)
if instrumentType != okxInstTypeSpot &&
instrumentType != okxInstTypeMargin &&
instrumentType != okxInstTypeSwap &&
Expand Down Expand Up @@ -1792,7 +1800,7 @@ func (ok *Okx) wsAuthChannelSubscription(operation, channel string, assetType as
var err error
var format currency.PairFormat
if params.InstrumentType {
instrumentType = strings.ToUpper(ok.GetInstrumentTypeFromAssetItem(assetType))
instrumentType = ok.GetInstrumentTypeFromAssetItem(assetType)
if instrumentType != okxInstTypeMargin &&
instrumentType != okxInstTypeSwap &&
instrumentType != okxInstTypeFutures &&
Expand Down
4 changes: 2 additions & 2 deletions exchanges/okx/okx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func (ok *Okx) UpdateTickers(ctx context.Context, assetType asset.Item) error {
return err
}
instrumentType := ok.GetInstrumentTypeFromAssetItem(assetType)
ticks, err := ok.GetTickers(ctx, strings.ToUpper(instrumentType), "", "")
ticks, err := ok.GetTickers(ctx, instrumentType, "", "")
if err != nil {
return err
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func (ok *Okx) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest)
if !ok.SupportsAsset(req.AssetType) {
return nil, fmt.Errorf("%w: %v", asset.ErrNotSupported, req.AssetType)
}
instrumentType := strings.ToUpper(ok.GetInstrumentTypeFromAssetItem(req.AssetType))
instrumentType := ok.GetInstrumentTypeFromAssetItem(req.AssetType)
endTime := req.EndTime
var resp []order.Detail
allOrders:
Expand Down

0 comments on commit c8537c4

Please sign in to comment.