Skip to content

Commit

Permalink
fix: fix bug in PositionAssets when position liquidity is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcre committed Sep 13, 2023
1 parent 3d04fb1 commit d36a931
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions x/amm/keeper/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 9 additions & 0 deletions x/liquidstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion x/liquidstaking/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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))
}

0 comments on commit d36a931

Please sign in to comment.