Improvements in config, currency, exchange, pair and helper code

This commit is contained in:
Adrian Gallagher
2018-01-17 13:42:09 +11:00
parent 3e30bb7213
commit 5dc6df72aa
12 changed files with 475 additions and 373 deletions

View File

@@ -62,8 +62,8 @@ const (
var (
CurrencyStore map[string]Rate
CurrencyStoreFixer map[string]float64
BaseCurrencies string
CryptoCurrencies string
BaseCurrencies []string
CryptoCurrencies []string
ErrCurrencyDataNotFetched = errors.New("yahoo currency data has not been fetched yet")
ErrCurrencyNotFound = errors.New("unable to find specified currency")
ErrQueryingYahoo = errors.New("unable to query Yahoo currency values")
@@ -103,104 +103,50 @@ func GetProvider() string {
// IsDefaultCurrency checks if the currency passed in matches the default
// FIAT currency
func IsDefaultCurrency(currency string) bool {
return common.StringContains(
DefaultCurrencies, common.StringToUpper(currency),
)
defaultCurrencies := common.SplitStrings(DefaultCurrencies, ",")
return common.DataContains(defaultCurrencies, common.StringToUpper(currency))
}
// IsDefaultCryptocurrency checks if the currency passed in matches the default
// CRYPTO currency
func IsDefaultCryptocurrency(currency string) bool {
return common.StringContains(
DefaultCryptoCurrencies, common.StringToUpper(currency),
)
cryptoCurrencies := common.SplitStrings(DefaultCryptoCurrencies, ",")
return common.DataContains(cryptoCurrencies, common.StringToUpper(currency))
}
// IsFiatCurrency checks if the currency passed is an enabled FIAT currency
func IsFiatCurrency(currency string) bool {
if BaseCurrencies == "" {
if len(BaseCurrencies) == 0 {
log.Println("IsFiatCurrency: BaseCurrencies string variable not populated")
return false
}
return common.StringContains(BaseCurrencies, common.StringToUpper(currency))
return common.DataContains(BaseCurrencies, common.StringToUpper(currency))
}
// IsCryptocurrency checks if the currency passed is an enabled CRYPTO currency.
func IsCryptocurrency(currency string) bool {
if CryptoCurrencies == "" {
if len(CryptoCurrencies) == 0 {
log.Println(
"IsCryptocurrency: CryptoCurrencies string variable not populated",
)
return false
}
return common.StringContains(CryptoCurrencies, common.StringToUpper(currency))
return common.DataContains(CryptoCurrencies, common.StringToUpper(currency))
}
// ContainsSeparator checks to see if the string passed contains "-" or "_"
// separated strings and returns what the separators were.
func ContainsSeparator(input string) (bool, string) {
separators := []string{"-", "_"}
var separatorsContainer []string
for _, x := range separators {
if common.StringContains(input, x) {
separatorsContainer = append(separatorsContainer, x)
}
}
if len(separatorsContainer) == 0 {
return false, ""
}
return true, strings.Join(separatorsContainer, ",")
}
// ContainsBaseCurrencyIndex checks the currency against the baseCurrencies and
// returns a bool and its corresponding basecurrency.
func ContainsBaseCurrencyIndex(baseCurrencies []string, currency string) (bool, string) {
for _, x := range baseCurrencies {
if common.StringContains(currency, x) {
return true, x
}
}
return false, ""
}
// ContainsBaseCurrency checks the currency against the baseCurrencies and
// returns a bool
func ContainsBaseCurrency(baseCurrencies []string, currency string) bool {
for _, x := range baseCurrencies {
if common.StringContains(currency, x) {
return true
}
}
return false
}
// CheckAndAddCurrency checks the string you passed with the input string array,
// if not already added, checks to see if it is part of the default currency
// list and returns the appended string.
func CheckAndAddCurrency(input []string, check string) []string {
for _, x := range input {
if IsDefaultCurrency(x) {
if IsDefaultCurrency(check) {
if check == x {
return input
}
continue
// Update updates the local crypto currency or base currency store
func Update(input []string, cryptos bool) {
for x := range input {
if cryptos {
if !common.DataContains(CryptoCurrencies, input[x]) {
CryptoCurrencies = append(CryptoCurrencies, input[x])
}
return input
} else if IsDefaultCryptocurrency(x) {
if IsDefaultCryptocurrency(check) {
if check == x {
return input
}
continue
} else {
if !common.DataContains(BaseCurrencies, input[x]) {
BaseCurrencies = append(BaseCurrencies, input[x])
}
return input
}
return input
}
input = append(input, check)
return input
}
// SeedCurrencyData takes the desired FIAT currency string, if not defined the
@@ -221,7 +167,7 @@ func SeedCurrencyData(fiatCurrencies string) error {
// MakecurrencyPairs takes all supported currency and turns them into pairs.
func MakecurrencyPairs(supportedCurrencies string) string {
currencies := common.SplitStrings(supportedCurrencies, ",")
pairs := []string{}
var pairs []string
count := len(currencies)
for i := 0; i < count; i++ {
currency := currencies[i]

View File

@@ -115,13 +115,11 @@ func TestIsDefaultCryptocurrency(t *testing.T) {
}
func TestIsFiatCurrency(t *testing.T) {
t.Parallel()
if IsFiatCurrency("") {
t.Error("Test failed. TestIsFiatCurrency returned true on an empty string")
}
BaseCurrencies = "USD,AUD"
BaseCurrencies = []string{"USD", "AUD"}
var str1, str2, str3 string = "BTC", "USD", "birds123"
if IsFiatCurrency(str1) {
@@ -142,13 +140,11 @@ func TestIsFiatCurrency(t *testing.T) {
}
func TestIsCryptocurrency(t *testing.T) {
t.Parallel()
if IsCryptocurrency("") {
t.Error("Test failed. TestIsCryptocurrency returned true on an empty string")
}
CryptoCurrencies = "BTC,LTC,DASH"
CryptoCurrencies = []string{"BTC", "LTC", "DASH"}
var str1, str2, str3 string = "USD", "BTC", "pterodactyl123"
if IsCryptocurrency(str1) {
@@ -168,156 +164,22 @@ func TestIsCryptocurrency(t *testing.T) {
}
}
func TestContainsSeparator(t *testing.T) {
t.Parallel()
func TestUpdate(t *testing.T) {
CryptoCurrencies = []string{"BTC", "LTC", "DASH"}
BaseCurrencies = []string{"USD", "AUD"}
var str1, str2, str3, str4 string = "ding-dong", "ding_dong", "dong_ding-dang", "ding"
Update([]string{"ETH"}, true)
Update([]string{"JPY"}, false)
doesIt, whatIsIt := ContainsSeparator(str1)
if doesIt != true || whatIsIt != "-" {
t.Errorf(
"Test Failed. ContainsSeparator: \nCannot find separator, %s.", str1,
)
}
doesIt2, whatIsIt2 := ContainsSeparator(str2)
if doesIt2 != true || whatIsIt2 != "_" {
t.Errorf(
"Test Failed. ContainsSeparator: \nCannot find separator, %s.", str2,
)
}
doesIt3, whatIsIt3 := ContainsSeparator(str3)
if doesIt3 != true || len(whatIsIt3) != 3 {
t.Errorf(
"Test Failed. ContainsSeparator: \nCannot find or incorrect separator, %s.",
str3,
)
}
doesIt4, whatIsIt4 := ContainsSeparator(str4)
if doesIt4 != false || whatIsIt4 != "" {
t.Errorf(
"Test Failed. ContainsSeparator: \nReturn Issues with string, %s.", str3,
)
}
}
func TestContainsBaseCurrencyIndex(t *testing.T) {
t.Parallel()
baseCurrencies := []string{"USD", "AUD", "EUR", "CNY"}
currency1, currency2 := "USD", "DINGDONG"
isIt, whatIsIt := ContainsBaseCurrencyIndex(baseCurrencies, currency1)
if !isIt && whatIsIt != "USD" {
t.Errorf(
"Test Failed. ContainsBaseCurrencyIndex: \nReturned: %t & %s, with Currency as %s.",
isIt, whatIsIt, currency1,
)
}
isIt2, whatIsIt2 := ContainsBaseCurrencyIndex(baseCurrencies, currency2)
if isIt2 && whatIsIt2 != "DINGDONG" {
t.Errorf(
"Test Failed. ContainsBaseCurrencyIndex: \nReturned: %t & %s, with Currency as %s.",
isIt2, whatIsIt2, currency2,
)
}
}
func TestContainsBaseCurrency(t *testing.T) {
t.Parallel()
baseCurrencies := []string{"USD", "AUD", "EUR", "CNY"}
currency1, currency2 := "USD", "DINGDONG"
isIt := ContainsBaseCurrency(baseCurrencies, currency1)
if !isIt {
t.Errorf("Test Failed. ContainsBaseCurrency: \nReturned: %t, with Currency as %s.",
isIt, currency1,
)
}
isIt2 := ContainsBaseCurrency(baseCurrencies, currency2)
if isIt2 {
t.Errorf("Test Failed. ContainsBaseCurrency: \nReturned: %t, with Currency as %s.",
isIt2, currency2,
)
}
}
func TestCheckAndAddCurrency(t *testing.T) {
t.Parallel()
inputFiat := []string{"USD", "AUD", "EUR"}
inputCrypto := []string{"BTC", "LTC", "ETH", "DOGE", "DASH", "XRP"}
testError := []string{"Testy"}
fiat := "USD"
fiatIncrease := "CNY"
crypto := "LTC"
cryptoIncrease := "XMR"
obtuse := "CATSANDDOGS"
appendedString := CheckAndAddCurrency(inputFiat, fiat)
if len(appendedString) > len(inputFiat) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.",
fiat,
)
}
appendedString = CheckAndAddCurrency(inputFiat, fiatIncrease)
if len(appendedString) <= len(inputFiat) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.",
fiatIncrease,
)
}
appendedString = CheckAndAddCurrency(inputFiat, crypto)
if len(appendedString) > len(inputFiat) {
t.Log(appendedString)
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.",
crypto,
)
}
appendedString = CheckAndAddCurrency(inputFiat, obtuse)
if len(appendedString) > len(inputFiat) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.",
obtuse,
if !IsCryptocurrency("ETH") {
t.Error(
"Test Failed. TestUpdate: \nCannot match currency: ETH",
)
}
appendedString = CheckAndAddCurrency(inputCrypto, crypto)
if len(appendedString) > len(inputCrypto) {
if !IsFiatCurrency("JPY") {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputCrytpo, currency as %s.",
crypto,
)
}
appendedString = CheckAndAddCurrency(inputCrypto, cryptoIncrease)
if len(appendedString) <= len(inputCrypto) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputCrytpo, currency as %s.",
cryptoIncrease,
)
}
appendedString = CheckAndAddCurrency(inputCrypto, fiat)
if len(appendedString) > len(inputCrypto) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputCrytpo, currency as %s.",
fiat,
)
}
appendedString = CheckAndAddCurrency(inputCrypto, obtuse)
if len(appendedString) > len(inputCrypto) {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputCrytpo, currency as %s.",
obtuse,
)
}
appendedString = CheckAndAddCurrency(testError, "USD")
if appendedString[0] != testError[0] {
t.Errorf(
"Test Failed. CheckAndAddCurrency: Error with inputCrytpo, basecurrency as %s.",
testError,
"Test Failed. TestUpdate: \nCannot match currency: JPY",
)
}
}

View File

@@ -126,3 +126,23 @@ func Contains(pairs []CurrencyPair, p CurrencyPair) bool {
}
return false
}
// FormatPairs formats a string array to a list of currency pairs with the
// supplied currency pair format
func FormatPairs(pairs []string, delimiter, index string) []CurrencyPair {
var result []CurrencyPair
for x := range pairs {
var p CurrencyPair
if delimiter != "" {
p = NewCurrencyPairDelimiter(pairs[x], delimiter)
} else {
if index != "" {
p = NewCurrencyPairFromIndex(pairs[x], index)
} else {
p = NewCurrencyPair(pairs[x][0:3], pairs[x][3:])
}
}
result = append(result, p)
}
return result
}

View File

@@ -251,3 +251,17 @@ func TestContains(t *testing.T) {
t.Errorf("Test failed. TestContains: Non-existant pair was found")
}
}
func TestFormatPairs(t *testing.T) {
if FormatPairs([]string{"BTC-USD"}, "-", "")[0].Pair().String() != "BTC-USD" {
t.Error("Test failed. TestFormatPairs: Expected pair was not found")
}
if FormatPairs([]string{"BTCUSD"}, "", "BTC")[0].Pair().String() != "BTCUSD" {
t.Error("Test failed. TestFormatPairs: Expected pair was not found")
}
if FormatPairs([]string{"ETHUSD"}, "", "")[0].Pair().String() != "ETHUSD" {
t.Error("Test failed. TestFormatPairs: Expected pair was not found")
}
}