Unverified Commit d812334d authored by alpo's avatar alpo Committed by GitHub
Browse files

remove all uses of two-asset binary search solver (#3084)

parent 4a67cddb
Showing with 0 additions and 66 deletions
+0 -66
......@@ -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 {
......
......@@ -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)
......
......@@ -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()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment