aboutsummaryrefslogtreecommitdiff
path: root/api/market_config.go
blob: c2248b354e876403361f7a67169ae845ab7e2e90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package api

import (
	"fmt"
	"strings"
	"time"

	"immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"

	"github.com/jloup/utils"
	"github.com/shopspring/decimal"
	"immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
)

type MarketConfigQuery struct {
	In struct {
		User   db.User
		Market string
	}
}

func (q MarketConfigQuery) ValidateParams() *Error {
	if q.In.Market != "poloniex" {
		return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
	}

	return nil
}

func (q MarketConfigQuery) Run() (interface{}, *Error) {
	config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
	if err != nil {
		return nil, NewInternalError(err)
	}

	if config == nil {
		configMap := make(map[string]string)
		configMap["key"] = ""
		configMap["secret"] = ""

		config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
		if err != nil {
			return nil, NewInternalError(err)
		}

	}

	if _, ok := config.Config["key"]; !ok {
		config.Config["key"] = ""
	}

	if _, ok := config.Config["secret"]; !ok {
		config.Config["secret"] = ""
	}

	return config.Config, nil
}

type MarketBalanceQuery struct {
	In struct {
		User     db.User
		Market   string
		Currency string
	}
}

func (q MarketBalanceQuery) ValidateParams() *Error {
	if q.In.Market != "poloniex" {
		return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
	}

	// TODO: we should request market for available currencies.
	if q.In.Currency != "BTC" && q.In.Currency != "USDT" && q.In.Currency != "ETH" {
		return &Error{BadRequest, "invalid currency, accept [BTC, USDT, ETH]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)}
	}

	return nil
}

func (q MarketBalanceQuery) Run() (interface{}, *Error) {
	config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
	if err != nil {
		return nil, NewInternalError(err)
	}

	if config.Config["key"] == "" || config.Config["secret"] == "" {
		return nil, &Error{InvalidMarketCredentials, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)}
	}

	result := struct {
		Value         decimal.Decimal            `json:"value"`
		ValueCurrency string                     `json:"valueCurrency"`
		Balance       map[string]markets.Balance `json:"balance"`
	}{}

	resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
		balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"])

		if utils.ErrIs(err, markets.InvalidCredentials) {
			return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
		}

		if utils.ErrIs(err, markets.IPRestricted) {
			return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
		}

		if err != nil {
			return NewInternalError(err)
		}

		for currency, value := range balance.Balances {
			if value.BTCValue.Abs().LessThan(decimal.NewFromFloat(0.0001)) {
				delete(balance.Balances, currency)
			}
		}

		result.Balance = balance.Balances
		result.ValueCurrency = "BTC"
		result.Value = balance.BTCValue.Round(8)

		return nil
	})

	if resultErr != nil {
		return nil, resultErr
	}

	return &result, nil
}

type UpdateMarketConfigQuery struct {
	In struct {
		User   db.User
		Market string
		Key    string
		Secret string
	}
}

func (q UpdateMarketConfigQuery) ValidateParams() *Error {
	if q.In.Market == "" {
		return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
	}

	q.In.Secret = strings.TrimSpace(q.In.Secret)
	q.In.Key = strings.TrimSpace(q.In.Key)

	return nil
}

func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
	configMap := make(map[string]string)
	if q.In.Key != "" {
		configMap["key"] = q.In.Key
	}
	if q.In.Secret != "" {
		configMap["secret"] = q.In.Secret
	}

	_, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
	if err != nil {
		return nil, NewInternalError(err)
	}

	return nil, nil
}