mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-08 23:16:54 +00:00
exchanges/engine: Add multichain deposit/withdrawal support (#794)
* Add exchange multichain support * Start tidying up * Add multichain transfer support for Bitfinex and fix poloniex bug * Add Coinbene multichain support * Start adjusting the deposit address manager * Fix deposit tests and further enhancements * Cleanup * Add bypass flag, expand tests plus error coverage for Huobi Adjust helpers * Address nitterinos * BFX wd changes * Address nitterinos * Minor fixes rebasing on master * Fix BFX acceptableMethods test * Add some TO-DOs for 2 tests WRT races * Fix acceptableMethods test round 2 * Address nitterinos
This commit is contained in:
@@ -7,55 +7,86 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
|
||||
)
|
||||
|
||||
// vars related to the deposit address helpers
|
||||
var (
|
||||
ErrDepositAddressStoreIsNil = errors.New("deposit address store is nil")
|
||||
ErrDepositAddressNotFound = errors.New("deposit address does not exist")
|
||||
ErrDepositAddressStoreIsNil = errors.New("deposit address store is nil")
|
||||
ErrDepositAddressNotFound = errors.New("deposit address does not exist")
|
||||
errDepositAddressChainNotFound = errors.New("deposit address for specified chain not found")
|
||||
errNoDepositAddressesRetrieved = errors.New("no deposit addresses retrieved")
|
||||
)
|
||||
|
||||
// DepositAddressManager manages the exchange deposit address store
|
||||
type DepositAddressManager struct {
|
||||
m sync.Mutex
|
||||
store map[string]map[string]string
|
||||
m sync.RWMutex
|
||||
store map[string]map[string][]deposit.Address
|
||||
}
|
||||
|
||||
// IsSynced returns whether or not the deposit address store has synced its data
|
||||
func (m *DepositAddressManager) IsSynced() bool {
|
||||
if m.store == nil {
|
||||
return false
|
||||
}
|
||||
m.m.RLock()
|
||||
defer m.m.RUnlock()
|
||||
return len(m.store) > 0
|
||||
}
|
||||
|
||||
// SetupDepositAddressManager returns a DepositAddressManager
|
||||
func SetupDepositAddressManager() *DepositAddressManager {
|
||||
return &DepositAddressManager{
|
||||
store: make(map[string]map[string]string),
|
||||
store: make(map[string]map[string][]deposit.Address),
|
||||
}
|
||||
}
|
||||
|
||||
// GetDepositAddressByExchangeAndCurrency returns a deposit address for the specified exchange and cryptocurrency
|
||||
// if it exists
|
||||
func (m *DepositAddressManager) GetDepositAddressByExchangeAndCurrency(exchName string, currencyItem currency.Code) (string, error) {
|
||||
m.m.Lock()
|
||||
defer m.m.Unlock()
|
||||
func (m *DepositAddressManager) GetDepositAddressByExchangeAndCurrency(exchName, chain string, currencyItem currency.Code) (deposit.Address, error) {
|
||||
m.m.RLock()
|
||||
defer m.m.RUnlock()
|
||||
|
||||
if len(m.store) == 0 {
|
||||
return "", ErrDepositAddressStoreIsNil
|
||||
return deposit.Address{}, ErrDepositAddressStoreIsNil
|
||||
}
|
||||
|
||||
r, ok := m.store[strings.ToUpper(exchName)]
|
||||
if !ok {
|
||||
return "", ErrExchangeNotFound
|
||||
return deposit.Address{}, ErrExchangeNotFound
|
||||
}
|
||||
|
||||
addr, ok := r[strings.ToUpper(currencyItem.String())]
|
||||
if !ok {
|
||||
return "", ErrDepositAddressNotFound
|
||||
return deposit.Address{}, ErrDepositAddressNotFound
|
||||
}
|
||||
|
||||
return addr, nil
|
||||
if len(addr) == 0 {
|
||||
return deposit.Address{}, errNoDepositAddressesRetrieved
|
||||
}
|
||||
|
||||
if chain != "" {
|
||||
for x := range addr {
|
||||
if strings.EqualFold(addr[x].Chain, chain) {
|
||||
return addr[x], nil
|
||||
}
|
||||
}
|
||||
return deposit.Address{}, errDepositAddressChainNotFound
|
||||
}
|
||||
|
||||
for x := range addr {
|
||||
if strings.EqualFold(addr[x].Chain, currencyItem.String()) {
|
||||
return addr[x], nil
|
||||
}
|
||||
}
|
||||
return addr[0], nil
|
||||
}
|
||||
|
||||
// GetDepositAddressesByExchange returns a list of cryptocurrency addresses for the specified
|
||||
// exchange if they exist
|
||||
func (m *DepositAddressManager) GetDepositAddressesByExchange(exchName string) (map[string]string, error) {
|
||||
m.m.Lock()
|
||||
defer m.m.Unlock()
|
||||
func (m *DepositAddressManager) GetDepositAddressesByExchange(exchName string) (map[string][]deposit.Address, error) {
|
||||
m.m.RLock()
|
||||
defer m.m.RUnlock()
|
||||
|
||||
if len(m.store) == 0 {
|
||||
return nil, ErrDepositAddressStoreIsNil
|
||||
@@ -66,11 +97,15 @@ func (m *DepositAddressManager) GetDepositAddressesByExchange(exchName string) (
|
||||
return nil, ErrDepositAddressNotFound
|
||||
}
|
||||
|
||||
return r, nil
|
||||
cpy := make(map[string][]deposit.Address, len(r))
|
||||
for k, v := range r {
|
||||
cpy[k] = v
|
||||
}
|
||||
return cpy, nil
|
||||
}
|
||||
|
||||
// Sync synchronises all deposit addresses
|
||||
func (m *DepositAddressManager) Sync(addresses map[string]map[string]string) error {
|
||||
func (m *DepositAddressManager) Sync(addresses map[string]map[string][]deposit.Address) error {
|
||||
if m == nil {
|
||||
return fmt.Errorf("deposit address manager %w", ErrNilSubsystem)
|
||||
}
|
||||
@@ -81,7 +116,7 @@ func (m *DepositAddressManager) Sync(addresses map[string]map[string]string) err
|
||||
}
|
||||
|
||||
for k, v := range addresses {
|
||||
r := make(map[string]string)
|
||||
r := make(map[string][]deposit.Address)
|
||||
for w, x := range v {
|
||||
r[strings.ToUpper(w)] = x
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user