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

fix: instant redeem #707

Merged
merged 3 commits into from
Dec 21, 2023
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
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
13 changes: 7 additions & 6 deletions 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 @@ -73,23 +74,23 @@ func (k *Keeper) GetTransactionSequenceID(channelID string, sequence uint64) str
func (k *Keeper) AdjustDepositsForRedemption(
ctx sdk.Context,
hc *liquidstakeibctypes.HostChain,
redeemableAmount sdk.Coin,
redeemAmount sdk.Coin,
) error {
redeemableDeposits, depositsAmount := k.GetRedeemableDepositsForHostChain(ctx, hc)
if depositsAmount.LT(redeemableAmount.Amount) {
return nil
if depositsAmount.LT(redeemAmount.Amount) {
return errorsmod.Wrapf(liquidstakeibctypes.ErrInsufficientDeposits, "deposits are lesser than amount to be redeemed, deposits present %s, required %s", depositsAmount.String(), redeemAmount.Amount.String())
}

for _, deposit := range redeemableDeposits {
// there is enough tokens in this deposit to fulfill the redeem request
if deposit.Amount.Amount.GT(redeemableAmount.Amount) || redeemableAmount.IsZero() {
deposit.Amount = deposit.Amount.Sub(redeemableAmount)
if deposit.Amount.Amount.GT(redeemAmount.Amount) || redeemAmount.IsZero() {
deposit.Amount = deposit.Amount.Sub(redeemAmount)
k.SetDeposit(ctx, deposit)
return nil
}

// the deposit is not enough to fulfill the redeem request, use it and remove it
redeemableAmount = redeemableAmount.Sub(deposit.Amount)
redeemAmount = redeemAmount.Sub(deposit.Amount)
k.DeleteDeposit(ctx, deposit)
}

Expand Down
9 changes: 7 additions & 2 deletions x/liquidstakeibc/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (suite *IntegrationTestSuite) TestAdjustDepositsForRedemption() {
redemptionAmount: sdk.Coin{Denom: HostDenom, Amount: sdk.NewInt(5000)},
},
{
name: "Case 2",
name: "try partial redeem",
deposits: []*types.Deposit{
{
ChainId: suite.chainB.ChainID,
Expand All @@ -119,6 +119,7 @@ func (suite *IntegrationTestSuite) TestAdjustDepositsForRedemption() {
epoch: {Denom: HostDenom, Amount: sdk.NewInt(3500)},
},
redemptionAmount: sdk.Coin{Denom: HostDenom, Amount: sdk.NewInt(5000)},
err: types.ErrInsufficientDeposits,
},
{
name: "Case 3",
Expand Down Expand Up @@ -210,7 +211,11 @@ func (suite *IntegrationTestSuite) TestAdjustDepositsForRedemption() {
t.redemptionAmount,
)

suite.Require().Equal(t.err, err)
if t.err == nil {
suite.Require().Equal(t.err, err)
} else {
suite.Require().ErrorContains(err, t.err.Error())
}

for epoch, deposit := range t.expected {
depositState, ok := suite.app.LiquidStakeIBCKeeper.GetDepositForChainAndEpoch(suite.ctx, suite.chainB.ChainID, epoch)
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")
)
Loading