diff --git a/CHANGELOG.md b/CHANGELOG.md index 8edd28898dbe..5d7838157c84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Features +* (types) [#19281](https://github.com/cosmos/cosmos-sdk/pull/19281) Added a new method, `IsGT`, for `types.Coin`. This method is used to check if a `types.Coin` is greater than another `types.Coin`. * (client) [#18557](https://github.com/cosmos/cosmos-sdk/pull/18557) Add `--qrcode` flag to `keys show` command to support displaying keys address QR code. * (client) [#18101](https://github.com/cosmos/cosmos-sdk/pull/18101) Add a `keyring-default-keyname` in `client.toml` for specifying a default key name, and skip the need to use the `--from` flag when signing transactions. * (tests) [#17868](https://github.com/cosmos/cosmos-sdk/pull/17868) Added helper method `SubmitTestTx` in testutil to broadcast test txns to test e2e tests. diff --git a/types/coin.go b/types/coin.go index 4b47401a096c..6df1c26e4ef6 100644 --- a/types/coin.go +++ b/types/coin.go @@ -68,6 +68,16 @@ func (coin Coin) IsZero() bool { return coin.Amount.IsZero() } +// IsGT returns true if they are the same type and the receiver is +// a greater value +func (coin Coin) IsGT(other Coin) bool { + if coin.Denom != other.Denom { + panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom)) + } + + return coin.Amount.GT(other.Amount) +} + // IsGTE returns true if they are the same type and the receiver is // an equal or greater value func (coin Coin) IsGTE(other Coin) bool { diff --git a/types/coin_test.go b/types/coin_test.go index d1136a4a5046..36cc32c8cf2e 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -302,6 +302,30 @@ func (s *coinTestSuite) TestQuoIntCoins() { } } +func (s *coinTestSuite) TestIsGTCoin() { + cases := []struct { + inputOne sdk.Coin + inputTwo sdk.Coin + expected bool + panics bool + }{ + {sdk.NewInt64Coin(testDenom1, 2), sdk.NewInt64Coin(testDenom1, 1), true, false}, + {sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), false, false}, + {sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 2), false, false}, + {sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true}, + } + + for tcIndex, tc := range cases { + tc := tc + if tc.panics { + s.Require().Panics(func() { tc.inputOne.IsGT(tc.inputTwo) }) + } else { + res := tc.inputOne.IsGT(tc.inputTwo) + s.Require().Equal(tc.expected, res, "coin GT relation is incorrect, tc #%d", tcIndex) + } + } +} + func (s *coinTestSuite) TestIsGTECoin() { cases := []struct { inputOne sdk.Coin