/
msg_server_unstake.go
77 lines (61 loc) · 2.13 KB
/
msg_server_unstake.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/rarimo/rarimo-core/x/rarimocore/types"
)
func (k msgServer) Unstake(goCtx context.Context, msg *types.MsgUnstake) (*types.MsgUnstakeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
// Only active party can unstake
if err := k.checkIsAnActiveParty(ctx, msg.Account); err != nil {
return nil, err
}
// Exists (non nil) cause checkIsAnActiveParty passes
party := getPartyByAccount(msg.Account, params.Parties)
// Only delegator or party can unstake
if msg.Creator != party.Account && msg.Creator != party.Delegator {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "invalid party account: not authorized")
}
// Choosing the receiver
receiver := party.Account
if party.Delegator != "" {
receiver = party.Delegator
}
if err := k.unstake(ctx, receiver); err != nil {
return nil, err
}
// Removing party from parties list
parties := make([]*types.Party, 0)
for _, p := range params.Parties {
if p.Account != msg.Account {
parties = append(parties, p)
}
}
params.Parties = parties
// Every parties list changes requires keys resharing
params.IsUpdateRequired = true
k.SetParams(ctx, params)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeUnstaked,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Creator),
sdk.NewAttribute(types.AttributeKeyPartyAccount, msg.Account),
),
})
return &types.MsgUnstakeResponse{}, nil
}
func (k Keeper) unstake(ctx sdk.Context, receiver string) error {
receiverAddr, _ := sdk.AccAddressFromBech32(receiver)
stakeCoin, err := k.getStakeCoin(ctx)
if err != nil {
return sdkerrors.Wrap(err, "failed to get stake coin")
}
// Transferring tokens from module account to receiver
if err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receiverAddr, sdk.NewCoins(stakeCoin)); err != nil {
return sdkerrors.Wrapf(err, "failed to send coins from module to account, account: %s", receiver)
}
return nil
}