Skip to content

Commit

Permalink
osmomath: mutative and efficient BigDec truncations with arbitrary de…
Browse files Browse the repository at this point in the history
…cimals (#6261)

* merge main

* Auto: update go.mod after push to pysel/bigdec-sdkdec that modified dependencies locally

* better doc

* mutative/non-mutative custom precision truncators

* selfrev: better variable name

* use better type

* selfrev: better docs

* selfrev: better error in case of failed test

* use new truncators in CalculatePriceToTick

* use osmomath.PrecisionDec

* supposedly fix e2e

* sdk.Precision -> osmomath.PrecisionDec

* pre-compute precision factors

* add edge case

* fix go mod after merge

* panic if converting to Dec with precsiion bigger than PrecisionDec

* bring back old case, expect panic

* add example to DecWithPrecision

* updates

* godoc updates

* changelog

* uint

* updates

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: roman <roman@osmosis.team>
(cherry picked from commit d5c8abc)

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	x/epochs/go.mod
#	x/epochs/go.sum
  • Loading branch information
pysel authored and mergify[bot] committed Sep 20, 2023
1 parent 3c734c9 commit 2dbd3cb
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

* [#6427](https://github.com/osmosis-labs/osmosis/pull/6427) sdk.Coins Mul and Quo helpers in osmoutils
<<<<<<< HEAD
* [#6437](https://github.com/osmosis-labs/osmosis/pull/6437) mutative version for QuoRoundUp. Replace some non-mutative calls with mutative for better performance.
* [#6416](https://github.com/osmosis-labs/osmosis/pull/6416) feat[CL]: add num initialized ticks query
=======
* [#6437](https://github.com/osmosis-labs/osmosis/pull/6437) mutative version for QuoRoundUp
* [#6261](https://github.com/osmosis-labs/osmosis/pull/6261) mutative and efficient BigDec truncations with arbitrary decimals
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
### Misc Improvements

Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ require (
github.com/mattn/go-sqlite3 v1.14.17
github.com/ory/dockertest/v3 v3.10.0
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
<<<<<<< HEAD
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920173345-6401a459cb14
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230920012324-f1a1ca887bd2
github.com/osmosis-labs/osmosis/x/epochs v0.0.2
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8
=======
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920192334-7fb82821ef90
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.5.1
Expand Down Expand Up @@ -56,14 +63,22 @@ require (
github.com/alingse/asasalint v0.0.11 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/butuzov/mirror v1.1.0 // indirect
<<<<<<< HEAD
=======
github.com/ccojocar/zxcvbn-go v1.0.1 // indirect
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/cosmos/gogoproto v1.4.11 // indirect
github.com/cosmos/iavl v0.19.5 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
<<<<<<< HEAD
github.com/go-playground/universal-translator v0.18.1 // indirect
=======
github.com/go-playground/locales v0.14.1 // indirect
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/gateway v1.1.0 // indirect
github.com/google/btree v1.1.2 // indirect
Expand Down
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ github.com/butuzov/mirror v1.1.0/go.mod h1:8Q0BdQU6rC6WILDiBM60DBfvV78OLJmMmixe7
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
<<<<<<< HEAD
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=
=======
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
Expand Down Expand Up @@ -932,7 +936,11 @@ github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
<<<<<<< HEAD
github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE=
=======
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
Expand Down Expand Up @@ -963,6 +971,7 @@ github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3 h1:
github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3/go.mod h1:mFGH2RJhuZa6vBuFMNe4JN85fGtIhROtdeQIyIw9isA=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
<<<<<<< HEAD
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920090526-bc02685001d4 h1:inSXiNHfKdWXrff3oU4FYFVmbK/9VYSIc73MtOpKApg=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920090526-bc02685001d4/go.mod h1:oBmsOov8oxuWoI/yMQwyKGA6QfP0cBxylLt75gFbT8s=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920173345-6401a459cb14 h1:SR8J54Bi55oHr1KN7E+8PS1IZDXJJDPuBG+hrn/JoQA=
Expand All @@ -973,6 +982,24 @@ github.com/osmosis-labs/osmosis/x/epochs v0.0.2 h1:aEeXHGCSJMgMtAvCucsD2RSaWZ8lI
github.com/osmosis-labs/osmosis/x/epochs v0.0.2/go.mod h1:8dvJFHTpu6SIxmOaSaEw0tHnQ/Z9blf5qsF/ZJnMVHo=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8 h1:BUGowctYQT0vdPgULrvwraEsW+sS6DnbzndTLKLmWVY=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8/go.mod h1:sR0lpev9mcm9/9RY50T1og3UC3WpZAsINh/OmgrmFlg=
=======
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad h1:I4m0TxfAAovSmxI0rYvijAVX6JhoYe12VmjM5vcABxU=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920134402-0622c30178eb h1:czhTR1cvT+a/f5EAz7kIrX3W9zEBWV/9TKIbxcfV2AE=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920134402-0622c30178eb/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920134650-28712c635b4f h1:SwZwcklK2EHIQLD8gVTqMsUTD1GpTyF6X8HlMywrxTc=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920134650-28712c635b4f/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920190757-3f566b965c22 h1:D2Qx9OPINxTQVaXQZwklajC04XUYGvY5g+8Z8rMMXas=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920190757-3f566b965c22/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920192334-7fb82821ef90 h1:GRKUSGlACYPYFfU5Beu7NBILX8UYHGTWw09gzTfsdwc=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230920192334-7fb82821ef90/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf h1:r5R/L3tzH+vGPahAdvnVB2Vo0KPhZR0oMNyX4+G2FEo=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:7VoXHwrSSx8Sii0UFc9YIixF6C/9XfV1pdU2Dliu4WA=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf h1:8lkIsAj3L7zxvOZbqVLNJRpSdDxaYhYfAIG7XjPaJiU=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf/go.mod h1:C0Uqe6X4N5ASA+1xZ6guaaJyUVKLcaVJIQa4Q4LG9Vk=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf h1:ZEi+yJJPgpYtmNwZ1bMiP5cMBDQ83FK/YGgmTnWmoAI=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf/go.mod h1:pqkAsS04Er1kFM41gg3aWTSaYHyLe8C9Dw3Sj8KMXk8=
>>>>>>> d5c8abca (osmomath: mutative and efficient BigDec truncations with arbitrary decimals (#6261))
github.com/osmosis-labs/wasmd v0.31.0-osmo-v16 h1:X747cZYdnqc/+RV48iPVeGprpVb/fUWSaKGsZUWrdbg=
github.com/osmosis-labs/wasmd v0.31.0-osmo-v16/go.mod h1:Rf8zW/GgBQyFRRB4s62VQHWA6sTlMFSjoDQQpoq64iI=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
Expand Down
65 changes: 53 additions & 12 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ var (
// initialized in init() since requires
// precision to be defined.
twoBigDec BigDec = MustNewBigDecFromStr("2")

// precisionFactors are used to adjust the scale of big.Int values to match the desired precision
precisionFactors = make(map[uint64]*big.Int)
)

// Decimal errors
Expand All @@ -70,6 +73,11 @@ func init() {
for i := 0; i <= PrecisionBigDec; i++ {
precisionMultipliers[i] = calcPrecisionMultiplier(int64(i))
}

for precision := uint64(0); precision <= PrecisionBigDec; precision++ {
precisionFactor := new(big.Int).Exp(big.NewInt(10), big.NewInt(PrecisionBigDec-int64(precision)), nil)
precisionFactors[precision] = precisionFactor
}
}

func precisionInt() *big.Int {
Expand Down Expand Up @@ -104,35 +112,35 @@ func NewBigDec(i int64) BigDec {
}

// create a new BigDec from integer with decimal place at prec
// CONTRACT: prec <= Precision
// CONTRACT: prec <= PrecisionBigDec
func NewBigDecWithPrec(i, prec int64) BigDec {
return BigDec{
new(big.Int).Mul(big.NewInt(i), precisionMultiplier(prec)),
}
}

// create a new BigDec from big integer assuming whole numbers
// CONTRACT: prec <= Precision
// CONTRACT: prec <= PrecisionBigDec
func NewBigDecFromBigInt(i *big.Int) BigDec {
return NewBigDecFromBigIntWithPrec(i, 0)
}

// create a new BigDec from big integer assuming whole numbers
// CONTRACT: prec <= Precision
// CONTRACT: prec <= PrecisionBigDec
func NewBigDecFromBigIntWithPrec(i *big.Int, prec int64) BigDec {
return BigDec{
new(big.Int).Mul(i, precisionMultiplier(prec)),
}
}

// create a new BigDec from big integer assuming whole numbers
// CONTRACT: prec <= Precision
// CONTRACT: prec <= PrecisionBigDec
func NewBigDecFromInt(i BigInt) BigDec {
return NewBigDecFromIntWithPrec(i, 0)
}

// create a new BigDec from big integer with decimal place at prec
// CONTRACT: prec <= Precision
// CONTRACT: prec <= PrecisionBigDec
func NewBigDecFromIntWithPrec(i BigInt, prec int64) BigDec {
return BigDec{
new(big.Int).Mul(i.BigInt(), precisionMultiplier(prec)),
Expand Down Expand Up @@ -567,23 +575,56 @@ func (d BigDec) MustFloat64() float64 {
// Dec returns the osmomath.Dec representation of a BigDec.
// Values in any additional decimal places are truncated.
func (d BigDec) Dec() Dec {
precisionDiff := PrecisionBigDec - PrecisionDec
precisionFactor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(precisionDiff)), nil)

if precisionDiff < 0 {
panic("invalid decimal precision")
return d.DecWithPrecision(PrecisionDec)
}

// DecWithPrecision converts BigDec to Dec with desired precision
// Example:
// BigDec: 1.010100000000153000000000000000000000
// precision: 4
// Output Dec: 1.010100000000000000
// Panics if precision exceeds PrecisionDec
func (d BigDec) DecWithPrecision(precision uint64) Dec {
var precisionFactor *big.Int
if precision > PrecisionDec {
panic(fmt.Sprintf("maximum Dec precision is (%v), provided (%v)", PrecisionDec, precision))
} else {
precisionFactor = precisionFactors[precision]
}

// Truncate any additional decimal values that exist due to BigDec's additional precision
// This relies on big.Int's Quo function doing floor division
intRepresentation := new(big.Int).Quo(d.BigInt(), precisionFactor)

// convert int representation back to SDK Dec precision
truncatedDec := NewDecFromBigIntWithPrec(intRepresentation, PrecisionDec)
truncatedDec := NewDecFromBigIntWithPrec(intRepresentation, int64(precision))

return truncatedDec
}

// ChopPrecisionMut truncates all decimals after precision numbers after decimal point. Mutative
// CONTRACT: precision <= PrecisionBigDec
// Panics if precision exceeds PrecisionBigDec
func (d *BigDec) ChopPrecisionMut(precision uint64) BigDec {
if precision > PrecisionBigDec {
panic(fmt.Sprintf("maximum BigDec precision is (%v), provided (%v)", PrecisionDec, precision))
}

precisionFactor := precisionFactors[precision]
// big.Quo truncates numbers that would have been after decimal point
d.i.Quo(d.i, precisionFactor)
d.i.Mul(d.i, precisionFactor)
return BigDec{d.i}
}

// ChopPrecision truncates all decimals after precision numbers after decimal point
// CONTRACT: precision <= PrecisionBigDec
// Panics if precision exceeds PrecisionBigDec
func (d *BigDec) ChopPrecision(precision uint64) BigDec {
copy := d.Clone()
return copy.ChopPrecisionMut(precision)
}

// DecRoundUp returns the osmomath.Dec representation of a BigDec.
// Round up at precision end.
// Values in any additional decimal places are truncated.
Expand Down Expand Up @@ -676,7 +717,7 @@ func chopPrecisionAndRoundUpBigDec(d *big.Int) *big.Int {
return chopPrecisionAndRoundUpMut(copy, precisionReuse)
}

// chopPrecisionAndRoundUpDec removes sdk.Precision amount of rightmost digits and rounds up.
// chopPrecisionAndRoundUpDec removes PrecisionDec amount of rightmost digits and rounds up.
// Non-mutative.
func chopPrecisionAndRoundUpDec(d *big.Int) *big.Int {
copy := new(big.Int).Set(d)
Expand Down
79 changes: 79 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -1502,6 +1503,84 @@ func (s *decimalTestSuite) TestPower() {
}
}

func (s *decimalTestSuite) TestDec_WithPrecision() {
tests := []struct {
d osmomath.BigDec
want osmomath.Dec
precision uint64
expPanic bool
}{
// test cases for basic SDKDec() conversion
{osmomath.NewBigDec(0), sdk.MustNewDecFromStr("0.000000000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDec(1), sdk.MustNewDecFromStr("1.000000000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDec(10), sdk.MustNewDecFromStr("10.000000000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDec(12340), sdk.MustNewDecFromStr("12340.000000000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDecWithPrec(12340, 4), sdk.MustNewDecFromStr("1.234000000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDecWithPrec(12340, 5), sdk.MustNewDecFromStr("0.123400000000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDecWithPrec(12340, 8), sdk.MustNewDecFromStr("0.000123400000000000"), osmomath.PrecisionDec, false},
{osmomath.NewBigDecWithPrec(1009009009009009009, 17), sdk.MustNewDecFromStr("10.090090090090090090"), osmomath.PrecisionDec, false},
// test cases with custom precision:
{osmomath.NewBigDec(0), sdk.MustNewDecFromStr("0.000000000000"), 12, false},
{osmomath.NewBigDec(1), sdk.MustNewDecFromStr("1.000000000000"), 12, false},
// specified precision is the same as the initial precision: 12.3453123123 -> 12.3453123123
{osmomath.NewBigDecWithPrec(123453123123, 10), sdk.MustNewDecFromStr("12.3453123123"), 10, false},
// cut precision to 5 decimals: 3212.4623423462346 - 3212.46234
{osmomath.NewBigDecWithPrec(32124623423462346, 13), sdk.MustNewDecFromStr("3212.46234"), 5, false},
// no decimal point: 18012004 -> 18012004
{osmomath.NewBigDecWithPrec(18012004, 0), sdk.MustNewDecFromStr("18012004"), 13, false},
// if we try to convert to osmomath.Dec while specifying bigger precision than sdk.Dec has, panics
{osmomath.NewBigDecWithPrec(1009009009009009009, 17), sdk.MustNewDecFromStr("10.090090090090090090"), osmomath.PrecisionDec + 2, true},
}

for tcIndex, tc := range tests {
if tc.expPanic {
s.Require().Panics(func() { tc.d.DecWithPrecision(tc.precision) })
} else {
var got osmomath.Dec
if tc.precision == osmomath.PrecisionDec {
got = tc.d.Dec()
} else {
got = tc.d.DecWithPrecision(tc.precision)
}
s.Require().Equal(tc.want, got, "bad Dec conversion, index: %v", tcIndex)
}
}
}

func (s *decimalTestSuite) TestChopPrecision_Mutative() {
tests := []struct {
startValue osmomath.BigDec
expectedMutResult osmomath.BigDec
precision uint64
}{
{osmomath.NewBigDec(0), osmomath.MustNewBigDecFromStr("0"), 0},
{osmomath.NewBigDec(1), osmomath.MustNewBigDecFromStr("1"), 0},
{osmomath.NewBigDec(10), osmomath.MustNewBigDecFromStr("10"), 2},
// how to read these comments: ab.cde(fgh) -> ab.cdefgh = initial BigDec; (fgh) = decimal places that will be truncated
// 5.1()
{osmomath.NewBigDecWithPrec(51, 1), osmomath.MustNewBigDecFromStr("5.1"), 1},
// 1.(0010)
{osmomath.NewBigDecWithPrec(10010, 4), osmomath.MustNewBigDecFromStr("1"), 0},
// 1009.31254(83952)
{osmomath.NewBigDecWithPrec(10093125483952, 10), osmomath.MustNewBigDecFromStr("1009.31254"), 5},
// 0.1009312548(3952)
{osmomath.NewBigDecWithPrec(10093125483952, 14), osmomath.MustNewBigDecFromStr("0.1009312548"), 10},
// Edge case: max precision. Should remain unchanged
{osmomath.MustNewBigDecFromStr("1.000000000000000000000000000000000001"), osmomath.MustNewBigDecFromStr("1.000000000000000000000000000000000001"), osmomath.PrecisionBigDec},
}
for id, tc := range tests {
name := "testcase_" + fmt.Sprint(id)
s.Run(name, func() {
startMut := tc.startValue.Clone()
startNonMut := tc.startValue.Clone()

resultMut := startMut.ChopPrecisionMut(tc.precision)
resultNonMut := startNonMut.ChopPrecision(tc.precision)

s.assertMutResult(tc.expectedMutResult, tc.startValue, resultMut, resultNonMut, startMut, startNonMut)
})
}
}
func (s *decimalTestSuite) TestQuoRoundUp_MutativeAndNonMutative() {
tests := []struct {
d1, d2, expQuoRoundUpMut osmomath.BigDec
Expand Down
2 changes: 1 addition & 1 deletion simulation/simtypes/random/sdkrand.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func RandomDecAmount(r *rand.Rand, max osmomath.Dec) osmomath.Dec {
randInt = big.NewInt(0).Rand(r, max.BigInt())
}

return osmomath.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
return osmomath.NewDecFromBigIntWithPrec(randInt, osmomath.PrecisionDec)
}

// RandTimestamp generates a random timestamp
Expand Down
3 changes: 1 addition & 2 deletions x/concentrated-liquidity/math/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,9 @@ func CalculatePriceToTick(price osmomath.BigDec) (tickIndex int64, err error) {
// N.B. this exists to maintain backwards compatibility with
// the old version of the function that operated on decimal with precision of 18.
if price.GTE(types.MinSpotPriceBigDec) {
// TODO: implement efficient big decimal truncation.
// It is acceptable to truncate price as the minimum we support is
// 10**-12 which is above the smallest value of sdk.Dec.
price = osmomath.BigDecFromDec(price.Dec())
price.ChopPrecisionMut(osmomath.PrecisionDec)
}

// The approach here is to try determine which "geometric spacing" are we in.
Expand Down
Loading

0 comments on commit 2dbd3cb

Please sign in to comment.