Skip to content

Commit

Permalink
currency: Make pairs.Add variadic (#1566)
Browse files Browse the repository at this point in the history
* Making Pairs(Add) Variatic

* Slight improvements

* Implementation overhaul

* Improvements

* Changing code which can use the variadic functionality

* Fixing silliness

* thing left over
  • Loading branch information
cranktakular authored Jul 12, 2024
1 parent c601575 commit 90fee94
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions currency/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions exchanges/sharedtestvalues/sharedtestvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions exchanges/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 90fee94

Please sign in to comment.