Skip to content

Commit

Permalink
Coinmarketcap implementation (thrasher-corp#243)
Browse files Browse the repository at this point in the history
* Updates requester package to allow unpacking of zipped files and defaults to warn if no JSON is present

* Initial addition of coinmarketcap functionality

* fix requested changes

* Fix issue with displaying false positive in request.go && reorder plan list

* Rename CurrencyProvider -> CryptocurrencyProvider
Skip seeding currency data if not enabled
Rm line in main.go

* Update test procedures and relevant json files

* Fix const issue within config.go
  • Loading branch information
shazbert authored and thrasher- committed Jan 31, 2019
1 parent f7810e7 commit 82a6222
Show file tree
Hide file tree
Showing 10 changed files with 1,819 additions and 10 deletions.
78 changes: 69 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ const (
WarningExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
WarningCurrencyExchangeProvider = "WARNING -- Currency exchange provider invalid valid. Reset to Fixer."
WarningPairsLastUpdatedThresholdExceeded = "WARNING -- Exchange %s: Last manual update of available currency pairs has exceeded %d days. Manual update required!"
APIURLNonDefaultMessage = "NON_DEFAULT_HTTP_LINK_TO_EXCHANGE_API"
WebsocketURLNonDefaultMessage = "NON_DEFAULT_HTTP_LINK_TO_WEBSOCKET_EXCHANGE_API"
)

// Constants here define unset default values displayed in the config.json
// file
const (
APIURLNonDefaultMessage = "NON_DEFAULT_HTTP_LINK_TO_EXCHANGE_API"
WebsocketURLNonDefaultMessage = "NON_DEFAULT_HTTP_LINK_TO_WEBSOCKET_EXCHANGE_API"
DefaultUnsetAPIKey = "Key"
DefaultUnsetAPISecret = "Secret"
DefaultUnsetAccountPlan = "accountPlan"
)

// Variables here are used for configuration
Expand Down Expand Up @@ -169,10 +177,20 @@ type BankTransaction struct {

// CurrencyConfig holds all the information needed for currency related manipulation
type CurrencyConfig struct {
ForexProviders []base.Settings `json:"forexProviders"`
Cryptocurrencies string `json:"cryptocurrencies"`
CurrencyPairFormat *CurrencyPairFormatConfig `json:"currencyPairFormat"`
FiatDisplayCurrency string `json:"fiatDisplayCurrency"`
ForexProviders []base.Settings `json:"forexProviders"`
CryptocurrencyProvider CryptocurrencyProvider `json:"cryptocurrencyProvider"`
Cryptocurrencies string `json:"cryptocurrencies"`
CurrencyPairFormat *CurrencyPairFormatConfig `json:"currencyPairFormat"`
FiatDisplayCurrency string `json:"fiatDisplayCurrency"`
}

// CryptocurrencyProvider defines coinmarketcap tools
type CryptocurrencyProvider struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Verbose bool `json:"verbose"`
APIkey string `json:"apiKey"`
AccountPlan string `json:"accountPlan"`
}

// CommunicationsConfig holds all the information needed for each
Expand Down Expand Up @@ -365,6 +383,20 @@ func (c *Config) UpdateCommunicationsConfig(config CommunicationsConfig) {
m.Unlock()
}

// GetCryptocurrencyProviderConfig returns the communications configuration
func (c *Config) GetCryptocurrencyProviderConfig() CryptocurrencyProvider {
m.Lock()
defer m.Unlock()
return c.Currency.CryptocurrencyProvider
}

// UpdateCryptocurrencyProviderConfig returns the communications configuration
func (c *Config) UpdateCryptocurrencyProviderConfig(config CryptocurrencyProvider) {
m.Lock()
c.Currency.CryptocurrencyProvider = config
m.Unlock()
}

// CheckCommunicationsConfig checks to see if the variables are set correctly
// from config.json
func (c *Config) CheckCommunicationsConfig() {
Expand Down Expand Up @@ -732,7 +764,9 @@ func (c *Config) CheckExchangeConfigValues() error {
return fmt.Errorf(ErrExchangeBaseCurrenciesEmpty, exch.Name)
}
if exch.AuthenticatedAPISupport { // non-fatal error
if exch.APIKey == "" || exch.APISecret == "" || exch.APIKey == "Key" || exch.APISecret == "Secret" {
if exch.APIKey == "" || exch.APISecret == "" ||
exch.APIKey == DefaultUnsetAPIKey ||
exch.APISecret == DefaultUnsetAPISecret {
c.Exchanges[i].AuthenticatedAPISupport = false
log.Warn(WarningExchangeAuthAPIDefaultOrEmptyValues, exch.Name)
} else if exch.Name == "ITBIT" || exch.Name == "Bitstamp" || exch.Name == "COINUT" || exch.Name == "CoinbasePro" {
Expand Down Expand Up @@ -844,7 +878,7 @@ func (c *Config) CheckCurrencyConfigValues() error {
Enabled: false,
Verbose: false,
RESTPollingDelay: 600,
APIKey: "Key",
APIKey: DefaultUnsetAPIKey,
APIKeyLvl: -1,
PrimaryProvider: false,
},
Expand All @@ -856,7 +890,7 @@ func (c *Config) CheckCurrencyConfigValues() error {
count := 0
for i := range c.Currency.ForexProviders {
if c.Currency.ForexProviders[i].Enabled {
if c.Currency.ForexProviders[i].APIKey == "Key" {
if c.Currency.ForexProviders[i].APIKey == DefaultUnsetAPIKey {
log.Warnf("%s forex provider API key not set. Please set this in your config.json file", c.Currency.ForexProviders[i].Name)
c.Currency.ForexProviders[i].Enabled = false
c.Currency.ForexProviders[i].PrimaryProvider = false
Expand All @@ -881,6 +915,32 @@ func (c *Config) CheckCurrencyConfigValues() error {
}
}

if c.Currency.CryptocurrencyProvider == (CryptocurrencyProvider{}) {
c.Currency.CryptocurrencyProvider.Name = "CoinMarketCap"
c.Currency.CryptocurrencyProvider.Enabled = false
c.Currency.CryptocurrencyProvider.Verbose = false
c.Currency.CryptocurrencyProvider.AccountPlan = DefaultUnsetAccountPlan
c.Currency.CryptocurrencyProvider.APIkey = DefaultUnsetAPIKey
}

if c.Currency.CryptocurrencyProvider.Enabled {
if c.Currency.CryptocurrencyProvider.APIkey == "" ||
c.Currency.CryptocurrencyProvider.APIkey == DefaultUnsetAPIKey {
log.Warnf("CryptocurrencyProvider enabled but api key is unset please set this in your config.json file")
}
if c.Currency.CryptocurrencyProvider.AccountPlan == "" ||
c.Currency.CryptocurrencyProvider.AccountPlan == DefaultUnsetAccountPlan {
log.Warnf("CryptocurrencyProvider enabled but account plan is unset please set this in your config.json file")
}
} else {
if c.Currency.CryptocurrencyProvider.APIkey == "" {
c.Currency.CryptocurrencyProvider.APIkey = DefaultUnsetAPIKey
}
if c.Currency.CryptocurrencyProvider.AccountPlan == "" {
c.Currency.CryptocurrencyProvider.AccountPlan = DefaultUnsetAccountPlan
}
}

if len(c.Currency.Cryptocurrencies) == 0 {
if len(c.Cryptocurrencies) != 0 {
c.Currency.Cryptocurrencies = c.Cryptocurrencies
Expand Down
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ func TestUpdateCommunicationsConfig(t *testing.T) {
}
}

func TestGetCryptocurrencyProviderConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetCryptocurrencyProviderConfig LoadConfig error", err)
}
_ = cfg.GetCryptocurrencyProviderConfig()
}

func TestUpdateCryptocurrencyProviderConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateCryptocurrencyProviderConfig LoadConfig error", err)
}

orig := cfg.GetCryptocurrencyProviderConfig()
cfg.UpdateCryptocurrencyProviderConfig(CryptocurrencyProvider{Name: "SERIOUS TESTING PROCEDURE!"})
if cfg.Currency.CryptocurrencyProvider.Name != "SERIOUS TESTING PROCEDURE!" {
t.Error("Test failed. UpdateCurrencyProviderConfig LoadConfig error")
}

cfg.UpdateCryptocurrencyProviderConfig(orig)
}

func TestCheckCommunicationsConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
Expand Down
7 changes: 7 additions & 0 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
"primaryProvider": false
}
],
"cryptocurrencyProvider": {
"name": "CoinMarketCap",
"enabled": false,
"verbose": false,
"apiKey": "Key",
"accountPlan": "accountPlan"
},
"cryptocurrencies": "BTC,LTC,ETH,XRP,NMC,NVC,PPC,XBT,DOGE,DASH",
"currencyPairFormat": {
"uppercase": true,
Expand Down
Loading

0 comments on commit 82a6222

Please sign in to comment.