From 842d175743eef1849f57ea0210fdfdc3c294d72c Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Fri, 11 Mar 2016 03:24:56 +1100 Subject: [PATCH] Improved currency handling. --- currency.go | 58 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/currency.go b/currency.go index 0f5c5aa9..169ce8f9 100644 --- a/currency.go +++ b/currency.go @@ -48,35 +48,61 @@ var ( ErrQueryingYahoo = errors.New("Unable to query Yahoo currency values.") ) +func IsDefaultCurrency(currency string) bool { + return StringContains(DEFAULT_CURRENCIES, StringToUpper(currency)) +} + func IsFiatCurrency(currency string) bool { - if StringContains(BaseCurrencies, StringToUpper(currency)) { - return true - } - return false + return StringContains(BaseCurrencies, StringToUpper(currency)) } func IsCryptocurrency(currency string) bool { - if StringContains(bot.config.Cryptocurrencies, StringToUpper(currency)) { - return true - } - return false + return StringContains(bot.config.Cryptocurrencies, StringToUpper(currency)) } func RetrieveConfigCurrencyPairs(config Config) error { - currencyPairs := SplitStrings(DEFAULT_CURRENCIES, ",") + var fiatCurrencies, cryptoCurrencies []string for _, exchange := range config.Exchanges { if exchange.Enabled { - currencies := SplitStrings(exchange.EnabledPairs, ",") - for _, x := range currencies { - currency := x[len(x)-3:] - if !StringContains(DEFAULT_CURRENCIES, currency) && !IsCryptocurrency(currency) { - currencyPairs = append(currencyPairs, currency) + enabledPairs := SplitStrings(exchange.EnabledPairs, ",") + baseCurrencies := SplitStrings(exchange.BaseCurrencies, ",") + + for _, x := range baseCurrencies { + for _, y := range enabledPairs { + if StringContains(y, "_") { + pairs := SplitStrings(y, "_") + for _, z := range pairs { + if z != x && !IsCryptocurrency(z) { + cryptoCurrencies = append(cryptoCurrencies, z) + } else if z == x && !IsDefaultCurrency(x) { + fiatCurrencies = append(fiatCurrencies, x) + } + } + } else { + if StringContains(y, x) { + if !IsDefaultCurrency(x) { + fiatCurrencies = append(fiatCurrencies, x) + } + currency := TrimString(y, x) + if !IsCryptocurrency(currency) { + cryptoCurrencies = append(cryptoCurrencies, currency) + } + } else { + if !IsCryptocurrency(y[0:3]) { + cryptoCurrencies = append(cryptoCurrencies, y[0:3]) + } + if !IsCryptocurrency(y[3:]) { + cryptoCurrencies = append(cryptoCurrencies, y[3:]) + } + } + } } } } } + bot.config.Cryptocurrencies = JoinStrings(StringSliceDifference(SplitStrings(bot.config.Cryptocurrencies, ","), cryptoCurrencies), ",") + BaseCurrencies = JoinStrings(StringSliceDifference(SplitStrings(DEFAULT_CURRENCIES, ","), fiatCurrencies), ",") - BaseCurrencies = JoinStrings(currencyPairs, ",") err := QueryYahooCurrencyValues(BaseCurrencies) if err != nil { @@ -107,7 +133,7 @@ func ConvertCurrency(amount float64, from, to string) (float64, error) { return 0, ErrCurrencyDataNotFetched } - currency := strings.ToUpper(from + to) + currency := StringToUpper(from + to) for i := 0; i < CurrencyStore.Query.YahooJSONResponseInfo.Count; i++ { if CurrencyStore.Query.Results.Rate[i].Id == currency { return amount * CurrencyStore.Query.Results.Rate[i].Rate, nil