From be79c767d6f51327a3351d942768b68a56ac4abf Mon Sep 17 00:00:00 2001 From: Dong Lieu <93205232+DongLieu@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:38:49 +0700 Subject: [PATCH] feat(math): add mutative api for BigDec.BigInt() (#6993) * add API BigIntMut for BigDec * Update osmomath/decimal.go Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> * rename * changelog --------- Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> --- CHANGELOG.md | 1 + osmomath/decimal.go | 9 +++++++++ osmomath/decimal_test.go | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41277b9623d..3a141e00876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,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()) +}