From 6569a47e1c98d4bb02fc9cd9f106efc8325c180b Mon Sep 17 00:00:00 2001 From: puneetmahajan Date: Thu, 21 Dec 2023 10:23:13 +0530 Subject: [PATCH] make adjust deposits to throw error instead of nil. --- CHANGELOG.md | 6 ++++++ x/liquidstakeibc/keeper/deposit.go | 3 ++- x/liquidstakeibc/keeper/msg_server.go | 18 ++---------------- x/liquidstakeibc/types/errors.go | 1 + 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a8270149..74cdaf63c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## Unreleased + +### Bug Fixes + +- [707](https://github.com/persistenceOne/pstake-native/pull/707) Fix liquidstakeibc redeem edge case for protecting cValue + ## [v2.8.0] - 2023-12-20 ### Features diff --git a/x/liquidstakeibc/keeper/deposit.go b/x/liquidstakeibc/keeper/deposit.go index aafce4b9d..b6722b62e 100644 --- a/x/liquidstakeibc/keeper/deposit.go +++ b/x/liquidstakeibc/keeper/deposit.go @@ -1,6 +1,7 @@ package keeper import ( + errorsmod "cosmossdk.io/errors" "strconv" "cosmossdk.io/math" @@ -77,7 +78,7 @@ func (k *Keeper) AdjustDepositsForRedemption( ) error { redeemableDeposits, depositsAmount := k.GetRedeemableDepositsForHostChain(ctx, hc) if depositsAmount.LT(redeemableAmount.Amount) { - return nil + return errorsmod.Wrapf(liquidstakeibctypes.ErrInsufficientDeposits, "deposits are lesser than amount to be redeemed, deposits present %s, required %s", depositsAmount.String(), redeemableAmount.Amount.String()) } for _, deposit := range redeemableDeposits { diff --git a/x/liquidstakeibc/keeper/msg_server.go b/x/liquidstakeibc/keeper/msg_server.go index 1113e770f..6f4b4253f 100644 --- a/x/liquidstakeibc/keeper/msg_server.go +++ b/x/liquidstakeibc/keeper/msg_server.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/telemetry" sdktypes "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" @@ -791,20 +790,6 @@ func (k msgServer) Redeem( redeemToken, _ := sdktypes.NewDecCoinFromDec(hc.IBCDenom(), redeemAmount).TruncateDecimal() // check if there is enough deposits to fulfill the instant redemption request - depositAccountBalance := k.bankKeeper.GetBalance( - ctx, - authtypes.NewModuleAddress(types.DepositModuleAccount), - hc.IBCDenom(), - ) - if redeemToken.IsGTE(depositAccountBalance) { - return nil, errorsmod.Wrapf( - sdkerrors.ErrInsufficientFunds, - "can't instant redeem %s tokens, only %s is available", - redeemToken.String(), - depositAccountBalance.Amount.String(), - ) - } - // subtract the redemption amount from the deposits if err := k.AdjustDepositsForRedemption(ctx, hc, redeemToken); err != nil { return nil, errorsmod.Wrapf( @@ -813,7 +798,8 @@ func (k msgServer) Redeem( ) } - // send the instant redeemed token from module to the account + // send the instant redeemed token from module to the account, + //this will error out if there are insufficient redeemTokens err = k.bankKeeper.SendCoinsFromModuleToAccount( ctx, types.DepositModuleAccount, diff --git a/x/liquidstakeibc/types/errors.go b/x/liquidstakeibc/types/errors.go index 68add5181..fc4c432d0 100644 --- a/x/liquidstakeibc/types/errors.go +++ b/x/liquidstakeibc/types/errors.go @@ -27,4 +27,5 @@ var ( ErrLSMNotEnabled = errorsmod.Register(ModuleName, 2019, "host chain has LSM staking disabled") ErrLSMDepositProcessing = errorsmod.Register(ModuleName, 2020, "already processing LSM deposit") ErrLSMValidatorInvalidState = errorsmod.Register(ModuleName, 2021, "validator invalid state") + ErrInsufficientDeposits = errorsmod.Register(ModuleName, 2022, "insufficent deposits") )