Skip to content

Commit

Permalink
Kraken: Sub Channel improvements
Browse files Browse the repository at this point in the history
* Use Websocket subscriptionChannels instead of local slice
* Remove ChannelID - Deprecated in docs
* Simplify ping handlers and hardcodes message
* Add Depth as configurable orderbook channel param
* Simplify auth/non-auth channel updates
* Add configurable Book depth
* Add configurable Candle timeframes

Kraken: Simplify all WS handlers with reqId
  • Loading branch information
gbjk committed Jul 24, 2024
1 parent 6d16367 commit 6c5c0e3
Show file tree
Hide file tree
Showing 8 changed files with 1,074 additions and 1,795 deletions.
1,636 changes: 467 additions & 1,169 deletions exchanges/kraken/kraken_test.go

Large diffs are not rendered by default.

57 changes: 20 additions & 37 deletions exchanges/kraken/kraken_types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package kraken

import (
"errors"
"time"

"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/types"
)

Expand Down Expand Up @@ -72,10 +72,19 @@ const (
statusOpen = "open"

krakenFormat = "2006-01-02T15:04:05.000Z"

// ChannelOrderbookDepthKey configures the orderbook depth in stream.ChannelSubscription.Params
ChannelOrderbookDepthKey = "_depth"
// ChannelCandlesTimeframeKey configures the candle bar timeframe in stream.ChannelSubscription.Params
ChannelCandlesTimeframeKey = "_timeframe"
)

var (
assetTranslator assetTranslatorStore

errNoWebsocketOrderbookData = errors.New("no websocket orderbook data")
errNoRequestID = errors.New("no RequestID in response")
errBadChannelSuffix = errors.New("bad websocket channel suffix")
)

// GenericResponse stores general response data for functions that only return success
Expand Down Expand Up @@ -498,43 +507,29 @@ type WithdrawStatusResponse struct {
Status string `json:"status"`
}

// WebsocketSubscriptionEventRequest handles WS subscription events
type WebsocketSubscriptionEventRequest struct {
Event string `json:"event"` // subscribe
RequestID int64 `json:"reqid,omitempty"` // Optional, client originated ID reflected in response message.
Pairs []string `json:"pair,omitempty"` // Array of currency pairs (pair1,pair2,pair3).
// WebsocketSubRequest contains request data for Subscribe/Unsubscribe to channels
type WebsocketSubRequest struct {
Event string `json:"event"`
RequestID int64 `json:"reqid,omitempty"`
Pairs []string `json:"pair,omitempty"`
Subscription WebsocketSubscriptionData `json:"subscription,omitempty"`
Channels subscription.List `json:"-"` // Keeps track of associated subscriptions in batched outgoings
}

// WebsocketBaseEventRequest Just has an "event" property
type WebsocketBaseEventRequest struct {
Event string `json:"event"` // eg "unsubscribe"
}

// WebsocketUnsubscribeByChannelIDEventRequest handles WS unsubscribe events
type WebsocketUnsubscribeByChannelIDEventRequest struct {
WebsocketBaseEventRequest
RequestID int64 `json:"reqid,omitempty"` // Optional, client originated ID reflected in response message.
Pairs []string `json:"pair,omitempty"` // Array of currency pairs (pair1,pair2,pair3).
ChannelID int64 `json:"channelID,omitempty"`
}

// WebsocketSubscriptionData contains details on WS channel
type WebsocketSubscriptionData struct {
Name string `json:"name,omitempty"` // ticker|ohlc|trade|book|spread|*, * for all (ohlc interval value is 1 if all channels subscribed)
Interval int64 `json:"interval,omitempty"` // Optional - Time interval associated with ohlc subscription in minutes. Default 1. Valid Interval values: 1|5|15|30|60|240|1440|10080|21600
Depth int64 `json:"depth,omitempty"` // Optional - depth associated with book subscription in number of levels each side, default 10. Valid Options are: 10, 25, 100, 500, 1000
Token string `json:"token,omitempty"` // Optional used for authenticated requests
Interval int `json:"interval,omitempty"` // Optional - Timeframe for candles subscription in minutes; default 1. Valid: 1|5|15|30|60|240|1440|10080|21600
Depth int `json:"depth,omitempty"` // Optional - Depth associated with orderbook; default 10. Valid: 10|25|100|500|1000
Token string `json:"token,omitempty"` // Optional - Token for authenticated channels

}

// WebsocketEventResponse holds all data response types
type WebsocketEventResponse struct {
WebsocketBaseEventRequest
Event string `json:"event"`
Status string `json:"status"`
Pair currency.Pair `json:"pair,omitempty"`
RequestID int64 `json:"reqid,omitempty"` // Optional, client originated ID reflected in response message.
RequestID int64 `json:"reqid,omitempty"`
Subscription WebsocketSubscriptionResponseData `json:"subscription,omitempty"`
ChannelName string `json:"channelName,omitempty"`
WebsocketSubscriptionEventResponse
Expand All @@ -551,23 +546,11 @@ type WebsocketSubscriptionResponseData struct {
Name string `json:"name"`
}

// WebsocketDataResponse defines a websocket data type
type WebsocketDataResponse []interface{}

// WebsocketErrorResponse defines a websocket error response
type WebsocketErrorResponse struct {
ErrorMessage string `json:"errorMessage"`
}

// WebsocketChannelData Holds relevant data for channels to identify what we're
// doing
type WebsocketChannelData struct {
Subscription string
Pair currency.Pair
ChannelID *int64
MaxDepth int
}

// WsTokenResponse holds the WS auth token
type WsTokenResponse struct {
Error []string `json:"error"`
Expand Down
Loading

0 comments on commit 6c5c0e3

Please sign in to comment.