forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
220 lines (184 loc) · 4.83 KB
/
manager.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
package currency
import (
"errors"
"fmt"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
var (
// ErrAssetAlreadyEnabled defines an error for the pairs management system
// that declares the asset is already enabled.
ErrAssetAlreadyEnabled = errors.New("asset already enabled")
// ErrPairAlreadyEnabled returns when enabling a pair that is already enabled
ErrPairAlreadyEnabled = errors.New("pair already enabled")
// ErrPairNotFound is returned when a currency pair is not found
ErrPairNotFound = errors.New("pair not found")
)
// GetAssetTypes returns a list of stored asset types
func (p *PairsManager) GetAssetTypes(enabled bool) asset.Items {
p.m.RLock()
defer p.m.RUnlock()
var assetTypes asset.Items
for k, ps := range p.Pairs {
if enabled && (ps.AssetEnabled == nil || !*ps.AssetEnabled) {
continue
}
assetTypes = append(assetTypes, k)
}
return assetTypes
}
// Get gets the currency pair config based on the asset type
func (p *PairsManager) Get(a asset.Item) (*PairStore, error) {
p.m.RLock()
defer p.m.RUnlock()
c, ok := p.Pairs[a]
if !ok {
return nil,
fmt.Errorf("cannot get pair store, asset type %s not supported", a)
}
return c, nil
}
// Store stores a new currency pair config based on its asset type
func (p *PairsManager) Store(a asset.Item, ps PairStore) {
p.m.Lock()
if p.Pairs == nil {
p.Pairs = make(map[asset.Item]*PairStore)
}
p.Pairs[a] = &ps
p.m.Unlock()
}
// Delete deletes a map entry based on the supplied asset type
func (p *PairsManager) Delete(a asset.Item) {
p.m.Lock()
defer p.m.Unlock()
if p.Pairs == nil {
return
}
delete(p.Pairs, a)
}
// GetPairs gets a list of stored pairs based on the asset type and whether
// they're enabled or not
func (p *PairsManager) GetPairs(a asset.Item, enabled bool) (Pairs, error) {
p.m.RLock()
defer p.m.RUnlock()
if p.Pairs == nil {
return nil, nil
}
c, ok := p.Pairs[a]
if !ok {
return nil, nil
}
if enabled {
for i := range c.Enabled {
if !c.Available.Contains(c.Enabled[i], true) {
return c.Enabled,
fmt.Errorf("enabled pair %s of asset type %s not contained in available list",
c.Enabled[i],
a)
}
}
return c.Enabled, nil
}
return c.Available, nil
}
// StorePairs stores a list of pairs based on the asset type and whether
// they're enabled or not
func (p *PairsManager) StorePairs(a asset.Item, pairs Pairs, enabled bool) {
p.m.Lock()
defer p.m.Unlock()
if p.Pairs == nil {
p.Pairs = make(map[asset.Item]*PairStore)
}
c, ok := p.Pairs[a]
if !ok {
p.Pairs[a] = new(PairStore)
c = p.Pairs[a]
}
if enabled {
c.Enabled = pairs
} else {
c.Available = pairs
}
}
// DisablePair removes the pair from the enabled pairs list if found
func (p *PairsManager) DisablePair(a asset.Item, pair Pair) error {
p.m.Lock()
defer p.m.Unlock()
c, err := p.getPairStore(a)
if err != nil {
return err
}
if !c.Enabled.Contains(pair, true) {
return errors.New("specified pair is not enabled")
}
c.Enabled = c.Enabled.Remove(pair)
return nil
}
// EnablePair adds a pair to the list of enabled pairs if it exists in the list
// of available pairs and isn't already added
func (p *PairsManager) EnablePair(a asset.Item, pair Pair) error {
p.m.Lock()
defer p.m.Unlock()
c, err := p.getPairStore(a)
if err != nil {
return err
}
if !c.Available.Contains(pair, true) {
return fmt.Errorf("%s %w in the list of available pairs",
pair, ErrPairNotFound)
}
if c.Enabled.Contains(pair, true) {
return fmt.Errorf("%s %w", pair, ErrPairAlreadyEnabled)
}
c.Enabled = c.Enabled.Add(pair)
return nil
}
// IsAssetEnabled checks to see if an asset is enabled
func (p *PairsManager) IsAssetEnabled(a asset.Item) error {
p.m.RLock()
defer p.m.RUnlock()
c, err := p.getPairStore(a)
if err != nil {
return err
}
if c.AssetEnabled == nil {
return errors.New("cannot ascertain if asset is enabled, variable is nil")
}
if !*c.AssetEnabled {
return fmt.Errorf("asset %s not enabled", a)
}
return nil
}
// SetAssetEnabled sets if an asset is enabled or disabled for first run
func (p *PairsManager) SetAssetEnabled(a asset.Item, enabled bool) error {
p.m.Lock()
defer p.m.Unlock()
c, err := p.getPairStore(a)
if err != nil {
return err
}
if c.AssetEnabled == nil {
c.AssetEnabled = convert.BoolPtr(enabled)
return nil
}
if !*c.AssetEnabled && !enabled {
return errors.New("asset already disabled")
} else if *c.AssetEnabled && enabled {
return ErrAssetAlreadyEnabled
}
*c.AssetEnabled = enabled
return nil
}
func (p *PairsManager) getPairStore(a asset.Item) (*PairStore, error) {
if p.Pairs == nil {
return nil, errors.New("pair manager not initialised")
}
c, ok := p.Pairs[a]
if !ok {
return nil, errors.New("asset type not found")
}
if c == nil {
return nil, errors.New("currency store is nil")
}
return c, nil
}