From 4bd2c92ec38e9facc6be054b2302bc898d082212 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Wed, 29 Mar 2017 13:33:01 +1100 Subject: [PATCH] Refactor Exchange code --- config/config.go | 5 +++++ exchanges/bitfinex/bitfinex_wrapper.go | 19 ++++------------- exchanges/exchange.go | 17 ++++++++++++++++ exchanges/gdax/gdax_wrapper.go | 28 +++++++++----------------- exchanges/gemini/gemini_wrapper.go | 19 ++++------------- exchanges/kraken/kraken_wrapper.go | 26 ++++++++---------------- exchanges/liqui/liqui_wrapper.go | 18 ++++------------- main.go | 3 ++- 8 files changed, 53 insertions(+), 82 deletions(-) diff --git a/config/config.go b/config/config.go index aad45229..998a1b31 100644 --- a/config/config.go +++ b/config/config.go @@ -42,6 +42,7 @@ var ( WarningWebserverRootWebFolderNotFound = "WARNING -- Webserver support disabled due to missing web folder." WarningExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values." RenamingConfigFile = "Renaming config file %s to %s." + Cfg Config ) type WebserverConfig struct { @@ -347,3 +348,7 @@ func (c *Config) LoadConfig() error { return nil } + +func GetConfig() *Config { + return &Cfg +} diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index 0661a282..eb32d3ad 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -29,21 +29,10 @@ func (b *Bitfinex) Run() { if err != nil { log.Printf("%s Failed to get available symbols.\n", b.GetName()) } else { - log.Println(exchangeProducts) - /* - exchangeProducts = common.SplitStrings(common.StringToUpper(common.JoinStrings(exchangeProducts, ",")), ",") - diff := common.StringSliceDifference(b.AvailablePairs, exchangeProducts) - if len(diff) > 0 { - exch, err := bot.config.GetExchangeConfig(b.Name) - if err != nil { - log.Println(err) - } else { - log.Printf("%s Updating available pairs. Difference: %s.\n", b.Name, diff) - exch.AvailablePairs = common.JoinStrings(exchangeProducts, ",") - bot.config.UpdateExchangeConfig(exch) - } - } - */ + err = b.UpdateAvailableCurrencies(exchangeProducts) + if err != nil { + log.Printf("%s Failed to get config.\n", b.GetName()) + } } for b.Enabled { diff --git a/exchanges/exchange.go b/exchanges/exchange.go index ba3e1a4a..8b09d2a0 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -83,3 +83,20 @@ func (e *ExchangeBase) SetAPIKeys(APIKey, APISecret, ClientID string, b64Decode e.APISecret = APISecret } } + +func (e *ExchangeBase) UpdateAvailableCurrencies(exchangeProducts []string) error { + exchangeProducts = common.SplitStrings(common.StringToUpper(common.JoinStrings(exchangeProducts, ",")), ",") + diff := common.StringSliceDifference(e.AvailablePairs, exchangeProducts) + if len(diff) > 0 { + cfg := config.GetConfig() + exch, err := cfg.GetExchangeConfig(e.Name) + if err != nil { + return err + } else { + log.Printf("%s Updating available pairs. Difference: %s.\n", e.Name, diff) + exch.AvailablePairs = common.JoinStrings(exchangeProducts, ",") + cfg.UpdateExchangeConfig(exch) + } + } + return nil +} diff --git a/exchanges/gdax/gdax_wrapper.go b/exchanges/gdax/gdax_wrapper.go index a8798144..a99d8290 100644 --- a/exchanges/gdax/gdax_wrapper.go +++ b/exchanges/gdax/gdax_wrapper.go @@ -29,26 +29,16 @@ func (g *GDAX) Run() { if err != nil { log.Printf("%s Failed to get available products.\n", g.GetName()) } else { - log.Println(exchangeProducts) - /* - currencies := []string{} - for _, x := range exchangeProducts { - if x.ID != "BTC" && x.ID != "USD" && x.ID != "GBP" { - currencies = append(currencies, x.ID[0:3]+x.ID[4:]) - } + currencies := []string{} + for _, x := range exchangeProducts { + if x.ID != "BTC" && x.ID != "USD" && x.ID != "GBP" { + currencies = append(currencies, x.ID[0:3]+x.ID[4:]) } - diff := common.StringSliceDifference(g.AvailablePairs, currencies) - if len(diff) > 0 { - exch, err := bot.config.GetExchangeConfig(g.Name) - if err != nil { - log.Println(err) - } else { - log.Printf("%s Updating available pairs. Difference: %s.\n", g.Name, diff) - exch.AvailablePairs = common.JoinStrings(currencies, ",") - bot.config.UpdateExchangeConfig(exch) - } - } - */ + } + err = g.UpdateAvailableCurrencies(currencies) + if err != nil { + log.Printf("%s Failed to get config.\n", g.GetName()) + } } for g.Enabled { diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index fab8d1e0..ab05ef24 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -23,21 +23,10 @@ func (g *Gemini) Run() { if err != nil { log.Printf("%s Failed to get available symbols.\n", g.GetName()) } else { - log.Println(exchangeProducts) - /* - exchangeProducts = common.SplitStrings(common.StringToUpper(common.JoinStrings(exchangeProducts, ",")), ",") - diff := common.StringSliceDifference(g.AvailablePairs, exchangeProducts) - if len(diff) > 0 { - exch, err := bot.config.GetExchangeConfig(g.Name) - if err != nil { - log.Println(err) - } else { - log.Printf("%s Updating available pairs. Difference: %s.\n", g.Name, diff) - exch.AvailablePairs = common.JoinStrings(exchangeProducts, ",") - bot.config.UpdateExchangeConfig(exch) - } - } - */ + err = g.UpdateAvailableCurrencies(exchangeProducts) + if err != nil { + log.Printf("%s Failed to get config.\n", g.GetName()) + } } for g.Enabled { diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index 8e20a440..43c31214 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -24,24 +24,14 @@ func (k *Kraken) Run() { if err != nil { log.Printf("%s Failed to get available symbols.\n", k.GetName()) } else { - log.Println(assetPairs) - /* - var exchangeProducts []string - for _, v := range assetPairs { - exchangeProducts = append(exchangeProducts, v.Altname) - } - diff := common.StringSliceDifference(k.AvailablePairs, exchangeProducts) - if len(diff) > 0 { - exch, err := bot.config.GetExchangeConfig(k.Name) - if err != nil { - log.Println(err) - } else { - log.Printf("%s Updating available pairs. Difference: %s.\n", k.Name, diff) - exch.AvailablePairs = common.JoinStrings(exchangeProducts, ",") - bot.config.UpdateExchangeConfig(exch) - } - } - */ + var exchangeProducts []string + for _, v := range assetPairs { + exchangeProducts = append(exchangeProducts, v.Altname) + } + err = k.UpdateAvailableCurrencies(exchangeProducts) + if err != nil { + log.Printf("%s Failed to get config.\n", k.GetName()) + } } for k.Enabled { diff --git a/exchanges/liqui/liqui_wrapper.go b/exchanges/liqui/liqui_wrapper.go index 531298d4..2e6b5975 100644 --- a/exchanges/liqui/liqui_wrapper.go +++ b/exchanges/liqui/liqui_wrapper.go @@ -27,20 +27,10 @@ func (l *Liqui) Run() { log.Printf("%s Unable to fetch info.\n", l.GetName()) } else { exchangeProducts := l.GetAvailablePairs(true) - log.Println(exchangeProducts) - /* - diff := common.StringSliceDifference(l.AvailablePairs, exchangeProducts) - if len(diff) > 0 { - exch, err := bot.config.GetExchangeConfig(l.Name) - if err != nil { - log.Println(err) - } else { - log.Printf("%s Updating available pairs. Difference: %s.\n", l.Name, diff) - exch.AvailablePairs = common.JoinStrings(exchangeProducts, ",") - bot.config.UpdateExchangeConfig(exch) - } - } - */ + err = l.UpdateAvailableCurrencies(exchangeProducts) + if err != nil { + log.Printf("%s Failed to get config.\n", l.GetName()) + } } pairs := []string{} diff --git a/main.go b/main.go index 35100ad0..25336012 100644 --- a/main.go +++ b/main.go @@ -54,7 +54,7 @@ type ExchangeMain struct { } type Bot struct { - config config.Config + config *config.Config exchange ExchangeMain exchanges []exchange.IBotExchange tickers []ticker.Ticker @@ -83,6 +83,7 @@ func setupBotExchanges() { func main() { HandleInterrupt() + bot.config = &config.Cfg log.Printf("Loading config file %s..\n", config.CONFIG_FILE) err := bot.config.LoadConfig()