forked from oss-jtyd/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddr_helpers.go
100 lines (81 loc) · 2.55 KB
/
addr_helpers.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
package engine
import (
"errors"
"strings"
"sync"
"github.com/thrasher-corp/gocryptotrader/currency"
)
// DepositAddressStore stores a list of exchange deposit addresses
type DepositAddressStore struct {
m sync.Mutex
Store map[string]map[string]string
}
// DepositAddressManager manages the exchange deposit address store
type DepositAddressManager struct {
Store DepositAddressStore
}
// vars related to the deposit address helpers
var (
ErrDepositAddressStoreIsNil = errors.New("deposit address store is nil")
ErrDepositAddressNotFound = errors.New("deposit address does not exist")
)
// Seed seeds the deposit address store
func (d *DepositAddressStore) Seed(coinData map[string]map[string]string) {
d.m.Lock()
defer d.m.Unlock()
if d.Store == nil {
d.Store = make(map[string]map[string]string)
}
for k, v := range coinData {
r := make(map[string]string)
for w, x := range v {
r[strings.ToUpper(w)] = x
}
d.Store[strings.ToUpper(k)] = r
}
}
// GetDepositAddress returns a deposit address based on the specified item
func (d *DepositAddressStore) GetDepositAddress(exchName string, item currency.Code) (string, error) {
d.m.Lock()
defer d.m.Unlock()
if len(d.Store) == 0 {
return "", ErrDepositAddressStoreIsNil
}
r, ok := d.Store[strings.ToUpper(exchName)]
if !ok {
return "", ErrExchangeNotFound
}
addr, ok := r[strings.ToUpper(item.String())]
if !ok {
return "", ErrDepositAddressNotFound
}
return addr, nil
}
// GetDepositAddresses returns a list of stored deposit addresses
func (d *DepositAddressStore) GetDepositAddresses(exchName string) (map[string]string, error) {
d.m.Lock()
defer d.m.Unlock()
if len(d.Store) == 0 {
return nil, ErrDepositAddressStoreIsNil
}
r, ok := d.Store[strings.ToUpper(exchName)]
if !ok {
return nil, ErrDepositAddressNotFound
}
return r, nil
}
// GetDepositAddressByExchange returns a deposit address for the specified exchange and cryptocurrency
// if it exists
func (d *DepositAddressManager) GetDepositAddressByExchange(exchName string, currencyItem currency.Code) (string, error) {
return d.Store.GetDepositAddress(exchName, currencyItem)
}
// GetDepositAddressesByExchange returns a list of cryptocurrency addresses for the specified
// exchange if they exist
func (d *DepositAddressManager) GetDepositAddressesByExchange(exchName string) (map[string]string, error) {
return d.Store.GetDepositAddresses(exchName)
}
// Sync synchronises all deposit addresses
func (d *DepositAddressManager) Sync() {
result := Bot.GetExchangeCryptocurrencyDepositAddresses()
d.Store.Seed(result)
}