Skip to content

Commit

Permalink
fixup! Websockets: Add Subscription configuration
Browse files Browse the repository at this point in the history
Websockets: Switch to kline.Interval for interval

There's a slight hack here that we're probably going to abuse Interval
for orderbook intervals too.
It that's too distasteful can break that out into rate, but honestly
think we'd end up using the same mechanics.
Maybe better to break kline.Interval out into common/interval/* in that
case
  • Loading branch information
gbjk committed Oct 23, 2023
1 parent ef21417 commit ec57509
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
9 changes: 5 additions & 4 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
"github.com/thrasher-corp/gocryptotrader/log"
Expand Down Expand Up @@ -314,10 +315,10 @@ type FeaturesEnabledConfig struct {
}

type EnabledSubscriptionConfig struct {

Check warning on line 317 in config/config_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type EnabledSubscriptionConfig should have comment or be unexported (revive)
Channel string `json:"channel"`
Interval string `json:"interval,omitempty"`
Levels int `json:"levels,omitempty"`
Authenticated bool `json:"authenticated,omitempty"`
Channel string `json:"channel"`
Interval kline.Interval `json:"interval,omitempty"`
Levels int `json:"levels,omitempty"`
Authenticated bool `json:"authenticated,omitempty"`
}

// FeaturesConfig stores the exchanges supported and enabled features
Expand Down
4 changes: 3 additions & 1 deletion exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,9 @@ func TestGenerateSubscriptions(t *testing.T) {
}
}
}
assert.ElementsMatch(t, subs, expected, "Should get the correct subscriptions")
if assert.Len(t, subs, len(expected), "Should have the correct number of subs") {
assert.ElementsMatch(t, subs, expected, "Should get the correct subscriptions")
}
}

var websocketDepthUpdate = []byte(`{"E":1608001030784,"U":7145637266,"a":[["19455.19000000","0.59490200"],["19455.37000000","0.00000000"],["19456.11000000","0.00000000"],["19456.16000000","0.00000000"],["19458.67000000","0.06400000"],["19460.73000000","0.05139800"],["19461.43000000","0.00000000"],["19464.59000000","0.00000000"],["19466.03000000","0.45000000"],["19466.36000000","0.00000000"],["19508.67000000","0.00000000"],["19572.96000000","0.00217200"],["24386.00000000","0.00256600"]],"b":[["19455.18000000","2.94649200"],["19453.15000000","0.01233600"],["19451.18000000","0.00000000"],["19446.85000000","0.11427900"],["19446.74000000","0.00000000"],["19446.73000000","0.00000000"],["19444.45000000","0.14937800"],["19426.75000000","0.00000000"],["19416.36000000","0.36052100"]],"e":"depthUpdate","s":"BTCUSDT","u":7145637297}`)
Expand Down
16 changes: 6 additions & 10 deletions exchanges/binance/binance_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (b *Binance) GenerateSubscriptions() ([]stream.ChannelSubscription, error)
func channelName(s stream.ChannelSubscription) (string, error) {
name, ok := subscriptionNames[s.Channel]
if !ok {
// TODO: Name error
// TODO: const err type please
return name, fmt.Errorf("%w: %s", errors.New("Unsupported subscription channel"), s.Channel)
}

Expand All @@ -595,17 +595,13 @@ func channelName(s stream.ChannelSubscription) (string, error) {
if s.Levels != 0 {
name += "@" + strconv.Itoa(s.Levels)
}
interval := s.Interval
if interval == "" {
interval = "100ms"
if s.Interval.Duration() == time.Second {
name += "@1000ms"
} else {
name += "@" + s.Interval.Short()
}
name += "@" + interval
case stream.CandlesSubscription:
interval := s.Interval
if interval == "" {
interval = "1m"
}
name += "_" + interval
name += "_" + s.Interval.Short()
}
return name, nil
}
Expand Down
4 changes: 2 additions & 2 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ func (b *Binance) SetDefaults() {
Subscriptions: []stream.ChannelSubscription{
{Channel: stream.TickerSubscription},
{Channel: stream.AllTradesSubscription},
{Channel: stream.CandlesSubscription, Interval: "1m"},
{Channel: stream.OrderbookSubscription, Interval: "100ms"},
{Channel: stream.CandlesSubscription, Interval: kline.OneMin},
{Channel: stream.OrderbookSubscription, Interval: kline.HundredMilliseconds},
},
},
}
Expand Down
11 changes: 11 additions & 0 deletions exchanges/kline/kline.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ func (i Interval) Short() string {
return s
}

// UnmarshalText implements the TextUnmarshaller interface for Intervals
// It does not validate the duration is aligned, only that it is a parsable duration
func (i *Interval) UnmarshalText(text []byte) error {
d, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*i = Interval(d)
return nil
}

// addPadding inserts padding time aligned when exchanges do not supply all data
// when there is no activity in a certain time interval.
// Start defines the request start and due to potential no activity from this
Expand Down
3 changes: 2 additions & 1 deletion exchanges/stream/stream_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ type ChannelSubscription struct {
Currency currency.Pair
Asset asset.Item
Params map[string]interface{}
Interval string
Interval kline.Interval
Levels int
Authenticated bool
}
Expand Down

0 comments on commit ec57509

Please sign in to comment.