diff options
Diffstat (limited to 'api/market_config.go')
-rw-r--r-- | api/market_config.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/api/market_config.go b/api/market_config.go index 3fd10ae..ce79184 100644 --- a/api/market_config.go +++ b/api/market_config.go | |||
@@ -3,7 +3,9 @@ package api | |||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | 5 | ||
6 | "github.com/shopspring/decimal" | ||
6 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" | 7 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" |
8 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets" | ||
7 | ) | 9 | ) |
8 | 10 | ||
9 | type MarketConfigQuery struct { | 11 | type MarketConfigQuery struct { |
@@ -42,6 +44,56 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) { | |||
42 | return config.Config, nil | 44 | return config.Config, nil |
43 | } | 45 | } |
44 | 46 | ||
47 | type MarketBalanceQuery struct { | ||
48 | In struct { | ||
49 | User db.User | ||
50 | Market string | ||
51 | Currency string | ||
52 | } | ||
53 | } | ||
54 | |||
55 | var Poloniex *markets.Poloniex | ||
56 | |||
57 | func init() { | ||
58 | Poloniex = markets.NewPoloniex() | ||
59 | Poloniex.StartTicker() | ||
60 | } | ||
61 | |||
62 | func (q MarketBalanceQuery) ValidateParams() *Error { | ||
63 | if q.In.Market != "poloniex" { | ||
64 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | ||
65 | } | ||
66 | |||
67 | // TODO: we should request market for available currencies. | ||
68 | if q.In.Currency != "BTC" || q.In.Currency != "USDT" { | ||
69 | return &Error{BadRequest, "invalid currency, accept [BTC, USDT]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)} | ||
70 | } | ||
71 | |||
72 | return nil | ||
73 | } | ||
74 | |||
75 | func (q MarketBalanceQuery) Run() (interface{}, *Error) { | ||
76 | config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | ||
77 | if err != nil { | ||
78 | return nil, NewInternalError(err) | ||
79 | } | ||
80 | |||
81 | if config.Config["key"] == "" || config.Config["secret"] == "" { | ||
82 | return nil, &Error{BadRequest, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)} | ||
83 | } | ||
84 | |||
85 | balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"], q.In.Currency) | ||
86 | if err != nil { | ||
87 | return nil, NewInternalError(err) | ||
88 | } | ||
89 | |||
90 | result := struct { | ||
91 | Balance decimal.Decimal `json:balance` | ||
92 | }{balance} | ||
93 | |||
94 | return &result, nil | ||
95 | } | ||
96 | |||
45 | type UpdateMarketConfigQuery struct { | 97 | type UpdateMarketConfigQuery struct { |
46 | In struct { | 98 | In struct { |
47 | User db.User | 99 | User db.User |