Refactor Exchange code

This commit is contained in:
Adrian Gallagher
2017-03-29 13:33:01 +11:00
parent a459d37f81
commit 4bd2c92ec3
8 changed files with 53 additions and 82 deletions

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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{}

View File

@@ -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()