Skip to content

Commit

Permalink
[LeverageLp]: Reduce the collateral by the same % as what the positio…
Browse files Browse the repository at this point in the history
…n is reduced (#825)

* reduce same amount of collateral

* add test
  • Loading branch information
amityadav0 authored Sep 27, 2024
1 parent 87336c8 commit 5d848e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions x/leveragelp/keeper/position_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions x/leveragelp/keeper/position_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5d848e0

Please sign in to comment.