Add various helper functions for exchanges and currency pairs.

Improve Kraken config/request currency handling.
Update config file to reflect changes.
This commit is contained in:
Adrian Gallagher
2018-01-15 16:53:12 +11:00
parent 16629c6c6e
commit 34eeed287a
8 changed files with 142 additions and 9 deletions

View File

@@ -69,6 +69,7 @@ type IBotExchange interface {
GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
UpdateOrderbook(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
GetEnabledCurrencies() []pair.CurrencyPair
GetAvailableCurrencies() []pair.CurrencyPair
GetExchangeAccountInfo() (AccountInfo, error)
GetAuthenticatedAPISupport() bool
}
@@ -109,6 +110,18 @@ func GetExchangeAssetTypes(exchName string) ([]string, error) {
return common.SplitStrings(exch.AssetTypes, ","), nil
}
// CompareCurrencyPairFormats checks and returns whether or not the two supplied
// config currency pairs match
func CompareCurrencyPairFormats(pair1 config.CurrencyPairFormatConfig, pair2 *config.CurrencyPairFormatConfig) bool {
if pair1.Delimiter != pair2.Delimiter ||
pair1.Uppercase != pair2.Uppercase ||
pair1.Separator != pair2.Separator ||
pair1.Index != pair2.Index {
return false
}
return true
}
// SetCurrencyPairFormat checks the exchange request and config currency pair
// formats and sets it to a default setting if it doesn't exist
func (e *Base) SetCurrencyPairFormat() error {
@@ -128,7 +141,13 @@ func (e *Base) SetCurrencyPairFormat() error {
}
update = true
} else {
e.RequestCurrencyPairFormat = *exch.RequestCurrencyPairFormat
if CompareCurrencyPairFormats(e.RequestCurrencyPairFormat,
exch.RequestCurrencyPairFormat) {
e.RequestCurrencyPairFormat = *exch.RequestCurrencyPairFormat
} else {
*exch.RequestCurrencyPairFormat = e.ConfigCurrencyPairFormat
update = true
}
}
if exch.ConfigCurrencyPairFormat == nil {
@@ -140,7 +159,13 @@ func (e *Base) SetCurrencyPairFormat() error {
}
update = true
} else {
e.ConfigCurrencyPairFormat = *exch.ConfigCurrencyPairFormat
if CompareCurrencyPairFormats(e.ConfigCurrencyPairFormat,
exch.ConfigCurrencyPairFormat) {
e.ConfigCurrencyPairFormat = *exch.ConfigCurrencyPairFormat
} else {
*exch.ConfigCurrencyPairFormat = e.ConfigCurrencyPairFormat
update = true
}
}
if update {

View File

@@ -83,6 +83,25 @@ func TestGetExchangeAssetTypes(t *testing.T) {
}
}
func TestCompareCurrencyPairFormats(t *testing.T) {
cfgOne := config.CurrencyPairFormatConfig{
Delimiter: "-",
Uppercase: true,
Index: "",
Separator: ",",
}
cfgTwo := cfgOne
if !CompareCurrencyPairFormats(cfgOne, &cfgTwo) {
t.Fatal("Test failed. CompareCurrencyPairFormats should be true")
}
cfgTwo.Delimiter = "~"
if CompareCurrencyPairFormats(cfgOne, &cfgTwo) {
t.Fatal("Test failed. CompareCurrencyPairFormats should not be true")
}
}
func TestSetCurrencyPairFormat(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
@@ -142,6 +161,12 @@ func TestSetCurrencyPairFormat(t *testing.T) {
b.RequestCurrencyPairFormat.Uppercase {
t.Fatal("Test failed. TestSetCurrencyPairFormat RequestCurrencyPairFormat values are incorrect")
}
// if currency pairs are the same as the config, should load from config
err = b.SetCurrencyPairFormat()
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat. Error %s", err)
}
}
func TestGetAuthenticatedAPISupport(t *testing.T) {

View File

@@ -60,7 +60,7 @@ func (k *Kraken) SetDefaults() {
k.RequestCurrencyPairFormat.Delimiter = ""
k.RequestCurrencyPairFormat.Uppercase = true
k.RequestCurrencyPairFormat.Separator = ","
k.ConfigCurrencyPairFormat.Delimiter = ""
k.ConfigCurrencyPairFormat.Delimiter = "-"
k.ConfigCurrencyPairFormat.Uppercase = true
k.AssetTypes = []string{ticker.Spot}
}

View File

@@ -29,11 +29,35 @@ func (k *Kraken) Run() {
if err != nil {
log.Printf("%s Failed to get available symbols.\n", k.GetName())
} else {
forceUpgrade := false
if !common.DataContains(k.EnabledPairs, "-") || !common.DataContains(k.AvailablePairs, "-") {
forceUpgrade = true
}
var exchangeProducts []string
for _, v := range assetPairs {
exchangeProducts = append(exchangeProducts, v.Altname)
if common.StringContains(v.Altname, ".d") {
continue
}
if v.Base[0] == 'X' {
v.Base = v.Base[1:]
}
if v.Quote[0] == 'Z' || v.Quote[0] == 'X' {
v.Quote = v.Quote[1:]
}
exchangeProducts = append(exchangeProducts, v.Base+"-"+v.Quote)
}
err = k.UpdateAvailableCurrencies(exchangeProducts, false)
if forceUpgrade {
enabledPairs := []string{"XBT-USD"}
log.Println("WARNING: Available pairs for Kraken reset due to config upgrade, please enable the ones you would like again")
err = k.UpdateEnabledCurrencies(enabledPairs, true)
if err != nil {
log.Printf("%s Failed to get config.\n", k.GetName())
}
}
err = k.UpdateAvailableCurrencies(exchangeProducts, forceUpgrade)
if err != nil {
log.Printf("%s Failed to get config.\n", k.GetName())
}