Skip to content

Commit

Permalink
Expose auth validator functionality for wrapper (thrasher-corp#416)
Browse files Browse the repository at this point in the history
* expose auth validator functionality for wrapper

* Add REST validation after keys set, package account types for future syncing

* Add transient error checking for initial creddemtial validation

* fix command types

* Addressed nits from glorious person

* Amalgamate body within error when not between 2xx status, added btcmarket specific auth error check

* nit fix for glorious person

* Format fix

* removed unused code

* check transient first then validate if its an exchange specific authentication error, all others will be disregarded

* Addressed glorious nits

* Addressed glorious nits

* Moved account processing to updateaccountinfo func and added in fetch account info

* Add GRPC Account streaming (NOTE: could not complete until sync item added)

* RM exchange check

* Address xtda nits

* RM comment code

* Fix linter issues

* used most recent protoc version

* lbank linter issues fixed

* Addressed nits and changed len check to range in for loops

* Fixed timeout issue

* thrasher nits addressed

* add string holdings
  • Loading branch information
shazbert authored Jan 31, 2020
1 parent db23af2 commit a32d16e
Show file tree
Hide file tree
Showing 75 changed files with 2,009 additions and 856 deletions.
2 changes: 1 addition & 1 deletion cmd/exchange_wrapper_coverage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func testWrappers(e exchange.IBotExchange) []string {
funcs = append(funcs, "UpdateTradablePairs")
}

_, err = e.GetAccountInfo()
_, err = e.FetchAccountInfo()
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "GetAccountInfo")
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/engine"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
Expand Down Expand Up @@ -398,15 +399,15 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
})
}

var r7 exchange.AccountInfo
r7, err = e.GetAccountInfo()
var r7 account.Holdings
r7, err = e.FetchAccountInfo()
msg = ""
if err != nil {
msg = err.Error()
responseContainer.ErrorCount++
}
responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{
Function: "GetAccountInfo",
Function: "FetchAccountInfo",
Error: msg,
Response: jsonifyInterface([]interface{}{r7}),
})
Expand Down
61 changes: 61 additions & 0 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,67 @@ func getAccountInfo(c *cli.Context) error {
return nil
}

var getAccountInfoStreamCommand = cli.Command{
Name: "getaccountinfostream",
Usage: "gets the account info stream for a specific exchange",
ArgsUsage: "<exchange>",
Action: getAccountInfoStream,
Flags: []cli.Flag{
cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the account info stream from",
},
},
}

func getAccountInfoStream(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowCommandHelp(c, "getaccountinfostream")
return nil
}

var exchangeName string

if c.IsSet("exchange") {
exchangeName = c.String("exchange")
} else {
exchangeName = c.Args().First()
}

if !validExchange(exchangeName) {
return errInvalidExchange
}

conn, err := setupClient()
if err != nil {
return err
}
defer conn.Close()

client := gctrpc.NewGoCryptoTraderClient(conn)
result, err := client.GetAccountInfoStream(context.Background(),
&gctrpc.GetAccountInfoRequest{Exchange: exchangeName})
if err != nil {
return err
}

for {
resp, err := result.Recv()
if err != nil {
return err
}

err = clearScreen()
if err != nil {
return err
}

fmt.Printf("Account balance stream for %s:\n\n", exchangeName)

fmt.Printf("%+v", resp)
}
}

var getConfigCommand = cli.Command{
Name: "getconfig",
Usage: "gets the config",
Expand Down
1 change: 1 addition & 0 deletions cmd/gctcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func main() {
getOrderbookCommand,
getOrderbooksCommand,
getAccountInfoCommand,
getAccountInfoStreamCommand,
getConfigCommand,
getPortfolioCommand,
getPortfolioSummaryCommand,
Expand Down
75 changes: 37 additions & 38 deletions engine/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ func dryrunParamInteraction(param string) {
}
}

// CheckExchangeExists returns true whether or not an exchange has already
// been loaded
func CheckExchangeExists(exchName string) bool {
for x := range Bot.Exchanges {
if strings.EqualFold(Bot.Exchanges[x].GetName(), exchName) {
return true
}
}
return false
}

// GetExchangeByName returns an exchange given an exchange name
func GetExchangeByName(exchName string) exchange.IBotExchange {
for x := range Bot.Exchanges {
Expand All @@ -86,7 +75,8 @@ func ReloadExchange(name string) error {
return ErrNoExchangesLoaded
}

if !CheckExchangeExists(name) {
e := GetExchangeByName(name)
if e == nil {
return ErrExchangeNotFound
}

Expand All @@ -95,7 +85,6 @@ func ReloadExchange(name string) error {
return err
}

e := GetExchangeByName(name)
e.Setup(exchCfg)
log.Debugf(log.ExchangeSys, "%s exchange reloaded successfully.\n", name)
return nil
Expand All @@ -107,7 +96,7 @@ func UnloadExchange(name string) error {
return ErrNoExchangesLoaded
}

if !CheckExchangeExists(name) {
if GetExchangeByName(name) == nil {
return ErrExchangeNotFound
}

Expand Down Expand Up @@ -139,7 +128,7 @@ func LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error {
var exch exchange.IBotExchange

if len(Bot.Exchanges) > 0 {
if CheckExchangeExists(name) {
if GetExchangeByName(name) != nil {
return ErrExchangeAlreadyLoaded
}
}
Expand Down Expand Up @@ -288,55 +277,65 @@ func LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error {

Bot.Exchanges = append(Bot.Exchanges, exch)

base := exch.GetBase()
if base.API.AuthenticatedSupport ||
base.API.AuthenticatedWebsocketSupport {
err = exch.ValidateCredentials()
if err != nil {
log.Warnf(log.ExchangeSys,
"%s: Cannot validate credentials, authenticated support has been disabled, Error: %s\n",
base.Name,
err)
base.API.AuthenticatedSupport = false
base.API.AuthenticatedWebsocketSupport = false
exchCfg.API.AuthenticatedSupport = false
exchCfg.API.AuthenticatedWebsocketSupport = false
}
}

if useWG {
exch.Start(wg)
} else {
wg := sync.WaitGroup{}
exch.Start(&wg)
wg.Wait()
tempWG := sync.WaitGroup{}
exch.Start(&tempWG)
tempWG.Wait()
}

return nil
}

// SetupExchanges sets up the exchanges used by the Bot
func SetupExchanges() {
var wg sync.WaitGroup
exchanges := Bot.Config.GetAllExchangeConfigs()
for x := range exchanges {
exch := exchanges[x]
if CheckExchangeExists(exch.Name) {
e := GetExchangeByName(exch.Name)
if e == nil {
log.Errorln(log.ExchangeSys, ErrExchangeNotFound)
continue
}

err := ReloadExchange(exch.Name)
configs := Bot.Config.GetAllExchangeConfigs()
for x := range configs {
if e := GetExchangeByName(configs[x].Name); e != nil {
err := ReloadExchange(configs[x].Name)
if err != nil {
log.Errorf(log.ExchangeSys, "ReloadExchange %s failed: %s\n", exch.Name, err)
log.Errorf(log.ExchangeSys, "ReloadExchange %s failed: %s\n", configs[x].Name, err)
continue
}

if !e.IsEnabled() {
UnloadExchange(exch.Name)
UnloadExchange(configs[x].Name)
continue
}
return
}
if !exch.Enabled && !Bot.Settings.EnableAllExchanges {
log.Debugf(log.ExchangeSys, "%s: Exchange support: Disabled\n", exch.Name)
if !configs[x].Enabled && !Bot.Settings.EnableAllExchanges {
log.Debugf(log.ExchangeSys, "%s: Exchange support: Disabled\n", configs[x].Name)
continue
}
err := LoadExchange(exch.Name, true, &wg)
err := LoadExchange(configs[x].Name, true, &wg)
if err != nil {
log.Errorf(log.ExchangeSys, "LoadExchange %s failed: %s\n", exch.Name, err)
log.Errorf(log.ExchangeSys, "LoadExchange %s failed: %s\n", configs[x].Name, err)
continue
}
log.Debugf(log.ExchangeSys,
"%s: Exchange support: Enabled (Authenticated API support: %s - Verbose mode: %s).\n",
exch.Name,
common.IsEnabled(exch.API.AuthenticatedSupport),
common.IsEnabled(exch.Verbose),
configs[x].Name,
common.IsEnabled(configs[x].API.AuthenticatedSupport),
common.IsEnabled(configs[x].Verbose),
)
}
wg.Wait()
Expand Down
8 changes: 4 additions & 4 deletions engine/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func SetupTest(t *testing.T) {
testSetup = true
}

if CheckExchangeExists(testExchange) {
if GetExchangeByName(testExchange) != nil {
return
}
err := LoadExchange(testExchange, false, nil)
Expand All @@ -31,7 +31,7 @@ func SetupTest(t *testing.T) {
}

func CleanupTest(t *testing.T) {
if !CheckExchangeExists(testExchange) {
if GetExchangeByName(testExchange) == nil {
return
}

Expand All @@ -45,11 +45,11 @@ func CleanupTest(t *testing.T) {
func TestCheckExchangeExists(t *testing.T) {
SetupTest(t)

if !CheckExchangeExists(testExchange) {
if GetExchangeByName(testExchange) == nil {
t.Errorf("TestGetExchangeExists: Unable to find exchange")
}

if CheckExchangeExists("Asdsad") {
if GetExchangeByName("Asdsad") != nil {
t.Errorf("TestGetExchangeExists: Non-existent exchange found")
}

Expand Down
Loading

0 comments on commit a32d16e

Please sign in to comment.