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

[x/gamm][stableswap]: Deprecate two-asset binary search solver in favor of multi-asset one #3084

Merged
merged 1 commit into from
Oct 21, 2022
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
38 changes: 0 additions & 38 deletions x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions x/gamm/pool-models/stableswap/amm_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 0 additions & 22 deletions x/gamm/pool-models/stableswap/amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down