mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-20 23:16:49 +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
248 lines
6.2 KiB
Go
248 lines
6.2 KiB
Go
package asset
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Public errors related to assets
|
|
var (
|
|
ErrNotSupported = errors.New("unsupported asset type")
|
|
ErrNotEnabled = errors.New("asset type not enabled")
|
|
ErrInvalidAsset = errors.New("asset is invalid")
|
|
)
|
|
|
|
// Item stores the asset type
|
|
type Item uint32
|
|
|
|
// Items stores a list of assets types
|
|
type Items []Item
|
|
|
|
// Const vars for asset package
|
|
const (
|
|
Empty Item = 0
|
|
Spot Item = 1 << iota
|
|
Margin
|
|
CrossMargin
|
|
MarginFunding
|
|
Index
|
|
Binary
|
|
PerpetualContract
|
|
PerpetualSwap
|
|
Futures
|
|
DeliveryFutures
|
|
UpsideProfitContract
|
|
DownsideProfitContract
|
|
CoinMarginedFutures
|
|
USDTMarginedFutures
|
|
USDCMarginedFutures
|
|
Options
|
|
OptionCombo
|
|
FutureCombo
|
|
LinearContract // Added to represent a USDT and USDC based linear derivatives(futures/perpetual) assets in Bybit V5
|
|
All
|
|
|
|
optionsFlag = OptionCombo | Options
|
|
futuresFlag = PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | LinearContract | FutureCombo
|
|
supportedFlag = Spot | Margin | CrossMargin | MarginFunding | Index | Binary | PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | Options | LinearContract | OptionCombo | FutureCombo
|
|
|
|
spot = "spot"
|
|
margin = "margin"
|
|
crossMargin = "cross_margin" // for Gateio exchange
|
|
marginFunding = "marginfunding"
|
|
index = "index"
|
|
binary = "binary"
|
|
perpetualContract = "perpetualcontract"
|
|
perpetualSwap = "perpetualswap"
|
|
swap = "swap"
|
|
futures = "futures"
|
|
deliveryFutures = "delivery"
|
|
upsideProfitContract = "upsideprofitcontract"
|
|
downsideProfitContract = "downsideprofitcontract"
|
|
coinMarginedFutures = "coinmarginedfutures"
|
|
usdtMarginedFutures = "usdtmarginedfutures"
|
|
usdcMarginedFutures = "usdcmarginedfutures"
|
|
options = "options"
|
|
optionCombo = "option_combo"
|
|
futureCombo = "future_combo"
|
|
all = "all"
|
|
)
|
|
|
|
var (
|
|
supportedList = Items{Spot, Margin, CrossMargin, MarginFunding, Index, Binary, PerpetualContract, PerpetualSwap, Futures, DeliveryFutures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, Options, LinearContract, OptionCombo, FutureCombo}
|
|
)
|
|
|
|
// Supported returns a list of supported asset types
|
|
func Supported() Items {
|
|
return supportedList
|
|
}
|
|
|
|
// String converts an Item to its string representation
|
|
func (a Item) String() string {
|
|
switch a {
|
|
case Spot:
|
|
return spot
|
|
case Margin:
|
|
return margin
|
|
case CrossMargin:
|
|
return crossMargin
|
|
case MarginFunding:
|
|
return marginFunding
|
|
case Index:
|
|
return index
|
|
case Binary:
|
|
return binary
|
|
case PerpetualContract:
|
|
return perpetualContract
|
|
case PerpetualSwap:
|
|
return perpetualSwap
|
|
case Futures:
|
|
return futures
|
|
case DeliveryFutures:
|
|
return deliveryFutures
|
|
case UpsideProfitContract:
|
|
return upsideProfitContract
|
|
case DownsideProfitContract:
|
|
return downsideProfitContract
|
|
case CoinMarginedFutures:
|
|
return coinMarginedFutures
|
|
case USDTMarginedFutures:
|
|
return usdtMarginedFutures
|
|
case USDCMarginedFutures:
|
|
return usdcMarginedFutures
|
|
case Options:
|
|
return options
|
|
case OptionCombo:
|
|
return optionCombo
|
|
case FutureCombo:
|
|
return futureCombo
|
|
case All:
|
|
return all
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// Strings converts an asset type array to a string array
|
|
func (a Items) Strings() []string {
|
|
assets := make([]string, len(a))
|
|
for x := range a {
|
|
assets[x] = a[x].String()
|
|
}
|
|
return assets
|
|
}
|
|
|
|
// Contains returns whether or not the supplied asset exists
|
|
// in the list of Items
|
|
func (a Items) Contains(i Item) bool {
|
|
if i.IsValid() {
|
|
for x := range a {
|
|
if a[x] == i {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// JoinToString joins an asset type array and converts it to a string
|
|
// with the supplied separator
|
|
func (a Items) JoinToString(separator string) string {
|
|
return strings.Join(a.Strings(), separator)
|
|
}
|
|
|
|
// IsValid returns whether or not the supplied asset type is valid or not
|
|
func (a Item) IsValid() bool {
|
|
return a != Empty && supportedFlag&a == a
|
|
}
|
|
|
|
// UnmarshalJSON conforms type to the umarshaler interface
|
|
func (a *Item) UnmarshalJSON(d []byte) error {
|
|
var assetString string
|
|
err := json.Unmarshal(d, &assetString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if assetString == "" {
|
|
return nil
|
|
}
|
|
|
|
ai, err := New(assetString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*a = ai
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON conforms type to the marshaller interface
|
|
func (a Item) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(a.String())
|
|
}
|
|
|
|
// New takes an input matches to relevant package assets
|
|
func New(input string) (Item, error) {
|
|
input = strings.ToLower(input)
|
|
switch input {
|
|
case spot:
|
|
return Spot, nil
|
|
case margin:
|
|
return Margin, nil
|
|
case marginFunding:
|
|
return MarginFunding, nil
|
|
case crossMargin:
|
|
return CrossMargin, nil
|
|
case deliveryFutures:
|
|
return DeliveryFutures, nil
|
|
case index:
|
|
return Index, nil
|
|
case binary:
|
|
return Binary, nil
|
|
case perpetualContract:
|
|
return PerpetualContract, nil
|
|
case perpetualSwap, swap:
|
|
return PerpetualSwap, nil
|
|
case futures:
|
|
return Futures, nil
|
|
case upsideProfitContract:
|
|
return UpsideProfitContract, nil
|
|
case downsideProfitContract:
|
|
return DownsideProfitContract, nil
|
|
case coinMarginedFutures:
|
|
return CoinMarginedFutures, nil
|
|
case usdtMarginedFutures:
|
|
return USDTMarginedFutures, nil
|
|
case usdcMarginedFutures:
|
|
return USDCMarginedFutures, nil
|
|
case options, "option":
|
|
return Options, nil
|
|
case optionCombo:
|
|
return OptionCombo, nil
|
|
case futureCombo:
|
|
return FutureCombo, nil
|
|
case all:
|
|
return All, nil
|
|
default:
|
|
return 0, fmt.Errorf("%w '%v', only supports %s", ErrNotSupported, input, supportedList)
|
|
}
|
|
}
|
|
|
|
// UseDefault returns default asset type
|
|
func UseDefault() Item {
|
|
return Spot
|
|
}
|
|
|
|
// IsFutures checks if the asset type is a futures contract based asset
|
|
func (a Item) IsFutures() bool {
|
|
return a != Empty && futuresFlag&a == a
|
|
}
|
|
|
|
// IsOptions checks if the asset type is options contract based asset
|
|
func (a Item) IsOptions() bool {
|
|
return a != Empty && optionsFlag&a == a
|
|
}
|