Skip to content

Commit

Permalink
add msgBeginRedelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet2019 committed Apr 30, 2024
1 parent d8e3202 commit 92dbcaf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
55 changes: 55 additions & 0 deletions x/liquidstake/keeper/liquidstake.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,61 @@ func (k Keeper) UnbondWithCap(
return lsmRedeemResp.Amount.Amount, nil
}

// RedelegateWithCap is a wrapper to invoke stakingKeeper.Redelegate but account for
// the amount of liquid staked shares and check against liquid staking cap.
func (k Keeper) RedelegateWithCap(
ctx sdk.Context,
delegatorAddress sdk.AccAddress,
validatorSrc sdk.ValAddress,
validatorDst sdk.ValAddress,
bondAmt math.Int,
) (time.Time, error) {
msgRedelegate := &stakingtypes.MsgBeginRedelegate{
DelegatorAddress: delegatorAddress.String(),
ValidatorSrcAddress: validatorSrc.String(),
ValidatorDstAddress: validatorDst.String(),
Amount: sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), bondAmt),
}
handler := k.router.Handler(msgRedelegate)
if handler == nil {
k.Logger(ctx).Error("failed to find redelegate handler")

return time.Time{}, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s", sdk.MsgTypeURL(msgRedelegate))
}
res, err := handler(ctx, msgRedelegate)
if err != nil {
k.Logger(ctx).Error(
"failed to execute redelegate msg",
types.ErrorKeyVal,
err,
types.MsgKeyVal,
msgRedelegate.String(),
)

return time.Time{}, errorsmod.Wrapf(types.ErrRedelegateFailed, "failed to send redelegate msg with err: %v", err)
}
ctx.EventManager().EmitEvents(res.GetEvents())

if len(res.MsgResponses) != 1 {
return time.Time{}, errorsmod.Wrapf(
types.ErrInvalidResponse,
"expected msg response should be exactly 1, got: %v, responses: %v",
len(res.MsgResponses), res.MsgResponses,
)
}

var msgRedelegateResponse stakingtypes.MsgBeginRedelegateResponse
if err = k.cdc.Unmarshal(res.MsgResponses[0].Value, &msgRedelegateResponse); err != nil {
return time.Time{}, errorsmod.Wrapf(
sdkerrors.ErrJSONUnmarshal,
"cannot unmarshal redelegate tx response message: %v",
err,
)
}

return msgRedelegateResponse.CompletionTime, nil
}

// LSMDelegate captures a staked amount from existing delegation using LSM, re-stakes from proxyAcc and
// mints stkXPRT worth of stk coin value according to NetAmount and performs LiquidDelegate.
func (k Keeper) LSMDelegate(
Expand Down
7 changes: 4 additions & 3 deletions x/liquidstake/keeper/rebalancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ func (k Keeper) TryRedelegation(ctx sdk.Context, re types.Redelegation) (complet
}

// calculate delShares from tokens with validation
shares, err := k.stakingKeeper.ValidateUnbondAmount(
_, err = k.stakingKeeper.ValidateUnbondAmount(
ctx, re.Delegator, srcVal, re.Amount,
)
if err != nil {
return time.Time{}, fmt.Errorf("failed to validate unbond amount: %w", err)
}

// when last, full redelegation of shares from delegation
amt := re.Amount
if re.Last {
shares = re.SrcValidator.GetDelShares(ctx, k.stakingKeeper)
amt = re.SrcValidator.GetLiquidTokens(ctx, k.stakingKeeper, false)
}
cachedCtx, writeCache := ctx.CacheContext()
completionTime, err = k.stakingKeeper.BeginRedelegation(cachedCtx, re.Delegator, srcVal, dstVal, shares)
completionTime, err = k.RedelegateWithCap(cachedCtx, re.Delegator, srcVal, dstVal, amt)
if err != nil {
return time.Time{}, fmt.Errorf("failed to begin redelegation: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions x/liquidstake/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ var (
ErrUnbondFailed = errors.Register(ModuleName, 23, "unbond failed")
ErrInvalidResponse = errors.Register(ModuleName, 24, "invalid response")
ErrUnstakeFailed = errors.Register(ModuleName, 25, "Unstaking failed")
ErrRedelegateFailed = errors.Register(ModuleName, 26, "Redelegate failed")
)

0 comments on commit 92dbcaf

Please sign in to comment.