/
query.go
483 lines (429 loc) · 12.9 KB
/
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package cli
import (
"context"
"errors"
"fmt"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/CudoVentures/cudos-node/x/nft/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the NFT module",
DisableFlagParsing: true,
}
queryCmd.AddCommand(
GetCmdQueryDenom(),
GetCmdQueryDenomByName(),
GetCmdQueryDenomBySymbol(),
GetCmdQueryDenoms(),
GetCmdQueryCollection(),
GetCmdQueryCollectionsByDenomIds(),
GetCmdQuerySupply(),
GetCmdQueryOwner(),
GetCmdQueryNFT(),
GetCmdQueryApprovedNFT(),
GetCmdQueryIsApprovedForAll(),
)
return queryCmd
}
// GetCmdQuerySupply queries the supply of a nft collection
func GetCmdQuerySupply() *cobra.Command {
cmd := &cobra.Command{
Use: "supply [denom-id]",
Short: "Query total supply of denom",
Long: "Query total supply of a collection or owner of NFTs.",
Example: fmt.Sprintf("$ %s query nft supply <denom-id>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var owner sdk.AccAddress
ownerStr, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}
if len(ownerStr) > 0 {
owner, err = sdk.AccAddressFromBech32(ownerStr)
if err != nil {
return err
}
}
// nolint: govet
if err := types.ValidateDenomID(args[0]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Supply(context.Background(), &types.QuerySupplyRequest{
DenomId: args[0],
Owner: owner.String(),
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
cmd.Flags().AddFlagSet(FsQuerySupply)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryOwner queries all the NFTs owned by an account
// todo: change the name of this to something like QueryAllNFTsOfOwner..
func GetCmdQueryOwner() *cobra.Command {
cmd := &cobra.Command{
Use: "owner [address]",
Short: "Query address NFT holdings",
Long: "Get the NFTs owned by an account address.",
Example: fmt.Sprintf("$ %s query nft owner <address> --denom-id=<denom-id>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if _, err := sdk.AccAddressFromBech32(args[0]); err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
denomID, err := cmd.Flags().GetString(FlagDenomID)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Owner(context.Background(), &types.QueryOwnerRequest{
DenomId: denomID,
Owner: args[0],
Pagination: pageReq,
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
cmd.Flags().AddFlagSet(FsQueryOwner)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "nfts")
return cmd
}
// GetCmdQueryCollection queries all the NFTs from a collection
func GetCmdQueryCollection() *cobra.Command {
cmd := &cobra.Command{
Use: "collection [denom-id]",
Short: "Query NFTs in a collection",
Long: "Get all the NFTs from a given collection.",
Example: fmt.Sprintf("$ %s query nft collection <denom-id>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if err := types.ValidateDenomID(args[0]); err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Collection(
context.Background(),
&types.QueryCollectionRequest{
DenomId: args[0],
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "nfts")
return cmd
}
// GetCmdQueryCollectionsByDenomIds queries for all the collections matching a set of denom ids
func GetCmdQueryCollectionsByDenomIds() *cobra.Command {
cmd := &cobra.Command{
Use: "collection [denom-id1,denom-id2,denom-id3..]",
Short: "Query Collections by denom ids",
Long: "Get all the collections for given denom ids.",
Example: fmt.Sprintf("$ %s query nft collectionByDenomIds <denom-id,denom-id2, denom-id3>", version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
denomIds := strings.Split(args[1], ",")
if len(denomIds) == 0 {
err := errors.New("denomIds array is empty")
return err
}
for _, denomId := range denomIds {
if err := types.ValidateDenomID(denomId); err != nil {
return err
}
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.CollectionsByDenomIds(context.Background(), &types.QueryCollectionsByIdsRequest{
DenomIds: denomIds,
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryDenoms queries all denoms
func GetCmdQueryDenoms() *cobra.Command {
cmd := &cobra.Command{
Use: "denoms",
Short: "Query all denoms",
Long: "Query all denominations of all collections of NFTs.",
Example: fmt.Sprintf("$ %s query nft denoms", version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Denoms(context.Background(), &types.QueryDenomsRequest{Pagination: pageReq})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "all denoms")
return cmd
}
// GetCmdQueryDenom queries the specified denom
func GetCmdQueryDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "denom [denom-id]",
Short: "Query a denom by Id",
Long: "Query the denom by the specified denom id.",
Example: fmt.Sprintf("$ %s query nft denom <denom-id>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if err := types.ValidateDenomID(args[0]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Denom(
context.Background(),
&types.QueryDenomRequest{DenomId: args[0]},
)
if err != nil {
return err
}
return clientCtx.PrintProto(resp.Denom)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryDenomByName queries the specified denom by name
func GetCmdQueryDenomByName() *cobra.Command {
cmd := &cobra.Command{
Use: "denom-by-name [denom-name]",
Short: "Query denom by name",
Long: "Query the denom by the specified denom name.",
Example: fmt.Sprintf("$ %s query nft denom <denom-name>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if err := types.ValidateDenomName(args[0]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.DenomByName(
context.Background(),
&types.QueryDenomByNameRequest{DenomName: args[0]},
)
if err != nil {
return err
}
return clientCtx.PrintProto(resp.Denom)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryDenomBySymbol queries the specified denom by symbol
func GetCmdQueryDenomBySymbol() *cobra.Command {
cmd := &cobra.Command{
Use: "denom-by-symbol [symbol]",
Short: "Query denom by symbol",
Long: "Query the denom by the specified symbol.",
Example: fmt.Sprintf("$ %s query nft denom <symbol>", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if err := types.ValidateDenomSymbol(args[0]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.DenomBySymbol(
context.Background(),
&types.QueryDenomBySymbolRequest{Symbol: args[0]},
)
if err != nil {
return err
}
return clientCtx.PrintProto(resp.Denom)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryNFT queries a single NFTs from a collection
// todo: rename this to QueryNFT in the Use:
func GetCmdQueryNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "token [denom-id] [token-id]",
Short: "Query single NFT",
Long: "Query a single NFT from a collection by denom id and token id.",
Example: fmt.Sprintf("$ %s query nft token <denom-id> <token-id>", version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
if err := types.ValidateDenomID(args[0]); err != nil {
return err
}
// nolint: govet
if err := types.ValidateTokenID(args[1]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.NFT(context.Background(), &types.QueryNFTRequest{
DenomId: args[0],
TokenId: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp.NFT)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryApprovedNFT queries the NFT and returns its approved operators list
func GetCmdQueryApprovedNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "approvals [denomId] [tokenId]",
Short: "Query the approved addresses for a NFT.",
Long: "Get the approved addresses for the NFT.",
Example: fmt.Sprintf("$ %s query nft approvals <denomId> <tokenId>", version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
denomId := args[0]
if err := types.ValidateDenomID(denomId); err != nil {
return err
}
// nolint: govet
tokenId := args[1]
if err := types.ValidateTokenID(tokenId); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.GetApprovalsNFT(context.Background(), &types.QueryApprovalsNFTRequest{
DenomId: denomId,
TokenId: tokenId,
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
cmd.Flags().AddFlagSet(FsQueryOwner)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryIsApprovedForAll queries if the operator address is authorized for owner address
func GetCmdQueryIsApprovedForAll() *cobra.Command {
cmd := &cobra.Command{
Use: "is-approved-for-all [owner] [operator]",
Short: "Query if an address is approved operator",
Long: "Query if an address is an authorized operator for another address",
Example: fmt.Sprintf("$ %s query nft isApprovedForAll <owner> <operator>", version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// nolint: govet
owner := args[0]
if _, err := sdk.AccAddressFromBech32(owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid owner address (%s)", err)
}
operator := args[1]
if _, err := sdk.AccAddressFromBech32(operator); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid operator address (%s)", err)
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.QueryApprovalsIsApprovedForAll(context.Background(), &types.QueryApprovalsIsApprovedForAllRequest{
Owner: owner,
Operator: operator,
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
cmd.Flags().AddFlagSet(FsQueryOwner)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}