Files
gocryptotrader/exchanges/subscription
Gareth Kirwan b41fe27684 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
2024-08-09 12:33:15 +10:00
..

GoCryptoTrader package Subscription

Build Status Software License GoDoc Coverage Status Go Report Card

This subscription package is part of the GoCryptoTrader codebase.

This is still in active development

You can track ideas, planned features and what's in progress on this Trello board: https://trello.com/b/ZAhMhpOy/gocryptotrader.

Join our slack to discuss all things related to GoCryptoTrader! GoCryptoTrader Slack

Exchange Subscriptions

Exchange Subscriptions are streams of data delivered via websocket.

GoCryptoTrader engine will subscribe automatically to configured channels. A subset of exchanges currently support user configured channels, with the remaining using hardcoded defaults. See configuration Features.Subscriptions for whether an exchange is configurable.

Templating

Exchange Contributors should implement GetSubscriptionTemplate to return a text/template Template.

Exchanges are free to implement template caching, a map or a mono-template, inline or file templates.

The template is provided with a single context structure:

  S              *subscription.Subscription
  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 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.

See exchanges/subscription/testdata/subscriptions.tmpl for an example mono-template showcasing various features

Templates do not need to worry about joining around separators; Trailing separators will be stripped automatically.

Contribution

Please feel free to submit any pull requests or suggest any desired features to be added.

When submitting a PR, please abide by our coding guidelines:

  • Code must adhere to the official Go formatting guidelines (i.e. uses gofmt).
  • Code must be documented adhering to the official Go commentary guidelines.
  • Code must adhere to our coding style.
  • Pull requests need to be based on and opened against the master branch.

Donations

If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:

bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc