diff --git a/.pending/bugfixes/sdk/4303-Fix-NewCoins-un b/.pending/bugfixes/sdk/4303-Fix-NewCoins-un new file mode 100644 index 000000000..c2b8ec6b1 --- /dev/null +++ b/.pending/bugfixes/sdk/4303-Fix-NewCoins-un @@ -0,0 +1 @@ +#4303 Fix NewCoins() underlying function for duplicate coins detection. diff --git a/types/coin.go b/types/coin.go index 4cc1312f4..9de3bda58 100644 --- a/types/coin.go +++ b/types/coin.go @@ -646,11 +646,12 @@ func findDup(coins Coins) int { return -1 } - prevDenom := coins[0] + prevDenom := coins[0].Denom for i := 1; i < len(coins); i++ { - if coins[i] == prevDenom { + if coins[i].Denom == prevDenom { return i } + prevDenom = coins[i].Denom } return -1 diff --git a/types/coin_test.go b/types/coin_test.go index 0a81ce7b9..ee4c2de64 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -593,3 +593,31 @@ func TestCoinsIsAnyGT(t *testing.T) { require.False(t, Coins{twoBtc, twoAtom, threeEth}.IsAnyGT(Coins{fiveAtom, sixEth})) require.False(t, Coins{twoAtom, sixEth}.IsAnyGT(Coins{twoBtc, fiveAtom})) } + +func TestFindDup(t *testing.T) { + abc := NewInt64Coin("abc", 10) + def := NewInt64Coin("def", 10) + ghi := NewInt64Coin("ghi", 10) + + type args struct { + coins Coins + } + tests := []struct { + name string + args args + want int + }{ + {"empty", args{NewCoins()}, -1}, + {"one coin", args{NewCoins(NewInt64Coin("xyz", 10))}, -1}, + {"no dups", args{Coins{abc, def, ghi}}, -1}, + {"dup at first position", args{Coins{abc, abc, def}}, 1}, + {"dup after first position", args{Coins{abc, def, def}}, 2}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := findDup(tt.args.coins); got != tt.want { + t.Errorf("findDup() = %v, want %v", got, tt.want) + } + }) + } +}