From 4283b78d0f2be18aafe607cd98f3c07da0c445ac Mon Sep 17 00:00:00 2001 From: DongLieu Date: Mon, 13 Nov 2023 13:47:12 +0700 Subject: [PATCH 1/4] add API BigIntMut for BigDec --- osmomath/decimal.go | 9 +++++++++ osmomath/decimal_test.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index b695c8dba98..a35936cf33f 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) BigIntMut() *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..eb5bb6bb8a6 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.BigIntMut()) + + // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change + p1 := d.BigIntMut() + p1.SetInt64(40) + s.Require().Equal(big.NewInt(40), d.BigIntMut()) + 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.BigIntMut()) + s.Require().NotEqual(big.NewInt(50), d.BigInt()) +} From 40c87d4a1cd23f38978eb6dd97f30db0ce9090d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=C3=B4ng=20Li=E1=BB=81u?= <93205232+DongLieu@users.noreply.github.com> Date: Mon, 20 Nov 2023 09:31:39 +0700 Subject: [PATCH 2/4] Update osmomath/decimal.go Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> --- osmomath/decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index a35936cf33f..eb8c10e2e6d 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -249,7 +249,7 @@ func (d BigDec) BigInt() *big.Int { } // BigIntMut converts BigDec to big.Int, mutative the input -func (d BigDec) BigIntMut() *big.Int { +func (d BigDec) ToBigInt() *big.Int { if d.IsNil() { return nil } From c5a3a65b5840699fa28ecb3876ccfa138953d00a Mon Sep 17 00:00:00 2001 From: DongLieu Date: Mon, 20 Nov 2023 09:33:59 +0700 Subject: [PATCH 3/4] rename --- osmomath/decimal_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index eb5bb6bb8a6..d6f517ea65a 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -1707,17 +1707,17 @@ func (s *decimalTestSuite) TestBigIntMut() { d := osmomath.NewBigDecFromBigInt(r) // Compare value of BigInt & BigIntMut - s.Require().Equal(d.BigInt(), d.BigIntMut()) + s.Require().Equal(d.BigInt(), d.ToBigInt()) // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change - p1 := d.BigIntMut() + p1 := d.ToBigInt() p1.SetInt64(40) - s.Require().Equal(big.NewInt(40), d.BigIntMut()) + 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.BigIntMut()) + s.Require().NotEqual(big.NewInt(50), d.ToBigInt()) s.Require().NotEqual(big.NewInt(50), d.BigInt()) } From d21a0a31bd547af192de99c72f5dbcbaa967b6d8 Mon Sep 17 00:00:00 2001 From: DongLieu Date: Mon, 4 Dec 2023 15:58:46 +0700 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ee0fd2bfd..9495bb0fa03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,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.