From 8cd8d142d80ed7737831779b69aec9460a241a77 Mon Sep 17 00:00:00 2001 From: jelysn <129082781+jelysn@users.noreply.github.com> Date: Tue, 25 Jun 2024 00:58:28 +0800 Subject: [PATCH] Leveragelp close position update fix (#615) * fix leveragelp close * add lpTokenPrice query * fix leveragelp close --- x/leveragelp/keeper/position_close.go | 30 +++++++++++++++++++++++++-- x/leveragelp/types/errors.go | 1 + x/stablestake/keeper/debt.go | 6 +++++- x/stablestake/types/errors.go | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/x/leveragelp/keeper/position_close.go b/x/leveragelp/keeper/position_close.go index 675f346d6..2c8e56ce0 100644 --- a/x/leveragelp/keeper/position_close.go +++ b/x/leveragelp/keeper/position_close.go @@ -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) diff --git a/x/leveragelp/types/errors.go b/x/leveragelp/types/errors.go index 1b973e9a4..e936979a0 100644 --- a/x/leveragelp/types/errors.go +++ b/x/leveragelp/types/errors.go @@ -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") ) diff --git a/x/stablestake/keeper/debt.go b/x/stablestake/keeper/debt.go index b6ea00121..7b615196f 100644 --- a/x/stablestake/keeper/debt.go +++ b/x/stablestake/keeper/debt.go @@ -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) diff --git a/x/stablestake/types/errors.go b/x/stablestake/types/errors.go index b1bf097e5..83218571b 100644 --- a/x/stablestake/types/errors.go +++ b/x/stablestake/types/errors.go @@ -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") )