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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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())
}