Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make a Denoms method on sdk.Coins #12627

Merged
merged 5 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration.
* (query) [#12253](https://github.com/cosmos/cosmos-sdk/pull/12253) Add `GenericFilteredPaginate` to the `query` package to improve UX.
* (telemetry) [#12405](https://github.com/cosmos/cosmos-sdk/pull/12405) Add _query_ calls metric to telemetry.
* (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins.
Copy link
Contributor Author

@ratankaliani ratankaliani Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linked! my bad


### Improvements

Expand Down
9 changes: 9 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ func (coins Coins) IsValid() bool {
return coins.Validate() == nil
}

// Denoms returns all denoms associated with a Coins object
func (coins Coins) Denoms() []string {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
res := make([]string, len(coins))
for i, coin := range coins {
res[i] = coin.Denom
}
return res
}

// Add adds two sets of coins.
//
// e.g.
Expand Down
29 changes: 29 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ func (s *coinTestSuite) TestCustomValidation() {
sdk.SetCoinDenomRegex(sdk.DefaultCoinDenomRegex)
}

func (s *coinTestSuite) TestCoinsDenoms() {

cases := []struct {
coins sdk.Coins
testOutput []string
expectPass bool
}{
{sdk.NewCoins(sdk.Coin{"ATOM", sdk.NewInt(1)}, sdk.Coin{"JUNO", sdk.NewInt(1)}, sdk.Coin{"OSMO", sdk.NewInt(1)}, sdk.Coin{"RAT", sdk.NewInt(1)}), []string{"ATOM", "JUNO", "OSMO", "RAT"}, true},
{sdk.NewCoins(sdk.Coin{"ATOM", sdk.NewInt(1)}, sdk.Coin{"JUNO", sdk.NewInt(1)}), []string{"ATOM"}, false},
}

for i, tc := range cases {
expectedOutput := tc.coins.Denoms()
count := 0
if len(expectedOutput) == len(tc.testOutput) {
for k := range tc.testOutput {
if tc.testOutput[k] != expectedOutput[k] {
count += 1
break
}
}
} else {
count += 1
}
s.Require().Equal(count == 0, tc.expectPass, "unexpected result for coins.Denoms, tc #%d", i)
}

}

func (s *coinTestSuite) TestAddCoin() {
cases := []struct {
inputOne sdk.Coin
Expand Down