forked from baron-chain/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrant.go
71 lines (58 loc) · 1.76 KB
/
grant.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
package feegrant
import (
"github.com/cosmos/gogoproto/proto"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var _ types.UnpackInterfacesMessage = &Grant{}
// NewGrant creates a new FeeAllowanceGrant.
//
//nolint:interfacer
func NewGrant(granter, grantee sdk.AccAddress, feeAllowance FeeAllowanceI) (Grant, error) {
msg, ok := feeAllowance.(proto.Message)
if !ok {
return Grant{}, errorsmod.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", feeAllowance)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return Grant{}, err
}
return Grant{
Granter: granter.String(),
Grantee: grantee.String(),
Allowance: any,
}, nil
}
// ValidateBasic performs basic validation on
// FeeAllowanceGrant
func (a Grant) ValidateBasic() error {
if a.Granter == "" {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "missing granter address")
}
if a.Grantee == "" {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "missing grantee address")
}
if a.Grantee == a.Granter {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization")
}
f, err := a.GetGrant()
if err != nil {
return err
}
return f.ValidateBasic()
}
// GetGrant unpacks allowance
func (a Grant) GetGrant() (FeeAllowanceI, error) {
allowance, ok := a.Allowance.GetCachedValue().(FeeAllowanceI)
if !ok {
return nil, errorsmod.Wrap(ErrNoAllowance, "failed to get allowance")
}
return allowance, nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (a Grant) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var allowance FeeAllowanceI
return unpacker.UnpackAny(a.Allowance, &allowance)
}