From 5d848e054e3d8b086ad5427900ffee9096ee6de9 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Fri, 27 Sep 2024 23:22:35 +0530 Subject: [PATCH] [LeverageLp]: Reduce the collateral by the same % as what the position is reduced (#825) * reduce same amount of collateral * add test --- x/leveragelp/keeper/position_close.go | 5 +++++ x/leveragelp/keeper/position_close_test.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/x/leveragelp/keeper/position_close.go b/x/leveragelp/keeper/position_close.go index 4b4a6d060..c19219bad 100644 --- a/x/leveragelp/keeper/position_close.go +++ b/x/leveragelp/keeper/position_close.go @@ -27,6 +27,11 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty repayAmount := debt.GetTotalLiablities().Mul(lpAmount).Quo(position.LeveragedLpAmount) + // Set collateral to same % as reduction in LP position + ratio := lpAmount.ToLegacyDec().Quo(position.LeveragedLpAmount.ToLegacyDec()) + collateralLeft := position.Collateral.Amount.Sub(position.Collateral.Amount.ToLegacyDec().Mul(ratio).TruncateInt()) + position.Collateral.Amount = collateralLeft + // Check if position has enough coins to repay else repay partial bal := k.bankKeeper.GetBalance(ctx, position.GetPositionAddress(), position.Collateral.Denom) userAmount := sdk.ZeroInt() diff --git a/x/leveragelp/keeper/position_close_test.go b/x/leveragelp/keeper/position_close_test.go index 6ca436997..c85a3e118 100644 --- a/x/leveragelp/keeper/position_close_test.go +++ b/x/leveragelp/keeper/position_close_test.go @@ -169,6 +169,10 @@ func (suite KeeperTestSuite) TestForceCloseLongPartial() { repayAmountOut, err := k.ForceCloseLong(suite.ctx, *position, pool, position.LeveragedLpAmount.Quo(sdk.NewInt(2)), false) suite.Require().NoError(err) suite.Require().Equal(repayAmount.Quo(sdk.NewInt(2)).String(), repayAmountOut.String()) + + // Collateral should be reduced by 50% + after, _ := k.GetPosition(suite.ctx, addr, 1) + suite.Require().Equal(position.Collateral.Amount.Quo(sdk.NewInt(2)).String(), after.Collateral.Amount.String()) } func (suite KeeperTestSuite) TestHealthDecreaseForInterest() {