Skip to content

Commit

Permalink
Improve currency pair handling code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thrasher- committed Apr 15, 2019
1 parent 32b4338 commit 5a42a41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 14 additions & 7 deletions currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions currency/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
2 changes: 2 additions & 0 deletions exchanges/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func TestGetEnabledCurrencies(t *testing.T) {
format := config.CurrencyPairFormatConfig{
Delimiter: "-",
Index: "",
Uppercase: true,
}

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

b.RequestCurrencyPairFormat = format
Expand Down

0 comments on commit 5a42a41

Please sign in to comment.