From 90fee94c76bf225081068edc2afa9a4b5f2fee0b Mon Sep 17 00:00:00 2001 From: cranktakular <43227667+cranktakular@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:36:05 +1000 Subject: [PATCH] currency: Make pairs.Add variadic (#1566) * Making Pairs(Add) Variatic * Slight improvements * Implementation overhaul * Improvements * Changing code which can use the variadic functionality * Fixing silliness * thing left over --- .../trackingcurrencies_test.go | 8 ++---- currency/pairs.go | 14 ++++++---- currency/pairs_test.go | 28 +++++++++++++++++-- .../sharedtestvalues/sharedtestvalues.go | 6 ++-- exchanges/subscription/subscription.go | 4 +-- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/backtester/funding/trackingcurrencies/trackingcurrencies_test.go b/backtester/funding/trackingcurrencies/trackingcurrencies_test.go index 75108915468..b3f18756275 100644 --- a/backtester/funding/trackingcurrencies/trackingcurrencies_test.go +++ b/backtester/funding/trackingcurrencies/trackingcurrencies_test.go @@ -53,12 +53,8 @@ func TestCreateUSDTrackingPairs(t *testing.T) { cp3 := currency.NewPair(currency.LTC, currency.BTC) exchB := exch.GetBase() eba := exchB.CurrencyPairs.Pairs[a] - eba.Available = eba.Available.Add(cp) - eba.Enabled = eba.Enabled.Add(cp) - eba.Available = eba.Available.Add(cp2) - eba.Enabled = eba.Enabled.Add(cp2) - eba.Available = eba.Available.Add(cp3) - eba.Enabled = eba.Enabled.Add(cp3) + eba.Available = eba.Available.Add(cp, cp2, cp3) + eba.Enabled = eba.Enabled.Add(cp, cp2, cp3) eba.AssetEnabled = convert.BoolPtr(true) err = em.Add(exch) diff --git a/currency/pairs.go b/currency/pairs.go index cd4d55c9b97..aea9f1683c1 100644 --- a/currency/pairs.go +++ b/currency/pairs.go @@ -211,12 +211,16 @@ func (p Pairs) Remove(pair Pair) (Pairs, error) { } // Add adds a specified pair to the list of pairs if it doesn't exist -func (p Pairs) Add(pair Pair) Pairs { - if p.Contains(pair, true) { - return p +func (p Pairs) Add(pairs ...Pair) Pairs { + merge := append(slices.Clone(p), pairs...) + var filterInt int + for x := len(p); x < len(merge); x++ { + if !merge[:len(p)+filterInt].Contains(merge[x], true) { + merge[len(p)+filterInt] = merge[x] + filterInt++ + } } - p = append(p, pair) - return p + return merge[:len(p)+filterInt] } // GetMatch returns either the pair that is equal including the reciprocal for diff --git a/currency/pairs_test.go b/currency/pairs_test.go index ee769ea4c2e..f305f846a12 100644 --- a/currency/pairs_test.go +++ b/currency/pairs_test.go @@ -267,19 +267,43 @@ func TestAdd(t *testing.T) { NewPair(LTC, USD), NewPair(LTC, USDT), } - // Test adding a new pair to the list of pairs p := NewPair(BTC, USDT) pairs = pairs.Add(p) if !pairs.Contains(p, true) || len(pairs) != 4 { t.Error("TestAdd unexpected result") } - // Now test adding a pair which already exists pairs = pairs.Add(p) if len(pairs) != 4 { t.Error("TestAdd unexpected result") } + // Test adding multiple pairs + pairs = pairs.Add(NewPair(BTC, LTC), NewPair(ETH, USD)) + if len(pairs) != 6 { + t.Error("TestAdd unexpected result") + } + // Test adding multiple duplicate pairs + pairs = pairs.Add(NewPair(ETH, USDT), NewPair(ETH, USDT)) + if len(pairs) != 7 { + t.Error("TestAdd unexpected result") + } + // Test whether the original pairs have been modified + pairsWithExtraBaggage := make(Pairs, 0, len(pairs)+3) + pairsWithExtraBaggage = append(pairsWithExtraBaggage, pairs...) + brain := NewPair(BRAIN, USD) + withBrain := pairsWithExtraBaggage.Add(NewPair(BTC, LTC), brain) + if len(pairs) != 7 { + t.Error("TestAdd unexpected result") + } + assert.Equal(t, brain, withBrain[len(withBrain)-1]) + badger := NewPair(BADGER, USD) + withBadger := pairsWithExtraBaggage.Add(NewPair(BTC, LTC), badger) + if len(pairs) != 7 { + t.Error("TestAdd unexpected result") + } + assert.Equal(t, badger, withBadger[len(withBadger)-1]) + assert.Equal(t, brain, withBrain[len(withBrain)-1]) } func TestContains(t *testing.T) { diff --git a/exchanges/sharedtestvalues/sharedtestvalues.go b/exchanges/sharedtestvalues/sharedtestvalues.go index 750fbae67dc..8f17fd82cc6 100644 --- a/exchanges/sharedtestvalues/sharedtestvalues.go +++ b/exchanges/sharedtestvalues/sharedtestvalues.go @@ -168,10 +168,8 @@ func SetupCurrencyPairsForExchangeAsset(t *testing.T, exch exchange.IBotExchange t.Fatal(err) } epLen := len(enabledPairs) - for i := range cp { - availPairs = availPairs.Add(cp[i]) - enabledPairs = enabledPairs.Add(cp[i]) - } + availPairs = availPairs.Add(cp...) + enabledPairs = enabledPairs.Add(cp...) if len(availPairs) != apLen { err = b.CurrencyPairs.StorePairs(a, availPairs, false) if err != nil { diff --git a/exchanges/subscription/subscription.go b/exchanges/subscription/subscription.go index 084be4cf786..695eb4203b0 100644 --- a/exchanges/subscription/subscription.go +++ b/exchanges/subscription/subscription.go @@ -159,8 +159,6 @@ func (s *Subscription) AddPairs(pairs ...currency.Pair) { return } s.m.Lock() - for _, p := range pairs { - s.Pairs = s.Pairs.Add(p) - } + s.Pairs = s.Pairs.Add(pairs...) s.m.Unlock() }