diff --git a/CHANGELOG.md b/CHANGELOG.md index 42bf0cc57b..4c16e2b954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (types) [#24093](https://github.com/cosmos/cosmos-sdk/pull/24093) 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/keys) [#24071](https://github.com/cosmos/cosmos-sdk/pull/24071) Add support for importing hex key using standard input. * (types) [#23780](https://github.com/cosmos/cosmos-sdk/pull/23780) Add a ValueCodec for the math.Uint type that can be used in collections maps. * (perf)[#24045](https://github.com/cosmos/cosmos-sdk/pull/24045) Sims: Replace runsim command with Go stdlib testing. CLI: `Commit` default true, `Lean`, `SimulateEveryOperation`, `PrintAllInvariants`, `DBBackend` params removed diff --git a/types/coin.go b/types/coin.go index 6b5ff76106..72aaccd6c7 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 6bc98dabff..a1a53baa6a 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -298,6 +298,56 @@ func (s *coinTestSuite) TestQuoIntCoins() { } } +func (s *coinTestSuite) TestIsGTCoin() { + cases := []struct { + name string + inputOne sdk.Coin + inputTwo sdk.Coin + expected bool + expPanics bool + }{ + { + name: "inputOne > inputTwo => true", + inputOne: sdk.NewInt64Coin(testDenom1, 2), + inputTwo: sdk.NewInt64Coin(testDenom1, 1), + expected: true, + expPanics: false, + }, + { + name: "inputOne == inputTwo => false", + inputOne: sdk.NewInt64Coin(testDenom1, 1), + inputTwo: sdk.NewInt64Coin(testDenom1, 1), + expected: false, + expPanics: false, + }, + { + name: "inputOne < inputTwo => false", + inputOne: sdk.NewInt64Coin(testDenom1, 1), + inputTwo: sdk.NewInt64Coin(testDenom1, 2), + expected: false, + expPanics: false, + }, + { + name: "different denoms => error (invalid coin denominations)", + inputOne: sdk.NewInt64Coin(testDenom1, 1), + inputTwo: sdk.NewInt64Coin(testDenom2, 1), + expected: false, + expPanics: true, + }, + } + + for tcIndex, tc := range cases { + s.Run(tc.name, func() { + if tc.expPanics { + 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 @@ -651,7 +701,6 @@ func (s *coinTestSuite) TestSafeSubCoin() { } for _, tc := range cases { - res, err := tc.inputOne.SafeSub(tc.inputTwo) if err != nil { s.Require().Contains(err.Error(), tc.expErrMsg) @@ -1282,7 +1331,6 @@ func (s *coinTestSuite) TestCoinValidate() { } for _, tc := range testCases { - t := s.T() t.Run(tc.name, func(t *testing.T) { err := tc.coin.Validate()