Skip to content

Commit

Permalink
fixed exit from staking reward without locking
Browse files Browse the repository at this point in the history
  • Loading branch information
faneaatiku committed May 30, 2024
1 parent 9c7b98c commit cc429a2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
15 changes: 13 additions & 2 deletions x/rewards/keeper/msg_server_exit_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func (k msgServer) ExitStaking(goCtx context.Context, msg *types.MsgExitStaking)
return nil, err
}

k.beginUnlock(ctx, participation, stakingReward)
err = k.beginUnlock(ctx, participation, stakingReward)
if err != nil {
return nil, err
}

k.RemoveStakingRewardParticipant(ctx, participation.Address, participation.RewardId)

remainingStakedAmount := stakedAmountInt.Sub(partCoins.AmountOf(stakingReward.StakingDenom))
Expand Down Expand Up @@ -78,7 +82,7 @@ func (k msgServer) ExitStaking(goCtx context.Context, msg *types.MsgExitStaking)
return &types.MsgExitStakingResponse{}, nil
}

func (k msgServer) beginUnlock(ctx sdk.Context, p types.StakingRewardParticipant, sr types.StakingReward) {
func (k msgServer) beginUnlock(ctx sdk.Context, p types.StakingRewardParticipant, sr types.StakingReward) error {
lockedUntil := k.epochKeeper.GetEpochCountByIdentifier(ctx, expirationEpoch)
lockedUntil += int64(sr.Lock) * 24
pending := types.PendingUnlockParticipant{
Expand All @@ -88,5 +92,12 @@ func (k msgServer) beginUnlock(ctx sdk.Context, p types.StakingRewardParticipant
Denom: sr.StakingDenom,
}

//in case the lock is 0 send the funds immediately
if sr.Lock == 0 {
return k.performUnlock(ctx, &pending)
}

k.SetPendingUnlockParticipant(ctx, pending)

return nil
}
32 changes: 20 additions & 12 deletions x/rewards/keeper/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@ func (k Keeper) UnlockAllPendingUnlockParticipantsByEpoch(ctx sdk.Context, epoch
pending := k.GetAllEpochPendingUnlockParticipant(ctx, epochNumber)
for _, p := range pending {
logger := k.Logger(ctx).With("pending_unlock", p)
partCoins, err := k.getAmountToCapture("", p.Denom, p.Amount, int64(1))
if err != nil {
logger.Error(err.Error())
continue
}

acc, err := sdk.AccAddressFromBech32(p.Address)
if err != nil {
logger.Error(err.Error())
continue
}

err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, acc, partCoins)
err := k.performUnlock(ctx, &p)
if err != nil {
logger.Error(err.Error())
continue
Expand All @@ -30,3 +19,22 @@ func (k Keeper) UnlockAllPendingUnlockParticipantsByEpoch(ctx sdk.Context, epoch
k.RemovePendingUnlockParticipant(ctx, p)
}
}

func (k Keeper) performUnlock(ctx sdk.Context, p *types.PendingUnlockParticipant) error {
partCoins, err := k.getAmountToCapture("", p.Denom, p.Amount, int64(1))
if err != nil {
return err
}

acc, err := sdk.AccAddressFromBech32(p.Address)
if err != nil {
return err
}

err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, acc, partCoins)
if err != nil {
return err
}

return nil
}

0 comments on commit cc429a2

Please sign in to comment.