From 8e7e2fb91446c95013c2ff2bf73049bbf4345456 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 2 Jan 2018 18:49:59 +1100 Subject: [PATCH] Add currency translation mapping For example BTC is also XBT --- currency/translation/translation.go | 32 ++++++++++++++++++ currency/translation/translation_test.go | 42 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 currency/translation/translation.go create mode 100644 currency/translation/translation_test.go diff --git a/currency/translation/translation.go b/currency/translation/translation.go new file mode 100644 index 00000000..decb9680 --- /dev/null +++ b/currency/translation/translation.go @@ -0,0 +1,32 @@ +package translation + +import ( + "errors" + + "github.com/thrasher-/gocryptotrader/currency/pair" +) + +var translations = map[pair.CurrencyItem]pair.CurrencyItem{ + "BTC": "XBT", + "DOGE": "XDG", + "USD": "USDT", +} + +// GetTranslation returns similar strings for a particular currency +func GetTranslation(currency pair.CurrencyItem) (pair.CurrencyItem, error) { + result, ok := translations[currency] + if !ok { + return "", errors.New("no translation found for specified currency") + } + + return result, nil +} + +// HasTranslation returns whether or not a particular currency has a translation +func HasTranslation(currency pair.CurrencyItem) bool { + _, ok := translations[currency] + if !ok { + return false + } + return true +} diff --git a/currency/translation/translation_test.go b/currency/translation/translation_test.go new file mode 100644 index 00000000..d88d0f2c --- /dev/null +++ b/currency/translation/translation_test.go @@ -0,0 +1,42 @@ +package translation + +import ( + "testing" + + "github.com/thrasher-/gocryptotrader/currency/pair" +) + +func TestGetTranslation(t *testing.T) { + currencyPair := pair.NewCurrencyPair("BTC", "USD") + expected := pair.CurrencyItem("XBT") + actual, err := GetTranslation(currencyPair.FirstCurrency) + if err != nil { + t.Error("GetTranslation: failed to retrieve translation for BTC") + } + + if expected != actual { + t.Error("GetTranslation: translation result was different to expected result") + } + + currencyPair.FirstCurrency = "ETH" + _, err = GetTranslation(currencyPair.FirstCurrency) + if err == nil { + t.Error("GetTranslation: no error on non translatable currency") + } +} + +func TestHasTranslation(t *testing.T) { + currencyPair := pair.NewCurrencyPair("BTC", "USD") + expected := true + actual := HasTranslation(currencyPair.FirstCurrency) + if expected != actual { + t.Error("HasTranslation: translation result was different to expected result") + } + + currencyPair.FirstCurrency = "ETH" + expected = false + actual = HasTranslation(currencyPair.FirstCurrency) + if expected != actual { + t.Error("HasTranslation: translation result was different to expected result") + } +}