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

[LeverageLp]: Reduce the collateral by the same % as what the position is reduced #825

Merged
merged 4 commits into from
Sep 27, 2024
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/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
Loading