aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjloup <jeanloup.jamet@gmail.com>2018-05-10 16:22:29 +0200
committerjloup <jeanloup.jamet@gmail.com>2018-05-10 16:22:29 +0200
commit299b6b6d9fb879c06e675ef240f361348629ff6c (patch)
tree27909cd54e53d58e612da93e15f1005af86870ec
parent3b8833854f83f75e3d16c1fdb869937f690e48ea (diff)
downloadFront-299b6b6d9fb879c06e675ef240f361348629ff6c.tar.gz
Front-299b6b6d9fb879c06e675ef240f361348629ff6c.tar.zst
Front-299b6b6d9fb879c06e675ef240f361348629ff6c.zip
Add column 'status' to market_configs.v0.0.9
-rw-r--r--api/api.go3
-rw-r--r--api/external_services.go4
-rw-r--r--api/market_config.go39
-rw-r--r--cmd/web/js/account.jsx6
-rw-r--r--db/market_config.go25
-rw-r--r--db/migrations.go11
-rw-r--r--markets/poloniex.go4
7 files changed, 81 insertions, 11 deletions
diff --git a/api/api.go b/api/api.go
index ece2a26..79a13a5 100644
--- a/api/api.go
+++ b/api/api.go
@@ -58,6 +58,9 @@ func ErrorIs(err error, code ErrorCode) bool {
58} 58}
59 59
60func NewInternalError(err error) *Error { 60func NewInternalError(err error) *Error {
61 if apiError, ok := err.(*Error); ok {
62 return apiError
63 }
61 return &Error{InternalError, "internal error", err} 64 return &Error{InternalError, "internal error", err}
62} 65}
63 66
diff --git a/api/external_services.go b/api/external_services.go
index c467171..41f4d87 100644
--- a/api/external_services.go
+++ b/api/external_services.go
@@ -7,8 +7,8 @@ import (
7) 7)
8 8
9// Use this to call external services. It will handle timeout and request cancellation gracefully. 9// Use this to call external services. It will handle timeout and request cancellation gracefully.
10func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error { 10func CallExternalService(tag string, timeout time.Duration, routine func() error) error {
11 routineDone := make(chan *Error) 11 routineDone := make(chan error)
12 12
13 go func() { 13 go func() {
14 routineDone <- routine() 14 routineDone <- routine()
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 }
diff --git a/cmd/web/js/account.jsx b/cmd/web/js/account.jsx
index d30abe7..03ca117 100644
--- a/cmd/web/js/account.jsx
+++ b/cmd/web/js/account.jsx
@@ -28,9 +28,15 @@ class PoloniexConfiguration extends React.Component {
28 } 28 }
29 29
30 handleCredentialsSubmit = () => { 30 handleCredentialsSubmit = () => {
31 this.setState({'status': 'loading'});
31 Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { 32 Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
32 if (err) { 33 if (err) {
33 console.error(err, data); 34 console.error(err, data);
35 if (err.code === 'invalid_market_credentials') {
36 this.setState({'status': 'invalidCredentials'});
37 } else if (err.code === 'ip_restricted_api_key') {
38 this.setState({'status': 'ipRestricted'});
39 }
34 return; 40 return;
35 } 41 }
36 42
diff --git a/db/market_config.go b/db/market_config.go
index b26c092..30b4538 100644
--- a/db/market_config.go
+++ b/db/market_config.go
@@ -1,11 +1,20 @@
1package db 1package db
2 2
3import "github.com/go-pg/pg" 3import (
4 "github.com/go-pg/pg"
5)
6
7type MarketConfigStatus string
8
9const MarketConfigEnabled = "enabled"
10const MarketConfigDisabled = "disabled"
11const MarketConfigInvalidCredentials = "invalid_credentials"
4 12
5type MarketConfig struct { 13type MarketConfig struct {
6 Id int64 14 Id int64
7 MarketName string `sql:",notnull"` 15 MarketName string
8 UserId int64 `sql:",notnull"` 16 UserId int64
17 Status MarketConfigStatus
9 Config map[string]string 18 Config map[string]string
10} 19}
11 20
@@ -43,3 +52,13 @@ func SetUserMarketConfig(userId int64, market string, newConfig map[string]strin
43 52
44 return &config, err 53 return &config, err
45} 54}
55
56func SetMarketConfigStatus(marketConfig MarketConfig, status MarketConfigStatus) (*MarketConfig, error) {
57 marketConfig.Status = status
58 _, err := DB.Model(&marketConfig).
59 OnConflict("(user_id, market_name) DO UPDATE").
60 Set("status = ?", status).
61 Insert()
62
63 return &marketConfig, err
64}
diff --git a/db/migrations.go b/db/migrations.go
index f6ecb60..f0df49c 100644
--- a/db/migrations.go
+++ b/db/migrations.go
@@ -78,4 +78,15 @@ var migrations []Migration = []Migration{
78 }, 78 },
79 Down: []string{"DROP VIEW view_balances", "DROP INDEX checkpoints_idx"}, 79 Down: []string{"DROP VIEW view_balances", "DROP INDEX checkpoints_idx"},
80 }, 80 },
81 {
82 Version: 201805101000,
83 Up: []string{
84 "CREATE TYPE market_config_status AS ENUM ('enabled', 'disabled', 'invalid_credentials')",
85 "ALTER TABLE market_configs ADD status market_config_status NOT NULL DEFAULT 'disabled'",
86 },
87 Down: []string{
88 "ALTER TABLE market_configs DROP COLUMN status",
89 "DROP TYPE market_config_status",
90 },
91 },
81} 92}
diff --git a/markets/poloniex.go b/markets/poloniex.go
index 58645b2..cadc829 100644
--- a/markets/poloniex.go
+++ b/markets/poloniex.go
@@ -20,7 +20,7 @@ func poloniexInvalidCredentialsError(err error) bool {
20 if err == nil { 20 if err == nil {
21 return false 21 return false
22 } 22 }
23 return strings.Contains(err.Error(), "Invalid API key/secret pair") 23 return strings.Contains(err.Error(), "Invalid API key/secret pair") || strings.Contains(err.Error(), "Set the API KEY and API SECRET")
24} 24}
25 25
26func poloniexRestrictedIPError(err error) bool { 26func poloniexRestrictedIPError(err error) bool {
@@ -65,7 +65,7 @@ func (p *Poloniex) TestCredentials(apiKey, apiSecret string) error {
65 return utils.Error{IPRestricted, "IP restricted api key"} 65 return utils.Error{IPRestricted, "IP restricted api key"}
66 } 66 }
67 67
68 return nil 68 return err
69} 69}
70 70
71func (p *Poloniex) GetBalance(apiKey, apiSecret string) (Summary, error) { 71func (p *Poloniex) GetBalance(apiKey, apiSecret string) (Summary, error) {