]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - api/market_config.go
Display short positions.
[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 return config.Config, nil
49 }
50
51 type MarketBalanceQuery struct {
52 In struct {
53 User db.User
54 Market string
55 Currency string
56 }
57 }
58
59 func (q MarketBalanceQuery) ValidateParams() *Error {
60 if q.In.Market != "poloniex" {
61 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
62 }
63
64 // TODO: we should request market for available currencies.
65 if q.In.Currency != "BTC" && q.In.Currency != "USDT" && q.In.Currency != "ETH" {
66 return &Error{BadRequest, "invalid currency, accept [BTC, USDT, ETH]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)}
67 }
68
69 return nil
70 }
71
72 func (q MarketBalanceQuery) Run() (interface{}, *Error) {
73 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
74 if err != nil {
75 return nil, NewInternalError(err)
76 }
77
78 if config.Config["key"] == "" || config.Config["secret"] == "" {
79 return nil, &Error{BadRequest, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)}
80 }
81
82 result := struct {
83 Value decimal.Decimal `json:"value"`
84 ValueCurrency string `json:"valueCurrency"`
85 Balance map[string]markets.Balance `json:"balance"`
86 }{}
87
88 resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
89 balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"])
90
91 if utils.ErrIs(err, markets.InvalidCredentials) {
92 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
93 }
94
95 if utils.ErrIs(err, markets.IPRestricted) {
96 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
97 }
98
99 if err != nil {
100 return NewInternalError(err)
101 }
102
103 for currency, value := range balance.Balances {
104 if value.BTCValue.Abs().LessThan(decimal.NewFromFloat(0.0001)) {
105 delete(balance.Balances, currency)
106 }
107 }
108
109 result.Balance = balance.Balances
110 result.ValueCurrency = "BTC"
111 result.Value = balance.BTCValue.Round(8)
112
113 return nil
114 })
115
116 if resultErr != nil {
117 return nil, resultErr
118 }
119
120 return &result, nil
121 }
122
123 type UpdateMarketConfigQuery struct {
124 In struct {
125 User db.User
126 Market string
127 Key string
128 Secret string
129 }
130 }
131
132 func (q UpdateMarketConfigQuery) ValidateParams() *Error {
133 if q.In.Market == "" {
134 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
135 }
136
137 q.In.Secret = strings.TrimSpace(q.In.Secret)
138 q.In.Key = strings.TrimSpace(q.In.Key)
139
140 return nil
141 }
142
143 func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
144 configMap := make(map[string]string)
145 if q.In.Key != "" {
146 configMap["key"] = q.In.Key
147 }
148 if q.In.Secret != "" {
149 configMap["secret"] = q.In.Secret
150 }
151
152 _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
153 if err != nil {
154 return nil, NewInternalError(err)
155 }
156
157 return nil, nil
158 }