mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-31 15:10:42 +00:00
Kucoin: Add subscription templating and various fixes (#1579)
* 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
This commit is contained in:
@@ -38,12 +38,30 @@ The template is provided with a single context structure:
|
||||
AssetPairs map[asset.Item]currency.Pairs
|
||||
AssetSeparator string
|
||||
PairSeparator string
|
||||
BatchSize string
|
||||
```
|
||||
|
||||
Subscriptions may fan out many channels for assets and pairs, to support exchanges which require individual subscriptions.
|
||||
To allow the template to communicate how to handle its output it should use the provided separators:
|
||||
To allow the template to communicate how to handle its output it should use the provided directives:
|
||||
- AssetSeparator should be added at the end of each section related to assets
|
||||
- PairSeparator should be added at the end of each pair
|
||||
- BatchSize should be added with a number directly before AssetSeparator to indicate pairs have been batched
|
||||
|
||||
Example:
|
||||
```
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- range $b := batch $pairs 30 -}}
|
||||
{{- $.S.Channel -}} : {{- $b.Join -}}
|
||||
{{ $.PairSeparator }}
|
||||
{{- end -}}
|
||||
{{- $.BatchSize -}} 30
|
||||
{{- $.AssetSeparator }}
|
||||
{{- end }}
|
||||
```
|
||||
|
||||
Assets and pairs should be output in the sequence in AssetPairs since text/template range function uses an sorted order for map keys.
|
||||
|
||||
Template functions may modify AssetPairs to update the subscription's pairs, e.g. Filtering out margin pairs already in spot subscription
|
||||
|
||||
We use separators like this because it allows mono-templates to decide at runtime whether to fan out.
|
||||
|
||||
|
||||
@@ -8,26 +8,46 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
type mockEx struct {
|
||||
pairs currency.Pairs
|
||||
assets asset.Items
|
||||
tpl string
|
||||
auth bool
|
||||
errPairs error
|
||||
errFormat error
|
||||
}
|
||||
|
||||
func newMockEx() *mockEx {
|
||||
pairs := currency.Pairs{btcusdtPair, ethusdcPair}
|
||||
for _, b := range []currency.Code{currency.LTC, currency.XRP, currency.TRX} {
|
||||
for _, q := range []currency.Code{currency.USDT, currency.USDC} {
|
||||
pairs = append(pairs, currency.NewPair(b, q))
|
||||
}
|
||||
}
|
||||
|
||||
return &mockEx{
|
||||
assets: asset.Items{asset.Spot, asset.Futures},
|
||||
pairs: pairs,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mockEx) GetEnabledPairs(_ asset.Item) (currency.Pairs, error) {
|
||||
return currency.Pairs{btcusdtPair, ethusdcPair}, m.errPairs
|
||||
return m.pairs, m.errPairs
|
||||
}
|
||||
|
||||
func (m *mockEx) GetPairFormat(_ asset.Item, _ bool) (currency.PairFormat, error) {
|
||||
return currency.PairFormat{Uppercase: true}, m.errFormat
|
||||
}
|
||||
|
||||
func (m *mockEx) GetSubscriptionTemplate(_ *Subscription) (*template.Template, error) {
|
||||
func (m *mockEx) GetSubscriptionTemplate(s *Subscription) (*template.Template, error) {
|
||||
if s.Channel == "nil" {
|
||||
return nil, nil
|
||||
}
|
||||
return template.New(m.tpl).
|
||||
Funcs(template.FuncMap{
|
||||
"assetName": func(a asset.Item) string {
|
||||
@@ -35,11 +55,18 @@ func (m *mockEx) GetSubscriptionTemplate(_ *Subscription) (*template.Template, e
|
||||
return "future"
|
||||
}
|
||||
return a.String()
|
||||
}}).
|
||||
},
|
||||
"updateAssetPairs": func(ap assetPairs) string {
|
||||
ap[asset.Futures] = nil
|
||||
ap[asset.Spot] = ap[asset.Spot][0:1]
|
||||
return ""
|
||||
},
|
||||
"batch": common.Batch[currency.Pairs],
|
||||
}).
|
||||
ParseFiles("testdata/" + m.tpl)
|
||||
}
|
||||
|
||||
func (m *mockEx) GetAssetTypes(_ bool) asset.Items { return asset.Items{asset.Spot, asset.Futures} }
|
||||
func (m *mockEx) GetAssetTypes(_ bool) asset.Items { return m.assets }
|
||||
func (m *mockEx) CanUseAuthenticatedWebsocketEndpoints() bool { return m.auth }
|
||||
|
||||
// equalLists is a utility function to compare subscription lists and show a pretty failure message
|
||||
|
||||
@@ -85,7 +85,7 @@ func fillAssetPairs(ap assetPairs, a asset.Item, e iExchange) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ap[a] = p.Format(f)
|
||||
ap[a] = common.SortStrings(p.Format(f))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -83,13 +83,15 @@ func TestListSetStates(t *testing.T) {
|
||||
// All other code is covered under TestExpandTemplates
|
||||
func TestAssetPairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
expErr := errors.New("Krypton is gone")
|
||||
for _, a := range []asset.Item{asset.Spot, asset.All} {
|
||||
e := newMockEx()
|
||||
l := &List{{Channel: CandlesChannel, Asset: a}}
|
||||
_, err := l.assetPairs(&mockEx{errPairs: expErr})
|
||||
assert.ErrorIs(t, err, expErr, "Should error correctly on GetEnabledPairs")
|
||||
_, err = l.assetPairs(&mockEx{errFormat: expErr})
|
||||
assert.ErrorIs(t, err, expErr, "Should error correctly on GetPairFormat")
|
||||
e.errFormat = errors.New("Krypton is back")
|
||||
_, err := l.assetPairs(e)
|
||||
assert.ErrorIs(t, err, e.errFormat, "Should error correctly on GetPairFormat")
|
||||
e.errPairs = errors.New("Krypton is gone")
|
||||
_, err = l.assetPairs(e)
|
||||
assert.ErrorIs(t, err, e.errPairs, "Should error correctly on GetEnabledPairs")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,25 +4,29 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
const (
|
||||
deviceControl = "\x11"
|
||||
groupSeparator = "\x1D"
|
||||
recordSeparator = "\x1E"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidAssetExpandPairs = errors.New("subscription template containing PairSeparator with must contain either specific Asset or AssetSeparator")
|
||||
errAssetRecords = errors.New("subscription template did not generate the expected number of asset records")
|
||||
errPairRecords = errors.New("subscription template did not generate the expected number of pair records")
|
||||
errAssetTemplateWithoutAll = errors.New("sub.Asset must be set to All if AssetSeparator is used in Channel template")
|
||||
errNoTemplateContent = errors.New("subscription template did not generate content")
|
||||
errInvalidTemplate = errors.New("GetSubscriptionTemplate did not return a template")
|
||||
errInvalidAssetExpandPairs = errors.New("subscription template containing PairSeparator with must contain either specific Asset or AssetSeparator")
|
||||
errAssetRecords = errors.New("subscription template did not generate the expected number of asset records")
|
||||
errPairRecords = errors.New("subscription template did not generate the expected number of pair records")
|
||||
errTooManyBatchSizePerAsset = errors.New("more than one BatchSize directive inside an AssetSeparator")
|
||||
errAssetTemplateWithoutAll = errors.New("sub.Asset must be set to All if AssetSeparator is used in Channel template")
|
||||
errNoTemplateContent = errors.New("subscription template did not generate content")
|
||||
errInvalidTemplate = errors.New("GetSubscriptionTemplate did not return a template")
|
||||
)
|
||||
|
||||
type tplCtx struct {
|
||||
@@ -30,6 +34,7 @@ type tplCtx struct {
|
||||
AssetPairs assetPairs
|
||||
PairSeparator string
|
||||
AssetSeparator string
|
||||
BatchSize string
|
||||
}
|
||||
|
||||
// ExpandTemplates returns a list of Subscriptions with Template expanded
|
||||
@@ -63,86 +68,132 @@ func (l List) ExpandTemplates(e iExchange) (List, error) {
|
||||
assets = append(assets, k)
|
||||
}
|
||||
slices.Sort(assets) // text/template ranges maps in sorted order
|
||||
|
||||
subs := List{}
|
||||
for _, s := range l {
|
||||
expanded, err2 := expandTemplate(e, s, maps.Clone(ap), assets)
|
||||
if err2 != nil {
|
||||
err = common.AppendError(err, fmt.Errorf("%s: %w", s, err2))
|
||||
} else {
|
||||
subs = append(subs, expanded...)
|
||||
}
|
||||
}
|
||||
|
||||
return subs, err
|
||||
}
|
||||
|
||||
func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.Items) (List, error) {
|
||||
if s.QualifiedChannel != "" {
|
||||
return List{s}, nil
|
||||
}
|
||||
|
||||
t, err := e.GetSubscriptionTemplate(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if t == nil {
|
||||
return nil, errInvalidTemplate
|
||||
}
|
||||
|
||||
subCtx := &tplCtx{
|
||||
S: s,
|
||||
PairSeparator: recordSeparator,
|
||||
AssetSeparator: groupSeparator,
|
||||
BatchSize: deviceControl + "BS",
|
||||
}
|
||||
|
||||
subs := List{}
|
||||
|
||||
for _, s := range l {
|
||||
if s.QualifiedChannel != "" {
|
||||
subs = append(subs, s)
|
||||
switch s.Asset {
|
||||
case asset.All:
|
||||
subCtx.AssetPairs = ap
|
||||
default:
|
||||
// This deliberately includes asset.Empty to harmonise handling
|
||||
subCtx.AssetPairs = assetPairs{
|
||||
s.Asset: ap[s.Asset],
|
||||
}
|
||||
assets = asset.Items{s.Asset}
|
||||
if s.Asset != asset.Empty && len(ap[s.Asset]) == 0 {
|
||||
return List{}, nil // Nothing is enabled for this sub asset
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
out := strings.TrimSpace(buf.String())
|
||||
|
||||
// Remove a single trailing AssetSeparator; don't use a cutset to avoid removing 2 or more
|
||||
out = strings.TrimSpace(strings.TrimSuffix(out, subCtx.AssetSeparator))
|
||||
|
||||
assetRecords := strings.Split(out, subCtx.AssetSeparator)
|
||||
if len(assetRecords) != len(assets) {
|
||||
return nil, fmt.Errorf("%w: Got %d; Expected %d", errAssetRecords, len(assetRecords), len(assets))
|
||||
}
|
||||
|
||||
for i, assetChannels := range assetRecords {
|
||||
a := assets[i]
|
||||
pairs := subCtx.AssetPairs[a]
|
||||
|
||||
xpandPairs := strings.Contains(assetChannels, subCtx.PairSeparator)
|
||||
|
||||
/* Batching:
|
||||
- We start by assuming we'll get 1 batch sized to contain all pairs. Maybe a comma-separated list, or just the asset name
|
||||
- If a BatchSize directive is found, we expect it to come right at the end, and be followed by the batch size as a number
|
||||
- We'll then split into N batches of that size
|
||||
- If no batchSize was declared, but we saw a PairSeparator, then we expect to see one line per pair, so batchSize is 1
|
||||
*/
|
||||
batchSize := len(pairs)
|
||||
if b := strings.Split(assetChannels, subCtx.BatchSize); len(b) > 2 {
|
||||
return nil, fmt.Errorf("%w for %s", errTooManyBatchSizePerAsset, a)
|
||||
} else if len(b) == 2 {
|
||||
assetChannels = b[0]
|
||||
if batchSize, err = strconv.Atoi(strings.TrimSpace(b[1])); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", s, common.GetTypeAssertError("int", b[1], "batchSize"))
|
||||
}
|
||||
} else if xpandPairs {
|
||||
batchSize = 1
|
||||
}
|
||||
|
||||
// Trim space, then only one pair separator, then any more space.
|
||||
assetChannels = strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(assetChannels), subCtx.PairSeparator))
|
||||
|
||||
if assetChannels == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
subCtx := &tplCtx{
|
||||
S: s,
|
||||
AssetPairs: ap,
|
||||
PairSeparator: recordSeparator,
|
||||
AssetSeparator: groupSeparator,
|
||||
batches := common.Batch(pairs, batchSize)
|
||||
|
||||
pairLines := strings.Split(assetChannels, subCtx.PairSeparator)
|
||||
|
||||
if s.Asset != asset.Empty && len(pairLines) != len(batches) {
|
||||
// The number of lines we get generated must match the number of pair batches we expect
|
||||
return nil, fmt.Errorf("%w for %s: Got %d; Expected %d", errPairRecords, a, len(pairLines), len(batches))
|
||||
}
|
||||
|
||||
t, err := e.GetSubscriptionTemplate(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if t == nil {
|
||||
return nil, errInvalidTemplate
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := t.Execute(buf, subCtx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out := buf.String()
|
||||
|
||||
subAssets := assets
|
||||
xpandPairs := strings.Contains(out, subCtx.PairSeparator)
|
||||
if xpandAssets := strings.Contains(out, subCtx.AssetSeparator); xpandAssets {
|
||||
if s.Asset != asset.All {
|
||||
return nil, errAssetTemplateWithoutAll
|
||||
for j, channel := range pairLines {
|
||||
c := s.Clone()
|
||||
c.Asset = a
|
||||
channel = strings.TrimSpace(channel)
|
||||
if channel == "" {
|
||||
return nil, fmt.Errorf("%w for %s: %s", errNoTemplateContent, a, s)
|
||||
}
|
||||
} else {
|
||||
if xpandPairs && (s.Asset == asset.All || s.Asset == asset.Empty) {
|
||||
// We don't currently support expanding Pairs without expanding Assets for All or Empty assets, but we could; waiting for a use-case
|
||||
return nil, errInvalidAssetExpandPairs
|
||||
}
|
||||
// No expansion so update expected Assets for consistent behaviour below
|
||||
subAssets = []asset.Item{s.Asset}
|
||||
}
|
||||
|
||||
out = strings.TrimRight(out, " \n\r\t"+subCtx.PairSeparator+subCtx.AssetSeparator)
|
||||
|
||||
assetRecords := strings.Split(out, subCtx.AssetSeparator)
|
||||
if len(assetRecords) != len(subAssets) {
|
||||
return nil, fmt.Errorf("%w: Got %d; Expected %d", errAssetRecords, len(assetRecords), len(subAssets))
|
||||
}
|
||||
|
||||
for i, assetChannels := range assetRecords {
|
||||
a := subAssets[i]
|
||||
assetChannels = strings.TrimRight(assetChannels, " \n\r\t"+recordSeparator)
|
||||
pairLines := strings.Split(assetChannels, subCtx.PairSeparator)
|
||||
pairs, ok := ap[a]
|
||||
if xpandPairs {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: %s", asset.ErrInvalidAsset, a)
|
||||
}
|
||||
if len(pairLines) != len(pairs) {
|
||||
return nil, fmt.Errorf("%w: Got %d; Expected %d", errPairRecords, len(pairLines), len(pairs))
|
||||
}
|
||||
}
|
||||
for j, channel := range pairLines {
|
||||
c := s.Clone()
|
||||
c.Asset = a
|
||||
channel = strings.TrimSpace(channel)
|
||||
if channel == "" {
|
||||
return nil, fmt.Errorf("%w: %s", errNoTemplateContent, s)
|
||||
}
|
||||
c.QualifiedChannel = strings.TrimSpace(channel)
|
||||
if xpandPairs {
|
||||
c.Pairs = currency.Pairs{pairs[j]}
|
||||
} else {
|
||||
c.Pairs = pairs
|
||||
}
|
||||
subs = append(subs, c)
|
||||
c.QualifiedChannel = channel
|
||||
if s.Asset != asset.Empty {
|
||||
c.Pairs = batches[j]
|
||||
}
|
||||
subs = append(subs, c)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
@@ -15,29 +16,41 @@ import (
|
||||
func TestExpandTemplates(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
e := &mockEx{
|
||||
tpl: "subscriptions.tmpl",
|
||||
}
|
||||
e := newMockEx()
|
||||
e.tpl = "subscriptions.tmpl"
|
||||
|
||||
// Functionality tests
|
||||
l := List{
|
||||
{Channel: "feature1"},
|
||||
{Channel: "feature2", Asset: asset.All, Pairs: currency.Pairs{btcusdtPair, ethusdcPair}, Interval: kline.FifteenMin},
|
||||
{Channel: "feature3", Asset: asset.All, Pairs: currency.Pairs{btcusdtPair, ethusdcPair}, Levels: 100},
|
||||
{Channel: "feature4", Authenticated: true},
|
||||
{Channel: "feature1", QualifiedChannel: "just one sub already processed"},
|
||||
{Channel: "single-channel"},
|
||||
{Channel: "expand-assets", Asset: asset.All, Interval: kline.FifteenMin},
|
||||
{Channel: "expand-pairs", Asset: asset.All, Levels: 1},
|
||||
{Channel: "expand-pairs", Asset: asset.Spot, Levels: 2},
|
||||
{Channel: "single-channel", QualifiedChannel: "just one sub already processed"},
|
||||
{Channel: "update-asset-pairs", Asset: asset.All},
|
||||
{Channel: "expand-pairs", Asset: asset.Spot, Pairs: e.pairs[0:2], Levels: 3},
|
||||
{Channel: "batching", Asset: asset.Spot},
|
||||
{Channel: "single-channel", Authenticated: true},
|
||||
}
|
||||
got, err := l.ExpandTemplates(e)
|
||||
require.NoError(t, err, "ExpandTemplates must not error")
|
||||
exp := List{
|
||||
{Channel: "feature1", QualifiedChannel: "feature1"},
|
||||
{Channel: "feature2", QualifiedChannel: "spot-feature2@15m", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair, ethusdcPair}, Interval: kline.FifteenMin},
|
||||
{Channel: "feature2", QualifiedChannel: "future-feature2@15m", Asset: asset.Futures, Pairs: currency.Pairs{btcusdtPair, ethusdcPair}, Interval: kline.FifteenMin},
|
||||
{Channel: "feature3", QualifiedChannel: "spot-USDTBTC-feature3@100", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair}, Levels: 100},
|
||||
{Channel: "feature3", QualifiedChannel: "spot-USDCETH-feature3@100", Asset: asset.Spot, Pairs: currency.Pairs{ethusdcPair}, Levels: 100},
|
||||
{Channel: "feature3", QualifiedChannel: "future-USDTBTC-feature3@100", Asset: asset.Futures, Pairs: currency.Pairs{btcusdtPair}, Levels: 100},
|
||||
{Channel: "feature3", QualifiedChannel: "future-USDCETH-feature3@100", Asset: asset.Futures, Pairs: currency.Pairs{ethusdcPair}, Levels: 100},
|
||||
{Channel: "feature1", QualifiedChannel: "just one sub already processed"},
|
||||
{Channel: "single-channel", QualifiedChannel: "single-channel"},
|
||||
{Channel: "expand-assets", QualifiedChannel: "spot-expand-assets@15m", Asset: asset.Spot, Pairs: e.pairs, Interval: kline.FifteenMin},
|
||||
{Channel: "expand-assets", QualifiedChannel: "future-expand-assets@15m", Asset: asset.Futures, Pairs: e.pairs, Interval: kline.FifteenMin},
|
||||
{Channel: "single-channel", QualifiedChannel: "just one sub already processed"},
|
||||
{Channel: "update-asset-pairs", QualifiedChannel: "spot-btcusdt-update-asset-pairs", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair}},
|
||||
{Channel: "expand-pairs", QualifiedChannel: "spot-USDTBTC-expand-pairs@3", Asset: asset.Spot, Pairs: e.pairs[:1], Levels: 3},
|
||||
{Channel: "expand-pairs", QualifiedChannel: "spot-USDCETH-expand-pairs@3", Asset: asset.Spot, Pairs: e.pairs[1:2], Levels: 3},
|
||||
}
|
||||
for _, p := range e.pairs {
|
||||
exp = append(exp, List{
|
||||
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@1", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 1},
|
||||
{Channel: "expand-pairs", QualifiedChannel: "future-" + p.Swap().String() + "-expand-pairs@1", Asset: asset.Futures, Pairs: currency.Pairs{p}, Levels: 1},
|
||||
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@2", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 2},
|
||||
}...)
|
||||
}
|
||||
for _, b := range common.Batch(common.SortStrings(e.pairs), 3) {
|
||||
exp = append(exp, &Subscription{Channel: "batching", QualifiedChannel: "spot-" + b.Join() + "-batching", Asset: asset.Spot, Pairs: b})
|
||||
}
|
||||
|
||||
if !equalLists(t, exp, got) {
|
||||
@@ -48,36 +61,58 @@ func TestExpandTemplates(t *testing.T) {
|
||||
got, err = l.ExpandTemplates(e)
|
||||
require.NoError(t, err, "ExpandTemplates must not error")
|
||||
exp = append(exp,
|
||||
&Subscription{Channel: "feature4", QualifiedChannel: "feature4-authed"},
|
||||
&Subscription{Channel: "single-channel", QualifiedChannel: "single-channel-authed"},
|
||||
)
|
||||
equalLists(t, exp, got)
|
||||
|
||||
_, err = List{{Channel: "feature2", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errAssetTemplateWithoutAll, "Should error correctly on xpand assets without All")
|
||||
// Test with just one asset to ensure asset.All works, and disabled assets don't error
|
||||
e.assets = e.assets[:1]
|
||||
l = List{
|
||||
{Channel: "expand-assets", Asset: asset.All, Interval: kline.OneHour},
|
||||
{Channel: "expand-pairs", Asset: asset.All, Levels: 4},
|
||||
{Channel: "single-channel", Asset: asset.Futures},
|
||||
}
|
||||
got, err = l.ExpandTemplates(e)
|
||||
require.NoError(t, err, "ExpandTemplates must not error")
|
||||
exp = List{
|
||||
{Channel: "expand-assets", QualifiedChannel: "spot-expand-assets@1h", Asset: asset.Spot, Pairs: e.pairs, Interval: kline.OneHour},
|
||||
}
|
||||
for _, p := range e.pairs {
|
||||
exp = append(exp, List{
|
||||
{Channel: "expand-pairs", QualifiedChannel: "spot-" + p.Swap().String() + "-expand-pairs@4", Asset: asset.Spot, Pairs: currency.Pairs{p}, Levels: 4},
|
||||
}...)
|
||||
}
|
||||
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", Asset: asset.All}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errInvalidAssetExpandPairs, "Should error correctly on xpand pairs but not assets")
|
||||
|
||||
_, err = List{{Channel: "error1"}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errInvalidAssetExpandPairs, "Should error correctly on xpand pairs but not assets")
|
||||
|
||||
_, err = List{{Channel: "error2"}}.ExpandTemplates(e)
|
||||
assert.ErrorContains(t, err, "wrong number of args for String", "Should error correctly with execution error")
|
||||
|
||||
_, err = List{{Channel: "non-existent"}}.ExpandTemplates(e)
|
||||
_, err = List{{Channel: "empty-content", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errNoTemplateContent, "Should error correctly when no content generated")
|
||||
assert.ErrorContains(t, err, "non-existent", "Should error correctly when no content generated")
|
||||
assert.ErrorContains(t, err, "empty-content", "Should error correctly when no content generated")
|
||||
|
||||
_, err = List{{Channel: "error3", Asset: asset.All}}.ExpandTemplates(e)
|
||||
_, err = List{{Channel: "error2", Asset: asset.All}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errAssetRecords, "Should error correctly when invalid number of asset entries")
|
||||
|
||||
_, err = List{{Channel: "error4", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
_, err = List{{Channel: "error3", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errPairRecords, "Should error correctly when invalid number of pair entries")
|
||||
|
||||
_, err = List{{Channel: "error4", Asset: asset.Margin}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, asset.ErrInvalidAsset, "Should error correctly when invalid asset")
|
||||
_, err = List{{Channel: "error4", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errTooManyBatchSizePerAsset, "Should error correctly when too many BatchSize directives")
|
||||
|
||||
_, err = List{{Channel: "error5", Asset: asset.Spot}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, common.ErrTypeAssertFailure, "Should error correctly when batch size isn't an int")
|
||||
|
||||
e.tpl = "parse-error.tmpl"
|
||||
e.tpl = "parse-error.tmpl"
|
||||
_, err = l.ExpandTemplates(e)
|
||||
assert.ErrorContains(t, err, "function \"explode\" not defined", "Should error correctly on unparsable template")
|
||||
@@ -90,10 +125,13 @@ func TestExpandTemplates(t *testing.T) {
|
||||
_, err = l.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, e.errPairs, "Should error correctly on GetEnabledPairs")
|
||||
|
||||
l = List{{Channel: "feature1", QualifiedChannel: "already happy"}}
|
||||
l = List{{Channel: "single-channel", QualifiedChannel: "already happy"}}
|
||||
got, err = l.ExpandTemplates(e)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 1, "Must get back the one sub")
|
||||
assert.Equal(t, "already happy", l[0].QualifiedChannel, "Should get back the one sub")
|
||||
assert.NotSame(t, got, l, "Should get back a different actual list")
|
||||
|
||||
_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
|
||||
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")
|
||||
}
|
||||
|
||||
39
exchanges/subscription/testdata/errors.tmpl
vendored
39
exchanges/subscription/testdata/errors.tmpl
vendored
@@ -1,13 +1,36 @@
|
||||
{{- if eq .S.Channel "error1" }}
|
||||
{{/* Error 1: Expand pairs but not assets, without specific asset */}}
|
||||
{{- .PairSeparator -}}
|
||||
{{/* Runtime error from executing */}}
|
||||
{{ .S.String 42 }}
|
||||
{{- else if eq .S.Channel "error2" }}
|
||||
{{/* Error 2: Runtime error from executing */}}
|
||||
{{ .S.String 42 }}
|
||||
{{/* Incorrect number of asset entries */}}
|
||||
{{- .AssetSeparator -}}
|
||||
{{- .AssetSeparator -}}
|
||||
{{- .AssetSeparator -}}
|
||||
{{- else if eq .S.Channel "error3" }}
|
||||
{{/* Error 3: Incorrect number of asset entries */}}
|
||||
{{- .AssetSeparator }}
|
||||
{{/* Incorrect number of pair entries */}}
|
||||
{{- .PairSeparator -}}
|
||||
{{- .PairSeparator -}}
|
||||
{{- .PairSeparator -}}
|
||||
{{- else if eq .S.Channel "error4" }}
|
||||
{{/* Error 3: Incorrect number of pair entries */}}
|
||||
{{- .PairSeparator }}
|
||||
{{/* Too many BatchSize commands */}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- $pairs.Join -}}
|
||||
{{- $.BatchSize -}}1
|
||||
{{- $.BatchSize -}}2
|
||||
{{- $.AssetSeparator -}}
|
||||
{{- end -}}
|
||||
{{- else if eq .S.Channel "error5" }}
|
||||
{{/* BatchSize without number */}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- $pairs.Join -}}
|
||||
{{- $.BatchSize -}}
|
||||
{{- $.AssetSeparator -}}
|
||||
{{- end -}}
|
||||
{{- else if eq .S.Channel "empty-content" }}
|
||||
{{/* Empty response for the pair */}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- range $pair := $pairs -}}
|
||||
{{- $.PairSeparator -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
@@ -1,21 +1,36 @@
|
||||
{{- if eq $.S.Channel "feature1" -}}
|
||||
{{/* Case 1: One channel to rule them all */}}
|
||||
feature1
|
||||
{{- else if eq $.S.Channel "feature2" -}}
|
||||
{{/* Case 2: One channel per asset */}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{ assetName $asset }}-feature2@ {{- $.S.Interval.Short }}
|
||||
{{- $.AssetSeparator }}
|
||||
{{- end }}
|
||||
{{- else if eq $.S.Channel "feature3" }}
|
||||
{{/* Case 3: One channel per pair per asset */}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- range $pair := $pairs -}}
|
||||
{{ assetName $asset }}-{{ $pair.Swap.String -}} -feature3@ {{- $.S.Levels }}
|
||||
{{- $.PairSeparator -}}
|
||||
{{- if eq $.S.Channel "single-channel" -}}
|
||||
single-channel
|
||||
{{- if $.S.Authenticated -}}
|
||||
-authed
|
||||
{{- end -}}
|
||||
{{- $.AssetSeparator -}}
|
||||
{{- end -}}
|
||||
{{- else if eq $.S.Channel "feature4" }}
|
||||
feature4-authed
|
||||
{{- else if eq $.S.Channel "expand-assets" -}}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{ assetName $asset }}-expand-assets@ {{- $.S.Interval.Short }}
|
||||
{{- $.AssetSeparator }}
|
||||
{{- end }}
|
||||
{{- else if eq $.S.Channel "expand-pairs" }}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- range $pair := $pairs -}}
|
||||
{{ assetName $asset }}-{{ $pair.Swap.String -}} -expand-pairs@ {{- $.S.Levels }}
|
||||
{{- $.PairSeparator -}}
|
||||
{{- end -}}
|
||||
{{- $.AssetSeparator -}}
|
||||
{{- end -}}
|
||||
{{- else if eq $.S.Channel "update-asset-pairs" }}
|
||||
{{- updateAssetPairs $.AssetPairs -}}
|
||||
spot-btcusdt-update-asset-pairs
|
||||
{{- $.PairSeparator -}}
|
||||
{{- $.AssetSeparator -}}
|
||||
{{/* futures doesn't output anything, but we need an asset separator, so this previous one must not be stripped */}}
|
||||
{{- $.AssetSeparator -}}
|
||||
{{- else if eq $.S.Channel "batching" }}
|
||||
{{- range $asset, $pairs := $.AssetPairs }}
|
||||
{{- if eq $asset.String "spot" }}
|
||||
{{- range $batch := batch $pairs 3 -}}
|
||||
{{ assetName $asset }}-{{ $batch.Join -}} -batching
|
||||
{{- $.PairSeparator -}}
|
||||
{{- end -}}
|
||||
{{- $.BatchSize -}} 3
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
Reference in New Issue
Block a user