diff --git a/CHANGELOG.md b/CHANGELOG.md index cc43ce91609..f4b3d8fdd1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Features +* [#6804](https://github.com/osmosis-labs/osmosis/pull/6993) feat(math): add mutative api for BigDec.BigInt() * [#6804](https://github.com/osmosis-labs/osmosis/pull/6804) feat: track and query protocol rev across all modules ### Fix Localosmosis docker-compose with state. diff --git a/osmomath/decimal.go b/osmomath/decimal.go index b695c8dba98..eb8c10e2e6d 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -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() diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index dc61ab96b5e..d6f517ea65a 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -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()) +}