feat(types): Implement .IsGT for types.Coin (#24093)

Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
Đông Liều 2025-03-26 11:44:28 +07:00 committed by GitHub
parent a2b7cea8a7
commit 2bf3947ce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 2 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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()