]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - api/market_config.go
c2248b354e876403361f7a67169ae845ab7e2e90
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
1 package api
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"
9
10 "github.com/jloup/utils"
11 "github.com/shopspring/decimal"
12 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
13 )
14
15 type MarketConfigQuery struct {
16 In struct {
17 User db.User
18 Market string
19 }
20 }
21
22 func (q MarketConfigQuery) ValidateParams() *Error {
23 if q.In.Market != "poloniex" {
24 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
25 }
26
27 return nil
28 }
29
30 func (q MarketConfigQuery) Run() (interface{}, *Error) {
31 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
32 if err != nil {
33 return nil, NewInternalError(err)
34 }
35
36 if config == nil {
37 configMap := make(map[string]string)
38 configMap["key"] = ""
39 configMap["secret"] = ""
40
41 config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
42 if err != nil {
43 return nil, NewInternalError(err)
44 }
45
46 }
47
48 if _, ok := config.Config["key"]; !ok {
49 config.Config["key"] = ""
50 }
51
52 if _, ok := config.Config["secret"]; !ok {
53 config.Config["secret"] = ""
54 }
55
56 return config.Config, nil
57 }
58
59 type MarketBalanceQuery struct {
60 In struct {
61 User db.User
62 Market string
63 Currency string
64 }
65 }
66
67 func (q MarketBalanceQuery) ValidateParams() *Error {
68 if q.In.Market != "poloniex" {
69 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
70 }
71
72 // TODO: we should request market for available currencies.
73 if q.In.Currency != "BTC" && q.In.Currency != "USDT" && q.In.Currency != "ETH" {
74 return &Error{BadRequest, "invalid currency, accept [BTC, USDT, ETH]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)}
75 }
76
77 return nil
78 }
79
80 func (q MarketBalanceQuery) Run() (interface{}, *Error) {
81 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
82 if err != nil {
83 return nil, NewInternalError(err)
84 }
85
86 if config.Config["key"] == "" || config.Config["secret"] == "" {
87 return nil, &Error{InvalidMarketCredentials, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)}
88 }
89
90 result := struct {
91 Value decimal.Decimal `json:"value"`
92 ValueCurrency string `json:"valueCurrency"`
93 Balance map[string]markets.Balance `json:"balance"`
94 }{}
95
96 resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
97 balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"])
98
99 if utils.ErrIs(err, markets.InvalidCredentials) {
100 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
101 }
102
103 if utils.ErrIs(err, markets.IPRestricted) {
104 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
105 }
106
107 if err != nil {
108 return NewInternalError(err)
109 }
110
111 for currency, value := range balance.Balances {
112 if value.BTCValue.Abs().LessThan(decimal.NewFromFloat(0.0001)) {
113 delete(balance.Balances, currency)
114 }
115 }
116
117 result.Balance = balance.Balances
118 result.ValueCurrency = "BTC"
119 result.Value = balance.BTCValue.Round(8)
120
121 return nil
122 })
123
124 if resultErr != nil {
125 return nil, resultErr
126 }
127
128 return &result, nil
129 }
130
131 type UpdateMarketConfigQuery struct {
132 In struct {
133 User db.User
134 Market string
135 Key string
136 Secret string
137 }
138 }
139
140 func (q UpdateMarketConfigQuery) ValidateParams() *Error {
141 if q.In.Market == "" {
142 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
143 }
144
145 q.In.Secret = strings.TrimSpace(q.In.Secret)
146 q.In.Key = strings.TrimSpace(q.In.Key)
147
148 return nil
149 }
150
151 func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
152 configMap := make(map[string]string)
153 if q.In.Key != "" {
154 configMap["key"] = q.In.Key
155 }
156 if q.In.Secret != "" {
157 configMap["secret"] = q.In.Secret
158 }
159
160 _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
161 if err != nil {
162 return nil, NewInternalError(err)
163 }
164
165 return nil, nil
166 }