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.go52
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
3import ( 3import (
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
9type MarketConfigQuery struct { 11type 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
47type MarketBalanceQuery struct {
48 In struct {
49 User db.User
50 Market string
51 Currency string
52 }
53}
54
55var Poloniex *markets.Poloniex
56
57func init() {
58 Poloniex = markets.NewPoloniex()
59 Poloniex.StartTicker()
60}
61
62func (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
75func (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
45type UpdateMarketConfigQuery struct { 97type UpdateMarketConfigQuery struct {
46 In struct { 98 In struct {
47 User db.User 99 User db.User