From 1efd8e0db0551b8e70f06d698823202650da7217 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 10 Jan 2025 00:21:02 +0000 Subject: [PATCH] Subscriptions: Fix IBotExchange not implementing sub.IExchange (#1765) Thusfar ExpandTemplates had only been called internally. All methods consumers might want to get, in this case GetPairFormat, need to be in the interface we're passing out, otherwise users can't call them (or things that use them, like ExpandTemplates) --- exchanges/interfaces.go | 1 + exchanges/subscription/interface_test.go | 16 ++++++++++++++++ exchanges/subscription/list.go | 7 ++++--- exchanges/subscription/template.go | 4 ++-- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 exchanges/subscription/interface_test.go diff --git a/exchanges/interfaces.go b/exchanges/interfaces.go index 0f6071a8..6ef32727 100644 --- a/exchanges/interfaces.go +++ b/exchanges/interfaces.go @@ -51,6 +51,7 @@ type IBotExchange interface { UpdateTradablePairs(ctx context.Context, forceUpdate bool) error GetEnabledPairs(a asset.Item) (currency.Pairs, error) GetAvailablePairs(a asset.Item) (currency.Pairs, error) + GetPairFormat(asset.Item, bool) (currency.PairFormat, error) SetPairs(pairs currency.Pairs, a asset.Item, enabled bool) error GetAssetTypes(enabled bool) asset.Items GetRecentTrades(ctx context.Context, p currency.Pair, a asset.Item) ([]trade.Data, error) diff --git a/exchanges/subscription/interface_test.go b/exchanges/subscription/interface_test.go new file mode 100644 index 00000000..c608c97c --- /dev/null +++ b/exchanges/subscription/interface_test.go @@ -0,0 +1,16 @@ +package subscription_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + exchange "github.com/thrasher-corp/gocryptotrader/exchanges" + shared "github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues" + "github.com/thrasher-corp/gocryptotrader/exchanges/subscription" +) + +// TestIExchange ensures that IExchange is a subset of IBotExchange, so when an exchange is passed by interface, it can still use ExpandTemplates +func TestIExchange(t *testing.T) { + assert.Implements(t, (*subscription.IExchange)(nil), exchange.IBotExchange(&shared.CustomEx{})) + var _ subscription.IExchange = exchange.IBotExchange(nil) +} diff --git a/exchanges/subscription/list.go b/exchanges/subscription/list.go index 6dc871a3..5cadd716 100644 --- a/exchanges/subscription/list.go +++ b/exchanges/subscription/list.go @@ -14,7 +14,8 @@ type List []*Subscription type assetPairs map[asset.Item]currency.Pairs -type iExchange interface { +// IExchange provides method requirements for exchanges to use subscription templating +type IExchange interface { GetAssetTypes(enabled bool) asset.Items GetEnabledPairs(asset.Item) (currency.Pairs, error) GetPairFormat(asset.Item, bool) (currency.PairFormat, error) @@ -93,7 +94,7 @@ func (l List) SetStates(state State) error { return err } -func fillAssetPairs(ap assetPairs, a asset.Item, e iExchange) error { +func fillAssetPairs(ap assetPairs, a asset.Item, e IExchange) error { p, err := e.GetEnabledPairs(a) if err != nil { return err @@ -107,7 +108,7 @@ func fillAssetPairs(ap assetPairs, a asset.Item, e iExchange) error { } // assetPairs returns a map of enabled pairs for the subscriptions in the list, formatted for the asset -func (l List) assetPairs(e iExchange) (assetPairs, error) { +func (l List) assetPairs(e IExchange) (assetPairs, error) { at := []asset.Item{} for _, a := range e.GetAssetTypes(true) { if e.IsAssetWebsocketSupported(a) { diff --git a/exchanges/subscription/template.go b/exchanges/subscription/template.go index ad48f3b4..c076a6c8 100644 --- a/exchanges/subscription/template.go +++ b/exchanges/subscription/template.go @@ -42,7 +42,7 @@ type tplCtx struct { // Calls e.GetSubscriptionTemplate to find a template for each subscription // Filters out Authenticated subscriptions if !e.CanUseAuthenticatedEndpoints // See README.md for more details -func (l List) ExpandTemplates(e iExchange) (List, error) { +func (l List) ExpandTemplates(e IExchange) (List, error) { if !slices.ContainsFunc(l, func(s *Subscription) bool { return s.QualifiedChannel == "" }) { // Empty list, or already processed return slices.Clone(l), nil @@ -82,7 +82,7 @@ func (l List) ExpandTemplates(e iExchange) (List, error) { return subs, err } -func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.Items) (List, error) { +func expandTemplate(e IExchange, s *Subscription, ap assetPairs, assets asset.Items) (List, error) { if s.QualifiedChannel != "" { return List{s}, nil }