-
Notifications
You must be signed in to change notification settings - Fork 2
/
currency_details.go
235 lines (213 loc) · 5.89 KB
/
currency_details.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package poloniex
import (
"errors"
"strconv"
"sync"
"github.com/thrasher-corp/gocryptotrader/currency"
)
// CurrencyDetails stores a map of currencies associated with their ID
type CurrencyDetails struct {
pairs map[float64]PairSummaryInfo
codes map[float64]CodeSummaryInfo
// Mutex used for future when we will periodically update this table every
// week or so in production
m sync.RWMutex
}
// PairSummaryInfo defines currency pair information
type PairSummaryInfo struct {
Pair currency.Pair
IsFrozen bool
PostOnly bool
}
// CodeSummaryInfo defines currency information
type CodeSummaryInfo struct {
Currency currency.Code
WithdrawalTXFee float64
MinimumConfirmations int64
DepositAddress string
WithdrawalDepositDisabled bool
Frozen bool
}
var (
errCannotLoadNoData = errors.New("cannot load websocket currency data as data is nil")
errNoDepositAddress = errors.New("no public deposit address for currency")
errPairMapIsNil = errors.New("cannot get currency pair, map is nil")
errCodeMapIsNil = errors.New("cannot get currency code, map is nil")
errCurrencyNotFoundInMap = errors.New("currency not found")
)
// loadPairs loads currency pair associations with unique identifiers from
// ticker data map
func (w *CurrencyDetails) loadPairs(data map[string]Ticker) error {
if data == nil {
return errCannotLoadNoData
}
w.m.Lock()
defer w.m.Unlock()
for k, v := range data {
pair, err := currency.NewPairFromString(k)
if err != nil {
return err
}
if w.pairs == nil {
w.pairs = make(map[float64]PairSummaryInfo)
}
w.pairs[v.ID] = PairSummaryInfo{
Pair: pair,
IsFrozen: v.IsFrozen == 1,
PostOnly: v.PostOnly == 1,
}
}
return nil
}
// loadCodes loads currency codes from currency map
func (w *CurrencyDetails) loadCodes(data map[string]Currencies) error {
if data == nil {
return errCannotLoadNoData
}
w.m.Lock()
defer w.m.Unlock()
for k, v := range data {
if v.Delisted == 1 {
continue
}
if w.codes == nil {
w.codes = make(map[float64]CodeSummaryInfo)
}
w.codes[v.ID] = CodeSummaryInfo{
Currency: currency.NewCode(k),
WithdrawalTXFee: v.TxFee,
MinimumConfirmations: v.MinConfirmations,
DepositAddress: v.DepositAddress,
WithdrawalDepositDisabled: v.WithdrawalDepositDisabled == 1,
Frozen: v.Frozen == 1,
}
}
return nil
}
// GetPair returns a currency pair by its ID
func (w *CurrencyDetails) GetPair(id float64) (currency.Pair, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.pairs == nil {
return currency.Pair{}, errPairMapIsNil
}
p, ok := w.pairs[id]
if ok {
return p.Pair, nil
}
// This is here so we can still log an order with the ID as the currency
// pair which you can then cross reference later with the exchange ID list,
// rather than error out.
op, err := currency.NewPairFromString(strconv.FormatFloat(id, 'f', -1, 64))
if err != nil {
return op, err
}
return op, errIDNotFoundInPairMap
}
// GetCode returns a currency code by its ID
func (w *CurrencyDetails) GetCode(id float64) (currency.Code, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return currency.Code{}, errCodeMapIsNil
}
c, ok := w.codes[id]
if ok {
return c.Currency, nil
}
return currency.Code{}, errIDNotFoundInCodeMap
}
// GetWithdrawalTXFee returns withdrawal transaction fee for the currency
func (w *CurrencyDetails) GetWithdrawalTXFee(c currency.Code) (float64, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return 0, errCodeMapIsNil
}
for _, v := range w.codes {
if v.Currency == c {
return v.WithdrawalTXFee, nil
}
}
return 0, errCurrencyNotFoundInMap
}
// GetDepositAddress returns the public deposit address details for the currency
func (w *CurrencyDetails) GetDepositAddress(c currency.Code) (string, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return "", errCodeMapIsNil
}
for _, v := range w.codes {
if v.Currency == c {
if v.DepositAddress == "" {
return "", errNoDepositAddress
}
return v.DepositAddress, nil
}
}
return "", errCurrencyNotFoundInMap
}
// IsWithdrawAndDepositsEnabled returns if withdrawals or deposits are enabled
func (w *CurrencyDetails) IsWithdrawAndDepositsEnabled(c currency.Code) (bool, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return false, errCodeMapIsNil
}
for _, v := range w.codes {
if v.Currency == c {
return !v.WithdrawalDepositDisabled, nil
}
}
return false, errCurrencyNotFoundInMap
}
// IsTradingEnabledForCurrency returns if the currency is allowed to be traded
func (w *CurrencyDetails) IsTradingEnabledForCurrency(c currency.Code) (bool, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return false, errCodeMapIsNil
}
for _, v := range w.codes {
if v.Currency == c {
return !v.Frozen, nil
}
}
return false, errCurrencyNotFoundInMap
}
// IsTradingEnabledForPair returns if the currency pair is allowed to be traded
func (w *CurrencyDetails) IsTradingEnabledForPair(pair currency.Pair) (bool, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return false, errCodeMapIsNil
}
for _, v := range w.pairs {
if v.Pair.Equal(pair) {
return !v.IsFrozen, nil
}
}
return false, errCurrencyNotFoundInMap
}
// IsPostOnlyForPair returns if an order is allowed to take liquidity from the
// books or reduce positions
func (w *CurrencyDetails) IsPostOnlyForPair(pair currency.Pair) (bool, error) {
w.m.RLock()
defer w.m.RUnlock()
if w.codes == nil {
return false, errCodeMapIsNil
}
for _, v := range w.pairs {
if v.Pair.Equal(pair) {
return v.PostOnly, nil
}
}
return false, errCurrencyNotFoundInMap
}
// isInitial checks state of maps to determine if they have been loaded or not
func (w *CurrencyDetails) isInitial() bool {
w.m.RLock()
defer w.m.RUnlock()
return w.codes == nil || w.pairs == nil
}