aboutsummaryrefslogtreecommitdiff
path: root/api/market_config.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/market_config.go')
-rw-r--r--api/market_config.go39
1 files changed, 35 insertions, 4 deletions
diff --git a/api/market_config.go b/api/market_config.go
index 81a92d1..e7b2341 100644
--- a/api/market_config.go
+++ b/api/market_config.go
@@ -75,11 +75,11 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
75 return nil, NewInternalError(err) 75 return nil, NewInternalError(err)
76 } 76 }
77 77
78 if config.Config["key"] == "" || config.Config["secret"] == "" { 78 if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
79 return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} 79 return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
80 } 80 }
81 81
82 resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error { 82 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
83 err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) 83 err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"])
84 84
85 if utils.ErrIs(err, markets.InvalidCredentials) { 85 if utils.ErrIs(err, markets.InvalidCredentials) {
@@ -98,7 +98,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
98 }) 98 })
99 99
100 if resultErr != nil { 100 if resultErr != nil {
101 return nil, resultErr 101 return nil, NewInternalError(resultErr)
102 } 102 }
103 103
104 return nil, nil 104 return nil, nil
@@ -133,7 +133,38 @@ func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
133 configMap["secret"] = q.In.Secret 133 configMap["secret"] = q.In.Secret
134 } 134 }
135 135
136 _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) 136 marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
137 if err != nil {
138 return nil, NewInternalError(err)
139 }
140
141 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
142 err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"])
143
144 if utils.ErrIs(err, markets.InvalidCredentials) {
145 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
146 }
147
148 if utils.ErrIs(err, markets.IPRestricted) {
149 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
150 }
151
152 if err != nil {
153 return NewInternalError(err)
154 }
155
156 return nil
157 })
158
159 var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
160
161 if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) {
162 newStatus = db.MarketConfigInvalidCredentials
163 } else if resultErr != nil {
164 return nil, NewInternalError(resultErr)
165 }
166
167 marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
137 if err != nil { 168 if err != nil {
138 return nil, NewInternalError(err) 169 return nil, NewInternalError(err)
139 } 170 }