Skip to content

Commit

Permalink
make adjust deposits to throw error instead of nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet2019 committed Dec 21, 2023
1 parent b6a04c8 commit 6569a47
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x/liquidstakeibc/keeper/deposit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"strconv"

"cosmossdk.io/math"
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 2 additions & 16 deletions x/liquidstakeibc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions x/liquidstakeibc/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit 6569a47

Please sign in to comment.