mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Currency: Variadic Pairs.Add This version of Pairs.Add is simpler and [more performant](https://gist.github.com/gbjk/06a1fc1832d04ee41213ca518938cf74) Behavioural difference: If there's nothing to add, the same slice is returned unaltered. This seems like good sauce * Currency: Variadic Remove * Common: Add Batch function * Common: Add common.SortStrings for stringers * Subscriptions: Add batching to templates * Subscriptions: Sort list of pairs * Kucoin: Switch to sub templating * Kucoin: Simplify channel prefix usage * Kucoin: Fix race on fetchedFuturesOrderbook * Subscriptions: Filter AssetPairs Now only the assetPairs relevant to the subscription are in the context * Subscriptions: Respect subscription Pairs * Subscriptions: Trim AssetSeparator early We want to trim before checking for "AssetSeparator vs All" because a template should be allowed to reuse a range template and generate just one trailing AssetSeparator whilst using a specific Asset * Kucoin: Fix empty margin asset added * Kucoin: Add Subscription batching Turns out that contary to the documentation, kucoin supports batching of all symbols and currencies * Kucoin: Fix checkSubscriptions and coverage * Subscriptions: Simplify error checking This reduces the complexity of error checking to just be "do we get the correct numbers". Fixes Asset.All with only one asset erroring on xpandPairs, because we trimmed the only asset separator, and then errored that we're not xpanding Assets and the asset on the sub is asset.All This use-case conflicted with commit 6bbd546d74, which required: ``` Subscriptions: Trim AssetSeparator early We want to trim before checking for "AssetSeparator vs All" because a template should be allowed to reuse a range template and generate just one trailing AssetSeparator whilst using a specific Asset ``` Now we set up the assets earlier, and we remove the check for xpandAssets, since the number of asset lines matching is all that matters. I've removed the asset tests for this, but they were correctly erroring on the number of asset lines instead. Everything hits coverage, as well. * Kucoin: Remove deprecated fundingBook endpoint * BTCMarkets: Use common.Batch
116 lines
2.7 KiB
Go
116 lines
2.7 KiB
Go
package subscription
|
|
|
|
import (
|
|
"slices"
|
|
"text/template"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
// List is a container of subscription pointers
|
|
type List []*Subscription
|
|
|
|
type assetPairs map[asset.Item]currency.Pairs
|
|
|
|
type iExchange interface {
|
|
GetAssetTypes(enabled bool) asset.Items
|
|
GetEnabledPairs(asset.Item) (currency.Pairs, error)
|
|
GetPairFormat(asset.Item, bool) (currency.PairFormat, error)
|
|
GetSubscriptionTemplate(*Subscription) (*template.Template, error)
|
|
CanUseAuthenticatedWebsocketEndpoints() bool
|
|
}
|
|
|
|
// Strings returns a sorted slice of subscriptions
|
|
func (l List) Strings() []string {
|
|
s := make([]string, len(l))
|
|
for i := range l {
|
|
s[i] = l[i].String()
|
|
}
|
|
slices.Sort(s)
|
|
return s
|
|
}
|
|
|
|
// GroupPairs groups subscriptions which are identical apart from the Pairs
|
|
// The returned List contains cloned Subscriptions, and the original Subscriptions are left alone
|
|
func (l List) GroupPairs() (n List) {
|
|
s := NewStore()
|
|
for _, sub := range l {
|
|
if found := s.match(&IgnoringPairsKey{sub}); found == nil {
|
|
s.unsafeAdd(sub.Clone())
|
|
} else {
|
|
found.AddPairs(sub.Pairs...)
|
|
}
|
|
}
|
|
return s.List()
|
|
}
|
|
|
|
// Clone returns a deep clone of the List
|
|
func (l List) Clone() List {
|
|
n := make(List, len(l))
|
|
for i, s := range l {
|
|
n[i] = s.Clone()
|
|
}
|
|
return n
|
|
}
|
|
|
|
// QualifiedChannels returns a sorted list of all the qualified Channels in the list
|
|
func (l List) QualifiedChannels() []string {
|
|
c := make([]string, len(l))
|
|
for i := range l {
|
|
c[i] = l[i].QualifiedChannel
|
|
}
|
|
slices.Sort(c)
|
|
return c
|
|
}
|
|
|
|
// SetStates sets the state for all the subs in a list
|
|
// Errors are collected for any subscriptions already in the state
|
|
// On error all changes are reverted
|
|
func (l List) SetStates(state State) error {
|
|
var err error
|
|
for _, sub := range l {
|
|
err = common.AppendError(err, sub.SetState(state))
|
|
}
|
|
return err
|
|
}
|
|
|
|
func fillAssetPairs(ap assetPairs, a asset.Item, e iExchange) error {
|
|
p, err := e.GetEnabledPairs(a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f, err := e.GetPairFormat(a, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ap[a] = common.SortStrings(p.Format(f))
|
|
return nil
|
|
}
|
|
|
|
// 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) {
|
|
at := e.GetAssetTypes(true)
|
|
ap := assetPairs{}
|
|
for _, s := range l {
|
|
switch s.Asset {
|
|
case asset.Empty:
|
|
// Nothing to do
|
|
case asset.All:
|
|
for _, a := range at {
|
|
if err := fillAssetPairs(ap, a, e); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
default:
|
|
if slices.Contains(at, s.Asset) {
|
|
if err := fillAssetPairs(ap, s.Asset, e); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ap, nil
|
|
}
|