Skip to content

Commit

Permalink
Kraken: Use websocket sub management
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
  • Loading branch information
gbjk committed Oct 1, 2023
1 parent 5d4f2a2 commit b54c999
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 717 deletions.
542 changes: 173 additions & 369 deletions exchanges/kraken/kraken_test.go

Large diffs are not rendered by default.

51 changes: 20 additions & 31 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/stream"
)

const (
Expand Down Expand Up @@ -75,6 +75,10 @@ const (

var (
assetTranslator assetTranslatorStore

errNoWebsocketOrderbookData = errors.New("no websocket orderbook data")
errNoRequestID = errors.New("no RequestID in response")
errMaxDepthMissing = errors.New("MaxDepth missing for subscription")
)

// GenericResponse stores general response data for functions that only return success
Expand Down Expand Up @@ -495,43 +499,37 @@ 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).
Subscription WebsocketSubscriptionData `json:"subscription,omitempty"`
Channels []stream.ChannelSubscription `json:"-"` // Keeps track of associated subscriptions in batched outgoings
}

// WebsocketBaseEventRequest Just has an "event" property
type WebsocketBaseEventRequest struct {
Event string `json:"event"` // eg "unsubscribe"
// WebsocketSubscribeRequest contains request data for Subscribing to channels
type WebsocketSubscribeRequest struct {
Event string `json:"event"` // "subscribe"
RequestID int64 `json:"reqid,omitempty"`
Pairs []string `json:"pair,omitempty"`
Subscription WebsocketSubscriptionData `json:"subscription,omitempty"`
}

// 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"`
// WebsocketUnsubscribeRequest contains request data for Unsubscribing from channels
type WebsocketUnsubscribeRequest struct {
Event string `json:"event"` // "unsubscribe"
RequestID int64 `json:"reqid,omitempty"`
Pairs []string `json:"pair,omitempty"`
Subscription WebsocketSubscriptionData `json:"subscription,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
Depth int `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

}

// 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 @@ -556,15 +554,6 @@ 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 b54c999

Please sign in to comment.