+type TestMarketCredentialsQuery struct {
+ In struct {
+ User db.User
+ Market string
+ }
+}
+
+func (q TestMarketCredentialsQuery) ValidateParams() *Error {
+ if q.In.Market != "poloniex" {
+ return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
+ }
+
+ return nil
+}
+
+func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
+ config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
+ if err != nil {
+ return nil, NewInternalError(err)
+ }
+
+ if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
+ return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
+ }
+
+ 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) {
+ 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 = config.Status
+
+ if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
+ newStatus = db.MarketConfigInvalidCredentials
+ } else if resultErr != nil {
+ return nil, NewInternalError(resultErr)
+ } else if resultErr != nil {
+ return nil, NewInternalError(resultErr)
+ }
+
+ if newStatus != config.Status {
+ config, err = db.SetMarketConfigStatus(*config, newStatus)
+ if err != nil {
+ return nil, NewInternalError(err)
+ }
+ }
+
+ return nil, nil
+}
+