Skip to content

Commit

Permalink
Leveragelp close position update fix (#615)
Browse files Browse the repository at this point in the history
* fix leveragelp close

* add lpTokenPrice query

* fix leveragelp close
  • Loading branch information
jelysn authored Jun 24, 2024
1 parent d802dbd commit 8cd8d14
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
30 changes: 28 additions & 2 deletions x/leveragelp/keeper/position_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,38 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty
}

userAmount := exitCoins[0].Amount.Sub(repayAmount)
positionOwner := sdk.MustAccAddressFromBech32(position.Address)
err = k.bankKeeper.SendCoins(ctx, position.GetPositionAddress(), positionOwner, sdk.Coins{sdk.NewCoin(position.Collateral.Denom, userAmount)})
if userAmount.IsNegative() {
return sdk.ZeroInt(), types.ErrNegUserAmountAfterRepay
}
if userAmount.IsPositive() {
positionOwner := sdk.MustAccAddressFromBech32(position.Address)
err = k.bankKeeper.SendCoins(ctx, position.GetPositionAddress(), positionOwner, sdk.Coins{sdk.NewCoin(position.Collateral.Denom, userAmount)})
if err != nil {
return sdk.ZeroInt(), err
}
}

// Update the pool health.
pool.LeveragedLpAmount = pool.LeveragedLpAmount.Sub(lpAmount)
k.UpdatePoolHealth(ctx, &pool)

ammPool, found := k.amm.GetPool(ctx, position.AmmPoolId)
if !found {
return sdk.ZeroInt(), types.ErrAmmPoolNotFound
}

// Update position health
positionHealth, err := k.GetPositionHealth(ctx, position, ammPool)
if err != nil {
return sdk.ZeroInt(), err
}
position.PositionHealth = positionHealth

// Update Liabilities
debt = k.stableKeeper.UpdateInterestStackedByAddress(ctx, position.GetPositionAddress())
position.Liabilities = debt.Borrowed

// Update leveragedLpAmount
position.LeveragedLpAmount = position.LeveragedLpAmount.Sub(lpAmount)
if position.LeveragedLpAmount.IsZero() {
err = k.DestroyPosition(ctx, position.Address, position.Id)
Expand Down
1 change: 1 addition & 0 deletions x/leveragelp/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ var (
ErrOnlyBaseCurrencyAllowed = errorsmod.Register(ModuleName, 35, "only base currency is allowed for leverage lp")
ErrInsufficientUsdcAfterOp = errorsmod.Register(ModuleName, 36, "insufficient amount of usdc after the operation for leveragelp withdrawal")
ErrInvalidCloseSize = errorsmod.Register(ModuleName, 37, "invalid close size")
ErrNegUserAmountAfterRepay = errorsmod.Register(ModuleName, 38, "negative user amount after repay")
)
6 changes: 5 additions & 1 deletion x/stablestake/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ func (k Keeper) Repay(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) err
repayAmount := amount.Amount.Sub(interestPayAmount)
debt.Borrowed = debt.Borrowed.Sub(repayAmount)

if !debt.Borrowed.IsPositive() {
if debt.Borrowed.IsNegative() {
return types.ErrNegativeBorrowed
}

if debt.Borrowed.IsZero() {
k.DeleteDebt(ctx, debt)
} else {
k.SetDebt(ctx, debt)
Expand Down
1 change: 1 addition & 0 deletions x/stablestake/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
ErrInvalidDepositDenom = errorsmod.Register(ModuleName, 1, "invalid deposit denom")
ErrInvalidBorrowDenom = errorsmod.Register(ModuleName, 2, "invalid borrow denom")
ErrRedemptionRateIsZero = errorsmod.Register(ModuleName, 3, "redemption rate is zero")
ErrNegativeBorrowed = errorsmod.Register(ModuleName, 4, "negative borrowed amount")
)

0 comments on commit 8cd8d14

Please sign in to comment.