Skip to content

Commit

Permalink
Currencies: Remove ContainsAll(p) error len(p)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 10, 2024
1 parent b41fe27 commit c465d8d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 109 deletions.
54 changes: 23 additions & 31 deletions currency/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (

// Public errors
var (
ErrAssetAlreadyEnabled = errors.New("asset already enabled")
ErrAssetIsNil = errors.New("asset is nil")
ErrAssetNotFound = errors.New("asset type not found in pair store")
ErrPairAlreadyEnabled = errors.New("pair already enabled")
ErrPairFormatIsNil = errors.New("pair format is nil")
ErrPairManagerNotInitialised = errors.New("pair manager not initialised")
ErrPairNotContainedInAvailablePairs = errors.New("pair not contained in available pairs")
ErrPairNotEnabled = errors.New("pair not enabled")
ErrPairNotFound = errors.New("pair not found")
ErrSymbolStringEmpty = errors.New("symbol string is empty")
ErrAssetAlreadyEnabled = errors.New("asset already enabled")
ErrAssetIsNil = errors.New("asset is nil")
ErrAssetNotFound = errors.New("asset type not found in pair store")
ErrPairAlreadyEnabled = errors.New("pair already enabled")
ErrPairFormatIsNil = errors.New("pair format is nil")
ErrPairManagerNotInitialised = errors.New("pair manager not initialised")
ErrPairNotAvailable = errors.New("pair not available")
ErrPairNotEnabled = errors.New("pair not enabled")
ErrPairNotFound = errors.New("pair not found")
ErrSymbolStringEmpty = errors.New("symbol string is empty")
)

var (
Expand Down Expand Up @@ -112,8 +112,7 @@ func (p *PairsManager) Delete(a asset.Item) {
p.mutex.Unlock()
}

// GetPairs gets a list of stored pairs based on the asset type and whether
// they're enabled or not
// GetPairs gets the list of pairs based on the asset type and whether they're enabled or not
func (p *PairsManager) GetPairs(a asset.Item, enabled bool) (Pairs, error) {
if !a.IsValid() {
return nil, fmt.Errorf("%s %w", a, asset.ErrNotSupported)
Expand All @@ -126,24 +125,10 @@ func (p *PairsManager) GetPairs(a asset.Item, enabled bool) (Pairs, error) {
return nil, nil
}

if !enabled {
return slices.Clone(pairStore.Available), nil
}

lenCheck := len(pairStore.Enabled)
if lenCheck == 0 {
return nil, nil
}

// NOTE: enabledPairs is declared before the next check for comparison
// reasons within exchange update pairs functionality.
enabledPairs := slices.Clone(pairStore.Enabled)

err := pairStore.Available.ContainsAll(pairStore.Enabled, true)
if err != nil {
err = fmt.Errorf("%w of asset type %s", err, a)
if enabled {
return slices.Clone(pairStore.Enabled), nil
}
return enabledPairs, err
return slices.Clone(pairStore.Available), nil
}

// StoreFormat stores a new format for request or config format.
Expand Down Expand Up @@ -205,8 +190,8 @@ func (p *PairsManager) GetFormat(a asset.Item, request bool) (PairFormat, error)
return *pFmt, nil
}

// StorePairs stores a list of pairs based on the asset type and whether
// they're enabled or not
// StorePairs stores a list of pairs based on the asset type and whether they're enabled or not
// All enabled pairs must be stored as available already
func (p *PairsManager) StorePairs(a asset.Item, pairs Pairs, enabled bool) error {
if !a.IsValid() {
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
Expand All @@ -226,8 +211,15 @@ func (p *PairsManager) StorePairs(a asset.Item, pairs Pairs, enabled bool) error
}

if enabled {
// All new enabled pairs must be in Available already
if err := pairStore.Available.ContainsAll(pairs, true); err != nil {
return fmt.Errorf("%w: %w for asset %s", ErrPairNotAvailable, err, a)
}
pairStore.Enabled = slices.Clone(pairs)
} else {
if err := pairs.ContainsAll(pairStore.Enabled, true); err != nil {
return fmt.Errorf("%w: %w for asset %s", ErrPairNotAvailable, err, a)
}
pairStore.Available = slices.Clone(pairs)
p.reindex()
}
Expand Down
37 changes: 9 additions & 28 deletions currency/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,40 +225,21 @@ func TestGetPairs(t *testing.T) {

p.Pairs = nil
pairs, err := p.GetPairs(asset.Spot, true)
if err != nil {
t.Fatal(err)
}

if pairs != nil {
t.Fatal("pairs shouldn't be populated")
}
require.NoError(t, err)
require.Empty(t, pairs)

p = initTest(t)
pairs, err = p.GetPairs(asset.Spot, true)
if err != nil {
t.Fatal(err)
}
if pairs == nil {
t.Fatal("pairs should be populated")
}
require.NoError(t, err)
require.NotNil(t, pairs)

pairs, err = p.GetPairs(asset.Empty, true)
if !errors.Is(err, asset.ErrNotSupported) {
t.Fatalf("received: '%v' but expected: '%v'", err, asset.ErrNotSupported)
}
assert.ErrorIs(t, err, asset.ErrNotSupported)
require.Empty(t, pairs)

if pairs != nil {
t.Fatal("pairs shouldn't be populated")
}

superfluous := NewPair(DASH, USDT)
newPairs := p.Pairs[asset.Spot].Enabled.Add(superfluous)
p.Pairs[asset.Spot].Enabled = newPairs

_, err = p.GetPairs(asset.Spot, true)
if err == nil {
t.Fatal("error cannot be nil")
}
pairs, err = p.GetPairs(asset.Margin, true)
require.NoError(t, err, "GetPairs must not error on valid but missing asset type")
require.Empty(t, pairs, "GetPairs must return an empty pointer on valid but missing asset type")
}

func TestStoreFormat(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ func (p Pairs) Contains(check Pair, exact bool) bool {

// ContainsAll checks to see if all pairs supplied are contained within the original pairs list
func (p Pairs) ContainsAll(check Pairs, exact bool) error {
if len(check) == 0 {
return ErrCurrencyPairsEmpty
}

comparative := slices.Clone(p)
list:
for x := range check {
Expand All @@ -144,7 +140,7 @@ list:
return fmt.Errorf("%s %w", check[x], ErrPairDuplication)
}

return fmt.Errorf("%s %w", check[x], ErrPairNotContainedInAvailablePairs)
return fmt.Errorf("%s %w", check[x], ErrPairNotFound)
}
return nil
}
Expand Down
46 changes: 8 additions & 38 deletions currency/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,40 +272,13 @@ func TestContainsAll(t *testing.T) {
NewPair(USD, ZRX),
}

err := pairs.ContainsAll(nil, true)
if !errors.Is(err, ErrCurrencyPairsEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrCurrencyPairsEmpty)
}

err = pairs.ContainsAll(Pairs{NewPair(BTC, USD)}, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

err = pairs.ContainsAll(Pairs{NewPair(USD, BTC)}, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

err = pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, false)
if !errors.Is(err, ErrPairNotContainedInAvailablePairs) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotContainedInAvailablePairs)
}

err = pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, true)
if !errors.Is(err, ErrPairNotContainedInAvailablePairs) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairNotContainedInAvailablePairs)
}

err = pairs.ContainsAll(pairs, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

err = pairs.ContainsAll(pairs, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
assert.ErrorIs(t, pairs.ContainsAll(nil, true), ErrCurrencyPairsEmpty)
assert.NoError(t, pairs.ContainsAll(Pairs{NewPair(BTC, USD)}, true))
assert.NoError(t, pairs.ContainsAll(Pairs{NewPair(USD, BTC)}, false))
assert.ErrorIs(t, pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, false), ErrPairNotFound)
assert.ErrorIs(t, pairs.ContainsAll(Pairs{NewPair(XRP, BTC)}, true), ErrPairNotFound)
assert.NoError(t, pairs.ContainsAll(pairs, true))
assert.NoError(t, pairs.ContainsAll(pairs, false))

var duplication = Pairs{
NewPair(BTC, USD),
Expand All @@ -314,10 +287,7 @@ func TestContainsAll(t *testing.T) {
NewPair(USD, ZRX),
}

err = pairs.ContainsAll(duplication, false)
if !errors.Is(err, ErrPairDuplication) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrPairDuplication)
}
assert.ErrorIs(t, pairs.ContainsAll(duplication, false), ErrPairDuplication)
}

func TestDeriveFrom(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions exchanges/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,11 @@ func (b *Base) UpdatePairs(incoming currency.Pairs, a asset.Item, enabled, force
// asset.

enabledPairs, err := b.CurrencyPairs.GetPairs(a, true)
if err != nil &&
!errors.Is(err, currency.ErrPairNotContainedInAvailablePairs) &&
!errors.Is(err, currency.ErrPairDuplication) {
if err != nil {
return err
}

if err == nil && !enabledPairs.HasFormatDifference(pFmt) {
if !enabledPairs.HasFormatDifference(pFmt) {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion exchanges/kraken/kraken_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ func (k *Kraken) GetLatestFundingRates(ctx context.Context, r *fundingrate.Lates
if ok, err := k.CurrencyPairs.IsPairAvailable(r.Pair, r.Asset); err != nil {
return nil, err
} else if !ok {
return nil, currency.ErrPairNotContainedInAvailablePairs
return nil, currency.ErrPairNotAvailable
}
}

Expand Down
3 changes: 2 additions & 1 deletion exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)

Expand Down Expand Up @@ -121,7 +122,7 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It
if len(s.Pairs) != 0 {
for a, pairs := range subCtx.AssetPairs {
if err := pairs.ContainsAll(s.Pairs, true); err != nil { //nolint:govet // Shadow, or gocritic will complain sloppyReassign
return nil, err
return nil, fmt.Errorf("%w: %w", currency.ErrPairNotEnabled, err)
}
subCtx.AssetPairs[a] = s.Pairs
}
Expand Down
3 changes: 2 additions & 1 deletion exchanges/subscription/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func TestExpandTemplates(t *testing.T) {
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")

_, err = List{{Channel: "single-channel", Asset: asset.Spot, Pairs: currency.Pairs{currency.NewPairWithDelimiter("NOPE", "POPE", "🐰")}}}.ExpandTemplates(e)
assert.ErrorIs(t, err, currency.ErrPairNotContainedInAvailablePairs, "Should error correctly when pair not available")
assert.ErrorIs(t, err, currency.ErrPairNotEnabled, "Should error correctly when pair not enabled")
assert.ErrorIs(t, err, currency.ErrPairNotFound, "Should error correctly when pair not enabled")

e.tpl = "errors.tmpl"

Expand Down

0 comments on commit c465d8d

Please sign in to comment.