Files
gocryptotrader/currency/manager.go
Ryan O'Hara-Reid 11da520dc8 Currency: Add additional functionality, refactor and improvements (#881)
* currency: Add method to derive pair

* currency: Add method to lower entire charset but used the slice copy and returned that. This will change the original, just gotta see if this is an issue, but the slice usually goes out of scope anyway.

* currency/pairs: add filter method

* currency: add function to derive select currencies from currency pairs

* currency/engine: slight adjustments

* currency: fix linter issue also shift burden of proof to caller instead of repair, more performant.

* currency: more linter

* pairs: optimize; reduce allocs/op and B/op

* currency: Add in function 'NewPairsFromString' for testing purposes

* currency: don't suppress error

* currency: stop panic on empty currency code

* currency: Add helper method to match currencies between exchanges

* currency: fixed my bad spelling

* currency: Implement stable coin checks, refactored base code methods, optimized upper and lower case strings for currency code/pairs

* currency: add pairs method to derive stable coins from internal list.

* Currency: Cleanup, fix tests.

* engine/exchanges/currency: fix whoops

* Currency: force govet no copy on Item datatype

* Currency: fix naughty linter issues

* exchange: revert change

* currency/config: fix config upgrade mistake

* currency: re-implement currency sub-systems

* *RetrieveConfigCurrencyPairs removed
*CheckCurrencyConfigValues to only provide warnings, add additional support when, disable when support is lost or not available and set default values.
*Drop Cryptocurrencies from configuration as this is not needed.
*Drop REST Poll delay field as this was unused.
*Update default values for currencyFileUpdateDuration & foreignExchangeUpdateDuration.
*Allow Role to be marshalled for file type.
*Refactor RunUpdater to verify and check config values and set default running foreign exchange provider.

* currency: cleanup

* currency: change match -> equal for comparison which is more of a standard and little easier to find

* currency: address nits

* currency: fix whoops

* currency: Add some more pairs methods

* currency: linter issues

* currency: RM unused field

* currency: rm verbose

* currency: fix word

* currency: gocritic

* currency: fix another whoopsie

* example_config: default to show log system name

* Currency: Force all support packages to use Equal method for comparison as there is a small comparison bug when checking upper and lower casing, this has a more of a pronounced impact between exchanges and client instances of currency generation

* currency: fix log name

* ordermanager: fix potential panic

* currency: small optim.

* engine: display correct bool and force shutdown

* currency: add function and fix regression
* Change ConvertCurrency -> ConvertFiat to be more precise
* ADD GetForeignExchangeRate to get specific exchange rate for fiat pair
* Fix currency display and formatting regression and tied in with config.Currency fields

* engine: fix tests

* currency: return the amount when no conversion needs to take place

* currency: reduce method name

* currency: Address nits glorious nits

* currency: fix linter

* currency: addr nits

* currency: check underlying role in test

* gct: change to EMPTYCODE and EMPTYPAIR across codebase

* currency: fix nits

* currency: this fixes test race but this issue has not been resolved. Please see: https://trello.com/c/54eizOIo/143-currency-package-upgrades

* currency: Add temp dir for testing

* Update engine/engine.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* documentation: update and regen

* currency: Address niterinos

* currency: Add test case for config upgrade when falling over to exchange rate host as default from exchangeRates provider

* currency: addr nits

* currency: fix whoops

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
2022-02-17 16:24:57 +11:00

227 lines
5.1 KiB
Go

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")
// errAssetNotEnabled defines an error for the pairs management system
// that declares the asset is not enabled.
errAssetNotEnabled = errors.New("asset not enabled")
// ErrAssetIsNil is an error when the asset has not been populated by the
// configuration
ErrAssetIsNil = errors.New("asset is nil")
)
// 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 fmt.Errorf("%s %w", a, ErrAssetIsNil)
}
if !*c.AssetEnabled {
return fmt.Errorf("%s %w", a, errAssetNotEnabled)
}
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
}