-
Notifications
You must be signed in to change notification settings - Fork 17
/
msg_server_withdraw_rewards.go
42 lines (33 loc) · 1.27 KB
/
msg_server_withdraw_rewards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package keeper
import (
"context"
"github.com/KYVENetwork/chain/util"
sdkErrors "cosmossdk.io/errors"
"github.com/KYVENetwork/chain/x/delegation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// WithdrawRewards calculates the current rewards of a delegator and transfers the balance to
// the delegator's wallet. Only the delegator himself can call this transaction.
func (k msgServer) WithdrawRewards(
goCtx context.Context,
msg *types.MsgWithdrawRewards,
) (*types.MsgWithdrawRewardsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check if the sender has delegated to the given staker
if !k.DoesDelegatorExist(ctx, msg.Staker, msg.Creator) {
return nil, sdkErrors.Wrapf(types.ErrNotADelegator, "%s does not delegate to %s", msg.Creator, msg.Staker)
}
// Withdraw all rewards of the sender.
reward := k.f1WithdrawRewards(ctx, msg.Staker, msg.Creator)
// Transfer reward $KYVE from this module to sender.
if err := util.TransferFromModuleToAddress(k.bankKeeper, ctx, types.ModuleName, msg.Creator, reward); err != nil {
return nil, err
}
// Emit a delegation event.
_ = ctx.EventManager().EmitTypedEvent(&types.EventWithdrawRewards{
Address: msg.Creator,
Staker: msg.Staker,
Amount: reward,
})
return &types.MsgWithdrawRewardsResponse{}, nil
}