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: Implement .IsLTE for types.Coin #11441

Merged
merged 6 commits into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions types/coin.go
Expand Up @@ -81,6 +81,16 @@ func (coin Coin) IsLT(other Coin) bool {
return coin.Amount.LT(other.Amount)
}

// IsLTE returns true if they are the same type and the receiver is
// an equal or smaller value
func (coin Coin) IsLTE(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)
}

// IsEqual returns true if the two sets of Coins have the same value
func (coin Coin) IsEqual(other Coin) bool {
if coin.Denom != other.Denom {
Expand Down
25 changes: 25 additions & 0 deletions types/coin_test.go
Expand Up @@ -233,6 +233,7 @@ func (s *coinTestSuite) TestIsGTECoin() {
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 2), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 2), false, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true},
}

Expand All @@ -247,6 +248,30 @@ func (s *coinTestSuite) TestIsGTECoin() {
}
}

func (s *coinTestSuite) TestIsLTECoin() {
cases := []struct {
inputOne sdk.Coin
inputTwo sdk.Coin
expected bool
panics bool
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 2), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true},
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
{sdk.NewInt64Coin(testDenom1, 2), sdk.NewInt64Coin(testDenom1, 1), false, false},
}

for tcIndex, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsLTE(tc.inputTwo) })
} else {
res := tc.inputOne.IsLTE(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin LTE relation is incorrect, tc #%d", tcIndex)
}
}
}

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