mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 23:16:53 +00:00
Currency package update (#247)
* Initial currency overhaul before service system implementation * Remove redundant currency string in orderbook.Base Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times Add error handling for process orderbook * Remove redundant currency string in ticker.Price Unexport lastupdated field in ticker.Price Add error handling for process ticker function and fix tests * Phase Two Update * Update translations to use map type - thankyou to kempeng for spotting this * Change pair method name from Display -> Format for better readability * Fixes misspelling and tests * Implement requested changes from GloriousCode * Remove reduntant function and streamlined return in currency_translation.go * Revert pair method naming conventions * Change currency naming conventions * Changed code type to exported Item type with underlying string to reduce complexity * Added interim orderbook process method to orderbook.Base type * Changed feebuilder struct field to currency.Pair * Adds fall over system for backup fx providers * deprecate function and children and fix linter issue with btcmarkets * Fixed requested changes * Fix bug and move mtx for rates * Fixed after rebase oopsies * Fix linter issues * Fixes race conditions in testing functions * Final phase coinmarketcap update * fix linter issues * Implement requested changes * Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates * Add a collection of tests to improve codecov * After rebase oopsy fixes for btse * Fix requested changes * fix after rebase oopsies and add more efficient comparison checks within currency pair * Fix linter issues
This commit is contained in:
committed by
Adrian Gallagher
parent
ed760e184e
commit
0990f9d118
167
helpers_test.go
167
helpers_test.go
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
@@ -44,22 +43,22 @@ func TestGetSpecificAvailablePairs(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
result := GetSpecificAvailablePairs(true, true, true, false)
|
||||
|
||||
if !pair.Contains(result, pair.NewCurrencyPair("BTC", "USD"), true) {
|
||||
if !result.Contains(currency.NewPairFromStrings("BTC", "USD"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
if !pair.Contains(result, pair.NewCurrencyPair("BTC", "USDT"), false) {
|
||||
if !result.Contains(currency.NewPairFromStrings("BTC", "USDT"), false) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
result = GetSpecificAvailablePairs(true, true, false, false)
|
||||
|
||||
if pair.Contains(result, pair.NewCurrencyPair("BTC", "USDT"), false) {
|
||||
if result.Contains(currency.NewPairFromStrings("BTC", "USDT"), false) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
result = GetSpecificAvailablePairs(true, false, false, true)
|
||||
if !pair.Contains(result, pair.NewCurrencyPair("LTC", "BTC"), false) {
|
||||
if !result.Contains(currency.NewPairFromStrings("LTC", "BTC"), false) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
}
|
||||
@@ -68,85 +67,104 @@ func TestIsRelatablePairs(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
// Test relational pairs with similar names
|
||||
result := IsRelatablePairs(pair.NewCurrencyPair("XBT", "USD"), pair.NewCurrencyPair("BTC", "USD"), false)
|
||||
result := IsRelatablePairs(currency.NewPairFromStrings("XBT", "USD"),
|
||||
currency.NewPairFromStrings("BTC", "USD"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relational pairs with similar names reversed
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("BTC", "USD"), pair.NewCurrencyPair("XBT", "USD"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("BTC", "USD"),
|
||||
currency.NewPairFromStrings("XBT", "USD"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relational pairs with similar names but with Tether support disabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("XBT", "USD"), pair.NewCurrencyPair("BTC", "USDT"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("XBT", "USD"),
|
||||
currency.NewPairFromStrings("BTC", "USDT"), false)
|
||||
if result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relational pairs with similar names but with Tether support enabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("XBT", "USDT"), pair.NewCurrencyPair("BTC", "USD"), true)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("XBT", "USDT"),
|
||||
currency.NewPairFromStrings("BTC", "USD"), true)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relational pairs with different ordering, a delimiter and with Tether support enabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("AE", "USDT"), pair.NewCurrencyPairDelimiter("USDT-AE", "-"), true)
|
||||
// Test relational pairs with different ordering, a delimiter and with
|
||||
// Tether support enabled
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("AE", "USDT"),
|
||||
currency.NewPairDelimiter("USDT-AE", "-"), true)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relational pairs with different ordering, a delimiter and with Tether support disabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("AE", "USDT"), pair.NewCurrencyPairDelimiter("USDT-AE", "-"), false)
|
||||
// Test relational pairs with different ordering, a delimiter and with
|
||||
// Tether support disabled
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("AE", "USDT"),
|
||||
currency.NewPairDelimiter("USDT-AE", "-"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl pairs with similar names and different fiat currencies
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("XBT", "EUR"), pair.NewCurrencyPair("BTC", "AUD"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("XBT", "EUR"),
|
||||
currency.NewPairFromStrings("BTC", "AUD"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl pairs with similar names, different fiat currencies and with different ordering
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("USD", "BTC"), pair.NewCurrencyPair("BTC", "EUR"), false)
|
||||
if !result {
|
||||
// Test relationl pairs with similar names, different fiat currencies and
|
||||
// with different ordering
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("USD", "BTC"),
|
||||
currency.NewPairFromStrings("BTC", "EUR"), false)
|
||||
if !result { // Is this really expected result???
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl pairs with similar names, different fiat currencies and with Tether enabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("USD", "BTC"), pair.NewCurrencyPair("BTC", "USDT"), true)
|
||||
// Test relationl pairs with similar names, different fiat currencies and
|
||||
// with Tether enabled
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("USD", "BTC"),
|
||||
currency.NewPairFromStrings("BTC", "USDT"), true)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl crypto pairs with similar names
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("LTC", "BTC"), pair.NewCurrencyPair("BTC", "LTC"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("LTC", "BTC"),
|
||||
currency.NewPairFromStrings("BTC", "LTC"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl crypto pairs with similar different pairs
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("LTC", "ETH"), pair.NewCurrencyPair("BTC", "ETH"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("LTC", "ETH"),
|
||||
currency.NewPairFromStrings("BTC", "ETH"), false)
|
||||
if result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl crypto pairs with similar different pairs and with USDT enabled
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("USDT", "USD"), pair.NewCurrencyPair("BTC", "USD"), true)
|
||||
// Test relationl crypto pairs with similar different pairs and with USDT
|
||||
// enabled
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("USDT", "USD"),
|
||||
currency.NewPairFromStrings("BTC", "USD"), true)
|
||||
if result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl crypto pairs with with similar names
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("XBT", "LTC"), pair.NewCurrencyPair("BTC", "LTC"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("XBT", "LTC"),
|
||||
currency.NewPairFromStrings("BTC", "LTC"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
// Test relationl crypto pairs with different ordering and similar names
|
||||
result = IsRelatablePairs(pair.NewCurrencyPair("LTC", "XBT"), pair.NewCurrencyPair("BTC", "LTC"), false)
|
||||
result = IsRelatablePairs(currency.NewPairFromStrings("LTC", "XBT"),
|
||||
currency.NewPairFromStrings("BTC", "LTC"), false)
|
||||
if !result {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
@@ -154,46 +172,54 @@ func TestIsRelatablePairs(t *testing.T) {
|
||||
|
||||
func TestGetRelatableCryptocurrencies(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
p := GetRelatableCryptocurrencies(pair.NewCurrencyPair("BTC", "LTC"))
|
||||
if !pair.Contains(p, pair.NewCurrencyPair("BTC", "ETH"), true) {
|
||||
p := GetRelatableCryptocurrencies(currency.NewPairFromStrings("BTC", "LTC"))
|
||||
if p.Contains(currency.NewPairFromStrings("BTC", "LTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if p.Contains(currency.NewPairFromStrings("BTC", "BTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if p.Contains(currency.NewPairFromStrings("LTC", "LTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if !p.Contains(currency.NewPairFromStrings("BTC", "ETH"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
backup := currency.CryptoCurrencies
|
||||
currency.CryptoCurrencies = append(currency.CryptoCurrencies, "BTC")
|
||||
|
||||
p = GetRelatableCryptocurrencies(pair.NewCurrencyPair("BTC", "LTC"))
|
||||
if !pair.Contains(p, pair.NewCurrencyPair("BTC", "ETH"), true) {
|
||||
p = GetRelatableCryptocurrencies(currency.NewPairFromStrings("BTC", "LTC"))
|
||||
if p.Contains(currency.NewPairFromStrings("BTC", "LTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if p.Contains(currency.NewPairFromStrings("BTC", "BTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if p.Contains(currency.NewPairFromStrings("LTC", "LTC"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
if !p.Contains(currency.NewPairFromStrings("BTC", "ETH"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
currency.CryptoCurrencies = backup
|
||||
}
|
||||
|
||||
func TestGetRelatableFiatCurrencies(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
p := GetRelatableFiatCurrencies(pair.NewCurrencyPair("BTC", "USD"))
|
||||
if !pair.Contains(p, pair.NewCurrencyPair("BTC", "EUR"), true) {
|
||||
p := GetRelatableFiatCurrencies(currency.NewPairFromStrings("BTC", "USD"))
|
||||
if !p.Contains(currency.NewPairFromStrings("BTC", "EUR"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
backup := currency.FiatCurrencies
|
||||
currency.FiatCurrencies = append(currency.FiatCurrencies, "USD")
|
||||
|
||||
p = GetRelatableFiatCurrencies(pair.NewCurrencyPair("BTC", "USD"))
|
||||
if !pair.Contains(p, pair.NewCurrencyPair("BTC", "ZAR"), true) {
|
||||
p = GetRelatableFiatCurrencies(currency.NewPairFromStrings("BTC", "USD"))
|
||||
if !p.Contains(currency.NewPairFromStrings("BTC", "ZAR"), true) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
currency.FiatCurrencies = backup
|
||||
}
|
||||
|
||||
func TestMapCurrenciesByExchange(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
var pairs = []pair.CurrencyPair{
|
||||
pair.NewCurrencyPair("BTC", "USD"),
|
||||
pair.NewCurrencyPair("BTC", "EUR"),
|
||||
var pairs = []currency.Pair{
|
||||
currency.NewPair(currency.BTC, currency.USD),
|
||||
currency.NewPair(currency.BTC, currency.EUR),
|
||||
}
|
||||
|
||||
result := MapCurrenciesByExchange(pairs, true)
|
||||
@@ -211,17 +237,17 @@ func TestMapCurrenciesByExchange(t *testing.T) {
|
||||
func TestGetExchangeNamesByCurrency(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
result := GetExchangeNamesByCurrency(pair.NewCurrencyPair("BTC", "USD"), true)
|
||||
result := GetExchangeNamesByCurrency(currency.NewPairFromStrings("BTC", "USD"), true)
|
||||
if !common.StringDataCompare(result, "Bitstamp") {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
result = GetExchangeNamesByCurrency(pair.NewCurrencyPair("BTC", "JPY"), true)
|
||||
result = GetExchangeNamesByCurrency(currency.NewPairFromStrings("BTC", "JPY"), true)
|
||||
if !common.StringDataCompare(result, "Bitflyer") {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
result = GetExchangeNamesByCurrency(pair.NewCurrencyPair("blah", "JPY"), true)
|
||||
result = GetExchangeNamesByCurrency(currency.NewPairFromStrings("blah", "JPY"), true)
|
||||
if len(result) > 0 {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
@@ -231,11 +257,22 @@ func TestGetSpecificOrderbook(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
LoadExchange("Bitstamp", false, nil)
|
||||
p := pair.NewCurrencyPair("BTC", "USD")
|
||||
|
||||
bids := []orderbook.Item{}
|
||||
bids = append(bids, orderbook.Item{Price: 1000, Amount: 1})
|
||||
|
||||
orderbook.ProcessOrderbook("Bitstamp", p, orderbook.Base{Pair: p, Bids: bids}, ticker.Spot)
|
||||
base := orderbook.Base{
|
||||
Pair: currency.NewPair(currency.BTC, currency.USD),
|
||||
Bids: bids,
|
||||
ExchangeName: "Bitstamp",
|
||||
AssetType: orderbook.Spot,
|
||||
}
|
||||
|
||||
err := base.Process()
|
||||
if err != nil {
|
||||
t.Fatal("Unexpected result", err)
|
||||
}
|
||||
|
||||
ob, err := GetSpecificOrderbook("BTCUSD", "Bitstamp", ticker.Spot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -257,8 +294,14 @@ func TestGetSpecificTicker(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
LoadExchange("Bitstamp", false, nil)
|
||||
p := pair.NewCurrencyPair("BTC", "USD")
|
||||
ticker.ProcessTicker("Bitstamp", p, ticker.Price{Last: 1000}, ticker.Spot)
|
||||
p := currency.NewPairFromStrings("BTC", "USD")
|
||||
|
||||
err := ticker.ProcessTicker("Bitstamp",
|
||||
ticker.Price{Pair: p, Last: 1000},
|
||||
ticker.Spot)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. ProcessTicker error", err)
|
||||
}
|
||||
|
||||
tick, err := GetSpecificTicker("BTCUSD", "Bitstamp", ticker.Spot)
|
||||
if err != nil {
|
||||
@@ -288,7 +331,7 @@ func TestGetCollatedExchangeAccountInfoByCoin(t *testing.T) {
|
||||
exchange.Account{
|
||||
Currencies: []exchange.AccountCurrencyInfo{
|
||||
{
|
||||
CurrencyName: "BTC",
|
||||
CurrencyName: currency.BTC,
|
||||
TotalValue: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
@@ -302,7 +345,7 @@ func TestGetCollatedExchangeAccountInfoByCoin(t *testing.T) {
|
||||
exchange.Account{
|
||||
Currencies: []exchange.AccountCurrencyInfo{
|
||||
{
|
||||
CurrencyName: "LTC",
|
||||
CurrencyName: currency.LTC,
|
||||
TotalValue: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
@@ -316,7 +359,7 @@ func TestGetCollatedExchangeAccountInfoByCoin(t *testing.T) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
amount, ok := result["BTC"]
|
||||
amount, ok := result[currency.BTC]
|
||||
if !ok {
|
||||
t.Fatal("Expected currency was not found in result map")
|
||||
}
|
||||
@@ -325,7 +368,7 @@ func TestGetCollatedExchangeAccountInfoByCoin(t *testing.T) {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
|
||||
_, ok = result["ETH"]
|
||||
_, ok = result[currency.ETH]
|
||||
if ok {
|
||||
t.Fatal("Unexpected result")
|
||||
}
|
||||
@@ -341,7 +384,7 @@ func TestGetAccountCurrencyInfoByExchangeName(t *testing.T) {
|
||||
exchange.Account{
|
||||
Currencies: []exchange.AccountCurrencyInfo{
|
||||
{
|
||||
CurrencyName: "BTC",
|
||||
CurrencyName: currency.BTC,
|
||||
TotalValue: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
@@ -368,7 +411,7 @@ func TestGetAccountCurrencyInfoByExchangeName(t *testing.T) {
|
||||
func TestGetExchangeHighestPriceByCurrencyPair(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
p := pair.NewCurrencyPair("BTC", "USD")
|
||||
p := currency.NewPairFromStrings("BTC", "USD")
|
||||
stats.Add("Bitfinex", p, ticker.Spot, 1000, 10000)
|
||||
stats.Add("Bitstamp", p, ticker.Spot, 1337, 10000)
|
||||
exchangeName, err := GetExchangeHighestPriceByCurrencyPair(p, ticker.Spot)
|
||||
@@ -380,7 +423,7 @@ func TestGetExchangeHighestPriceByCurrencyPair(t *testing.T) {
|
||||
t.Error("Unexpected result")
|
||||
}
|
||||
|
||||
_, err = GetExchangeHighestPriceByCurrencyPair(pair.NewCurrencyPair("BTC", "AUD"), ticker.Spot)
|
||||
_, err = GetExchangeHighestPriceByCurrencyPair(currency.NewPairFromStrings("BTC", "AUD"), ticker.Spot)
|
||||
if err == nil {
|
||||
t.Error("Unexpected result")
|
||||
}
|
||||
@@ -389,7 +432,7 @@ func TestGetExchangeHighestPriceByCurrencyPair(t *testing.T) {
|
||||
func TestGetExchangeLowestPriceByCurrencyPair(t *testing.T) {
|
||||
SetupTestHelpers(t)
|
||||
|
||||
p := pair.NewCurrencyPair("BTC", "USD")
|
||||
p := currency.NewPairFromStrings("BTC", "USD")
|
||||
stats.Add("Bitfinex", p, ticker.Spot, 1000, 10000)
|
||||
stats.Add("Bitstamp", p, ticker.Spot, 1337, 10000)
|
||||
exchangeName, err := GetExchangeLowestPriceByCurrencyPair(p, ticker.Spot)
|
||||
@@ -401,7 +444,7 @@ func TestGetExchangeLowestPriceByCurrencyPair(t *testing.T) {
|
||||
t.Error("Unexpected result")
|
||||
}
|
||||
|
||||
_, err = GetExchangeLowestPriceByCurrencyPair(pair.NewCurrencyPair("BTC", "AUD"), ticker.Spot)
|
||||
_, err = GetExchangeLowestPriceByCurrencyPair(currency.NewPairFromStrings("BTC", "AUD"), ticker.Spot)
|
||||
if err == nil {
|
||||
t.Error("Unexpected reuslt")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user