From d0239e90e12ba4bec3b1607bc2751e0881593ee2 Mon Sep 17 00:00:00 2001 From: alpo <62043214+AlpinYukseloglu@users.noreply.github.com> Date: Tue, 14 Jun 2022 21:23:36 -0500 Subject: [PATCH] Tests: Edge case tests for CalcJoinPoolShares (#1776) * edge case tests for CalcJoinPoolShares * add test comments * move tests to increase coverage and add further comments * remove expectLiq from new tests * fix conflicts Co-authored-by: Roman --- .../pool-models/balancer/amm_joinpool_test.go | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/x/gamm/pool-models/balancer/amm_joinpool_test.go b/x/gamm/pool-models/balancer/amm_joinpool_test.go index ef4adefc9b6..8419de7528d 100644 --- a/x/gamm/pool-models/balancer/amm_joinpool_test.go +++ b/x/gamm/pool-models/balancer/amm_joinpool_test.go @@ -379,6 +379,46 @@ var calcSingleAssetJoinTestCases = []calcJoinSharesTestCase{ expectShares: sdk.ZeroInt(), expErr: sdkerrors.Wrapf(types.ErrDenomNotFoundInPool, fmt.Sprintf(balancer.ErrMsgFormatNoPoolAssetFound, doesNotExistDenom)), }, + { + // Pool liquidity is changed by 1e-12 / 2 + // P_issued = 1e20 * 1e-12 / 2 = 1e8 / 2 = 50_000_000 + name: "minimum input single asset equal liquidity", + swapFee: sdk.MustNewDecFromStr("0"), + poolAssets: []balancer.PoolAsset{ + { + Token: sdk.NewInt64Coin("uosmo", 1_000_000_000_000), + Weight: sdk.NewInt(100), + }, + { + Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000), + Weight: sdk.NewInt(100), + }, + }, + tokensIn: sdk.NewCoins( + sdk.NewInt64Coin("uosmo", 1), + ), + expectShares: sdk.NewInt(50_000_000), + }, + { + // P_issued should be 1/10th that of the previous test + // p_issued = 50_000_000 / 10 = 5_000_000 + name: "minimum input single asset imbalanced liquidity", + swapFee: sdk.MustNewDecFromStr("0"), + poolAssets: []balancer.PoolAsset{ + { + Token: sdk.NewInt64Coin("uosmo", 10_000_000_000_000), + Weight: sdk.NewInt(100), + }, + { + Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000), + Weight: sdk.NewInt(100), + }, + }, + tokensIn: sdk.NewCoins( + sdk.NewInt64Coin("uosmo", 1), + ), + expectShares: sdk.NewInt(5_000_000), + }, } func (suite *KeeperTestSuite) TestCalcJoinPoolShares() { @@ -501,6 +541,49 @@ func (suite *KeeperTestSuite) TestCalcJoinPoolShares() { ), expectShares: sdk.NewInt(1250000000000 + 609374990000), }, + { + // This test doubles the liquidity in a fresh pool, so it should generate the base number of LP shares for pool creation as new shares + // This is set to 1e20 (or 100 * 10^18) for Osmosis, so we should expect: + // P_issued = 1e20 + name: "minimum input with two assets and minimum liquidity", + swapFee: sdk.MustNewDecFromStr("0"), + poolAssets: []balancer.PoolAsset{ + { + Token: sdk.NewInt64Coin("uosmo", 1), + Weight: sdk.NewInt(100), + }, + { + Token: sdk.NewInt64Coin("uatom", 1), + Weight: sdk.NewInt(100), + }, + }, + tokensIn: sdk.NewCoins( + sdk.NewInt64Coin("uosmo", 1), + sdk.NewInt64Coin("uatom", 1), + ), + expectShares: sdk.NewInt(1e18).Mul(sdk.NewInt(100)), + }, + { + // Pool liquidity is changed by 1e-12 + // P_issued = 1e20 * 1e-12 = 1e8 + name: "minimum input two assets equal liquidity", + swapFee: sdk.MustNewDecFromStr("0"), + poolAssets: []balancer.PoolAsset{ + { + Token: sdk.NewInt64Coin("uosmo", 1_000_000_000_000), + Weight: sdk.NewInt(100), + }, + { + Token: sdk.NewInt64Coin("uatom", 1_000_000_000_000), + Weight: sdk.NewInt(100), + }, + }, + tokensIn: sdk.NewCoins( + sdk.NewInt64Coin("uosmo", 1), + sdk.NewInt64Coin("uatom", 1), + ), + expectShares: sdk.NewInt(100_000_000), + }, } testCases = append(testCases, calcSingleAssetJoinTestCases...)