diff --git a/config/config.go b/config/config.go index 0ea6c1d9..61fc6128 100644 --- a/config/config.go +++ b/config/config.go @@ -226,7 +226,7 @@ func (c *Config) CheckWebserverConfigValues() error { // pairs either cryptoCurrencies or fiatCurrencies func (c *Config) RetrieveConfigCurrencyPairs() error { cryptoCurrencies := common.SplitStrings(c.Cryptocurrencies, ",") - fiatCurrencies := common.SplitStrings(currency.DEFAULT_CURRENCIES, ",") + fiatCurrencies := common.SplitStrings(currency.DefaultCurrencies, ",") for _, s := range cryptoCurrencies { _, err := strconv.Atoi(s) diff --git a/currency/currency.go b/currency/currency.go index c4ee3471..dbd057f1 100644 --- a/currency/currency.go +++ b/currency/currency.go @@ -11,8 +11,9 @@ import ( "github.com/thrasher-/gocryptotrader/common" ) +// Rate holds the current exchange rates for the currency pair. type Rate struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"Name"` Rate float64 `json:",string"` Date string `json:"Date"` @@ -21,12 +22,14 @@ type Rate struct { Bid float64 `json:",string"` } +// YahooJSONResponseInfo is a sub type that holds JSON response info type YahooJSONResponseInfo struct { Count int `json:"count"` Created time.Time `json:"created"` Lang string `json:"lang"` } +// YahooJSONResponse holds Yahoo API responses type YahooJSONResponse struct { Query struct { YahooJSONResponseInfo @@ -37,31 +40,44 @@ type YahooJSONResponse struct { } const ( - MAX_CURRENCY_PAIRS_PER_REQUEST = 350 - YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql" - YAHOO_DATABASE = "store://datatables.org/alltableswithkeys" - DEFAULT_CURRENCIES = "USD,AUD,EUR,CNY" - DEFAULT_CRYPTOCURRENCIES = "BTC,LTC,ETH,DOGE,DASH,XRP,XMR" + maxCurrencyPairsPerRequest = 350 + yahooYQLURL = "http://query.yahooapis.com/v1/public/yql" + yahooDatabase = "store://datatables.org/alltableswithkeys" + // DefaultCurrencies has the default minimum of FIAT values + DefaultCurrencies = "USD,AUD,EUR,CNY" + // DefaultCryptoCurrencies has the default minimum of crytpocurrency values + DefaultCryptoCurrencies = "BTC,LTC,ETH,DOGE,DASH,XRP,XMR" ) +// Variables for package which includes base error strings & exportable +// queries var ( CurrencyStore map[string]Rate 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.") - ErrQueryingYahooZeroCount = errors.New("Yahoo returned zero currency data.") + 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") + ErrQueryingYahooZeroCount = errors.New("yahoo returned zero currency data") ) +// IsDefaultCurrency checks if the currency passed in matches the default +// FIAT currency func IsDefaultCurrency(currency string) bool { - return common.StringContains(DEFAULT_CURRENCIES, common.StringToUpper(currency)) + return common.StringContains( + DefaultCurrencies, common.StringToUpper(currency), + ) } +// IsDefaultCryptocurrency checks if the currency passed in matches the default +// CRYPTO currency func IsDefaultCryptocurrency(currency string) bool { - return common.StringContains(DEFAULT_CRYPTOCURRENCIES, common.StringToUpper(currency)) + return common.StringContains( + DefaultCryptoCurrencies, common.StringToUpper(currency), + ) } +// IsFiatCurrency checks if the currency passed is an enabled FIAT currency func IsFiatCurrency(currency string) bool { if BaseCurrencies == "" { log.Println("IsFiatCurrency: BaseCurrencies string variable not populated") @@ -69,13 +85,18 @@ func IsFiatCurrency(currency string) bool { return common.StringContains(BaseCurrencies, common.StringToUpper(currency)) } +// IsCryptocurrency checks if the currency passed is an enabled CRYPTO currency. func IsCryptocurrency(currency string) bool { if CryptoCurrencies == "" { - log.Println("IsCryptocurrency: CryptoCurrencies string variable not populated") + log.Println( + "IsCryptocurrency: CryptoCurrencies string variable not populated", + ) } return common.StringContains(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 @@ -87,11 +108,12 @@ func ContainsSeparator(input string) (bool, string) { } if len(separatorsContainer) == 0 { return false, "" - } else { - return true, strings.Join(separatorsContainer, ",") } + 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) { @@ -101,6 +123,8 @@ func ContainsBaseCurrencyIndex(baseCurrencies []string, currency string) (bool, 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) { @@ -110,6 +134,9 @@ func ContainsBaseCurrency(baseCurrencies []string, currency string) bool { 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) { @@ -118,40 +145,39 @@ func CheckAndAddCurrency(input []string, check string) []string { return input } continue - } else { - return input } + return input } else if IsDefaultCryptocurrency(x) { if IsDefaultCryptocurrency(check) { if check == x { return input } continue - } else { - return input } - } else { return input } + return input } - input = append(input, check) return input } +// SeedCurrencyData takes the desired FIAT currency string, if not defined the +// function will assign it the default values. The function will query +// yahoo for the currency values and will seed currency data. func SeedCurrencyData(fiatCurrencies string) error { if fiatCurrencies == "" { - fiatCurrencies = DEFAULT_CURRENCIES + fiatCurrencies = DefaultCurrencies } err := QueryYahooCurrencyValues(fiatCurrencies) if err != nil { return ErrQueryingYahoo } - return nil } +// MakecurrencyPairs takes all supported currency and turns them into pairs. func MakecurrencyPairs(supportedCurrencies string) string { currencies := common.SplitStrings(supportedCurrencies, ",") pairs := []string{} @@ -167,9 +193,10 @@ func MakecurrencyPairs(supportedCurrencies string) string { return common.JoinStrings(pairs, ",") } +// ConvertCurrency for example converts $1 USD to the equivalent Japanese Yen +// or vice versa. func ConvertCurrency(amount float64, from, to string) (float64, error) { currency := common.StringToUpper(from + to) - if CurrencyStore[currency].Name != currency { err := SeedCurrencyData(currency[:len(from)] + "," + currency[len(to):]) if err != nil { @@ -185,24 +212,29 @@ func ConvertCurrency(amount float64, from, to string) (float64, error) { return 0, ErrCurrencyNotFound } +// FetchYahooCurrencyData seeds the variable CurrencyStore; this is a +// map[string]Rate func FetchYahooCurrencyData(currencyPairs []string) error { values := url.Values{} - values.Set("q", fmt.Sprintf("SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", common.JoinStrings(currencyPairs, ","))) + values.Set( + "q", fmt.Sprintf("SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", + common.JoinStrings(currencyPairs, ",")), + ) values.Set("format", "json") - values.Set("env", YAHOO_DATABASE) + values.Set("env", yahooDatabase) headers := make(map[string]string) headers["Content-Type"] = "application/x-www-form-urlencoded" - resp, err := common.SendHTTPRequest("POST", YAHOO_YQL_URL, headers, strings.NewReader(values.Encode())) - + resp, err := common.SendHTTPRequest( + "POST", yahooYQLURL, headers, strings.NewReader(values.Encode()), + ) if err != nil { return err } yahooResp := YahooJSONResponse{} err = common.JSONDecode([]byte(resp), &yahooResp) - if err != nil { return err } @@ -212,25 +244,29 @@ func FetchYahooCurrencyData(currencyPairs []string) error { } for i := 0; i < yahooResp.Query.YahooJSONResponseInfo.Count; i++ { - CurrencyStore[yahooResp.Query.Results.Rate[i].Id] = yahooResp.Query.Results.Rate[i] + CurrencyStore[yahooResp.Query.Results.Rate[i].ID] = yahooResp.Query.Results.Rate[i] } - return nil } +// QueryYahooCurrencyValues takes in desired currencies, creates pairs then +// uses FetchYahooCurrencyData to seed CurrencyStore func QueryYahooCurrencyValues(currencies string) error { CurrencyStore = make(map[string]Rate) currencyPairs := common.SplitStrings(MakecurrencyPairs(currencies), ",") - log.Printf("%d fiat currency pairs generated. Fetching Yahoo currency data (this may take a minute)..\n", len(currencyPairs)) + log.Printf( + "%d fiat currency pairs generated. Fetching Yahoo currency data (this may take a minute)..\n", + len(currencyPairs), + ) var err error var pairs []string index := 0 - if len(currencyPairs) > MAX_CURRENCY_PAIRS_PER_REQUEST { + if len(currencyPairs) > maxCurrencyPairsPerRequest { for index < len(currencyPairs) { - if len(currencyPairs)-index > MAX_CURRENCY_PAIRS_PER_REQUEST { - pairs = currencyPairs[index : index+MAX_CURRENCY_PAIRS_PER_REQUEST] - index += MAX_CURRENCY_PAIRS_PER_REQUEST + if len(currencyPairs)-index > maxCurrencyPairsPerRequest { + pairs = currencyPairs[index : index+maxCurrencyPairsPerRequest] + index += maxCurrencyPairsPerRequest } else { pairs = currencyPairs[index:len(currencyPairs)] index += (len(currencyPairs) - index) @@ -243,7 +279,6 @@ func QueryYahooCurrencyValues(currencies string) error { } else { pairs = currencyPairs[index:len(currencyPairs)] err = FetchYahooCurrencyData(pairs) - if err != nil { return err } diff --git a/currency/currency_test.go b/currency/currency_test.go index 340021cb..c2d96115 100644 --- a/currency/currency_test.go +++ b/currency/currency_test.go @@ -13,13 +13,20 @@ func TestIsDefaultCurrency(t *testing.T) { var str1, str2, str3 string = "USD", "usd", "cats123" if !IsDefaultCurrency(str1) { - t.Errorf("Test Failed. TestIsDefaultCurrency: \nCannot match currency, %s.", str1) + t.Errorf( + "Test Failed. TestIsDefaultCurrency: \nCannot match currency, %s.", str1, + ) } if !IsDefaultCurrency(str2) { - t.Errorf("Test Failed. TestIsDefaultCurrency: \nCannot match currency, %s.", str2) + t.Errorf( + "Test Failed. TestIsDefaultCurrency: \nCannot match currency, %s.", str2, + ) } if IsDefaultCurrency(str3) { - t.Errorf("Test Failed. TestIsDefaultCurrency: \nFunction return is incorrect with, %s.", str3) + t.Errorf( + "Test Failed. TestIsDefaultCurrency: \nFunction return is incorrect with, %s.", + str3, + ) } } @@ -29,13 +36,22 @@ func TestIsDefaultCryptocurrency(t *testing.T) { var str1, str2, str3 string = "BTC", "btc", "dogs123" if !IsDefaultCryptocurrency(str1) { - t.Errorf("Test Failed. TestIsDefaultCryptocurrency: \nCannot match currency, %s.", str1) + t.Errorf( + "Test Failed. TestIsDefaultCryptocurrency: \nCannot match currency, %s.", + str1, + ) } if !IsDefaultCryptocurrency(str2) { - t.Errorf("Test Failed. TestIsDefaultCryptocurrency: \nCannot match currency, %s.", str2) + t.Errorf( + "Test Failed. TestIsDefaultCryptocurrency: \nCannot match currency, %s.", + str2, + ) } if IsDefaultCryptocurrency(str3) { - t.Errorf("Test Failed. TestIsDefaultCryptocurrency: \nFunction return is incorrect with, %s.", str3) + t.Errorf( + "Test Failed. TestIsDefaultCryptocurrency: \nFunction return is incorrect with, %s.", + str3, + ) } } @@ -46,13 +62,19 @@ func TestIsFiatCurrency(t *testing.T) { var str1, str2, str3 string = "BTC", "USD", "birds123" if IsFiatCurrency(str1) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str1) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str1, + ) } if !IsFiatCurrency(str2) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str2) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str2, + ) } if IsFiatCurrency(str3) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str3) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str3, + ) } } @@ -63,13 +85,19 @@ func TestIsCryptocurrency(t *testing.T) { var str1, str2, str3 string = "USD", "BTC", "pterodactyl123" if IsCryptocurrency(str1) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str1) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str1, + ) } if !IsCryptocurrency(str2) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str2) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str2, + ) } if IsCryptocurrency(str3) { - t.Errorf("Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str3) + t.Errorf( + "Test Failed. TestIsFiatCurrency: \nCannot match currency, %s.", str3, + ) } } @@ -80,19 +108,28 @@ func TestContainsSeparator(t *testing.T) { doesIt, whatIsIt := ContainsSeparator(str1) if doesIt != true || whatIsIt != "-" { - t.Errorf("Test Failed. ContainsSeparator: \nCannot find separator, %s.", str1) + 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) + 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) + 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) + t.Errorf( + "Test Failed. ContainsSeparator: \nReturn Issues with string, %s.", str3, + ) } } @@ -104,11 +141,17 @@ func TestContainsBaseCurrencyIndex(t *testing.T) { isIt, whatIsIt := ContainsBaseCurrencyIndex(baseCurrencies, currency1) if !isIt && whatIsIt != "USD" { - t.Errorf("Test Failed. ContainsBaseCurrencyIndex: \nReturned: %t & %s, with Currency as %s.", isIt, whatIsIt, currency1) + 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) + t.Errorf( + "Test Failed. ContainsBaseCurrencyIndex: \nReturned: %t & %s, with Currency as %s.", + isIt2, whatIsIt2, currency2, + ) } } @@ -120,11 +163,15 @@ func TestContainsBaseCurrency(t *testing.T) { isIt := ContainsBaseCurrency(baseCurrencies, currency1) if !isIt { - t.Errorf("Test Failed. ContainsBaseCurrency: \nReturned: %t, with Currency as %s.", isIt, currency1) + 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) + t.Errorf("Test Failed. ContainsBaseCurrency: \nReturned: %t, with Currency as %s.", + isIt2, currency2, + ) } } @@ -133,6 +180,7 @@ func TestCheckAndAddCurrency(t *testing.T) { inputFiat := []string{"USD", "AUD", "EUR"} inputCrypto := []string{"BTC", "LTC", "ETH", "DOGE", "DASH", "XRP"} + testError := []string{"Testy"} fiat := "USD" fiatIncrease := "CNY" crypto := "LTC" @@ -141,37 +189,69 @@ func TestCheckAndAddCurrency(t *testing.T) { appendedString := CheckAndAddCurrency(inputFiat, fiat) if len(appendedString) > len(inputFiat) { - t.Errorf("Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.", fiat) + 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) + 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) + 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) + t.Errorf( + "Test Failed. CheckAndAddCurrency: Error with inputFiat, currency as %s.", + obtuse, + ) } appendedString = CheckAndAddCurrency(inputCrypto, crypto) if len(appendedString) > len(inputCrypto) { - t.Errorf("Test Failed. CheckAndAddCurrency: Error with inputCrytpo, currency as %s.", crypto) + 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) + 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) + 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) + 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, + ) } } @@ -184,23 +264,34 @@ func TestSeedCurrencyData(t *testing.T) { err := SeedCurrencyData(currencyRequestDefault) if err != nil { - t.Errorf("Test Failed. SeedCurrencyData: Error %s with currency as %s.", err, currencyRequestDefault) + t.Errorf( + "Test Failed. SeedCurrencyData: Error %s with currency as %s.", + err, currencyRequestDefault, + ) } err2 := SeedCurrencyData(currencyRequestUSDAUD) if err2 != nil { - t.Errorf("Test Failed. SeedCurrencyData: Error %s with currency as %s.", err2, currencyRequestUSDAUD) + t.Errorf( + "Test Failed. SeedCurrencyData: Error %s with currency as %s.", + err2, currencyRequestUSDAUD, + ) } err3 := SeedCurrencyData(currencyRequestObtuse) if err3 == nil { - t.Errorf("Test Failed. SeedCurrencyData: Error %s with currency as %s.", err3, currencyRequestObtuse) + t.Errorf( + "Test Failed. SeedCurrencyData: Error %s with currency as %s.", + err3, currencyRequestObtuse, + ) } } func TestMakecurrencyPairs(t *testing.T) { t.Parallel() - lengthDefault := len(common.SplitStrings(DEFAULT_CURRENCIES, ",")) - fiatPairsLength := len(common.SplitStrings(MakecurrencyPairs(DEFAULT_CURRENCIES), ",")) + lengthDefault := len(common.SplitStrings(DefaultCurrencies, ",")) + fiatPairsLength := len( + common.SplitStrings(MakecurrencyPairs(DefaultCurrencies), ","), + ) if lengthDefault*(lengthDefault-1) > fiatPairsLength { t.Error("Test Failed. MakecurrencyPairs: Error, mismatched length") @@ -210,7 +301,7 @@ func TestMakecurrencyPairs(t *testing.T) { func TestConvertCurrency(t *testing.T) { t.Parallel() - fiatCurrencies := DEFAULT_CURRENCIES + fiatCurrencies := DefaultCurrencies for _, currencyFrom := range common.SplitStrings(fiatCurrencies, ",") { for _, currencyTo := range common.SplitStrings(fiatCurrencies, ",") { if currencyFrom == currencyTo { @@ -218,14 +309,18 @@ func TestConvertCurrency(t *testing.T) { } else { floatyMcfloat, err := ConvertCurrency(1000, currencyFrom, currencyTo) if err != nil { - t.Errorf("Test Failed. ConvertCurrency: Error %s with return: %.2f Currency 1: %s Currency 2: %s", - err, floatyMcfloat, currencyFrom, currencyTo) + t.Errorf( + "Test Failed. ConvertCurrency: Error %s with return: %.2f Currency 1: %s Currency 2: %s", + err, floatyMcfloat, currencyFrom, currencyTo, + ) } if reflect.TypeOf(floatyMcfloat).String() != "float64" { t.Error("Test Failed. ConvertCurrency: Error, incorrect return type") } if floatyMcfloat <= 0 { - t.Error("Test Failed. ConvertCurrency: Error, negative return or a serious issue with current fiat") + t.Error( + "Test Failed. ConvertCurrency: Error, negative return or a serious issue with current fiat", + ) } } } @@ -235,7 +330,7 @@ func TestConvertCurrency(t *testing.T) { func TestFetchYahooCurrencyData(t *testing.T) { t.Parallel() var fetchData []string - fiatCurrencies := DEFAULT_CURRENCIES + fiatCurrencies := DefaultCurrencies for _, currencyOne := range common.SplitStrings(fiatCurrencies, ",") { for _, currencyTwo := range common.SplitStrings(fiatCurrencies, ",") { @@ -255,14 +350,13 @@ func TestFetchYahooCurrencyData(t *testing.T) { func TestQueryYahooCurrencyValues(t *testing.T) { t.Parallel() - err := QueryYahooCurrencyValues(DEFAULT_CURRENCIES) + err := QueryYahooCurrencyValues(DefaultCurrencies) if err != nil { t.Errorf("Test Failed. QueryYahooCurrencyValues: Error, %s", err) } - err2 := QueryYahooCurrencyValues(DEFAULT_CRYPTOCURRENCIES) - if err2 == nil { - t.Errorf("Test Failed. QueryYahooCurrencyValues: Error, %s", err2) + err = QueryYahooCurrencyValues(DefaultCryptoCurrencies) + if err == nil { + t.Errorf("Test Failed. QueryYahooCurrencyValues: Error, %s", err) } - } diff --git a/currency/pair/pair.go b/currency/pair/pair.go index e1a24e98..4ab23c97 100644 --- a/currency/pair/pair.go +++ b/currency/pair/pair.go @@ -2,38 +2,49 @@ package pair import "strings" +// CurrencyItem is an exported string with methods to manipulate the data instead +// of using array/slice access modifiers type CurrencyItem string +// Lower converts the CurrencyItem object c to lowercase func (c CurrencyItem) Lower() CurrencyItem { return CurrencyItem(strings.ToLower(string(c))) } +// Upper converts the CurrencyItem object c to uppercase func (c CurrencyItem) Upper() CurrencyItem { return CurrencyItem(strings.ToUpper(string(c))) } +// String converts the CurrencyItem object c to string func (c CurrencyItem) String() string { return string(c) } +// CurrencyPair holds currency pair information type CurrencyPair struct { Delimiter string `json:"delimiter"` FirstCurrency CurrencyItem `json:"first_currency"` SecondCurrency CurrencyItem `json:"second_currency"` } +// GetFirstCurrency returns the first currency item func (c CurrencyPair) GetFirstCurrency() CurrencyItem { return c.FirstCurrency } +// GetSecondCurrency returns the second currency item func (c CurrencyPair) GetSecondCurrency() CurrencyItem { return c.SecondCurrency } +// Pair returns a currency pair string func (c CurrencyPair) Pair() CurrencyItem { return c.FirstCurrency + CurrencyItem(c.Delimiter) + c.SecondCurrency } +// NewCurrencyPairDelimiter splits the desired currency string at delimeter, +// the returns a CurrencyPair struct func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair { result := strings.Split(currency, delimiter) return CurrencyPair{ @@ -43,6 +54,7 @@ func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair { } } +// NewCurrencyPair returns a CurrencyPair without a delimiter func NewCurrencyPair(firstCurrency, secondCurrency string) CurrencyPair { return CurrencyPair{ FirstCurrency: CurrencyItem(firstCurrency), @@ -50,10 +62,12 @@ func NewCurrencyPair(firstCurrency, secondCurrency string) CurrencyPair { } } +// NewCurrencyPairFromString converts currency string into a new CurrencyPair +// with or without delimeter func NewCurrencyPairFromString(currency string) CurrencyPair { - delmiters := []string{"_", "-"} + delimiters := []string{"_", "-"} var delimiter string - for _, x := range delmiters { + for _, x := range delimiters { if strings.Contains(currency, x) { delimiter = x return NewCurrencyPairDelimiter(currency, delimiter) diff --git a/currency/pair/pair_test.go b/currency/pair/pair_test.go index 227ef872..cb6213ac 100644 --- a/currency/pair/pair_test.go +++ b/currency/pair/pair_test.go @@ -1,8 +1,6 @@ package pair -import ( - "testing" -) +import "testing" func TestLower(t *testing.T) { t.Parallel() @@ -43,7 +41,10 @@ func TestGetFirstCurrency(t *testing.T) { actual := pair.GetFirstCurrency() expected := CurrencyItem("BTC") if actual != expected { - t.Errorf("Test failed. GetFirstCurrency(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. GetFirstCurrency(): %s was not equal to expected value: %s", + actual, expected, + ) } } @@ -53,7 +54,10 @@ func TestGetSecondCurrency(t *testing.T) { actual := pair.GetSecondCurrency() expected := CurrencyItem("USD") if actual != expected { - t.Errorf("Test failed. GetSecondCurrency(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. GetSecondCurrency(): %s was not equal to expected value: %s", + actual, expected, + ) } } @@ -63,7 +67,10 @@ func TestPair(t *testing.T) { actual := pair.Pair() expected := CurrencyItem("BTCUSD") if actual != expected { - t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Pair(): %s was not equal to expected value: %s", + actual, expected, + ) } } @@ -73,7 +80,10 @@ func TestNewCurrencyPair(t *testing.T) { actual := pair.Pair() expected := CurrencyItem("BTCUSD") if actual != expected { - t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Pair(): %s was not equal to expected value: %s", + actual, expected, + ) } } @@ -83,13 +93,19 @@ func TestNewCurrencyPairDelimiter(t *testing.T) { actual := pair.Pair() expected := CurrencyItem("BTC-USD") if actual != expected { - t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Pair(): %s was not equal to expected value: %s", + actual, expected, + ) } actual = CurrencyItem(pair.Delimiter) expected = "-" if actual != expected { - t.Errorf("Test failed. Delmiter: %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Delmiter: %s was not equal to expected value: %s", + actual, expected, + ) } } @@ -100,7 +116,10 @@ func TestNewCurrencyPairFromString(t *testing.T) { actual := pair.Pair() expected := CurrencyItem("BTC-USD") if actual != expected { - t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Pair(): %s was not equal to expected value: %s", + actual, expected, + ) } pairStr = "BTCUSD" @@ -108,6 +127,9 @@ func TestNewCurrencyPairFromString(t *testing.T) { actual = pair.Pair() expected = CurrencyItem("BTCUSD") if actual != expected { - t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + t.Errorf( + "Test failed. Pair(): %s was not equal to expected value: %s", + actual, expected, + ) } } diff --git a/exchanges/bitfinex/bitfinex_test.go b/exchanges/bitfinex/bitfinex_test.go index d06baa70..c5dbbfc6 100644 --- a/exchanges/bitfinex/bitfinex_test.go +++ b/exchanges/bitfinex/bitfinex_test.go @@ -41,9 +41,9 @@ func TestSetup(t *testing.T) { RESTPollingDelay: time.Duration(10), Verbose: true, Websocket: true, - BaseCurrencies: currency.DEFAULT_CURRENCIES, - AvailablePairs: currency.MakecurrencyPairs(currency.DEFAULT_CURRENCIES), - EnabledPairs: currency.MakecurrencyPairs(currency.DEFAULT_CURRENCIES), + BaseCurrencies: currency.DefaultCurrencies, + AvailablePairs: currency.MakecurrencyPairs(currency.DefaultCurrencies), + EnabledPairs: currency.MakecurrencyPairs(currency.DefaultCurrencies), } setup := Bitfinex{} setup.Setup(testConfig) diff --git a/exchanges/stats/stats.go b/exchanges/stats/stats.go index 40dc7183..3578211e 100644 --- a/exchanges/stats/stats.go +++ b/exchanges/stats/stats.go @@ -46,7 +46,7 @@ func (this ByVolume) Swap(i, j int) { func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) { if currency.BaseCurrencies == "" { - currency.BaseCurrencies = currency.DEFAULT_CURRENCIES + currency.BaseCurrencies = currency.DefaultCurrencies } if !currency.IsFiatCurrency(fiat) {