Skip to content

Commit

Permalink
Minor general cleanup and bug fixes (thrasher-corp#443)
Browse files Browse the repository at this point in the history
* Bugfix, remove non-needed code and cleanup some code

* Run go mod tidy

* Remove non-needed test and fix tautological err

* Fix Huobi interim var reference
  • Loading branch information
thrasher- authored Feb 12, 2020
1 parent 1f3bb58 commit 168c966
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 256 deletions.
150 changes: 0 additions & 150 deletions cmd/huobi_auth/main.go

This file was deleted.

5 changes: 3 additions & 2 deletions cmd/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func getOnlineOfflinePortfolio(coins []portfolio.Coin, online bool) {

func main() {
var inFile, key string
flag.StringVar(&inFile, "infile", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&inFile, "config", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()

Expand Down Expand Up @@ -132,7 +132,8 @@ func main() {
bf := bitfinex.Bitfinex{}
bf.SetDefaults()
bf.Verbose = false
ticker, errf := bf.GetTicker(y.Coin.String() + currency.USD.String())
pair := "t" + y.Coin.String() + currency.USD.String()
ticker, errf := bf.GetTicker(pair)
if errf != nil {
log.Println(errf)
} else {
Expand Down
15 changes: 7 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/thrasher-corp/gocryptotrader/connchecker"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
Expand Down Expand Up @@ -703,20 +702,20 @@ func (c *Config) GetExchangeConfig(name string) (*ExchangeConfig, error) {
return nil, fmt.Errorf(ErrExchangeNotFound, name)
}

// GetForexProviderConfig returns a forex provider configuration by its name
func (c *Config) GetForexProviderConfig(name string) (base.Settings, error) {
// GetForexProvider returns a forex provider configuration by its name
func (c *Config) GetForexProvider(name string) (currency.FXSettings, error) {
m.Lock()
defer m.Unlock()
for i := range c.Currency.ForexProviders {
if strings.EqualFold(c.Currency.ForexProviders[i].Name, name) {
return c.Currency.ForexProviders[i], nil
}
}
return base.Settings{}, errors.New("provider not found")
return currency.FXSettings{}, errors.New("provider not found")
}

// GetForexProvidersConfig returns a list of available forex providers
func (c *Config) GetForexProvidersConfig() []base.Settings {
// GetForexProviders returns a list of available forex providers
func (c *Config) GetForexProviders() []currency.FXSettings {
m.Lock()
defer m.Unlock()
return c.Currency.ForexProviders
Expand Down Expand Up @@ -978,10 +977,10 @@ func (c *Config) CheckCurrencyConfigValues() error {

if len(fxProviders) != len(c.Currency.ForexProviders) {
for x := range fxProviders {
_, err := c.GetForexProviderConfig(fxProviders[x])
_, err := c.GetForexProvider(fxProviders[x])
if err != nil {
log.Warnf(log.Global, "%s forex provider not found, adding to config..\n", fxProviders[x])
c.Currency.ForexProviders = append(c.Currency.ForexProviders, base.Settings{
c.Currency.ForexProviders = append(c.Currency.ForexProviders, currency.FXSettings{
Name: fxProviders[x],
RESTPollingDelay: 600,
APIKey: DefaultUnsetAPIKey,
Expand Down
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,25 +1129,25 @@ func TestGetForexProviderConfig(t *testing.T) {
if err != nil {
t.Error("GetForexProviderConfig. LoadConfig error", err)
}
_, err = cfg.GetForexProviderConfig("Fixer")
_, err = cfg.GetForexProvider("Fixer")
if err != nil {
t.Error("GetForexProviderConfig error", err)
}

_, err = cfg.GetForexProviderConfig("this is not a forex provider")
_, err = cfg.GetForexProvider("this is not a forex provider")
if err == nil {
t.Error("GetForexProviderConfig no error for invalid provider")
}
}

func TestGetForexProvidersConfig(t *testing.T) {
func TestGetForexProviders(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(TestFile, true)
if err != nil {
t.Error(err)
}

if r := cfg.GetForexProvidersConfig(); len(r) != 5 {
if r := cfg.GetForexProviders(); len(r) != 5 {
t.Error("unexpected length of forex providers")
}
}
Expand Down
3 changes: 1 addition & 2 deletions config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
Expand Down Expand Up @@ -291,7 +290,7 @@ type BankTransaction struct {

// CurrencyConfig holds all the information needed for currency related manipulation
type CurrencyConfig struct {
ForexProviders []base.Settings `json:"forexProviders"`
ForexProviders []currency.FXSettings `json:"forexProviders"`
CryptocurrencyProvider CryptocurrencyProvider `json:"cryptocurrencyProvider"`
Cryptocurrencies currency.Currencies `json:"cryptocurrencies"`
CurrencyPairFormat *CurrencyPairFormatConfig `json:"currencyPairFormat"`
Expand Down
3 changes: 1 addition & 2 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,7 @@
},
"credentials": {
"key": "Key",
"secret": "Secret",
"pemKey": "-----BEGIN EC PRIVATE KEY-----\nJUSTADUMMY\n-----END EC PRIVATE KEY-----\n"
"secret": "Secret"
},
"credentialsValidator": {
"requiresKey": true,
Expand Down
7 changes: 1 addition & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,6 @@ func (e *Engine) Start() error {
}
}

var newFxSettings []currency.FXSettings
for _, d := range e.Config.Currency.ForexProviders {
newFxSettings = append(newFxSettings, currency.FXSettings(d))
}

err := currency.RunStorageUpdater(currency.BotOverrides{
Coinmarketcap: e.Settings.EnableCoinmarketcapAnalysis,
FxCurrencyConverter: e.Settings.EnableCurrencyConverter,
Expand All @@ -376,7 +371,7 @@ func (e *Engine) Start() error {
FxOpenExchangeRates: e.Settings.EnableOpenExchangeRates,
},
&currency.MainConfiguration{
ForexProviders: newFxSettings,
ForexProviders: e.Config.GetForexProviders(),
CryptocurrencyProvider: coinmarketcap.Settings(e.Config.Currency.CryptocurrencyProvider),
Cryptocurrencies: e.Config.Currency.Cryptocurrencies,
FiatDisplayCurrency: e.Config.Currency.FiatDisplayCurrency,
Expand Down
2 changes: 1 addition & 1 deletion engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func (s *RPCServer) RemovePortfolioAddress(ctx context.Context, r *gctrpc.Remove

// GetForexProviders returns a list of available forex providers
func (s *RPCServer) GetForexProviders(ctx context.Context, r *gctrpc.GetForexProvidersRequest) (*gctrpc.GetForexProvidersResponse, error) {
providers := Bot.Config.GetForexProvidersConfig()
providers := Bot.Config.GetForexProviders()
if len(providers) == 0 {
return nil, fmt.Errorf("forex providers is empty")
}
Expand Down
2 changes: 1 addition & 1 deletion exchanges/coinbene/coinbene.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ func (c *Coinbene) SendAuthHTTPRequest(method, path, epPath string, isSwap bool,
}
finalBody = bytes.NewBufferString(string(tempBody))
preSign = timestamp + method + authPath + epPath + string(tempBody)
case params == nil:
default:
preSign = timestamp + method + authPath + epPath
}
tempSign := crypto.GetHMAC(crypto.HashSHA256,
Expand Down
42 changes: 3 additions & 39 deletions exchanges/huobi/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package huobi

import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
Expand Down Expand Up @@ -764,44 +758,14 @@ func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url
}

hmac := crypto.GetHMAC(crypto.HashSHA256, []byte(payload), []byte(h.API.Credentials.Secret))
signature := crypto.Base64Encode(hmac)
values.Set("Signature", signature)

if h.API.Credentials.PEMKey != "" && h.API.PEMKeySupport {
pemKey := strings.NewReader(h.API.Credentials.PEMKey)
pemBytes, err := ioutil.ReadAll(pemKey)
if err != nil {
return fmt.Errorf("%s unable to ioutil.ReadAll PEM key: %s", h.Name, err)
}

block, _ := pem.Decode(pemBytes)
if block == nil {
return fmt.Errorf("%s PEM block is nil", h.Name)
}

x509Encoded := block.Bytes
privKey, err := x509.ParseECPrivateKey(x509Encoded)
if err != nil {
return fmt.Errorf("%s unable to ParseECPrivKey: %s", h.Name, err)
}

r, s, err := ecdsa.Sign(rand.Reader, privKey, crypto.GetSHA256([]byte(signature)))
if err != nil {
return fmt.Errorf("%s unable to sign: %s", h.Name, err)
}

privSig := r.Bytes()
privSig = append(privSig, s.Bytes()...)
values.Set("PrivateSignature", crypto.Base64Encode(privSig))
}

values.Set("Signature", crypto.Base64Encode(hmac))
urlPath := h.API.Endpoints.URL + common.EncodeURLValues(endpoint, values)

var body []byte
if data != nil {
encoded, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("%s unable to marshal data: %s", h.Name, err)
return err
}
body = encoded
}
Expand All @@ -812,7 +776,7 @@ func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url
Path: urlPath,
Headers: headers,
Body: bytes.NewReader(body),
Result: result,
Result: &interim,
AuthRequest: true,
Verbose: h.Verbose,
HTTPDebugging: h.HTTPDebugging,
Expand Down
Loading

0 comments on commit 168c966

Please sign in to comment.