Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions: Relax subscription validation for non-existent pairs #1635

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It

subs := List{}

if len(s.Pairs) != 0 {
// We deliberately do not check Availability of sub Pairs because users have edge cases to subscribe to non-existent pairs
for a := range ap {
ap[a] = s.Pairs
}
}

switch s.Asset {
case asset.All:
subCtx.AssetPairs = ap
Expand All @@ -118,15 +125,6 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It
}
}

if len(s.Pairs) != 0 {
for a, pairs := range subCtx.AssetPairs {
if err := pairs.ContainsAll(s.Pairs, true); err != nil { //nolint:govet // Shadow, or gocritic will complain sloppyReassign
return nil, err
}
subCtx.AssetPairs[a] = s.Pairs
}
}

buf := &bytes.Buffer{}
if err := t.Execute(buf, subCtx); err != nil { //nolint:govet // Shadow, or gocritic will complain sloppyReassign
return nil, err
Expand Down
11 changes: 8 additions & 3 deletions exchanges/subscription/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ func TestExpandTemplates(t *testing.T) {
}
equalLists(t, exp, got)

// Users can specify pairs which aren't available, even across diverse assets
// Use-case: Coinbasepro user sub for futures BTC-USD would return all BTC pairs and all USD pairs, even though BTC-USD might not be enabled or available
gloriousCode marked this conversation as resolved.
Show resolved Hide resolved
p := currency.Pairs{currency.NewPairWithDelimiter("BEAR", "PEAR", "🐻")}
got, err = List{{Channel: "expand-pairs", Asset: asset.All, Pairs: p}}.ExpandTemplates(e)
require.NoError(t, err, "Must not error with fictional pairs")
exp = List{{Channel: "expand-pairs", QualifiedChannel: "spot-PEARBEAR-expand-pairs@0", Asset: asset.Spot, Pairs: p}}
equalLists(t, exp, got)

// Error cases
_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")

_, err = List{{Channel: "single-channel", Asset: asset.Spot, Pairs: currency.Pairs{currency.NewPairWithDelimiter("NOPE", "POPE", "🐰")}}}.ExpandTemplates(e)
assert.ErrorIs(t, err, currency.ErrPairNotContainedInAvailablePairs, "Should error correctly when pair not available")

e.tpl = "errors.tmpl"

_, err = List{{Channel: "error1"}}.ExpandTemplates(e)
Expand Down
Loading