mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-29 23:16:51 +00:00
Improvements in config, currency, exchange, pair and helper code
This commit is contained in:
166
config/config.go
166
config/config.go
@@ -8,11 +8,11 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/portfolio"
|
||||
"github.com/thrasher-/gocryptotrader/smsglobal"
|
||||
)
|
||||
@@ -117,8 +117,66 @@ type ExchangeConfig struct {
|
||||
RequestCurrencyPairFormat *CurrencyPairFormatConfig `json:"RequestCurrencyPairFormat"`
|
||||
}
|
||||
|
||||
// GetConfigEnabledExchanges returns the number of exchanges that are enabled.
|
||||
func (c *Config) GetConfigEnabledExchanges() int {
|
||||
// SupportsPair returns true or not whether the exchange supports the supplied
|
||||
// pair
|
||||
func (c *Config) SupportsPair(exchName string, p pair.CurrencyPair) (bool, error) {
|
||||
pairs, err := c.GetAvailablePairs(exchName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return pair.Contains(pairs, p), nil
|
||||
}
|
||||
|
||||
// GetAvailablePairs returns a list of currency pairs for a specifc exchange
|
||||
func (c *Config) GetAvailablePairs(exchName string) ([]pair.CurrencyPair, error) {
|
||||
exchCfg, err := c.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pairs := pair.FormatPairs(common.SplitStrings(exchCfg.AvailablePairs, ","),
|
||||
exchCfg.ConfigCurrencyPairFormat.Delimiter,
|
||||
exchCfg.ConfigCurrencyPairFormat.Index)
|
||||
return pairs, nil
|
||||
}
|
||||
|
||||
// GetEnabledPairs returns a list of currency pairs for a specifc exchange
|
||||
func (c *Config) GetEnabledPairs(exchName string) ([]pair.CurrencyPair, error) {
|
||||
exchCfg, err := c.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pairs := pair.FormatPairs(common.SplitStrings(exchCfg.EnabledPairs, ","),
|
||||
exchCfg.ConfigCurrencyPairFormat.Delimiter,
|
||||
exchCfg.ConfigCurrencyPairFormat.Index)
|
||||
return pairs, nil
|
||||
}
|
||||
|
||||
// GetEnabledExchanges returns a list of enabled exchanges
|
||||
func (c *Config) GetEnabledExchanges() []string {
|
||||
var enabledExchs []string
|
||||
for i := range c.Exchanges {
|
||||
if c.Exchanges[i].Enabled {
|
||||
enabledExchs = append(enabledExchs, c.Exchanges[i].Name)
|
||||
}
|
||||
}
|
||||
return enabledExchs
|
||||
}
|
||||
|
||||
// GetDisabledExchanges returns a list of disabled exchanges
|
||||
func (c *Config) GetDisabledExchanges() []string {
|
||||
var disabledExchs []string
|
||||
for i := range c.Exchanges {
|
||||
if !c.Exchanges[i].Enabled {
|
||||
disabledExchs = append(disabledExchs, c.Exchanges[i].Name)
|
||||
}
|
||||
}
|
||||
return disabledExchs
|
||||
}
|
||||
|
||||
// CountEnabledExchanges returns the number of exchanges that are enabled.
|
||||
func (c *Config) CountEnabledExchanges() int {
|
||||
counter := 0
|
||||
for i := range c.Exchanges {
|
||||
if c.Exchanges[i].Enabled {
|
||||
@@ -128,6 +186,26 @@ func (c *Config) GetConfigEnabledExchanges() int {
|
||||
return counter
|
||||
}
|
||||
|
||||
// GetConfigCurrencyPairFormat returns the config currency pair format
|
||||
// for a specific exchange
|
||||
func (c *Config) GetConfigCurrencyPairFormat(exchName string) (*CurrencyPairFormatConfig, error) {
|
||||
exchCfg, err := c.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exchCfg.ConfigCurrencyPairFormat, nil
|
||||
}
|
||||
|
||||
// GetRequestCurrencyPairFormat returns the request currency pair format
|
||||
// for a specific exchange
|
||||
func (c *Config) GetRequestCurrencyPairFormat(exchName string) (*CurrencyPairFormatConfig, error) {
|
||||
exchCfg, err := c.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exchCfg.RequestCurrencyPairFormat, nil
|
||||
}
|
||||
|
||||
// GetCurrencyPairDisplayConfig retrieves the currency pair display preference
|
||||
func (c *Config) GetCurrencyPairDisplayConfig() *CurrencyPairFormatConfig {
|
||||
return c.CurrencyPairFormat
|
||||
@@ -253,59 +331,49 @@ func (c *Config) RetrieveConfigCurrencyPairs() error {
|
||||
cryptoCurrencies := common.SplitStrings(c.Cryptocurrencies, ",")
|
||||
fiatCurrencies := common.SplitStrings(currency.DefaultCurrencies, ",")
|
||||
|
||||
for _, s := range cryptoCurrencies {
|
||||
_, err := strconv.Atoi(s)
|
||||
if err != nil && common.StringContains(c.Cryptocurrencies, s) {
|
||||
for x := range c.Exchanges {
|
||||
if !c.Exchanges[x].Enabled {
|
||||
continue
|
||||
} else {
|
||||
return errors.New("RetrieveConfigCurrencyPairs: Incorrect Crypto-Currency")
|
||||
}
|
||||
}
|
||||
|
||||
for _, exchange := range c.Exchanges {
|
||||
if exchange.Enabled {
|
||||
baseCurrencies := common.SplitStrings(exchange.BaseCurrencies, ",")
|
||||
enabledCurrencies := common.SplitStrings(exchange.EnabledPairs, ",")
|
||||
|
||||
for _, currencyPair := range enabledCurrencies {
|
||||
ok, separator := currency.ContainsSeparator(currencyPair)
|
||||
if ok {
|
||||
pair := common.SplitStrings(currencyPair, separator)
|
||||
for _, x := range pair {
|
||||
ok, _ = currency.ContainsBaseCurrencyIndex(baseCurrencies, x)
|
||||
if !ok {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, x)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ok, idx := currency.ContainsBaseCurrencyIndex(baseCurrencies, currencyPair)
|
||||
if ok {
|
||||
curr := strings.Replace(currencyPair, idx, "", -1)
|
||||
|
||||
if currency.ContainsBaseCurrency(baseCurrencies, curr) {
|
||||
fiatCurrencies = currency.CheckAndAddCurrency(fiatCurrencies, curr)
|
||||
} else {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, curr)
|
||||
}
|
||||
|
||||
if currency.ContainsBaseCurrency(baseCurrencies, idx) {
|
||||
fiatCurrencies = currency.CheckAndAddCurrency(fiatCurrencies, idx)
|
||||
} else {
|
||||
cryptoCurrencies = currency.CheckAndAddCurrency(cryptoCurrencies, idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
baseCurrencies := common.SplitStrings(c.Exchanges[x].BaseCurrencies, ",")
|
||||
for y := range baseCurrencies {
|
||||
if !common.DataContains(fiatCurrencies, baseCurrencies[y]) {
|
||||
fiatCurrencies = append(fiatCurrencies, baseCurrencies[y])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currency.BaseCurrencies = common.JoinStrings(fiatCurrencies, ",")
|
||||
if common.StringContains(currency.BaseCurrencies, "RUR") {
|
||||
currency.BaseCurrencies = strings.Replace(currency.BaseCurrencies, "RUR", "RUB", -1)
|
||||
}
|
||||
c.Cryptocurrencies = common.JoinStrings(cryptoCurrencies, ",")
|
||||
currency.CryptoCurrencies = c.Cryptocurrencies
|
||||
for x := range c.Exchanges {
|
||||
if !c.Exchanges[x].Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
pairs, err := c.GetEnabledPairs(c.Exchanges[x].Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for y := range pairs {
|
||||
if !common.DataContains(fiatCurrencies, pairs[y].FirstCurrency.String()) &&
|
||||
!common.DataContains(cryptoCurrencies, pairs[y].FirstCurrency.String()) {
|
||||
cryptoCurrencies = append(cryptoCurrencies, pairs[y].FirstCurrency.String())
|
||||
}
|
||||
|
||||
if !common.DataContains(fiatCurrencies, pairs[y].SecondCurrency.String()) &&
|
||||
!common.DataContains(cryptoCurrencies, pairs[y].SecondCurrency.String()) {
|
||||
cryptoCurrencies = append(cryptoCurrencies, pairs[y].SecondCurrency.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currency.Update(fiatCurrencies, false)
|
||||
currency.Update(cryptoCurrencies, true)
|
||||
|
||||
for x := range currency.BaseCurrencies {
|
||||
if currency.BaseCurrencies[x] == "RUR" {
|
||||
currency.BaseCurrencies[x] = "RUB"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,146 @@ package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
)
|
||||
|
||||
func TestGetConfigEnabledExchanges(t *testing.T) {
|
||||
defaultEnabledExchanges := 18
|
||||
func TestSupportsPair(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestSupportsPair. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.SupportsPair("asdf", pair.NewCurrencyPair("BTC", "USD"))
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. TestSupportsPair. Non-existant exchange returned nil error",
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.SupportsPair("Bitfinex", pair.NewCurrencyPair("BTC", "USD"))
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestSupportsPair. Incorrect values. Err: %s", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAvailablePairs(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetAvailablePairs. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.GetAvailablePairs("asdf")
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. TestGetAvailablePairs. Non-existant exchange returned nil error",
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.GetAvailablePairs("Bitfinex")
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetAvailablePairs. Incorrect values. Err: %s", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnabledPairs(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetEnabledPairs. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.GetEnabledPairs("asdf")
|
||||
if err == nil {
|
||||
t.Error(
|
||||
"Test failed. TestGetEnabledPairs. Non-existant exchange returned nil error",
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.GetEnabledPairs("Bitfinex")
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetEnabledPairs. Incorrect values. Err: %s", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnabledExchanges(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetEnabledExchanges. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
exchanges := cfg.GetEnabledExchanges()
|
||||
if len(exchanges) != 19 {
|
||||
t.Error(
|
||||
"Test failed. TestGetEnabledExchanges. Enabled exchanges value mismatch",
|
||||
)
|
||||
}
|
||||
|
||||
if !common.DataContains(exchanges, "Bitfinex") {
|
||||
t.Error(
|
||||
"Test failed. TestGetEnabledExchanges. Expected exchange Bitfinex not found",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDisabledExchanges(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetDisabledExchanges. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
exchanges := cfg.GetDisabledExchanges()
|
||||
if len(exchanges) != 0 {
|
||||
t.Error(
|
||||
"Test failed. TestGetDisabledExchanges. Enabled exchanges value mismatch",
|
||||
)
|
||||
}
|
||||
|
||||
exchCfg, err := cfg.GetExchangeConfig("Bitfinex")
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetDisabledExchanges. GetExchangeConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
exchCfg.Enabled = false
|
||||
err = cfg.UpdateExchangeConfig(exchCfg)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetDisabledExchanges. UpdateExchangeConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
if len(cfg.GetDisabledExchanges()) != 1 {
|
||||
t.Error(
|
||||
"Test failed. TestGetDisabledExchanges. Enabled exchanges value mismatch",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountEnabledExchanges(t *testing.T) {
|
||||
defaultEnabledExchanges := 19
|
||||
GetConfigEnabledExchanges := GetConfig()
|
||||
err := GetConfigEnabledExchanges.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
@@ -13,12 +149,59 @@ func TestGetConfigEnabledExchanges(t *testing.T) {
|
||||
"Test failed. GetConfigEnabledExchanges load config error: " + err.Error(),
|
||||
)
|
||||
}
|
||||
enabledExch := GetConfigEnabledExchanges.GetConfigEnabledExchanges()
|
||||
enabledExch := GetConfigEnabledExchanges.CountEnabledExchanges()
|
||||
if enabledExch != defaultEnabledExchanges {
|
||||
t.Error("Test failed. GetConfigEnabledExchanges is wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetConfigCurrencyPairFormat(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetConfigCurrencyPairFormat. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
_, err = cfg.GetConfigCurrencyPairFormat("asdasdasd")
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetRequestCurrencyPairFormat. Non-existant exchange returned nil error",
|
||||
)
|
||||
}
|
||||
|
||||
exchFmt, err := cfg.GetConfigCurrencyPairFormat("Liqui")
|
||||
if !exchFmt.Uppercase || exchFmt.Delimiter != "_" {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetConfigCurrencyPairFormat. Invalid values",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestCurrencyPairFormat(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetRequestCurrencyPairFormat. LoadConfig Error: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
_, err = cfg.GetRequestCurrencyPairFormat("asdasdasd")
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetRequestCurrencyPairFormat. Non-existant exchange returned nil error",
|
||||
)
|
||||
}
|
||||
|
||||
exchFmt, err := cfg.GetRequestCurrencyPairFormat("Liqui")
|
||||
if exchFmt.Uppercase || exchFmt.Delimiter != "_" || exchFmt.Separator != "-" {
|
||||
t.Errorf(
|
||||
"Test failed. TestGetRequestCurrencyPairFormat. Invalid values",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCurrencyPairDisplayConfig(t *testing.T) {
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
@@ -221,6 +404,7 @@ func TestCheckWebserverConfigValues(t *testing.T) {
|
||||
"Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
err = checkWebserverConfigValues.CheckWebserverConfigValues()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
@@ -229,6 +413,21 @@ func TestCheckWebserverConfigValues(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
checkWebserverConfigValues.Webserver.WebsocketConnectionLimit = -1
|
||||
err = checkWebserverConfigValues.CheckWebserverConfigValues()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues: %s",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
if checkWebserverConfigValues.Webserver.WebsocketConnectionLimit != 1 {
|
||||
t.Error(
|
||||
"Test failed. checkWebserverConfigValues.CheckWebserverConfigValues error",
|
||||
)
|
||||
}
|
||||
|
||||
checkWebserverConfigValues.Webserver.ListenAddress = ":0"
|
||||
err = checkWebserverConfigValues.CheckWebserverConfigValues()
|
||||
if err == nil {
|
||||
@@ -263,17 +462,17 @@ func TestCheckWebserverConfigValues(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRetrieveConfigCurrencyPairs(t *testing.T) {
|
||||
retrieveConfigCurrencyPairs := GetConfig()
|
||||
err := retrieveConfigCurrencyPairs.LoadConfig(ConfigTestFile)
|
||||
cfg := GetConfig()
|
||||
err := cfg.LoadConfig(ConfigTestFile)
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.LoadConfig: %s", err.Error(),
|
||||
"Test failed. TestRetrieveConfigCurrencyPairs.LoadConfig: %s", err.Error(),
|
||||
)
|
||||
}
|
||||
err = retrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs()
|
||||
err = cfg.RetrieveConfigCurrencyPairs()
|
||||
if err != nil {
|
||||
t.Errorf(
|
||||
"Test failed. checkWebserverConfigValues.RetrieveConfigCurrencyPairs: %s",
|
||||
"Test failed. TestRetrieveConfigCurrencyPairs.RetrieveConfigCurrencyPairs: %s",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user