-
Notifications
You must be signed in to change notification settings - Fork 18
/
api_feegrant.go
142 lines (130 loc) · 4.82 KB
/
api_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
package client
import (
"context"
"time"
"cosmossdk.io/math"
gnfdsdktypes "github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
)
type IFeeGrantClient interface {
GrantBasicAllowance(ctx context.Context, granteeAddr string, feeAllowanceAmount math.Int, expiration *time.Time, txOption gnfdsdktypes.TxOption) (string, error)
QueryBasicAllowance(ctx context.Context, granterAddr, granteeAddr string) (*feegrant.BasicAllowance, error)
// for generic allowance(BasicAllowance, PeriodicAllowance, AllowedMsgAllowance)
GrantAllowance(ctx context.Context, granteeAddr string, allowance feegrant.FeeAllowanceI, txOption gnfdsdktypes.TxOption) (string, error)
QueryAllowance(ctx context.Context, granterAddr, granteeAddr string) (*feegrant.Grant, error)
QueryAllowances(ctx context.Context, granteeAddr string) ([]*feegrant.Grant, error)
RevokeAllowance(ctx context.Context, granteeAddr string, txOption gnfdsdktypes.TxOption) (string, error)
}
// GrantBasicAllowance grants the grantee the BasicAllowance with specified amount and expiration.
func (c *Client) GrantBasicAllowance(ctx context.Context, granteeAddr string, feeAllowanceAmount math.Int, expiration *time.Time, txOption gnfdsdktypes.TxOption) (string, error) {
grantee, err := sdk.AccAddressFromHexUnsafe(granteeAddr)
if err != nil {
return "", err
}
bnb := sdk.NewCoins(sdk.NewCoin(gnfdsdktypes.Denom, feeAllowanceAmount))
allowance := feegrant.BasicAllowance{
SpendLimit: bnb,
Expiration: expiration,
}
msg, err := feegrant.NewMsgGrantAllowance(&allowance, c.defaultAccount.GetAddress(), grantee)
if err != nil {
return "", err
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// GrantAllowance provides a generic way to grant different types of allowance(BasicAllowance, PeriodicAllowance, AllowedMsgAllowance), the user needs to construct the desired type of allowance
func (c *Client) GrantAllowance(ctx context.Context, granteeAddr string, allowance feegrant.FeeAllowanceI, txOption gnfdsdktypes.TxOption) (string, error) {
grantee, err := sdk.AccAddressFromHexUnsafe(granteeAddr)
if err != nil {
return "", err
}
msg, err := feegrant.NewMsgGrantAllowance(allowance, c.defaultAccount.GetAddress(), grantee)
if err != nil {
return "", err
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// RevokeAllowance revokes allowance on a grantee by the granter
func (c *Client) RevokeAllowance(ctx context.Context, granteeAddr string, txOption gnfdsdktypes.TxOption) (string, error) {
grantee, err := sdk.AccAddressFromHexUnsafe(granteeAddr)
if err != nil {
return "", err
}
msg := feegrant.NewMsgRevokeAllowance(c.defaultAccount.GetAddress(), grantee)
if err != nil {
return "", err
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{&msg}, &txOption)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, nil
}
// QueryBasicAllowance queries the BasicAllowance
func (c *Client) QueryBasicAllowance(ctx context.Context, granterAddr, granteeAddr string) (*feegrant.BasicAllowance, error) {
allowance, err := c.QueryAllowance(ctx, granterAddr, granteeAddr)
if err != nil {
return nil, err
}
basicAllowance := &feegrant.BasicAllowance{}
if err = c.chainClient.GetCodec().Unmarshal(allowance.Allowance.GetValue(), basicAllowance); err != nil {
return nil, err
}
return basicAllowance, nil
}
func (c *Client) QueryAllowance(ctx context.Context, granterAddr, granteeAddr string) (*feegrant.Grant, error) {
_, err := sdk.AccAddressFromHexUnsafe(granterAddr)
if err != nil {
return nil, err
}
_, err = sdk.AccAddressFromHexUnsafe(granteeAddr)
if err != nil {
return nil, err
}
req := &feegrant.QueryAllowanceRequest{
Granter: granterAddr,
Grantee: granteeAddr,
}
response, err := c.chainClient.FeegrantQueryClient.Allowance(ctx, req)
if err != nil {
return nil, err
}
return response.Allowance, nil
}
func (c *Client) QueryAllowances(ctx context.Context, granteeAddr string) ([]*feegrant.Grant, error) {
_, err := sdk.AccAddressFromHexUnsafe(granteeAddr)
if err != nil {
return nil, err
}
req := &feegrant.QueryAllowancesRequest{
Grantee: granteeAddr,
}
response, err := c.chainClient.FeegrantQueryClient.Allowances(ctx, req)
if err != nil {
return nil, err
}
return response.Allowances, nil
}
func (c *Client) QueryGranterAllowances(ctx context.Context, granterAddr string) ([]*feegrant.Grant, error) {
_, err := sdk.AccAddressFromHexUnsafe(granterAddr)
if err != nil {
return nil, err
}
req := &feegrant.QueryAllowancesByGranterRequest{
Granter: granterAddr,
}
response, err := c.chainClient.FeegrantQueryClient.AllowancesByGranter(ctx, req)
if err != nil {
return nil, err
}
return response.Allowances, nil
}