-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathmsg_server.go
105 lines (83 loc) · 2.7 KB
/
msg_server.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
package keeper
import (
"context"
"strings"
"cosmossdk.io/core/event"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/feegrant"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the feegrant MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(k Keeper) feegrant.MsgServer {
return &msgServer{
Keeper: k,
}
}
var _ feegrant.MsgServer = msgServer{}
// GrantAllowance grants an allowance from the granter's funds to be used by the grantee.
func (k msgServer) GrantAllowance(ctx context.Context, msg *feegrant.MsgGrantAllowance) (*feegrant.MsgGrantAllowanceResponse, error) {
if strings.EqualFold(msg.Grantee, msg.Granter) {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization")
}
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, err
}
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, err
}
if f, _ := k.GetAllowance(ctx, granter, grantee); f != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "fee allowance already exists")
}
allowance, err := msg.GetFeeAllowanceI()
if err != nil {
return nil, err
}
if err := allowance.ValidateBasic(); err != nil {
return nil, err
}
err = k.Keeper.GrantAllowance(ctx, granter, grantee, allowance)
if err != nil {
return nil, err
}
return &feegrant.MsgGrantAllowanceResponse{}, nil
}
// RevokeAllowance revokes a fee allowance between a granter and grantee.
func (k msgServer) RevokeAllowance(ctx context.Context, msg *feegrant.MsgRevokeAllowance) (*feegrant.MsgRevokeAllowanceResponse, error) {
if msg.Grantee == msg.Granter {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different")
}
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, err
}
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, err
}
err = k.Keeper.revokeAllowance(ctx, granter, grantee)
if err != nil {
return nil, err
}
return &feegrant.MsgRevokeAllowanceResponse{}, nil
}
// PruneAllowances removes expired allowances from the store.
func (k msgServer) PruneAllowances(ctx context.Context, req *feegrant.MsgPruneAllowances) (*feegrant.MsgPruneAllowancesResponse, error) {
// 75 is an arbitrary value, we can change it later if needed
err := k.RemoveExpiredAllowances(ctx, 75)
if err != nil {
return nil, err
}
if err := k.EventService.EventManager(ctx).EmitKV(
feegrant.EventTypePruneFeeGrant,
event.NewAttribute(feegrant.AttributeKeyPruner, req.Pruner),
); err != nil {
return nil, err
}
return &feegrant.MsgPruneAllowancesResponse{}, nil
}