1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package api
import (
"fmt"
"strings"
"time"
"github.com/jloup/utils"
"git.immae.eu/Cryptoportfolio/Front.git/db"
"git.immae.eu/Cryptoportfolio/Front.git/markets"
)
type MarketConfigQuery struct {
In struct {
User db.User
Market string
}
}
func (q MarketConfigQuery) 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 MarketConfigQuery) Run() (interface{}, *Error) {
config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
if err != nil {
return nil, NewInternalError(err)
}
if config == nil {
configMap := make(map[string]string)
configMap["key"] = ""
configMap["secret"] = ""
config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
if err != nil {
return nil, NewInternalError(err)
}
}
if _, ok := config.Config["key"]; !ok {
config.Config["key"] = ""
}
if _, ok := config.Config["secret"]; !ok {
config.Config["secret"] = ""
}
return config.Config, nil
}
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
})
if resultErr != nil {
return nil, NewInternalError(resultErr)
}
return nil, nil
}
type UpdateMarketConfigQuery struct {
In struct {
User db.User
Market string
Key string
Secret string
}
}
func (q UpdateMarketConfigQuery) ValidateParams() *Error {
if q.In.Market == "" {
return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
}
q.In.Secret = strings.TrimSpace(q.In.Secret)
q.In.Key = strings.TrimSpace(q.In.Key)
return nil
}
func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
configMap := make(map[string]string)
if q.In.Key != "" {
configMap["key"] = q.In.Key
}
if q.In.Secret != "" {
configMap["secret"] = q.In.Secret
}
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)
}
return nil, nil
}
type GetPortfolioQuery struct {
In struct {
User db.User
Market string
}
}
func (q GetPortfolioQuery) 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 GetPortfolioQuery) Run() (interface{}, *Error) {
marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
if err != nil {
return nil, NewInternalError(err)
}
report, err := GetWeekPortfolio(*marketConfig)
if ErrorIs(err, NotFound) {
return nil, err.(*Error)
}
if err != nil {
return nil, NewInternalError(err)
}
return report.Round(), nil
}
|