mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* drop common uuid v4 func and imported package as needed * removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests. * add in string * explicitly throw away return error value * atleast return the error that websocket initialise returns * return error when not connected * fix comment * Adds comments * move package declarations * drop append whenever we call supported * remove unused import * Change incorrect spelling * fix tests * fix go import issue
194 lines
4.0 KiB
Go
194 lines
4.0 KiB
Go
package currency
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/rand"
|
|
"strings"
|
|
|
|
log "github.com/thrasher-corp/gocryptotrader/logger"
|
|
)
|
|
|
|
// NewPairsFromStrings takes in currency pair strings and returns a currency
|
|
// pair list
|
|
func NewPairsFromStrings(pairs []string) Pairs {
|
|
var ps Pairs
|
|
for _, p := range pairs {
|
|
if p == "" {
|
|
continue
|
|
}
|
|
|
|
ps = append(ps, NewPairFromString(p))
|
|
}
|
|
return ps
|
|
}
|
|
|
|
// Strings returns a slice of strings referring to each currency pair
|
|
func (p Pairs) Strings() []string {
|
|
var list []string
|
|
for i := range p {
|
|
list = append(list, p[i].String())
|
|
}
|
|
return list
|
|
}
|
|
|
|
// Join returns a comma separated list of currency pairs
|
|
func (p Pairs) Join() string {
|
|
return strings.Join(p.Strings(), ",")
|
|
}
|
|
|
|
// Format formats the pair list to the exchange format configuration
|
|
func (p Pairs) Format(delimiter, index string, uppercase bool) Pairs {
|
|
var pairs Pairs
|
|
for i := range p {
|
|
var formattedPair = Pair{
|
|
Delimiter: delimiter,
|
|
Base: p[i].Base,
|
|
Quote: p[i].Quote,
|
|
}
|
|
if index != "" {
|
|
newP, err := NewPairFromIndex(p[i].String(), index)
|
|
if err != nil {
|
|
log.Errorf(log.Global,
|
|
"failed to create NewPairFromIndex. Err: %s\n", err)
|
|
continue
|
|
}
|
|
formattedPair.Base = newP.Base
|
|
formattedPair.Quote = newP.Quote
|
|
}
|
|
|
|
if uppercase {
|
|
pairs = append(pairs, formattedPair.Upper())
|
|
} else {
|
|
pairs = append(pairs, formattedPair.Lower())
|
|
}
|
|
}
|
|
return pairs
|
|
}
|
|
|
|
// UnmarshalJSON comforms type to the umarshaler interface
|
|
func (p *Pairs) UnmarshalJSON(d []byte) error {
|
|
var pairs string
|
|
err := json.Unmarshal(d, &pairs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// If no pairs enabled in config just continue
|
|
if pairs == "" {
|
|
return nil
|
|
}
|
|
|
|
var allThePairs Pairs
|
|
for _, data := range strings.Split(pairs, ",") {
|
|
allThePairs = append(allThePairs, NewPairFromString(data))
|
|
}
|
|
|
|
*p = allThePairs
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON conforms type to the marshaler interface
|
|
func (p Pairs) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(p.Join())
|
|
}
|
|
|
|
// Upper returns an upper formatted pair list
|
|
func (p Pairs) Upper() Pairs {
|
|
var upper Pairs
|
|
for i := range p {
|
|
upper = append(upper, p[i].Upper())
|
|
}
|
|
return upper
|
|
}
|
|
|
|
// Slice exposes the underlying type
|
|
func (p Pairs) Slice() []Pair {
|
|
return p
|
|
}
|
|
|
|
// Contains checks to see if a specified pair exists inside a currency pair
|
|
// array
|
|
func (p Pairs) Contains(check Pair, exact bool) bool {
|
|
for _, pair := range p.Slice() {
|
|
if exact {
|
|
if pair.Equal(check) {
|
|
return true
|
|
}
|
|
} else {
|
|
if pair.EqualIncludeReciprocal(check) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// RemovePairsByFilter checks to see if a pair contains a specific currency
|
|
// and removes it from the list of pairs
|
|
func (p Pairs) RemovePairsByFilter(filter Code) Pairs {
|
|
var pairs Pairs
|
|
for _, pair := range p.Slice() {
|
|
if pair.ContainsCurrency(filter) {
|
|
continue
|
|
}
|
|
pairs = append(pairs, pair)
|
|
}
|
|
return pairs
|
|
}
|
|
|
|
// Remove removes the specified pair from the list of pairs if it exists
|
|
func (p Pairs) Remove(pair Pair) Pairs {
|
|
var pairs Pairs
|
|
for x := range p {
|
|
if p[x].Equal(pair) {
|
|
continue
|
|
}
|
|
pairs = append(pairs, p[x])
|
|
}
|
|
return pairs
|
|
}
|
|
|
|
// Add adds a specified pair to the list of pairs if it doesn't exist
|
|
func (p Pairs) Add(pair Pair) Pairs {
|
|
if p.Contains(pair, true) {
|
|
return p
|
|
}
|
|
p = append(p, pair)
|
|
return p
|
|
}
|
|
|
|
// FindDifferences returns pairs which are new or have been removed
|
|
func (p Pairs) FindDifferences(pairs Pairs) (newPairs, removedPairs Pairs) {
|
|
for x := range pairs {
|
|
if pairs[x].String() == "" {
|
|
continue
|
|
}
|
|
if !p.Contains(pairs[x], true) {
|
|
newPairs = append(newPairs, pairs[x])
|
|
}
|
|
}
|
|
for _, oldPair := range p {
|
|
if oldPair.String() == "" {
|
|
continue
|
|
}
|
|
if !pairs.Contains(oldPair, true) {
|
|
removedPairs = append(removedPairs, oldPair)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetRandomPair returns a random pair from a list of pairs
|
|
func (p Pairs) GetRandomPair() Pair {
|
|
pairsLen := len(p)
|
|
|
|
if pairsLen == 0 {
|
|
return Pair{Base: NewCode(""), Quote: NewCode("")}
|
|
}
|
|
|
|
return p[rand.Intn(pairsLen)]
|
|
}
|
|
|
|
// Pairs defines a list of pairs
|
|
type Pairs []Pair
|