diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c23240a067..3dd43bd60ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#3611](https://github.com/osmosis-labs/osmosis/pull/3611),[#3647](https://github.com/osmosis-labs/osmosis/pull/3647) Introduce osmocli, to automate thousands of lines of CLI boilerplate * [#3634](https://github.com/osmosis-labs/osmosis/pull/3634) (Makefile) Ensure correct golang version in make build and make install. (Thank you @jhernandezb ) +* [#3711](https://github.com/osmosis-labs/osmosis/pull/3711) Use Dec instead of Int for additive `ErrTolerace` in `osmoutils`. ## v13.0.0 diff --git a/osmoutils/binary_search.go b/osmoutils/binary_search.go index 27ea4b4598f..03016a395a6 100644 --- a/osmoutils/binary_search.go +++ b/osmoutils/binary_search.go @@ -23,7 +23,7 @@ import ( // RoundingDir = RoundUnconstrained. // Note that if AdditiveTolerance == 0, then this is equivalent to a standard compare. type ErrTolerance struct { - AdditiveTolerance sdk.Int + AdditiveTolerance sdk.Dec MultiplicativeTolerance sdk.Dec RoundingDir osmomath.RoundingDirection } @@ -33,7 +33,7 @@ type ErrTolerance struct { // returns 1 if not, and expected > actual. // returns -1 if not, and expected < actual func (e ErrTolerance) Compare(expected sdk.Int, actual sdk.Int) int { - diff := expected.Sub(actual).Abs() + diff := expected.ToDec().Sub(actual.ToDec()).Abs() comparisonSign := 0 if expected.GT(actual) { @@ -71,7 +71,7 @@ func (e ErrTolerance) Compare(expected sdk.Int, actual sdk.Int) int { } // Check multiplicative tolerance equations if !e.MultiplicativeTolerance.IsNil() && !e.MultiplicativeTolerance.IsZero() { - errTerm := diff.ToDec().Quo(sdk.MinInt(expected.Abs(), actual.Abs()).ToDec()) + errTerm := diff.Quo(sdk.MinInt(expected.Abs(), actual.Abs()).ToDec()) if errTerm.GT(e.MultiplicativeTolerance) { return comparisonSign } @@ -117,7 +117,7 @@ func (e ErrTolerance) CompareBigDec(expected osmomath.BigDec, actual osmomath.Bi } } - if diff.GT(osmomath.BigDecFromSDKDec(e.AdditiveTolerance.ToDec())) { + if diff.GT(osmomath.BigDecFromSDKDec(e.AdditiveTolerance)) { return comparisonSign } } diff --git a/osmoutils/binary_search_test.go b/osmoutils/binary_search_test.go index 95f7d5afb56..22fd55ea188 100644 --- a/osmoutils/binary_search_test.go +++ b/osmoutils/binary_search_test.go @@ -12,7 +12,7 @@ import ( ) var ( - withinOne = ErrTolerance{AdditiveTolerance: sdk.OneInt()} + withinOne = ErrTolerance{AdditiveTolerance: sdk.OneDec()} withinFactor8 = ErrTolerance{MultiplicativeTolerance: sdk.NewDec(8)} zero = osmomath.ZeroDec() ) @@ -29,10 +29,10 @@ func TestBinarySearch(t *testing.T) { output := sdk.Int(result) return output, nil } - noErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroInt()} - testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 20)} - testErrToleranceMultiplicative := ErrTolerance{AdditiveTolerance: sdk.ZeroInt(), MultiplicativeTolerance: sdk.NewDec(10)} - testErrToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 20), MultiplicativeTolerance: sdk.NewDec(1 << 3)} + noErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroDec()} + testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 20)} + testErrToleranceMultiplicative := ErrTolerance{AdditiveTolerance: sdk.ZeroDec(), MultiplicativeTolerance: sdk.NewDec(10)} + testErrToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 20), MultiplicativeTolerance: sdk.NewDec(1 << 3)} tests := map[string]struct { f func(sdk.Int) (sdk.Int, error) lowerbound sdk.Int @@ -153,9 +153,9 @@ var fnMap = map[string]searchFn{"line": lineF, "cubic": cubicF, "neg_cubic": neg // This function tests that any value in a given range can be reached within expected num iterations. func TestIterationDepthRandValue(t *testing.T) { tests := map[string]binarySearchTestCase{} - exactEqual := ErrTolerance{AdditiveTolerance: sdk.ZeroInt()} - withinOne := ErrTolerance{AdditiveTolerance: sdk.OneInt()} - within32 := ErrTolerance{AdditiveTolerance: sdk.OneInt().MulRaw(32)} + exactEqual := ErrTolerance{AdditiveTolerance: sdk.ZeroDec()} + withinOne := ErrTolerance{AdditiveTolerance: sdk.OneDec()} + within32 := ErrTolerance{AdditiveTolerance: sdk.OneDec().Mul(sdk.NewDec(32))} createRandInput := func(fnName string, lowerbound, upperbound int64, errTolerance ErrTolerance, maxNumIters int, errToleranceName string) { @@ -227,8 +227,8 @@ func runBinarySearchTestCases(t *testing.T, tests map[string]binarySearchTestCas } func TestBinarySearchBigDec(t *testing.T) { - testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30)} - errToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30), MultiplicativeTolerance: sdk.NewDec(1 << 3)} + testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30)} + errToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30), MultiplicativeTolerance: sdk.NewDec(1 << 3)} twoTo50 := osmomath.NewBigDec(1 << 50) twoTo25PlusOne := osmomath.NewBigDec(1 + (1 << 25)) @@ -279,7 +279,7 @@ func TestBinarySearchBigDec(t *testing.T) { } func TestBinarySearchRoundingBehavior(t *testing.T) { - withinTwoTo30 := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30)} + withinTwoTo30 := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30)} twoTo50 := osmomath.NewBigDec(1 << 50) // twoTo25PlusOne := osmomath.NewBigDec(1 + (1 << 25)) @@ -318,10 +318,10 @@ func TestBinarySearchRoundingBehavior(t *testing.T) { } func TestErrTolerance_Compare(t *testing.T) { - ZeroErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroInt(), MultiplicativeTolerance: sdk.Dec{}} - NonZeroErrAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(10), MultiplicativeTolerance: sdk.Dec{}} - NonZeroErrMultiplicative := ErrTolerance{AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDec(10)} - NonZeroErrBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1), MultiplicativeTolerance: sdk.NewDec(10)} + ZeroErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroDec(), MultiplicativeTolerance: sdk.Dec{}} + NonZeroErrAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(10), MultiplicativeTolerance: sdk.Dec{}} + NonZeroErrMultiplicative := ErrTolerance{AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDec(10)} + NonZeroErrBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1), MultiplicativeTolerance: sdk.NewDec(10)} tests := []struct { name string tol ErrTolerance diff --git a/x/gamm/pool-models/balancer/pool_test.go b/x/gamm/pool-models/balancer/pool_test.go index 8cfa372137e..2adbf032a11 100644 --- a/x/gamm/pool-models/balancer/pool_test.go +++ b/x/gamm/pool-models/balancer/pool_test.go @@ -569,7 +569,7 @@ func (suite *BalancerTestSuite) TestBalancerCalculateAmountOutAndIn_InverseRelat suite.Require().NotNil(pool) errTolerance := osmoutils.ErrTolerance{ - AdditiveTolerance: sdk.OneInt(), MultiplicativeTolerance: sdk.Dec{}} + AdditiveTolerance: sdk.OneDec(), MultiplicativeTolerance: sdk.Dec{}} sut := func() { test_helpers.TestCalculateAmountOutAndIn_InverseRelationship(suite.T(), ctx, pool, poolAssetIn.Token.Denom, poolAssetOut.Token.Denom, tc.initialCalcOut, swapFeeDec, errTolerance) } diff --git a/x/gamm/pool-models/internal/cfmm_common/lp.go b/x/gamm/pool-models/internal/cfmm_common/lp.go index 61d4f526c2d..47cbf4ee9ed 100644 --- a/x/gamm/pool-models/internal/cfmm_common/lp.go +++ b/x/gamm/pool-models/internal/cfmm_common/lp.go @@ -155,7 +155,7 @@ func BinarySearchSingleAssetJoin( } // We accept an additive tolerance of 1 LP share error and round down - errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.OneInt(), MultiplicativeTolerance: sdk.Dec{}, RoundingDir: osmomath.RoundDown} + errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.OneDec(), MultiplicativeTolerance: sdk.Dec{}, RoundingDir: osmomath.RoundDown} numLPShares, err = osmoutils.BinarySearch( estimateCoinOutGivenShares, diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index cdbdbf5a24c..8081fcb6164 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -327,7 +327,7 @@ func solveCFMMBinarySearchMulti(xReserve, yReserve, wSumSquares, yIn osmomath.Bi maxIterations := 256 // we use a geometric error tolerance that guarantees approximately 10^-12 precision on outputs - errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)} + errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)} // if yIn is positive, we want to under-estimate the amount of xOut. // This means, we want x_out to be rounded down, as x_out = x_init - x_final, for x_init > x_final. diff --git a/x/gamm/pool-models/stableswap/amm_test.go b/x/gamm/pool-models/stableswap/amm_test.go index dacdbd8315d..332fdbfada4 100644 --- a/x/gamm/pool-models/stableswap/amm_test.go +++ b/x/gamm/pool-models/stableswap/amm_test.go @@ -727,7 +727,7 @@ func (suite *StableSwapTestSuite) Test_StableSwap_CalculateAmountOutAndIn_Invers pool := createTestPool(suite.T(), tc.poolLiquidity, swapFeeDec, exitFeeDec, tc.scalingFactors) suite.Require().NotNil(pool) errTolerance := osmoutils.ErrTolerance{ - AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)} + AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)} test_helpers.TestCalculateAmountOutAndIn_InverseRelationship(suite.T(), ctx, pool, tc.denomIn, tc.denomOut, tc.initialCalcOut, swapFeeDec, errTolerance) }) }