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

[stableswap]: simplify CFMM solver #2697

Merged
merged 16 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
62 changes: 60 additions & 2 deletions x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import (
)

var (
cubeRootTwo, _ = osmomath.NewBigDec(2).ApproxRoot(3)
threeCubeRootTwo = cubeRootTwo.MulInt64(3)
cubeRootTwo, _ = osmomath.NewBigDec(2).ApproxRoot(3)
threeRootTwo, _ = osmomath.NewBigDec(3).ApproxRoot(2)
cubeRootThree, _ = osmomath.NewBigDec(3).ApproxRoot(3)
threeCubeRootTwo = cubeRootTwo.MulInt64(3)
cubeRootSixSquared, _ = (osmomath.NewBigDec(6).MulInt64(6)).ApproxRoot(3)
twoCubeRootThree = cubeRootThree.MulInt64(2)
twentySevenRootTwo, _ = osmomath.NewBigDec(27).ApproxRoot(2)
)

// solidly CFMM is xy(x^2 + y^2) = k
Expand Down Expand Up @@ -276,6 +281,59 @@ func solveCfmmMulti(xReserve, yReserve, wSumSquares, yIn osmomath.BigDec) osmoma
return a
}

// solidly CFMM is xy(x^2 + y^2) = k
// So we want to solve for a given addition of `b` units of y into the pool,
// how many units `a` of x do we get out.
// Let y' = y + b
// we solve k = (x'y')(x'^2 + y^2) for x', using the following equation {wolfram alpha link}
Copy link
Member

Choose a reason for hiding this comment

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

Needs wolfram alpha link, or equation we test and explanation of correctness

Copy link
Contributor Author

@AlpinYukseloglu AlpinYukseloglu Sep 20, 2022

Choose a reason for hiding this comment

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

I put a wolfram alpha link of the original equation, a desmos link for our simplified one, and direct equation + our key abstractions – let me know what you think!

// Then we use that to derive the change in x as x_out = x' - x
func solveCfmmDirect(xReserve, yReserve, yIn osmomath.BigDec) osmomath.BigDec {
if !xReserve.IsPositive() || !yReserve.IsPositive() || !yIn.IsPositive() {
panic("invalid input: reserves and input must be positive")
}

if yIn.GT(yReserve) {
panic("invalid input: cannot trade greater than reserve amount into CFMM")
}

// find k using existing reserves
k := cfmmConstant(xReserve, yReserve)

// find new yReserve after join
y_new := yReserve.Add(yIn)

// store powers to simplify calculations
y2 := y_new.Mul(y_new)
y3 := y2.Mul(y_new)
y4 := y3.Mul(y_new)
large_term := (y4.Quo(k)).Mul(osmomath.NewBigDec(2).Quo(twentySevenRootTwo))
large_term2 := large_term.Mul(large_term)

// solve for new xReserve using new yReserve and old k using a solver derived from xy(x^2 + y^2) = k
sqrt_term, err := (osmomath.OneDec().Add(large_term2)).ApproxRoot(2)
if err != nil {
panic(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

Lets take the equation we want to compute, write out the variable substitutions we do, then show that each of this is very clearly computing the desired variable

Copy link
Contributor Author

@AlpinYukseloglu AlpinYukseloglu Sep 20, 2022

Choose a reason for hiding this comment

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

Took a crack at this!


common_factor, err := (y2.MulInt64(9).Mul(k).Mul((sqrt_term.Add(osmomath.OneDec())))).ApproxRoot(3)
if err != nil {
panic(err)
}

term1 := cubeRootTwo.Mul(common_factor).Quo(y_new)
term2 := twoCubeRootThree.Mul(y3).Quo(common_factor)
x_new := (term1.Sub(term2)).Quo(cubeRootSixSquared)

// find amount of x to output using initial and final xReserve values
xOut := xReserve.Sub(x_new)

if xOut.GTE(xReserve) {
panic("invalid output: greater than full pool reserves")
}

return xOut
}

func approxDecEqual(a, b, tol osmomath.BigDec) bool {
diff := a.Sub(b).Abs()
return diff.Quo(a).LTE(tol) && diff.Quo(b).LTE(tol)
Expand Down
233 changes: 188 additions & 45 deletions x/gamm/pool-models/stableswap/amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stableswap

import (
"fmt"
"math/big"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,58 +13,178 @@ import (
"github.com/osmosis-labs/osmosis/v12/x/gamm/pool-models/internal/test_helpers"
)

// twoAssetCFMMTestCase defines a testcase for TestCFMMInvariantTwoAssets
// and TestCFMMInvariantTwoAssetsDirect
type twoAssetCFMMTestCase struct {
xReserve osmomath.BigDec
yReserve osmomath.BigDec
yIn osmomath.BigDec
expectPanic bool
}

var twoAssetCFMMTestCases = map[string]twoAssetCFMMTestCase{
// sanity checks
"small pool small input": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(1),
expectPanic: false,
},
"small pool large input": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(99),
expectPanic: false,
},
"medium pool medium join": {
xReserve: osmomath.NewBigDec(100000),
yReserve: osmomath.NewBigDec(100000),
yIn: osmomath.NewBigDec(10000),
expectPanic: false,
},
"large pool medium join": {
xReserve: osmomath.NewBigDec(10000000),
yReserve: osmomath.NewBigDec(10000000),
yIn: osmomath.NewBigDec(10000),
expectPanic: false,
},
"large pool large join": {
xReserve: osmomath.NewBigDec(10000000),
yReserve: osmomath.NewBigDec(10000000),
yIn: osmomath.NewBigDec(1000000),
expectPanic: false,
},
"very large pool medium join": {
xReserve: osmomath.NewBigDec(1000000000),
yReserve: osmomath.NewBigDec(1000000000),
yIn: osmomath.NewBigDec(100000),
expectPanic: false,
},
"trillion token pool billion token join": {
xReserve: osmomath.NewBigDec(1000000000000),
yReserve: osmomath.NewBigDec(1000000000000),
yIn: osmomath.NewBigDec(1000000000),
expectPanic: false,
},

// uneven reserves
"xReserve double yReserve (small)": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(50),
yIn: osmomath.NewBigDec(1),
expectPanic: false,
},
"yReserve double xReserve (small)": {
xReserve: osmomath.NewBigDec(50),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(1),
expectPanic: false,
},
"xReserve double yReserve (large)": {
xReserve: osmomath.NewBigDec(13789470),
yReserve: osmomath.NewBigDec(59087324),
yIn: osmomath.NewBigDec(1047829),
expectPanic: false,
},
"yReserve double xReserve (large)": {
xReserve: osmomath.NewBigDec(50000000),
yReserve: osmomath.NewBigDec(100000000),
yIn: osmomath.NewBigDec(1000000),
expectPanic: false,
},
"uneven medium pool medium join": {
xReserve: osmomath.NewBigDec(123456),
yReserve: osmomath.NewBigDec(434245),
yIn: osmomath.NewBigDec(23314),
expectPanic: false,
},
"uneven large pool medium join": {
xReserve: osmomath.NewBigDec(11023432),
yReserve: osmomath.NewBigDec(17432897),
yIn: osmomath.NewBigDec(89734),
expectPanic: false,
},
"uneven large pool large join": {
xReserve: osmomath.NewBigDec(38987364),
yReserve: osmomath.NewBigDec(52893462),
yIn: osmomath.NewBigDec(9819874),
expectPanic: false,
},
"uneven very large pool medium join": {
xReserve: osmomath.NewBigDec(1473891748),
yReserve: osmomath.NewBigDec(7438971234),
yIn: osmomath.NewBigDec(100000),
expectPanic: false,
},
"uneven trillion token pool billion token join": {
xReserve: osmomath.NewBigDec(2678234328934),
yReserve: osmomath.NewBigDec(1573912437894),
yIn: osmomath.NewBigDec(53789183748),
expectPanic: false,
},

// panic catching
"yIn greater than pool reserves": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(1000),
expectPanic: true,
},
"xReserve negative": {
xReserve: osmomath.NewBigDec(-100),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(1),
expectPanic: true,
},
"yReserve negative": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(-100),
yIn: osmomath.NewBigDec(1),
expectPanic: true,
},
"yIn negative": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(-1),
expectPanic: true,
},

// overflows
"xReserve near max bitlen": {
xReserve: osmomath.NewDecFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(1024), nil), big.NewInt(1))),
yReserve: osmomath.NewBigDec(100),
yIn: osmomath.NewBigDec(1),
expectPanic: true,
},
"yReserve near max bitlen": {
xReserve: osmomath.NewBigDec(100),
yReserve: osmomath.NewDecFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(1024), nil), big.NewInt(1))),
yIn: osmomath.NewBigDec(1),
expectPanic: true,
},
"both assets near max bitlen": {
xReserve: osmomath.NewDecFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(1024), nil), big.NewInt(1))),
yReserve: osmomath.NewDecFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(1024), nil), big.NewInt(1))),
yIn: osmomath.NewBigDec(1),
expectPanic: true,
},
"max power yReserve check (approx 2^73)": {
xReserve: osmomath.NewBigDec(1),
yReserve: osmomath.NewDecFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(1024/14), nil), big.NewInt(1))),
yIn: osmomath.NewBigDec(1),
expectPanic: false,
},
}

type StableSwapTestSuite struct {
test_helpers.CfmmCommonTestSuite
}

func TestCFMMInvariantTwoAssets(t *testing.T) {
kErrTolerance := osmomath.OneDec()

tests := map[string]struct {
xReserve osmomath.BigDec
yReserve osmomath.BigDec
yIn osmomath.BigDec
expectPanic bool
}{
"small pool small input": {
osmomath.NewBigDec(100),
osmomath.NewBigDec(100),
osmomath.NewBigDec(1),
false,
},
"small pool large input": {
osmomath.NewBigDec(100),
osmomath.NewBigDec(100),
osmomath.NewBigDec(1000),
false,
},
// This test fails due to a bug in our original solver
// {
// sdk.NewDec(100000),
// sdk.NewDec(100000),
// sdk.NewDec(10000),
// },

// panic catching
"xReserve negative": {
osmomath.NewBigDec(-100),
osmomath.NewBigDec(100),
osmomath.NewBigDec(1),
true,
},
"yReserve negative": {
osmomath.NewBigDec(100),
osmomath.NewBigDec(-100),
osmomath.NewBigDec(1),
true,
},
"yIn negative": {
osmomath.NewBigDec(100),
osmomath.NewBigDec(100),
osmomath.NewBigDec(-1),
true,
},
}
// TODO: switch solveCfmm to binary search and replace this with test case suite
tests := map[string]twoAssetCFMMTestCase{}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
Expand All @@ -89,6 +210,28 @@ func TestCFMMInvariantTwoAssets(t *testing.T) {
}
}

func TestCFMMInvariantTwoAssetsDirect(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 cfmm
k0 := cfmmConstant(test.xReserve, test.yReserve)
xOut := solveCfmmDirect(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 TestCFMMInvariantMultiAssets(t *testing.T) {
kErrTolerance := osmomath.OneDec()

Expand Down