mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 15:10:46 +00:00
* Websockets: Move Subscription to its own package This allows the small type to be imported from both `config` and from `stream` without an import cycle, so we don't have to repeat ourselves * Subs: Renamed Currency to Pair This was being mis-used through much of the code, and since we're already touching everything, we might as well fix it * Websockets: Add Subscription configuration * Binance: Add subscription configuration * Kucoin: Subscription configuration * Simplify GenerateDefaultSubs * Improve TestGenSubs coverage * Test Candle Sub generation * Support Candle intervals * Full responsibility for formatting Channel name on GenerateDefaultSubs OR consumer of Subscribe * Simplify generatePayloads as a result * Fix test coverage of asset types in processMarketSnapshot * Exchanges: Abstract ParallelChanOp * Tests: Generic ws mock instances * Kucoin: Fix intermittent conflict in test currs Use isolated test instance for `TestGetOpenInterest`. `TestGetOpenInterest` would occassionally change pairs before GenerateDefault Subs.
100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package currency
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// NewCurrenciesFromStringArray returns a Currencies object from strings
|
|
func NewCurrenciesFromStringArray(currencies []string) Currencies {
|
|
list := make(Currencies, 0, len(currencies))
|
|
for i := range currencies {
|
|
if currencies[i] == "" {
|
|
continue
|
|
}
|
|
list = append(list, NewCode(currencies[i]))
|
|
}
|
|
return list
|
|
}
|
|
|
|
// Currencies define a range of supported currency codes
|
|
type Currencies []Code
|
|
|
|
// Add adds a currency to the list if it doesn't exist
|
|
func (c Currencies) Add(a Code) Currencies {
|
|
if !c.Contains(a) {
|
|
c = append(c, a)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Strings returns an array of currency strings
|
|
func (c Currencies) Strings() []string {
|
|
list := make([]string, len(c))
|
|
for i := range c {
|
|
list[i] = c[i].String()
|
|
}
|
|
return list
|
|
}
|
|
|
|
// Contains checks to see if a currency code is contained in the currency list
|
|
func (c Currencies) Contains(check Code) bool {
|
|
for i := range c {
|
|
if c[i].Equal(check) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Join returns a comma serparated string
|
|
func (c Currencies) Join() string {
|
|
return strings.Join(c.Strings(), ",")
|
|
}
|
|
|
|
// UnmarshalJSON comforms type to the umarshaler interface
|
|
func (c *Currencies) UnmarshalJSON(d []byte) error {
|
|
var configCurrencies string
|
|
err := json.Unmarshal(d, &configCurrencies)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
curr := strings.Split(configCurrencies, ",")
|
|
allTheCurrencies := make(Currencies, len(curr))
|
|
for i := range curr {
|
|
allTheCurrencies[i] = NewCode(curr[i])
|
|
}
|
|
|
|
*c = allTheCurrencies
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON conforms type to the marshaler interface
|
|
func (c Currencies) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(c.Join())
|
|
}
|
|
|
|
// Match returns if the full list equals the supplied list
|
|
func (c Currencies) Match(other Currencies) bool {
|
|
if len(c) != len(other) {
|
|
return false
|
|
}
|
|
|
|
match:
|
|
for x := range c {
|
|
for y := range other {
|
|
if c[x].Equal(other[y]) {
|
|
continue match
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// HasData checks to see if Currencies type has actual currencies
|
|
func (c Currencies) HasData() bool {
|
|
return len(c) != 0
|
|
}
|