Skip to content

Commit

Permalink
fixup! Websockets: Add Subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 7, 2023
1 parent 0a0b81a commit 054e4ea
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
20 changes: 6 additions & 14 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
Expand Down Expand Up @@ -307,19 +306,12 @@ type FeaturesSupportedConfig struct {

// FeaturesEnabledConfig stores the exchanges enabled features
type FeaturesEnabledConfig struct {
AutoPairUpdates bool `json:"autoPairUpdates"`
Websocket bool `json:"websocketAPI"`
SaveTradeData bool `json:"saveTradeData"`
TradeFeed bool `json:"tradeFeed"`
FillsFeed bool `json:"fillsFeed"`
Subscriptions []subscription.Subscription `json:"subscriptions,omitempty"`
}

type EnabledSubscriptionConfig struct {
Channel string `json:"channel"`
Interval kline.Interval `json:"interval,omitempty"`
Levels int `json:"levels,omitempty"`
Authenticated bool `json:"authenticated,omitempty"`
AutoPairUpdates bool `json:"autoPairUpdates"`
Websocket bool `json:"websocketAPI"`
SaveTradeData bool `json:"saveTradeData"`
TradeFeed bool `json:"tradeFeed"`
FillsFeed bool `json:"fillsFeed"`
Subscriptions []*subscription.Subscription `json:"subscriptions,omitempty"`
}

// FeaturesConfig stores the exchanges supported and enabled features
Expand Down
2 changes: 1 addition & 1 deletion exchanges/exchange_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type FeaturesEnabled struct {
SaveTradeData bool
TradeFeed bool
FillsFeed bool
Subscriptions []subscription.Subscription
Subscriptions []*subscription.Subscription
}

// FeaturesSupported stores the exchanges supported features
Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Subscription struct {
// MarshalJSON generates a JSON representation of a Subscription, specifically for config writing
// The only reason it exists is to avoid having to make Pair a pointer, since that would be generally painful
// If Pair becomes a pointer, this method is redundant and should be removed
func (s Subscription) MarshalJSON() ([]byte, error) {
func (s *Subscription) MarshalJSON() ([]byte, error) {
// None of the usual type embedding tricks seem to work for not emitting an nil Pair
// The embedded type's Pair always fills the empty value
type MaybePair struct {
Expand Down
8 changes: 4 additions & 4 deletions exchanges/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ func TestEnsureKeyed(t *testing.T) {
// TestMarshalling logic test
func TestMarshaling(t *testing.T) {
t.Parallel()
j, err := json.Marshal(Subscription{Channel: CandlesChannel})
j, err := json.Marshal(&Subscription{Channel: CandlesChannel})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"candles"}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4})
j, err = json.Marshal(&Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"orderbook","interval":"5m","levels":4}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4, Pair: currency.NewPair(currency.BTC, currency.USDT)})
j, err = json.Marshal(&Subscription{Channel: OrderbookChannel, Interval: kline.FiveMin, Levels: 4, Pair: currency.NewPair(currency.BTC, currency.USDT)})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"orderbook","interval":"5m","levels":4,"pair":"BTCUSDT"}`, string(j), "Marshalling should be clean and concise")

j, err = json.Marshal(Subscription{Channel: MyTradesChannel, Authenticated: true})
j, err = json.Marshal(&Subscription{Channel: MyTradesChannel, Authenticated: true})
assert.NoError(t, err, "Marshalling should not error")
assert.Equal(t, `{"channel":"myTrades","authenticated":true}`, string(j), "Marshalling should be clean and concise")
}

0 comments on commit 054e4ea

Please sign in to comment.