diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 4354b122f6..3b7c8e741f 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -31,4 +31,5 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +* [#13381](https://github.com/cosmos/cosmos-sdk/pull/13381) Add uint `IsNil` method. * [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package, call it `LegacyDec`. diff --git a/math/uint.go b/math/uint.go index 8ce6105e9f..81da2d9ef8 100644 --- a/math/uint.go +++ b/math/uint.go @@ -18,6 +18,11 @@ func (u Uint) BigInt() *big.Int { return new(big.Int).Set(u.i) } +// IsNil returns true if Uint is uninitialized +func (u Uint) IsNil() bool { + return u.i == nil +} + // NewUintFromBigUint constructs Uint from big.Uint func NewUintFromBigInt(i *big.Int) Uint { u, err := checkNewUint(i) diff --git a/math/uint_test.go b/math/uint_test.go index 3077e50941..428672cb2c 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -91,6 +91,11 @@ func (s *uintTestSuite) TestUintPanics() { s.Require().True(sdkmath.ZeroUint().LTE(sdkmath.OneUint())) } +func (s *uintTestSuite) TestIsNil() { + s.Require().False(sdkmath.OneUint().IsNil()) + s.Require().True(sdkmath.Uint{}.IsNil()) +} + func (s *uintTestSuite) TestArithUint() { for d := 0; d < 1000; d++ { n1 := uint64(rand.Uint32())