-
Notifications
You must be signed in to change notification settings - Fork 29
/
grpc_query.go
209 lines (169 loc) · 6.55 KB
/
grpc_query.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/Finschia/finschia-sdk/store/prefix"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
"github.com/Finschia/finschia-sdk/types/query"
"github.com/Finschia/finschia-sdk/x/token"
)
type queryServer struct {
keeper Keeper
}
// NewQueryServer returns an implementation of the token QueryServer interface
// for the provided Keeper.
func NewQueryServer(keeper Keeper) token.QueryServer {
return &queryServer{
keeper: keeper,
}
}
var _ token.QueryServer = queryServer{}
func (s queryServer) addressFromBech32GRPC(address string, context string) (sdk.AccAddress, error) {
addr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return nil, status.Error(codes.InvalidArgument, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress.Wrap(address), context).Error())
}
return addr, nil
}
// Balance queries the number of tokens of a given class owned by the owner.
func (s queryServer) Balance(c context.Context, req *token.QueryBalanceRequest) (*token.QueryBalanceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
addr, err := s.addressFromBech32GRPC(req.Address, "address")
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
balance := s.keeper.GetBalance(ctx, req.ContractId, addr)
return &token.QueryBalanceResponse{Amount: balance}, nil
}
// Supply queries the number of tokens from the given contract id.
func (s queryServer) Supply(c context.Context, req *token.QuerySupplyRequest) (*token.QuerySupplyResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
supply := s.keeper.GetSupply(ctx, req.ContractId)
return &token.QuerySupplyResponse{Amount: supply}, nil
}
// Minted queries the number of tokens from the given contract id.
func (s queryServer) Minted(c context.Context, req *token.QueryMintedRequest) (*token.QueryMintedResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
minted := s.keeper.GetMinted(ctx, req.ContractId)
return &token.QueryMintedResponse{Amount: minted}, nil
}
// Burnt queries the number of tokens from the given contract id.
func (s queryServer) Burnt(c context.Context, req *token.QueryBurntRequest) (*token.QueryBurntResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
burnt := s.keeper.GetBurnt(ctx, req.ContractId)
return &token.QueryBurntResponse{Amount: burnt}, nil
}
// Contract queries an token metadata based on its contract id.
func (s queryServer) Contract(c context.Context, req *token.QueryContractRequest) (*token.QueryContractResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
class, err := s.keeper.GetClass(ctx, req.ContractId)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
return &token.QueryContractResponse{Contract: *class}, nil
}
func (s queryServer) GranteeGrants(c context.Context, req *token.QueryGranteeGrantsRequest) (*token.QueryGranteeGrantsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
grantee, err := s.addressFromBech32GRPC(req.Grantee, "grantee")
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(s.keeper.storeKey)
grantStore := prefix.NewStore(store, grantKeyPrefixByGrantee(req.ContractId, grantee))
var grants []token.Grant
pageRes, err := query.Paginate(grantStore, req.Pagination, func(key []byte, _ []byte) error {
permission := token.Permission(key[0])
grants = append(grants, token.Grant{
Grantee: req.Grantee,
Permission: permission,
})
return nil
})
if err != nil {
return nil, err
}
return &token.QueryGranteeGrantsResponse{Grants: grants, Pagination: pageRes}, nil
}
func (s queryServer) IsOperatorFor(c context.Context, req *token.QueryIsOperatorForRequest) (*token.QueryIsOperatorForResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
operator, err := s.addressFromBech32GRPC(req.Operator, "operator")
if err != nil {
return nil, err
}
holder, err := s.addressFromBech32GRPC(req.Holder, "holder")
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
_, err = s.keeper.GetAuthorization(ctx, req.ContractId, holder, operator)
authorized := err == nil
return &token.QueryIsOperatorForResponse{Authorized: authorized}, nil
}
func (s queryServer) HoldersByOperator(c context.Context, req *token.QueryHoldersByOperatorRequest) (*token.QueryHoldersByOperatorResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if err := token.ValidateContractID(req.ContractId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
operator, err := s.addressFromBech32GRPC(req.Operator, "operator")
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(s.keeper.storeKey)
authorizationStore := prefix.NewStore(store, authorizationKeyPrefixByOperator(req.ContractId, operator))
var holders []string
pageRes, err := query.Paginate(authorizationStore, req.Pagination, func(key []byte, value []byte) error {
holder := sdk.AccAddress(key)
holders = append(holders, holder.String())
return nil
})
if err != nil {
return nil, err
}
return &token.QueryHoldersByOperatorResponse{Holders: holders, Pagination: pageRes}, nil
}