Improve currency pair handling code

Fixes these happy little accidents:
1) Config: Actually use enabled pairs for config.GetEnabledPairs
2) Pair format: Handle edge case for pairs DASHKRW vs DSHKRW and improve testing
This commit is contained in:
Adrian Gallagher
2019-04-15 14:49:46 +10:00
parent 32b43387fd
commit 5a42a4162b
4 changed files with 27 additions and 12 deletions

View File

@@ -610,7 +610,7 @@ func (c *Config) GetEnabledPairs(exchName string) (currency.Pairs, error) {
return nil, err
}
return exchCfg.AvailablePairs.Format(exchCfg.ConfigCurrencyPairFormat.Delimiter,
return exchCfg.EnabledPairs.Format(exchCfg.ConfigCurrencyPairFormat.Delimiter,
exchCfg.ConfigCurrencyPairFormat.Index,
exchCfg.ConfigCurrencyPairFormat.Uppercase), nil
}

View File

@@ -4,6 +4,7 @@ import (
"math/rand"
"github.com/thrasher-/gocryptotrader/common"
log "github.com/thrasher-/gocryptotrader/logger"
)
// NewPairsFromStrings takes in currency pair strings and returns a currency
@@ -41,19 +42,25 @@ func (p Pairs) Join() string {
func (p Pairs) Format(delimiter, index string, uppercase bool) Pairs {
var pairs Pairs
for i := range p {
var formattedPair Pair
formattedPair.Delimiter = delimiter
formattedPair.Base = p[i].Base
formattedPair.Quote = p[i].Quote
var formattedPair = Pair{
Delimiter: delimiter,
Base: p[i].Base,
Quote: p[i].Quote,
}
if index != "" {
formattedPair.Quote = NewCode(index)
newP, err := NewPairFromIndex(p[i].String(), index)
if err != nil {
log.Errorf("failed to create NewPairFromIndex. Err: %s", err)
continue
}
formattedPair.Base = newP.Base
formattedPair.Quote = newP.Quote
}
if uppercase {
pairs = append(pairs, formattedPair.Upper())
} else {
pairs = append(pairs, formattedPair)
pairs = append(pairs, formattedPair.Lower())
}
}
return pairs

View File

@@ -50,13 +50,19 @@ func TestPairsFormat(t *testing.T) {
expected = "btc:usd,btc:aud,btc:ltc"
if pairs.Format(":", "", false).Join() != expected {
t.Errorf("Test Failed - Pairs Join() error expected %s but received %s",
expected, pairs.Format("-", "", true).Join())
expected, pairs.Format(":", "", false).Join())
}
expected = "btc:krw,btc:krw,btc:krw"
if pairs.Format(":", "krw", false).Join() != expected {
if pairs.Format(":", "KRW", false).Join() != "" {
t.Errorf("Test Failed - Pairs Join() error expected %s but received %s",
expected, pairs.Format("-", "", true).Join())
expected, pairs.Format(":", "KRW", true).Join())
}
pairs = NewPairsFromStrings([]string{"DASHKRW", "BTCKRW"})
expected = "dash-krw,btc-krw"
if pairs.Format("-", "KRW", false).Join() != expected {
t.Errorf("Test Failed - Pairs Join() error expected %s but received %s",
expected, pairs.Format("-", "KRW", false).Join())
}
}

View File

@@ -401,6 +401,7 @@ func TestGetEnabledCurrencies(t *testing.T) {
format := config.CurrencyPairFormatConfig{
Delimiter: "-",
Index: "",
Uppercase: true,
}
b.RequestCurrencyPairFormat = format
@@ -466,6 +467,7 @@ func TestGetAvailableCurrencies(t *testing.T) {
format := config.CurrencyPairFormatConfig{
Delimiter: "-",
Index: "",
Uppercase: true,
}
b.RequestCurrencyPairFormat = format