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(math): add mutative api for BigDec.BigInt() #6993

Merged
merged 9 commits into from
Dec 7, 2023
Merged
9 changes: 9 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ func (d BigDec) BigInt() *big.Int {
return cp.Set(d.i)
}

// BigIntMut converts BigDec to big.Int, mutative the input
func (d BigDec) ToBigInt() *big.Int {
if d.IsNil() {
return nil
}

return d.i
}

// addition
func (d BigDec) Add(d2 BigDec) BigDec {
copy := d.Clone()
Expand Down
20 changes: 20 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,3 +1701,23 @@ func (s *decimalTestSuite) TestQuoTruncate_MutativeAndNonMutative() {
})
}
}

func (s *decimalTestSuite) TestBigIntMut() {
r := big.NewInt(30)
d := osmomath.NewBigDecFromBigInt(r)

// Compare value of BigInt & BigIntMut
s.Require().Equal(d.BigInt(), d.ToBigInt())

// Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change
p1 := d.ToBigInt()
p1.SetInt64(40)
s.Require().Equal(big.NewInt(40), d.ToBigInt())
s.Require().Equal(big.NewInt(40), d.BigInt())

// Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change
p2 := d.BigInt()
p2.SetInt64(50)
s.Require().NotEqual(big.NewInt(50), d.ToBigInt())
s.Require().NotEqual(big.NewInt(50), d.BigInt())
}