mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
config consistency check to ensure an enabled pair is supported by the exchange
This commit is contained in:
@@ -462,6 +462,54 @@ func (c *Config) CheckCommunicationsConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckPairConsistency checks to see if the enabled pair exists in the
|
||||
// available pairs list
|
||||
func (c *Config) CheckPairConsistency(exchName string) error {
|
||||
enabledPairs, err := c.GetEnabledPairs(exchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
availPairs, err := c.GetAvailablePairs(exchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var pairs, pairsRemoved []pair.CurrencyPair
|
||||
update := false
|
||||
for x := range enabledPairs {
|
||||
if !pair.Contains(availPairs, enabledPairs[x], true) {
|
||||
update = true
|
||||
pairsRemoved = append(pairsRemoved, enabledPairs[x])
|
||||
continue
|
||||
}
|
||||
pairs = append(pairs, enabledPairs[x])
|
||||
}
|
||||
|
||||
if !update {
|
||||
return nil
|
||||
}
|
||||
|
||||
exchCfg, err := c.GetExchangeConfig(exchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(pairs) == 0 {
|
||||
exchCfg.EnabledPairs = availPairs[0].Pair().String()
|
||||
} else {
|
||||
exchCfg.EnabledPairs = common.JoinStrings(pair.PairsToStringArray(pairs), ",")
|
||||
}
|
||||
|
||||
err = c.UpdateExchangeConfig(exchCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Exchange %s: Removing enabled pair(s) %v from enabled pairs as it isn't an available pair", exchName, pair.PairsToStringArray(pairsRemoved))
|
||||
return nil
|
||||
}
|
||||
|
||||
// SupportsPair returns true or not whether the exchange supports the supplied
|
||||
// pair
|
||||
func (c *Config) SupportsPair(exchName string, p pair.CurrencyPair) (bool, error) {
|
||||
@@ -672,6 +720,11 @@ func (c *Config) CheckExchangeConfigValues() error {
|
||||
c.Exchanges[i].HTTPTimeout = configDefaultHTTPTimeout
|
||||
}
|
||||
|
||||
err := c.CheckPairConsistency(exch.Name)
|
||||
if err != nil {
|
||||
log.Printf("Exchange %s: CheckPairConsistency error: %s", exch.Name, err)
|
||||
}
|
||||
|
||||
if len(exch.BankAccounts) == 0 {
|
||||
c.Exchanges[i].BankAccounts = append(c.Exchanges[i].BankAccounts, BankAccount{})
|
||||
} else {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -207,3 +207,12 @@ func FindPairDifferences(oldPairs, newPairs []string) ([]string, []string) {
|
||||
}
|
||||
return newPs, removedPs
|
||||
}
|
||||
|
||||
// PairsToStringArray returns a list of pairs as a string array
|
||||
func PairsToStringArray(pairs []CurrencyPair) []string {
|
||||
var p []string
|
||||
for x := range pairs {
|
||||
p = append(p, pairs[x].Pair().String())
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -358,3 +358,15 @@ func TestFindPairDifferences(t *testing.T) {
|
||||
t.Error("Test failed. TestFindPairDifferences: Unexpected values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPairsToStringArray(t *testing.T) {
|
||||
var pairs []CurrencyPair
|
||||
pairs = append(pairs, NewCurrencyPair("BTC", "USD"))
|
||||
|
||||
expected := []string{"BTCUSD"}
|
||||
actual := PairsToStringArray(pairs)
|
||||
|
||||
if actual[0] != expected[0] {
|
||||
t.Error("Test failed. TestPairsToStringArray: Unexpected values")
|
||||
}
|
||||
}
|
||||
|
||||
44
testdata/configtest.json
vendored
44
testdata/configtest.json
vendored
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user