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

fix: fix bug in PositionAssets when position liquidity is zero #210

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
25 changes: 24 additions & 1 deletion x/amm/keeper/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
}
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))
}
Loading