-
Notifications
You must be signed in to change notification settings - Fork 44
/
models_feegrant.go
162 lines (130 loc) · 3.89 KB
/
models_feegrant.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package types
import (
"fmt"
"time"
errors "cosmossdk.io/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"
proto "github.com/gogo/protobuf/proto"
)
var _ codectypes.UnpackInterfacesMessage = &Grant{}
// Grantee represents a generic grant grantee
type Grantee interface {
proto.Message
isGrantee()
Validate() error
}
// NewUserGrantee is a constructor for the UserGrantee type
func NewUserGrantee(user string) *UserGrantee {
return &UserGrantee{
User: user,
}
}
// isGrantee implements Grantee
func (t *UserGrantee) isGrantee() {}
// isGrantee implements Grantee
func (t *UserGrantee) Validate() error {
_, err := sdk.AccAddressFromBech32(t.User)
if err != nil {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid grantee address")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewGroupGrantee is a constructor for the GroupGrantee type
func NewGroupGrantee(groupID uint32) *GroupGrantee {
return &GroupGrantee{
GroupID: groupID,
}
}
// isGrantee implements Grantee
func (t *GroupGrantee) isGrantee() {}
// isGrantee implements Grantee
func (t *GroupGrantee) Validate() error {
if t.GroupID == 0 {
return fmt.Errorf("invalid group id: %d", t.GroupID)
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewGrant is a constructor for the Grant type
func NewGrant(subspaceID uint64, granter string, grantee Grantee, feeAllowance feegranttypes.FeeAllowanceI) Grant {
allowanceProto, isProto := feeAllowance.(proto.Message)
if !isProto {
panic(errors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", feeAllowance))
}
allowanceAny, err := codectypes.NewAnyWithValue(allowanceProto)
if err != nil {
panic("failed to pack allowance to any type")
}
granteeAny, err := codectypes.NewAnyWithValue(grantee)
if err != nil {
panic("failed to pack grantee to any type")
}
return Grant{
SubspaceID: subspaceID,
Granter: granter,
Grantee: granteeAny,
Allowance: allowanceAny,
}
}
// Validate implements fmt.Validator
func (g Grant) Validate() error {
if g.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", g.SubspaceID)
}
_, err := sdk.AccAddressFromBech32(g.Granter)
if err != nil {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid granter address")
}
grantee, isGrantee := g.Grantee.GetCachedValue().(Grantee)
if !isGrantee {
return fmt.Errorf("invalid grantee type: %T", grantee)
}
err = grantee.Validate()
if err != nil {
return err
}
if u, isUserGrantee := grantee.(*UserGrantee); isUserGrantee {
if u.User == g.Granter {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization")
}
}
f, err := g.GetUnpackedAllowance()
if err != nil {
return err
}
return f.ValidateBasic()
}
// GetUnpackedAllowance returns unpacked allowance
func (g Grant) GetUnpackedAllowance() (feegranttypes.FeeAllowanceI, error) {
allowance, isAllowance := g.Allowance.GetCachedValue().(feegranttypes.FeeAllowanceI)
if !isAllowance {
return nil, fmt.Errorf("failed to unpack allowance")
}
return allowance, nil
}
// GetExpiration returns the expiration time
func (g Grant) GetExpiration() *time.Time {
f, err := g.GetUnpackedAllowance()
if err != nil {
panic(err)
}
exp, err := f.ExpiresAt()
if err != nil {
panic(err)
}
return exp
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (g Grant) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var grantee Grantee
err := unpacker.UnpackAny(g.Grantee, &grantee)
if err != nil {
return err
}
var allowance feegranttypes.FeeAllowanceI
return unpacker.UnpackAny(g.Allowance, &allowance)
}