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.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/api/market_config.go b/api/market_config.go
index 3fd10ae..d85af4d 100644
--- a/api/market_config.go
+++ b/api/market_config.go
@@ -2,7 +2,13 @@ package api
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
5 "strings"
6 "time"
5 7
8 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"
9
10 "github.com/jloup/utils"
11 "github.com/shopspring/decimal"
6 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" 12 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
7) 13)
8 14
@@ -42,6 +48,73 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) {
42 return config.Config, nil 48 return config.Config, nil
43} 49}
44 50
51type MarketBalanceQuery struct {
52 In struct {
53 User db.User
54 Market string
55 Currency string
56 }
57}
58
59func (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
72func (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]decimal.Decimal `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 err != nil {
96 return NewInternalError(err)
97 }
98
99 value, err := Poloniex.ComputeAccountBalanceValue(balance, q.In.Currency)
100 if err != nil {
101 return NewInternalError(err)
102 }
103
104 result.Balance = balance
105 result.ValueCurrency = q.In.Currency
106 result.Value = value.Round(8)
107
108 return nil
109 })
110
111 if resultErr != nil {
112 return nil, resultErr
113 }
114
115 return &result, nil
116}
117
45type UpdateMarketConfigQuery struct { 118type UpdateMarketConfigQuery struct {
46 In struct { 119 In struct {
47 User db.User 120 User db.User
@@ -56,6 +129,9 @@ func (q UpdateMarketConfigQuery) ValidateParams() *Error {
56 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} 129 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
57 } 130 }
58 131
132 q.In.Secret = strings.TrimSpace(q.In.Secret)
133 q.In.Key = strings.TrimSpace(q.In.Key)
134
59 return nil 135 return nil
60} 136}
61 137