/
msg_server_treasury.go
106 lines (89 loc) · 3.83 KB
/
msg_server_treasury.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v5/x/subspaces/types"
)
// GrantTreasuryAuthorization defines a rpc method for MsgGrantTreasuryAuthorization
func (k msgServer) GrantTreasuryAuthorization(goCtx context.Context, msg *types.MsgGrantTreasuryAuthorization) (*types.MsgGrantTreasuryAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
subspace, found := k.GetSubspace(ctx, msg.SubspaceID)
if !found {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("subspace with id %d not found", msg.SubspaceID)
}
if !k.HasPermission(ctx, msg.SubspaceID, types.RootSectionID, msg.Granter, types.PermissionManageTreasuryAuthorization) {
return nil, types.ErrPermissionDenied.Wrap("you cannot manage this subspace's treasury authorizations")
}
treasury, err := sdk.AccAddressFromBech32(subspace.Treasury)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid treasury address: %s", treasury.String())
}
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", msg.Grantee)
}
k.createAccountIfNotExists(ctx, grantee.String())
authorization, err := msg.Grant.GetAuthorization()
if err != nil {
return nil, err
}
err = k.authzk.SaveGrant(ctx, grantee, treasury, authorization, msg.Grant.Expiration)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeyAction, sdk.MsgTypeURL(msg)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter),
),
sdk.NewEvent(
types.EventTypeGrantTreasuryAuthorization,
sdk.NewAttribute(types.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyGranter, msg.Granter),
sdk.NewAttribute(types.AttributeKeyGrantee, msg.Grantee),
),
})
return &types.MsgGrantTreasuryAuthorizationResponse{}, nil
}
// RevokeTreasuryAuthorization defines a rpc method for MsgRevokeTreasuryAuthorization
func (k msgServer) RevokeTreasuryAuthorization(goCtx context.Context, msg *types.MsgRevokeTreasuryAuthorization) (*types.MsgRevokeTreasuryAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
subspace, found := k.GetSubspace(ctx, msg.SubspaceID)
if !found {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("subspace with id %d not found", msg.SubspaceID)
}
if !k.HasPermission(ctx, msg.SubspaceID, types.RootSectionID, msg.Granter, types.PermissionManageTreasuryAuthorization) {
return nil, types.ErrPermissionDenied.Wrap("you cannot manage this subspace's treasury authorizations")
}
treasury, err := sdk.AccAddressFromBech32(subspace.Treasury)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid treasury address: %s", treasury.String())
}
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", msg.Grantee)
}
err = k.authzk.DeleteGrant(ctx, grantee, treasury, msg.MsgTypeUrl)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeyAction, sdk.MsgTypeURL(msg)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter),
),
sdk.NewEvent(
types.EventTypeRevokeTreasuryAuthorization,
sdk.NewAttribute(types.AttributeKeySubspaceID, fmt.Sprintf("%d", msg.SubspaceID)),
sdk.NewAttribute(types.AttributeKeyGranter, msg.Granter),
sdk.NewAttribute(types.AttributeKeyGrantee, msg.Grantee),
),
})
return &types.MsgRevokeTreasuryAuthorizationResponse{}, nil
}