From e629881866e9828bf38440b775dab02fdc8aad2f Mon Sep 17 00:00:00 2001 From: King Date: Wed, 13 Sep 2023 11:38:47 +0900 Subject: [PATCH] fix: fix bug in PositionAssets when position liquidity is zero --- x/amm/keeper/position.go | 5 +++++ x/amm/keeper/position_test.go | 25 ++++++++++++++++++++++++- x/liquidstaking/keeper/keeper_test.go | 9 +++++++++ x/liquidstaking/keeper/tally_test.go | 8 +++++++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/x/amm/keeper/position.go b/x/amm/keeper/position.go index b4930ffe..d67d11de 100644 --- a/x/amm/keeper/position.go +++ b/x/amm/keeper/position.go @@ -190,6 +190,11 @@ func (k Keeper) PositionAssets(ctx sdk.Context, positionId uint64) (coin0, coin1 return coin0, coin1, sdkerrors.Wrap(sdkerrors.ErrNotFound, "position not found") } pool := k.MustGetPool(ctx, position.PoolId) + if position.Liquidity.IsZero() { + coin0 = sdk.NewInt64Coin(pool.Denom0, 0) + coin1 = sdk.NewInt64Coin(pool.Denom1, 0) + return + } ctx, _ = ctx.CacheContext() _, amt0, amt1 := k.modifyPosition( ctx, pool, position.MustGetOwnerAddress(), position.LowerTick, position.UpperTick, position.Liquidity.Neg()) diff --git a/x/amm/keeper/position_test.go b/x/amm/keeper/position_test.go index 1f47e75f..2ef00fa6 100644 --- a/x/amm/keeper/position_test.go +++ b/x/amm/keeper/position_test.go @@ -97,7 +97,7 @@ func (s *KeeperTestSuite) TestRewardsPool() { lpAddr, pool2.Id, utils.ParseDec("9"), utils.ParseDec("12"), utils.ParseCoins("100_000000uatom,1000_000000uusd")) ordererAddr := s.FundedAccount(2, enoughCoins) - s.PlaceLimitOrder(market1.Id, ordererAddr, true, utils.ParseDec("6"), sdk.NewDec(1_000000),0 ) + s.PlaceLimitOrder(market1.Id, ordererAddr, true, utils.ParseDec("6"), sdk.NewDec(1_000000), 0) s.PlaceLimitOrder(market2.Id, ordererAddr, false, utils.ParseDec("9"), sdk.NewDec(1_000000), 0) s.AssertEqual(utils.ParseCoins("1499ucre,2620uusd"), s.GetAllBalances(pool1.MustGetRewardsPoolAddress())) @@ -139,3 +139,26 @@ func (s *KeeperTestSuite) TestLastRemoveLiquidity() { // No balances left in the pool. s.AssertEqual(sdk.Coins{}, s.GetAllBalances(pool.MustGetReserveAddress())) } + +func (s *KeeperTestSuite) TestPositionAssets_ZeroLiquidity() { + _, pool := s.CreateMarketAndPool("ucre", "uusd", utils.ParseDec("5")) + + lpAddr1 := s.FundedAccount(1, enoughCoins) + + position, _, _ := s.AddLiquidity( + lpAddr1, pool.Id, utils.ParseDec("4"), utils.ParseDec("6"), + utils.ParseCoins("100_000000ucre,500_000000uusd")) + + coin0, coin1, err := s.keeper.PositionAssets(s.Ctx, position.Id) + s.Require().NoError(err) + s.AssertEqual(utils.ParseCoin("82529840ucre"), coin0) + s.AssertEqual(utils.ParseCoin("499999999uusd"), coin1) + + // Remove all liquidity from the position. + s.RemoveLiquidity(lpAddr1, position.Id, position.Liquidity) + + coin0, coin1, err = s.keeper.PositionAssets(s.Ctx, position.Id) + s.Require().NoError(err) + s.AssertEqual(utils.ParseCoin("0ucre"), coin0) + s.AssertEqual(utils.ParseCoin("0uusd"), coin1) +} diff --git a/x/liquidstaking/keeper/keeper_test.go b/x/liquidstaking/keeper/keeper_test.go index 9499d820..6b443b31 100644 --- a/x/liquidstaking/keeper/keeper_test.go +++ b/x/liquidstaking/keeper/keeper_test.go @@ -366,6 +366,15 @@ func (s *KeeperTestSuite) addLiquidity( return } +func (s *KeeperTestSuite) removeLiquidity( + ownerAddr sdk.AccAddress, positionId uint64, liquidity sdk.Int) (position ammtypes.Position, amt sdk.Coins) { + s.T().Helper() + var err error + position, amt, err = s.app.AMMKeeper.RemoveLiquidity(s.ctx, ownerAddr, ownerAddr, positionId, liquidity) + s.Require().NoError(err) + return +} + func (s *KeeperTestSuite) createPublicPosition( poolId uint64, lowerPrice, upperPrice sdk.Dec, minBidAmt sdk.Int, feeRate sdk.Dec) liquidammtypes.PublicPosition { s.T().Helper() diff --git a/x/liquidstaking/keeper/tally_test.go b/x/liquidstaking/keeper/tally_test.go index fd72b02b..d92d0ed9 100644 --- a/x/liquidstaking/keeper/tally_test.go +++ b/x/liquidstaking/keeper/tally_test.go @@ -569,7 +569,7 @@ func (s *KeeperTestSuite) TestCalcLiquidStakingVotingPower() { s.Require().Equal(sdk.NewInt(200_000000), s.keeper.CalcLiquidStakingVotingPower(s.ctx, delB)) _, pool := s.createMarketAndPool(params.LiquidBondDenom, sdk.DefaultBondDenom, utils.ParseDec("1")) - s.addLiquidity( + position1, _, _ := s.addLiquidity( delA, pool.Id, utils.ParseDec("1"), utils.ParseDec("1.2"), sdk.NewCoins( sdk.NewInt64Coin(params.LiquidBondDenom, 10_000000), @@ -609,4 +609,10 @@ func (s *KeeperTestSuite) TestCalcLiquidStakingVotingPower() { // There might be a small amount of error again. s.Require().Equal(sdk.NewInt(99_999998), s.keeper.CalcLiquidStakingVotingPower(s.ctx, delA)) s.Require().Equal(sdk.NewInt(199_999998), s.keeper.CalcLiquidStakingVotingPower(s.ctx, delB)) + + // Remove all liquidity + s.removeLiquidity(delA, position1.Id, position1.Liquidity) + + s.Require().Equal(sdk.NewInt(99_999998), s.keeper.CalcLiquidStakingVotingPower(s.ctx, delA)) + s.Require().Equal(sdk.NewInt(199_999998), s.keeper.CalcLiquidStakingVotingPower(s.ctx, delB)) }