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

precision increase solution to infinite loop bug #5612

Merged
merged 69 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
ab831f3
repro infinite loop in swap logic
AlpinYukseloglu Jun 22, 2023
3f9df41
precision increase solution to infinite loop bug
p0mvn Jun 23, 2023
7d992c7
fix
p0mvn Jun 23, 2023
b6db5ea
remove logic
p0mvn Jun 23, 2023
a9bdb33
remove error
p0mvn Jun 23, 2023
d107a16
updates
p0mvn Jun 25, 2023
b9b6a58
updates
p0mvn Jun 25, 2023
e07c97b
updates
p0mvn Jun 25, 2023
d175fd6
updates
p0mvn Jun 25, 2023
914d1d2
Merge branch 'main' into roman/precision-inc-bug
p0mvn Jun 28, 2023
e6fccd3
begin switching zeroForOneStrategy.ComputeSwapWithinBucketInGivenOut
p0mvn Jun 28, 2023
1ac001c
switch remaining zero for one swap step and add rounding comments
p0mvn Jun 28, 2023
cf7c5ab
begin switching cur sqrt price to big dec in swaps
p0mvn Jun 28, 2023
b5f0380
try adding CalculateSqrtPriceToTickBigDec
p0mvn Jun 28, 2023
6cca545
one for zero out given in tests
p0mvn Jun 28, 2023
fa78f67
fix one for zero tests
p0mvn Jun 28, 2023
9cb3379
update one for zero tests
p0mvn Jun 28, 2023
d015454
in-progress test
p0mvn Jun 29, 2023
42241d3
updates
p0mvn Jun 29, 2023
52d7617
fix TestComputeSwapStepOutGivenIn_ZeroForOne
p0mvn Jun 29, 2023
970bcaf
fix TestComputeSwapStepInGivenOut_ZeroForOne
p0mvn Jun 29, 2023
0a77a4c
convert current sqrt price to BigDec and resolve syntax errors
p0mvn Jun 29, 2023
596a757
updates
p0mvn Jun 29, 2023
68717bc
clean up
p0mvn Jun 29, 2023
ea315fc
clean ups
p0mvn Jun 29, 2023
8eeac17
clean ups
p0mvn Jun 29, 2023
ff65cf1
fix TestComputeSwapState_Inverse
p0mvn Jun 29, 2023
445b58d
comment
p0mvn Jun 29, 2023
4976fec
updates
p0mvn Jun 29, 2023
f9c393e
clean ups
p0mvn Jun 29, 2023
d509138
Merge branch 'main' into roman/precision-inc-bug
p0mvn Jun 29, 2023
5a61b5d
remove unused function
p0mvn Jun 29, 2023
19eb70e
remove unused GetNextSqrtPriceFromAmount1OutRoundingDown
p0mvn Jun 29, 2023
e5bdf60
switch GetNextSqrtPriceFromAmount0InRoundingUp to big dec
p0mvn Jun 29, 2023
740d51c
fully convert GetNextSqrtPriceFromAmount0OutRoundingUp to big dec
p0mvn Jun 29, 2023
d192088
clean up math tests more
p0mvn Jun 29, 2023
57fd285
more math test clean up
p0mvn Jun 29, 2023
e434e9f
fully convert calc methods to big dec
p0mvn Jun 29, 2023
8d7fb70
fix model package tests
p0mvn Jun 29, 2023
c7bf6f8
fix TestAddToPosition
p0mvn Jun 29, 2023
7562387
fix TestCalculateSpotPrice
p0mvn Jun 29, 2023
e2d2480
fix TestSingleSidedAddToPosition
p0mvn Jun 29, 2023
f375469
fix TestUninitializePool
p0mvn Jun 29, 2023
5a91731
go mod update
p0mvn Jun 29, 2023
4b92717
big dec comparison correction and prints
p0mvn Jun 29, 2023
abdd04e
fix equality issues
p0mvn Jun 29, 2023
94b2d3d
convert tests to big dec
p0mvn Jun 29, 2023
c8eebaf
fix a few swap tests
p0mvn Jun 30, 2023
b28c5bb
small e2e fix
p0mvn Jun 30, 2023
2064474
more swap test fixes
p0mvn Jun 30, 2023
cf8e5b3
updates
p0mvn Jun 30, 2023
369a655
fix another test
p0mvn Jun 30, 2023
64bd82d
please get fixed
p0mvn Jun 30, 2023
bec4ebc
fix all out given in swap tests
p0mvn Jun 30, 2023
bad6419
e2e
p0mvn Jun 30, 2023
4b91093
fix all one for zero in given out
p0mvn Jun 30, 2023
bd18637
I can fix you
p0mvn Jun 30, 2023
f2fb617
clean ups
p0mvn Jun 30, 2023
fe1e084
MulRoundUp test
p0mvn Jun 30, 2023
dea96d6
test SDKDecRoundUp
p0mvn Jun 30, 2023
cfa290c
clean up
p0mvn Jun 30, 2023
fbc9d1d
revert launch.json
p0mvn Jun 30, 2023
85ec720
comment
p0mvn Jun 30, 2023
e17c8b2
Merge branch 'main' into roman/precision-inc-bug
p0mvn Jun 30, 2023
55126a8
Merge branch 'main' into roman/precision-inc-bug
p0mvn Jul 1, 2023
82f33ce
add zero for one swap strategy ULP tests
p0mvn Jul 2, 2023
56471c5
typo
p0mvn Jul 2, 2023
b5d6707
remove errors
p0mvn Jul 2, 2023
92b4d04
remove prints
p0mvn Jul 2, 2023
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
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,24 @@
"-test.timeout",
"30m",
"-test.run",
"TestKeeperTestSuite/TestYourName",
// TestKeeperTestSuite/TestInfiniteSwapLoop
"TestKeeperTestSuite/TestSwapOutGivenIn_Contiguous_Initialized_TickSpacingOne/tick_spacing_one,_contiguosly_initialized/zero_for_one,_swap_to_the_middle_tick_to_the_left_of_the_original_current",
// "TestKeeperTestSuite/TestInfiniteSwapLoop",
"-test.v"
],
},
{
"name": "x/concentrated-liquidity/swapstrategy",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/x/concentrated-liquidity/swapstrategy",
"args": [
"-test.timeout",
"30m",
"-test.run",
"TestStrategyTestSuite/TestComputeSwapStepInGivenOut_OneForZero/7",
// "TestStrategyTestSuite/TestComputeSwapStepOutGivenIn_OneForZero/7",
"-test.v"
],
},
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v16/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (suite *UpgradeTestSuite) TestUpgrade() {
suite.Require().Equal(v16.DAIIBCDenom, concentratedTypePool.GetToken1())

// Validate that the spot price of the CL pool is what we expect
suite.Require().Equal(0, multiplicativeTolerance.CompareBigDec(osmomath.BigDecFromSDKDec(concentratedTypePool.GetCurrentSqrtPrice().Power(2)), osmomath.BigDecFromSDKDec(balancerSpotPrice)))
suite.Require().Equal(0, multiplicativeTolerance.CompareBigDec(concentratedTypePool.GetCurrentSqrtPrice().PowerInteger(2), osmomath.BigDecFromSDKDec(balancerSpotPrice)))

// Validate that link was created.
migrationInfo, err := suite.App.GAMMKeeper.GetAllMigrationInfo(suite.Ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230621002052-afb82fbaa312
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230602130523-f9a94d8bbd10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230621002052-afb82fbaa312 h1:7Y/948riUlpIfFSAUIx0rmRy3B+Mdk6alHwOa8lTNXY=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230621002052-afb82fbaa312/go.mod h1:JTym95/bqrSnG5MPcXr1YDhv43JdCeo3p+iDbazoX68=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230622012610-25ef7aa8f2a5 h1:MTIKbW26Ire7kKL/JOPsXaVpbgTxfHdL8ySXHRcF3SM=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230622012610-25ef7aa8f2a5/go.mod h1:FqFOfj9+e5S1I7hR3baGUHrqje3g32EOHAXoOf7R00M=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6 h1:Kmkx5Rh72+LB8AL6dc6fZA+IVR0INu0YIiMF2ScDhaQ=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230629191111-f375469de8b6/go.mod h1:JTym95/bqrSnG5MPcXr1YDhv43JdCeo3p+iDbazoX68=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343 h1:7V2b3+mSnLnK0Px+Dl3vnxAQgk4SV8e9ohfJ/tKsq0M=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230623115558-38aaab07d343/go.mod h1:FqFOfj9+e5S1I7hR3baGUHrqje3g32EOHAXoOf7R00M=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304 h1:RIrWLzIiZN5Xd2JOfSOtGZaf6V3qEQYg6EaDTAkMnCo=
Expand Down
33 changes: 31 additions & 2 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (

var (
precisionReuse = new(big.Int).Exp(big.NewInt(10), big.NewInt(Precision), nil)
precisionReuseSDK = new(big.Int).Exp(big.NewInt(10), big.NewInt(sdk.Precision), nil)
fivePrecision = new(big.Int).Quo(precisionReuse, big.NewInt(2))
precisionMultipliers []*big.Int
zeroInt = big.NewInt(0)
Expand Down Expand Up @@ -305,6 +306,17 @@ func (d BigDec) MulTruncate(d2 BigDec) BigDec {
return BigDec{chopped}
}

// multiplication round up
func (d BigDec) MulRoundUp(d2 BigDec) BigDec {
mul := new(big.Int).Mul(d.i, d2.i)
chopped := chopPrecisionAndRoundUpBigDec(mul)

if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return BigDec{chopped}
}

// multiplication
func (d BigDec) MulInt(i BigInt) BigDec {
mul := new(big.Int).Mul(d.i, i.i)
Expand Down Expand Up @@ -381,7 +393,7 @@ func (d BigDec) QuoRoundUp(d2 BigDec) BigDec {
mul.Mul(mul, precisionReuse)

quo := new(big.Int).Quo(mul, d2.i)
chopped := chopPrecisionAndRoundUp(quo)
chopped := chopPrecisionAndRoundUpBigDec(quo)

if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
Expand Down Expand Up @@ -557,6 +569,13 @@ func (d BigDec) SDKDec() sdk.Dec {
return truncatedDec
}

// SDKDecRoundUp returns the Sdk.Dec representation of a BigDec.
// Round up at precision end.
// Values in any additional decimal places are truncated.
func (d BigDec) SDKDecRoundUp() sdk.Dec {
return sdk.NewDecFromBigIntWithPrec(chopPrecisionAndRoundUpSDKDec(d.i), sdk.Precision)
}

// BigDecFromSdkDec returns the BigDec representation of an SDKDec.
// Values in any additional decimal places are truncated.
func BigDecFromSDKDec(d sdk.Dec) BigDec {
Expand Down Expand Up @@ -628,8 +647,18 @@ func chopPrecisionAndRound(d *big.Int) *big.Int {
}
}

// chopPrecisionAndRoundUpBigDec removes a Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUpBigDec(d *big.Int) *big.Int {
return chopPrecisionAndRoundUp(d, precisionReuse)
}

// chopPrecisionAndRoundUpSDKDec removes sdk.Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUpSDKDec(d *big.Int) *big.Int {
return chopPrecisionAndRoundUp(d, precisionReuseSDK)
}

// chopPrecisionAndRoundUp removes a Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUp(d *big.Int) *big.Int {
func chopPrecisionAndRoundUp(d *big.Int, precisionReuse *big.Int) *big.Int {
// remove the negative and add it back when returning
if d.Sign() == -1 {
// make d positive, compute chopped value, and then un-mutate d
Expand Down
77 changes: 60 additions & 17 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ func (s *decimalTestSuite) TestSdkDec() {
}
}

func (s *decimalTestSuite) TestSdkDecRoundUp() {
tests := []struct {
d osmomath.BigDec
want sdk.Dec
expPanic bool
}{
{osmomath.NewBigDec(0), sdk.MustNewDecFromStr("0.000000000000000000"), false},
{osmomath.NewBigDec(1), sdk.MustNewDecFromStr("1.000000000000000000"), false},
{osmomath.NewBigDec(10), sdk.MustNewDecFromStr("10.000000000000000000"), false},
{osmomath.NewBigDec(12340), sdk.MustNewDecFromStr("12340.000000000000000000"), false},
{osmomath.NewDecWithPrec(12340, 4), sdk.MustNewDecFromStr("1.234000000000000000"), false},
{osmomath.NewDecWithPrec(12340, 5), sdk.MustNewDecFromStr("0.123400000000000000"), false},
{osmomath.NewDecWithPrec(12340, 8), sdk.MustNewDecFromStr("0.000123400000000000"), false},
{osmomath.NewDecWithPrec(1009009009009009009, 17), sdk.MustNewDecFromStr("10.090090090090090090"), false},
{osmomath.NewDecWithPrec(1009009009009009009, 19), sdk.MustNewDecFromStr("0.100900900900900901"), false},
}
for tcIndex, tc := range tests {
if tc.expPanic {
s.Require().Panics(func() { tc.d.SDKDecRoundUp() })
} else {
value := tc.d.SDKDecRoundUp()
s.Require().Equal(tc.want, value, "bad SdkDec(), index: %v", tcIndex)
}
}
}
Comment on lines +247 to +271
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: all tests are coped from SDKDec. Added the last one to display round up behavior


func (s *decimalTestSuite) TestBigDecFromSdkDec() {
tests := []struct {
d sdk.Dec
Expand Down Expand Up @@ -356,40 +382,40 @@ func (s *decimalTestSuite) TestDecsEqual() {
func (s *decimalTestSuite) TestArithmetic() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: all tests below are copied from: osmosis-labs/cosmos-sdk#430

tests := []struct {
d1, d2 osmomath.BigDec
expMul, expMulTruncate osmomath.BigDec
expMul, expMulTruncate, expMulRoundUp osmomath.BigDec
expQuo, expQuoRoundUp, expQuoTruncate osmomath.BigDec
expAdd, expSub osmomath.BigDec
}{
// d1 d2 MUL MulTruncate QUO QUORoundUp QUOTrunctate ADD SUB
{osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(-1)},
{osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1)},

{osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(2), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(-2), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(2)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(-2)},
// d1 d2 MUL MulTruncate MulRoundUp QUO QUORoundUp QUOTrunctate ADD SUB
{osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(1), osmomath.NewBigDec(-1)},
{osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1)},

{osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(2), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(-2), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(2)},
{osmomath.NewBigDec(-1), osmomath.NewBigDec(1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(-1), osmomath.NewBigDec(0), osmomath.NewBigDec(-2)},

{
osmomath.NewBigDec(3), osmomath.NewBigDec(7), osmomath.NewBigDec(21), osmomath.NewBigDec(21),
osmomath.NewBigDec(3), osmomath.NewBigDec(7), osmomath.NewBigDec(21), osmomath.NewBigDec(21), osmomath.NewBigDec(21),
osmomath.MustNewDecFromStr("0.428571428571428571428571428571428571"), osmomath.MustNewDecFromStr("0.428571428571428571428571428571428572"), osmomath.MustNewDecFromStr("0.428571428571428571428571428571428571"),
osmomath.NewBigDec(10), osmomath.NewBigDec(-4),
},
{
osmomath.NewBigDec(2), osmomath.NewBigDec(4), osmomath.NewBigDec(8), osmomath.NewBigDec(8), osmomath.NewDecWithPrec(5, 1), osmomath.NewDecWithPrec(5, 1), osmomath.NewDecWithPrec(5, 1),
osmomath.NewBigDec(2), osmomath.NewBigDec(4), osmomath.NewBigDec(8), osmomath.NewBigDec(8), osmomath.NewBigDec(8), osmomath.NewDecWithPrec(5, 1), osmomath.NewDecWithPrec(5, 1), osmomath.NewDecWithPrec(5, 1),
osmomath.NewBigDec(6), osmomath.NewBigDec(-2),
},

{osmomath.NewBigDec(100), osmomath.NewBigDec(100), osmomath.NewBigDec(10000), osmomath.NewBigDec(10000), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(200), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(100), osmomath.NewBigDec(100), osmomath.NewBigDec(10000), osmomath.NewBigDec(10000), osmomath.NewBigDec(10000), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(200), osmomath.NewBigDec(0)},

{
osmomath.NewDecWithPrec(15, 1), osmomath.NewDecWithPrec(15, 1), osmomath.NewDecWithPrec(225, 2), osmomath.NewDecWithPrec(225, 2),
osmomath.NewDecWithPrec(15, 1), osmomath.NewDecWithPrec(15, 1), osmomath.NewDecWithPrec(225, 2), osmomath.NewDecWithPrec(225, 2), osmomath.NewDecWithPrec(225, 2),
osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(3), osmomath.NewBigDec(0),
},
{
osmomath.NewDecWithPrec(3333, 4), osmomath.NewDecWithPrec(333, 4), osmomath.NewDecWithPrec(1109889, 8), osmomath.NewDecWithPrec(1109889, 8),
osmomath.NewDecWithPrec(3333, 4), osmomath.NewDecWithPrec(333, 4), osmomath.NewDecWithPrec(1109889, 8), osmomath.NewDecWithPrec(1109889, 8), osmomath.NewDecWithPrec(1109889, 8),
osmomath.MustNewDecFromStr("10.009009009009009009009009009009009009"), osmomath.MustNewDecFromStr("10.009009009009009009009009009009009010"), osmomath.MustNewDecFromStr("10.009009009009009009009009009009009009"),
osmomath.NewDecWithPrec(3666, 4), osmomath.NewDecWithPrec(3, 1),
},
Expand All @@ -401,10 +427,12 @@ func (s *decimalTestSuite) TestArithmetic() {
resSub := tc.d1.Sub(tc.d2)
resMul := tc.d1.Mul(tc.d2)
resMulTruncate := tc.d1.MulTruncate(tc.d2)
resMulRoundUp := tc.d1.MulRoundUp(tc.d2)
s.Require().True(tc.expAdd.Equal(resAdd), "exp %v, res %v, tc %d", tc.expAdd, resAdd, tcIndex)
s.Require().True(tc.expSub.Equal(resSub), "exp %v, res %v, tc %d", tc.expSub, resSub, tcIndex)
s.Require().True(tc.expMul.Equal(resMul), "exp %v, res %v, tc %d", tc.expMul, resMul, tcIndex)
s.Require().True(tc.expMulTruncate.Equal(resMulTruncate), "exp %v, res %v, tc %d", tc.expMulTruncate, resMulTruncate, tcIndex)
s.Require().True(tc.expMulRoundUp.Equal(resMulRoundUp), "exp %v, res %v, tc %d", tc.expMulRoundUp, resMulRoundUp, tcIndex)

if tc.d2.IsZero() { // panic for divide by zero
s.Require().Panics(func() { tc.d1.Quo(tc.d2) })
Expand All @@ -423,6 +451,21 @@ func (s *decimalTestSuite) TestArithmetic() {
}
}

func (s *decimalTestSuite) TestMulRoundUp_RoundingAtPrecisionEnd() {
var (
a = osmomath.MustNewDecFromStr("0.000000000000000000000000000000000009")
b = osmomath.MustNewDecFromStr("0.000000000000000000000000000000000009")
expectedRoundUp = osmomath.MustNewDecFromStr("0.000000000000000000000000000000000001")
expectedTruncate = osmomath.MustNewDecFromStr("0.000000000000000000000000000000000000")
)

actualRoundUp := a.MulRoundUp(b)
s.Require().Equal(expectedRoundUp.String(), actualRoundUp.String(), "exp %v, res %v", expectedRoundUp, actualRoundUp)

actualTruncate := a.MulTruncate(b)
s.Require().Equal(expectedTruncate.String(), actualTruncate.String(), "exp %v, res %v", expectedRoundUp, actualTruncate)
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *decimalTestSuite) TestBankerRoundChop() {
tests := []struct {
d1 osmomath.BigDec
Expand Down
2 changes: 1 addition & 1 deletion proto/osmosis/concentrated-liquidity/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ message Pool {
string token1 = 7;

string current_sqrt_price = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.customtype) = "github.com/osmosis-labs/osmosis/osmomath.BigDec",
(gogoproto.moretags) = "yaml:\"spot_price\"",
(gogoproto.nullable) = false
];
Expand Down
Loading