}
func NewInternalError(err error) *Error {
+ if apiError, ok := err.(*Error); ok {
+ return apiError
+ }
return &Error{InternalError, "internal error", err}
}
)
// Use this to call external services. It will handle timeout and request cancellation gracefully.
-func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error {
- routineDone := make(chan *Error)
+func CallExternalService(tag string, timeout time.Duration, routine func() error) error {
+ routineDone := make(chan error)
go func() {
routineDone <- routine()
return nil, NewInternalError(err)
}
- if config.Config["key"] == "" || config.Config["secret"] == "" {
+ if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
}
- resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
+ resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"])
if utils.ErrIs(err, markets.InvalidCredentials) {
})
if resultErr != nil {
- return nil, resultErr
+ return nil, NewInternalError(resultErr)
}
return nil, nil
configMap["secret"] = q.In.Secret
}
- _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+ marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+ if err != nil {
+ return nil, NewInternalError(err)
+ }
+
+ resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
+ err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"])
+
+ if utils.ErrIs(err, markets.InvalidCredentials) {
+ return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
+ }
+
+ if utils.ErrIs(err, markets.IPRestricted) {
+ return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
+ }
+
+ if err != nil {
+ return NewInternalError(err)
+ }
+
+ return nil
+ })
+
+ var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
+
+ if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) {
+ newStatus = db.MarketConfigInvalidCredentials
+ } else if resultErr != nil {
+ return nil, NewInternalError(resultErr)
+ }
+
+ marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
if err != nil {
return nil, NewInternalError(err)
}
}
handleCredentialsSubmit = () => {
+ this.setState({'status': 'loading'});
Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
if (err) {
console.error(err, data);
+ if (err.code === 'invalid_market_credentials') {
+ this.setState({'status': 'invalidCredentials'});
+ } else if (err.code === 'ip_restricted_api_key') {
+ this.setState({'status': 'ipRestricted'});
+ }
return;
}
package db
-import "github.com/go-pg/pg"
+import (
+ "github.com/go-pg/pg"
+)
+
+type MarketConfigStatus string
+
+const MarketConfigEnabled = "enabled"
+const MarketConfigDisabled = "disabled"
+const MarketConfigInvalidCredentials = "invalid_credentials"
type MarketConfig struct {
Id int64
- MarketName string `sql:",notnull"`
- UserId int64 `sql:",notnull"`
+ MarketName string
+ UserId int64
+ Status MarketConfigStatus
Config map[string]string
}
return &config, err
}
+
+func SetMarketConfigStatus(marketConfig MarketConfig, status MarketConfigStatus) (*MarketConfig, error) {
+ marketConfig.Status = status
+ _, err := DB.Model(&marketConfig).
+ OnConflict("(user_id, market_name) DO UPDATE").
+ Set("status = ?", status).
+ Insert()
+
+ return &marketConfig, err
+}
},
Down: []string{"DROP VIEW view_balances", "DROP INDEX checkpoints_idx"},
},
+ {
+ Version: 201805101000,
+ Up: []string{
+ "CREATE TYPE market_config_status AS ENUM ('enabled', 'disabled', 'invalid_credentials')",
+ "ALTER TABLE market_configs ADD status market_config_status NOT NULL DEFAULT 'disabled'",
+ },
+ Down: []string{
+ "ALTER TABLE market_configs DROP COLUMN status",
+ "DROP TYPE market_config_status",
+ },
+ },
}
if err == nil {
return false
}
- return strings.Contains(err.Error(), "Invalid API key/secret pair")
+ return strings.Contains(err.Error(), "Invalid API key/secret pair") || strings.Contains(err.Error(), "Set the API KEY and API SECRET")
}
func poloniexRestrictedIPError(err error) bool {
return utils.Error{IPRestricted, "IP restricted api key"}
}
- return nil
+ return err
}
func (p *Poloniex) GetBalance(apiKey, apiSecret string) (Summary, error) {