From 0b04698f296633a26f3ace20babf505672ddbd64 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 14 Apr 2022 17:30:17 -0400 Subject: [PATCH 1/2] add calc unit tests with 0 fees --- x/gamm/pool-models/balancer/amm_test.go | 109 +++++++++++++++++++++++ x/gamm/pool-models/balancer/util_test.go | 42 +++++++++ 2 files changed, 151 insertions(+) create mode 100644 x/gamm/pool-models/balancer/util_test.go diff --git a/x/gamm/pool-models/balancer/amm_test.go b/x/gamm/pool-models/balancer/amm_test.go index b8df955f09f..fd09c2a186a 100644 --- a/x/gamm/pool-models/balancer/amm_test.go +++ b/x/gamm/pool-models/balancer/amm_test.go @@ -1,7 +1,11 @@ package balancer_test import ( + "fmt" + "testing" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" ) @@ -77,3 +81,108 @@ func (suite *KeeperTestSuite) TestBalancerSpotPrice() { } } } + +func TestCalculateAmountOutAndIn_InverseRelationship(t *testing.T) { + type testcase struct { + denomOut string + initialPoolOut int64 + initialWeightOut int64 + initialCalcOut int64 + + denomIn string + initialPoolIn int64 + initialWeightIn int64 + } + + testcases := []testcase{ + { + denomOut: "uosmo", + initialPoolOut: 1_000_000_000_000, + initialWeightOut: 100, + initialCalcOut: 100, + + denomIn: "ion", + initialPoolIn: 1_000_000_000_000, + initialWeightIn: 100, + }, + { + denomOut: "uosmo", + initialPoolOut: 1_000, + initialWeightOut: 100, + initialCalcOut: 100, + + denomIn: "ion", + initialPoolIn: 1_000_000, + initialWeightIn: 100, + }, + { + denomOut: "uosmo", + initialPoolOut: 1_000, + initialWeightOut: 100, + initialCalcOut: 100, + + denomIn: "ion", + initialPoolIn: 1_000_000, + initialWeightIn: 100, + }, + { + denomOut: "uosmo", + initialPoolOut: 1_000, + initialWeightOut: 200, + initialCalcOut: 100, + + denomIn: "ion", + initialPoolIn: 1_000_000, + initialWeightIn: 50, + }, + } + + getTestCaseName := func(tc testcase) string { + return fmt.Sprintf("tokenOutInitial: %d, tokenInInitial: %d, initialOut: %d", + tc.initialPoolOut, + tc.initialPoolIn, + tc.initialCalcOut, + ) + } + + for _, tc := range testcases { + t.Run(getTestCaseName(tc), func(t *testing.T) { + ctx := createTestContext(t) + + poolAssetOut := balancer.PoolAsset{ + Token: sdk.NewInt64Coin(tc.denomOut, tc.initialPoolOut), + Weight: sdk.NewInt(tc.initialWeightOut), + } + + poolAssetIn := balancer.PoolAsset{ + Token: sdk.NewInt64Coin(tc.denomIn, tc.initialPoolIn), + Weight: sdk.NewInt(tc.initialWeightIn), + } + + pool := createTestPool(t, []balancer.PoolAsset{ + poolAssetOut, + poolAssetIn, + }, + "0", + "0", + ) + require.NotNil(t, pool) + + initialOut := sdk.NewInt64Coin(poolAssetOut.Token.Denom, tc.initialCalcOut) + initialOutCoins := sdk.NewCoins(initialOut) + + actualTokenIn, err := pool.CalcInAmtGivenOut(ctx, initialOutCoins, poolAssetIn.Token.Denom, sdk.ZeroDec()) + require.NoError(t, err) + + inverseTokenOut, err := pool.CalcOutAmtGivenIn(ctx, sdk.NewCoins(sdk.NewInt64Coin(poolAssetIn.Token.Denom, actualTokenIn.Amount.TruncateInt64())), poolAssetOut.Token.Denom, sdk.ZeroDec()) + require.NoError(t, err) + + require.Equal(t, initialOut.Denom, inverseTokenOut.Denom) + + expected := initialOut.Amount.ToDec() + actual := inverseTokenOut.Amount.RoundInt().ToDec() // must round to be able to compare with expected. + + require.Equal(t, expected, actual) + }) + } +} diff --git a/x/gamm/pool-models/balancer/util_test.go b/x/gamm/pool-models/balancer/util_test.go new file mode 100644 index 00000000000..5315f8b41f7 --- /dev/null +++ b/x/gamm/pool-models/balancer/util_test.go @@ -0,0 +1,42 @@ +package balancer_test + +import ( + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/store/rootmulti" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" + dbm "github.com/tendermint/tm-db" + + "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" + "github.com/osmosis-labs/osmosis/v7/x/gamm/types" +) + +func createTestPool(t *testing.T, poolAssets []balancer.PoolAsset, swapFee, exitFee string) types.PoolI { + swapFeeDec, err := sdk.NewDecFromStr(swapFee) + require.NoError(t, err) + + exitFeeDec, err := sdk.NewDecFromStr(exitFee) + require.NoError(t, err) + + pool, err := balancer.NewBalancerPool(1, balancer.PoolParams{ + SwapFee: swapFeeDec, + ExitFee: exitFeeDec, + }, poolAssets, "", time.Now()) + + require.NoError(t, err) + + return &pool +} + +func createTestContext(t *testing.T) sdk.Context { + db := dbm.NewMemDB() + logger := log.NewNopLogger() + + ms := rootmulti.NewStore(db, logger) + + return sdk.NewContext(ms, tmtypes.Header{}, false, logger) +} From bc2aaa71ed36a363f55b5aae7c404606159f041a Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 18 Apr 2022 19:41:44 -0400 Subject: [PATCH 2/2] change test name to TestCalculateAmountOutAndIn_InverseRelationship_ZeroSwapFee --- x/gamm/pool-models/balancer/amm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/gamm/pool-models/balancer/amm_test.go b/x/gamm/pool-models/balancer/amm_test.go index fd09c2a186a..6f13ea35e72 100644 --- a/x/gamm/pool-models/balancer/amm_test.go +++ b/x/gamm/pool-models/balancer/amm_test.go @@ -82,7 +82,7 @@ func (suite *KeeperTestSuite) TestBalancerSpotPrice() { } } -func TestCalculateAmountOutAndIn_InverseRelationship(t *testing.T) { +func TestCalculateAmountOutAndIn_InverseRelationship_ZeroSwapFee(t *testing.T) { type testcase struct { denomOut string initialPoolOut int64