Skip to content

Commit

Permalink
fixup! Gemini: Add subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 20, 2024
1 parent 7077234 commit 2111398
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 8 additions & 7 deletions exchanges/gemini/gemini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,16 +1313,17 @@ func TestGenerateSubscriptions(t *testing.T) {
subs, err := g.generateSubscriptions()
require.NoError(t, err)
exp := subscription.List{
{Asset: asset.Spot, Channel: subscription.CandlesChannel, Pairs: p, QualifiedChannel: "candles_1d"},
{Asset: asset.Spot, Channel: subscription.CandlesChannel, Pairs: p, QualifiedChannel: "candles_1d", Interval: kline.OneDay},
{Asset: asset.Spot, Channel: subscription.OrderbookChannel, Pairs: p, QualifiedChannel: "l2"},
}
testsubs.EqualLists(t, exp, subs)

for _, i := range []kline.Interval{kline.OneMin, kline.FiveMin, kline.FifteenMin, kline.ThirtyMin, kline.OneHour, kline.SixHour, kline.OneDay} {
exp := i.Short()
if exp == "24h" {
exp = "1d"
}
t.Log(i.Short())
for _, i := range []kline.Interval{kline.OneMin, kline.FiveMin, kline.FifteenMin, kline.ThirtyMin, kline.OneHour, kline.SixHour} {
subs, err = subscription.List{{Asset: asset.Spot, Channel: subscription.CandlesChannel, Pairs: p, Interval: i}}.ExpandTemplates(g)
assert.NoErrorf(t, err, "ExpandTemplates should not error on interval %s", i)
require.NotEmpty(t, subs)
assert.Equal(t, "candles_"+i.Short(), subs[0].QualifiedChannel)
}
subs, err = subscription.List{{Asset: asset.Spot, Channel: subscription.CandlesChannel, Pairs: p, Interval: kline.FourHour}}.ExpandTemplates(g)

Check failure on line 1327 in exchanges/gemini/gemini_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to subs (ineffassign)
assert.ErrorIs(t, err, kline.ErrUnsupportedInterval, "ExpandTemplates should error on invalid interval")
}
17 changes: 15 additions & 2 deletions exchanges/gemini/gemini_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func (g *Gemini) generateSubscriptions() (subscription.List, error) {

// GetSubscriptionTemplate returns a subscription channel template
func (g *Gemini) GetSubscriptionTemplate(_ *subscription.Subscription) (*template.Template, error) {
return template.New("master.tmpl").Funcs(template.FuncMap{"channelName": channelName}).Parse(subTplText)
return template.New("master.tmpl").Funcs(template.FuncMap{
"channelName": channelName,
"interval": channelInterval,
}).Parse(subTplText)
}

// Subscribe sends a websocket message to receive data from the channel
Expand Down Expand Up @@ -572,10 +575,20 @@ func channelName(s *subscription.Subscription) string {
panic(fmt.Errorf("%w: %s", subscription.ErrNotSupported, s.Channel))
}

func channelInterval(i kline.Interval) string {
switch i {
case kline.OneMin, kline.FiveMin, kline.FifteenMin, kline.ThirtyMin, kline.OneHour, kline.SixHour:
return i.Short()
case kline.OneDay:
return "1d"
}
panic(fmt.Errorf("%w: %s", kline.ErrUnsupportedInterval, i.Short()))
}

const subTplText = `
{{ range $asset, $pairs := $.AssetPairs }}
{{- channelName $.S -}}
{{- with $i := $.S.Interval -}} _ {{- $i.Short }}{{ end -}}
{{- with $i := $.S.Interval -}} _ {{- interval $i }}{{ end -}}
{{- $.AssetSeparator }}
{{- end }}
`

0 comments on commit 2111398

Please sign in to comment.