Skip to content

Commit

Permalink
rm forced bool, add tests and fix casing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan O'Hara-Reid committed Feb 16, 2024
1 parent dca60a8 commit 74fbfdb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
8 changes: 7 additions & 1 deletion currency/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,19 @@ func (b *BaseCodes) Register(c string, newRole Role) Code {

var format bool
// Digits fool upper and lower casing. So find first letter and check case.
var letterfound bool
for x := range c {
if unicode.IsLetter(rune(c[x])) {
if letterfound = unicode.IsLetter(rune(c[x])); letterfound {
format = unicode.IsUpper(rune(c[x]))
break
}
}

// If no letter found, default to upper case
if !format && !letterfound {
format = true
}

// Force upper string storage and matching
c = strings.ToUpper(c)

Expand Down
2 changes: 1 addition & 1 deletion currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (p Pairs) FindDifferences(incoming Pairs, pairFmt PairFormat) (PairDifferen
return PairDifference{
New: newPairs,
Remove: removedPairs,
FormatDifference: p.HasFormatDifference(pairFmt),
FormatDifference: len(p) > 0 && len(incoming) > 0 && p.HasFormatDifference(pairFmt),
}, nil
}

Expand Down
51 changes: 28 additions & 23 deletions exchanges/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (
errExchangeIsNil = errors.New("exchange is nil")
errBatchSizeZero = errors.New("batch size cannot be 0")
errExchangeMismatch = errors.New("exchange instance does not match base")
errSetupNotCalled = errors.New("setup not called")
)

// SetRequester sets the instance of the requester
Expand Down Expand Up @@ -698,7 +699,11 @@ func (b *Base) EnsureOnePairEnabled() error {

// UpdatePairs updates the exchange currency pairs for either enabledPairs or
// availablePairs
func (b *Base) UpdatePairs(incoming currency.Pairs, a asset.Item, enabled, force bool) error {
func (b *Base) UpdatePairs(incoming currency.Pairs, a asset.Item, enabled bool) error {
if b.Config == nil || b.Config.CurrencyPairs == nil {
return fmt.Errorf("cannot update pairs %w", errSetupNotCalled)
}

pFmt, err := b.GetPairFormat(a, false)
if err != nil {
return err
Expand All @@ -719,38 +724,35 @@ func (b *Base) UpdatePairs(incoming currency.Pairs, a asset.Item, enabled, force
return err
}

if force || len(diff.New) != 0 || len(diff.Remove) != 0 || diff.FormatDifference {
if len(diff.New) != 0 || len(diff.Remove) != 0 || diff.FormatDifference {
var updateType string
if enabled {
updateType = "enabled"
} else {
updateType = "available"
}

if force {
log.Debugf(log.ExchangeSys,
"%s forced update of %s [%v] pairs.",
if diff.FormatDifference {
log.Debugf(log.ExchangeSys, "%s Updating %s pairs [%v] - Format difference detected.\n",
b.Name,
updateType,
strings.ToUpper(a.String()))
} else {
if len(diff.New) > 0 {
log.Debugf(log.ExchangeSys,
"%s Updating %s pairs [%v] - Added: %s.\n",
b.Name,
updateType,
strings.ToUpper(a.String()),
diff.New)
}
if len(diff.Remove) > 0 {
log.Debugf(log.ExchangeSys,
"%s Updating %s pairs [%v] - Removed: %s.\n",
b.Name,
updateType,
strings.ToUpper(a.String()),
diff.Remove)
}
}
if len(diff.New) > 0 {
log.Debugf(log.ExchangeSys, "%s Updating %s pairs [%v] - Added: %s.\n",
b.Name,
updateType,
strings.ToUpper(a.String()),
diff.New)
}
if len(diff.Remove) > 0 {
log.Debugf(log.ExchangeSys, "%s Updating %s pairs [%v] - Removed: %s.\n",
b.Name,
updateType,
strings.ToUpper(a.String()),
diff.Remove)
}

err = b.Config.CurrencyPairs.StorePairs(a, incoming, enabled)
if err != nil {
return err
Expand Down Expand Up @@ -1983,12 +1985,15 @@ func (b *Base) GetDefaultConfig(ctx context.Context, instance LimitedScope) (*co
// the exchanges config.
func (h *Base) UpdateTradablePairs(ctx context.Context, instance LimitedScope) error {

Check warning on line 1986 in exchanges/exchange.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name h should be consistent with previous receiver name b for Base (revive)
assets := h.GetAssetTypes(false)
if len(assets) == 0 {
return fmt.Errorf("%s %w: no specific asset types are set", h.GetName(), errSetDefaultsNotCalled)
}
for x := range assets {
pairs, err := instance.FetchTradablePairs(ctx, assets[x])
if err != nil {
return err
}
err = h.UpdatePairs(pairs, assets[x], false, true)
err = h.UpdatePairs(pairs, assets[x], false)
if err != nil {
return err
}
Expand Down
81 changes: 64 additions & 17 deletions exchanges/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,18 +1372,18 @@ func TestUpdatePairs(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true)
if err != nil {
t.Errorf("TestUpdatePairs error: %s", err)
}

err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false)
if err != nil {
t.Errorf("TestUpdatePairs error: %s", err)
}

// Test updating the same new products, diff should be 0
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true)
if err != nil {
t.Errorf("TestUpdatePairs error: %s", err)
}
Expand All @@ -1394,7 +1394,7 @@ func TestUpdatePairs(t *testing.T) {
t.Fatal(err)
}

err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true, true)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, true)
if err != nil {
t.Errorf("TestUpdatePairs error: %s", err)
}
Expand All @@ -1408,13 +1408,13 @@ func TestUpdatePairs(t *testing.T) {
t.Fatal(err)
}
UAC.Name = defaultTestExchange
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false)
if err != nil {
t.Errorf("Exchange UpdatePairs() error: %s", err)
}

// Test updating the same new products, diff should be 0
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false)
if err != nil {
t.Errorf("Exchange UpdatePairs() error: %s", err)
}
Expand All @@ -1424,7 +1424,7 @@ func TestUpdatePairs(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false, true)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false)
if err != nil {
t.Errorf("Forced Exchange UpdatePairs() error: %s", err)
}
Expand All @@ -1434,7 +1434,7 @@ func TestUpdatePairs(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false, false)
err = UAC.UpdatePairs(exchangeProducts, asset.Spot, false)
if err != nil {
t.Errorf("Exchange UpdatePairs() error: %s", err)
}
Expand All @@ -1445,24 +1445,24 @@ func TestUpdatePairs(t *testing.T) {
t.Fatal(err)
}
pairs := currency.Pairs{currency.EMPTYPAIR, p}
err = UAC.UpdatePairs(pairs, asset.Spot, true, true)
err = UAC.UpdatePairs(pairs, asset.Spot, true)
if !errors.Is(err, currency.ErrCurrencyPairEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, currency.ErrCurrencyPairEmpty)
}

pairs = currency.Pairs{p, p}
err = UAC.UpdatePairs(pairs, asset.Spot, false, true)
err = UAC.UpdatePairs(pairs, asset.Spot, false)
if !errors.Is(err, currency.ErrPairDuplication) {
t.Fatalf("received: '%v' but expected: '%v'", err, currency.ErrPairDuplication)
}

pairs = currency.Pairs{p}
err = UAC.UpdatePairs(pairs, asset.Spot, false, true)
err = UAC.UpdatePairs(pairs, asset.Spot, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

err = UAC.UpdatePairs(pairs, asset.Spot, true, true)
err = UAC.UpdatePairs(pairs, asset.Spot, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
Expand All @@ -1486,7 +1486,7 @@ func TestUpdatePairs(t *testing.T) {
currency.NewPair(currency.LTC, currency.USD),
currency.NewPair(currency.LTC, currency.USDT),
}
err = UAC.UpdatePairs(pairs, asset.Spot, true, true)
err = UAC.UpdatePairs(pairs, asset.Spot, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
Expand All @@ -1497,7 +1497,7 @@ func TestUpdatePairs(t *testing.T) {
currency.NewPair(currency.LARIX, currency.USD),
currency.NewPair(currency.LTC, currency.USDT),
}
err = UAC.UpdatePairs(pairs, asset.Spot, false, true)
err = UAC.UpdatePairs(pairs, asset.Spot, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
Expand Down Expand Up @@ -1533,7 +1533,7 @@ func TestUpdatePairs(t *testing.T) {
unintentionalInput,
}

err = UAC.UpdatePairs(pairs, asset.Spot, true, true)
err = UAC.UpdatePairs(pairs, asset.Spot, true)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
Expand All @@ -1546,7 +1546,7 @@ func TestUpdatePairs(t *testing.T) {
currency.NewPair(currency.LINK, currency.USD),
}

err = UAC.UpdatePairs(pairs, asset.Spot, false, true)
err = UAC.UpdatePairs(pairs, asset.Spot, false)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
Expand Down Expand Up @@ -3245,10 +3245,31 @@ func (f *FakeBase) SetDefaults() {
f.Name = "test"
f.Requester, _ = request.New("test", common.NewHTTPClientWithTimeout(time.Second))
f.Features.Supports.RESTCapabilities.AutoPairUpdates = true
requestFmt := &currency.PairFormat{Delimiter: currency.DashDelimiter, Uppercase: true}
configFmt := &currency.PairFormat{Delimiter: currency.DashDelimiter, Uppercase: true}
_ = f.SetGlobalPairsManager(requestFmt, configFmt, asset.Spot)
}

func (f *FakeBase) FetchTradablePairs(context.Context, asset.Item) (currency.Pairs, error) {
return nil, nil
return currency.Pairs{currency.NewPair(currency.BTC, currency.USDT)}, nil
}

func (f *FakeBase) Setup(exch *config.Exchange) error {
// TODO: Abstract from wrapper.
err := exch.Validate()
if err != nil {
return err
}
if !exch.Enabled {
f.SetEnabled(false)
return nil
}
err = f.SetupDefaults(exch)
if err != nil {
return err
}

return nil
}

func TestGetCachedOpenInterest(t *testing.T) {
Expand Down Expand Up @@ -3365,3 +3386,29 @@ func TestGetDefaultConfig(t *testing.T) {
assert.Equal(t, "test", c.Name)
assert.Equal(t, cpy, exch.Requester)
}

func TestUpdateTradablePairs(t *testing.T) {
t.Parallel()

exch := &FakeBase{}
err := exch.UpdateTradablePairs(context.Background(), exch)
assert.ErrorIs(t, err, errSetDefaultsNotCalled)

exch.SetDefaults()

err = exch.UpdateTradablePairs(context.Background(), exch)
assert.ErrorIs(t, err, errSetupNotCalled)

exch.Setup(&config.Exchange{Name: "test", Enabled: true})

Check failure on line 3402 in exchanges/exchange_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `exch.Setup` is not checked (errcheck)

err = exch.UpdateTradablePairs(context.Background(), exch)
require.NoError(t, err)

pair, err := exch.GetAvailablePairs(asset.Spot)
require.NoError(t, err)

require.Len(t, pair, 1)
if !pair.Contains(currency.NewPair(currency.BTC, currency.USDT), true) {
t.Fatal("pair not found")
}
}

0 comments on commit 74fbfdb

Please sign in to comment.