Skip to content

Commit

Permalink
feat(math): add mutative api for BigDec.BigInt() (#6993)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
DongLieu and mattverse committed Dec 7, 2023
1 parent 5bb2f7b commit be79c76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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())
}

0 comments on commit be79c76

Please sign in to comment.