From 22e74b870ee822395e2682784ed3c9490c6b3499 Mon Sep 17 00:00:00 2001 From: alpo Date: Thu, 20 Oct 2022 19:46:26 -0700 Subject: [PATCH] remove all uses of two-asset binary search solver --- x/gamm/pool-models/stableswap/amm.go | 38 ------------------- .../pool-models/stableswap/amm_bench_test.go | 6 --- x/gamm/pool-models/stableswap/amm_test.go | 22 ----------- 3 files changed, 66 deletions(-) diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index 537d189931f..54043078293 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -64,9 +64,6 @@ func cfmmConstantMulti(xReserve, yReserve, u, v osmomath.BigDec) osmomath.BigDec // and the following expression for `a` in multi-asset pools: // xyz(x^2 + y^2 + w) = (x - a)(y + b)z((x - a)^2 + (y + b)^2 + w) func solveCfmm(xReserve, yReserve osmomath.BigDec, remReserves []osmomath.BigDec, yIn osmomath.BigDec) osmomath.BigDec { - if len(remReserves) == 0 { - return solveCFMMBinarySearch(cfmmConstant)(xReserve, yReserve, yIn) - } wSumSquares := osmomath.ZeroDec() for _, assetReserve := range remReserves { wSumSquares = wSumSquares.Add(assetReserve.Mul(assetReserve)) @@ -161,41 +158,6 @@ var ( k_threshold = osmomath.NewDecWithPrec(1, 1) // Correct within a factor of 1 * 10^{-1} ) -// solveCFMMBinarySearch searches the correct dx using binary search over constant K. -// added for future extension -func solveCFMMBinarySearch(constantFunction func(osmomath.BigDec, osmomath.BigDec) osmomath.BigDec) func(osmomath.BigDec, osmomath.BigDec, osmomath.BigDec) osmomath.BigDec { - return func(xReserve, yReserve, yIn osmomath.BigDec) osmomath.BigDec { - if !xReserve.IsPositive() || !yReserve.IsPositive() { - panic("invalid input: reserves and input must be positive") - } else if yIn.Abs().GTE(yReserve) { - panic("cannot input more than pool reserves") - } - k := constantFunction(xReserve, yReserve) - yFinal := yReserve.Add(yIn) - xLowEst := osmomath.ZeroDec() - // we set upper bound at 2 * xReserve to accommodate negative yIns - xHighEst := xReserve.Mul(osmomath.NewBigDec(2)) - maxIterations := 256 - errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.OneInt(), MultiplicativeTolerance: sdk.Dec{}} - - // create single-input CFMM to pass into binary search - calc_x_est := func(xEst osmomath.BigDec) (osmomath.BigDec, error) { - return constantFunction(xEst, yFinal), nil - } - - x_est, err := osmoutils.BinarySearchBigDec(calc_x_est, xLowEst, xHighEst, k, errTolerance, maxIterations) - if err != nil { - panic(err) - } - - xOut := xReserve.Sub(x_est) - if xOut.GTE(xReserve) { - panic("invalid output: greater than full pool reserves") - } - return xOut - } -} - // solveCFMMBinarySearch searches the correct dx using binary search over constant K. // added for future extension func solveCFMMBinarySearchMulti(xReserve, yReserve, wSumSquares, yIn osmomath.BigDec) osmomath.BigDec { diff --git a/x/gamm/pool-models/stableswap/amm_bench_test.go b/x/gamm/pool-models/stableswap/amm_bench_test.go index e07cf136911..4a779a79238 100644 --- a/x/gamm/pool-models/stableswap/amm_bench_test.go +++ b/x/gamm/pool-models/stableswap/amm_bench_test.go @@ -14,12 +14,6 @@ func BenchmarkCFMM(b *testing.B) { } } -func BenchmarkBinarySearchTwoAsset(b *testing.B) { - for i := 0; i < b.N; i++ { - runCalcTwoAsset(solveCFMMBinarySearch(cfmmConstant)) - } -} - func BenchmarkBinarySearchMultiAsset(b *testing.B) { for i := 0; i < b.N; i++ { runCalcMultiAsset(solveCFMMBinarySearchMulti) diff --git a/x/gamm/pool-models/stableswap/amm_test.go b/x/gamm/pool-models/stableswap/amm_test.go index c02baeb7816..da8c9a34eaa 100644 --- a/x/gamm/pool-models/stableswap/amm_test.go +++ b/x/gamm/pool-models/stableswap/amm_test.go @@ -406,28 +406,6 @@ func TestCFMMInvariantTwoAssets(t *testing.T) { } } -func TestCFMMInvariantTwoAssetsBinarySearch(t *testing.T) { - kErrTolerance := osmomath.OneDec() - - tests := twoAssetCFMMTestCases - - for name, test := range tests { - t.Run(name, func(t *testing.T) { - // system under test - sut := func() { - // using two-asset binary search cfmm solver - k0 := cfmmConstant(test.xReserve, test.yReserve) - xOut := solveCFMMBinarySearch(cfmmConstant)(test.xReserve, test.yReserve, test.yIn) - - k1 := cfmmConstant(test.xReserve.Sub(xOut), test.yReserve.Add(test.yIn)) - osmomath.DecApproxEq(t, k0, k1, kErrTolerance) - } - - osmoassert.ConditionalPanic(t, test.expectPanic, sut) - }) - } -} - func TestCFMMInvariantTwoAssetsDirect(t *testing.T) { kErrTolerance := osmomath.OneDec()