Skip to content

Commit

Permalink
moved up RR invariant check and removed balance check
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs committed Aug 6, 2024
1 parent c9b390d commit 059843d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
1 change: 1 addition & 0 deletions x/stakeibc/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (k Keeper) AssertStrideAndDayEpochRelationship(ctx sdk.Context) {
}
}

// TODO [cleanup]: Update to be CheckRedemptionRateWithinSafetyBound and only throw an error (instead of a bool)
// safety check: ensure the redemption rate is NOT below our min safety threshold && NOT above our max safety threshold on host zone
func (k Keeper) IsRedemptionRateWithinSafetyBounds(ctx sdk.Context, zone types.HostZone) (bool, error) {
// Get the wide bounds
Expand Down
22 changes: 7 additions & 15 deletions x/stakeibc/keeper/redeem_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -48,30 +49,20 @@ func (k Keeper) RedeemStake(ctx sdk.Context, msg *types.MsgRedeemStake) (*types.
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid receiver address (%s)", err)
}

// construct desired unstaking amount from host zone
stDenom := types.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)
nativeAmount := sdk.NewDecFromInt(msg.Amount).Mul(hostZone.RedemptionRate).TruncateInt()

if nativeAmount.GT(hostZone.TotalDelegations) {
return nil, errorsmod.Wrapf(types.ErrInvalidAmount, "cannot unstake an amount g.t. staked balance on host zone: %v", msg.Amount)
}

// safety check: redemption rate must be within safety bounds
rateIsSafe, err := k.IsRedemptionRateWithinSafetyBounds(ctx, hostZone)
if !rateIsSafe || (err != nil) {
errMsg := fmt.Sprintf("IsRedemptionRateWithinSafetyBounds check failed. hostZone: %s, err: %s", hostZone.String(), err.Error())
return nil, errorsmod.Wrapf(types.ErrRedemptionRateOutsideSafetyBounds, errMsg)
}

// safety checks on the coin
// - Redemption amount must be positive
if !nativeAmount.IsPositive() {
// construct desired unstaking amount from host zone
nativeAmount := sdk.NewDecFromInt(msg.Amount).Mul(hostZone.RedemptionRate).TruncateInt()
if nativeAmount.LTE(sdkmath.ZeroInt()) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "amount must be greater than 0. found: %v", msg.Amount)
}
// - Creator owns at least "amount" stAssets
balance := k.bankKeeper.GetBalance(ctx, sender, stDenom)
if balance.Amount.LT(msg.Amount) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "balance is lower than redemption amount. redemption amount: %v, balance %v: ", msg.Amount, balance.Amount)
if nativeAmount.GT(hostZone.TotalDelegations) {
return nil, errorsmod.Wrapf(types.ErrInvalidAmount, "cannot unstake an amount g.t. staked balance on host zone: %v", msg.Amount)
}

// ----------------- UNBONDING RECORD KEEPING -----------------
Expand Down Expand Up @@ -119,6 +110,7 @@ func (k Keeper) RedeemStake(ctx sdk.Context, msg *types.MsgRedeemStake) (*types.
}

// Escrow user's balance
stDenom := types.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)
redeemCoin := sdk.NewCoins(sdk.NewCoin(stDenom, msg.Amount))
depositAddress, err := sdk.AccAddressFromBech32(hostZone.DepositAddress)
if err != nil {
Expand Down

0 comments on commit 059843d

Please sign in to comment.