-
Notifications
You must be signed in to change notification settings - Fork 28
/
query.go
264 lines (234 loc) · 6.59 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
package cli
import (
"context"
"fmt"
"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/irisnet/irismod/modules/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(),
GetCmdQueryDenoms(),
GetCmdQueryCollection(),
GetCmdQuerySupply(),
GetCmdQueryOwner(),
GetCmdQueryNFT(),
)
return queryCmd
}
// GetCmdQuerySupply queries the supply of a nft collection
func GetCmdQuerySupply() *cobra.Command {
cmd := &cobra.Command{
Use: "supply [denom-id]",
Long: "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
}
}
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
func GetCmdQueryOwner() *cobra.Command {
cmd := &cobra.Command{
Use: "owner [address]",
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
}
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]",
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
}
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
}
// GetCmdQueryDenoms queries all denoms
func GetCmdQueryDenoms() *cobra.Command {
cmd := &cobra.Command{
Use: "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]",
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
}
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
}
// GetCmdQueryNFT queries a single NFTs from a collection
func GetCmdQueryNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "token [denom-id] [token-id]",
Long: "Query a single NFT from a collection.",
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
}
if err := types.ValidateDenomID(args[0]); err != nil {
return err
}
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
}