github.com/Finschia/finschia-sdk@v0.48.1/x/collection/client/cli/query.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 8 "github.com/Finschia/finschia-sdk/client" 9 "github.com/Finschia/finschia-sdk/client/flags" 10 sdk "github.com/Finschia/finschia-sdk/types" 11 "github.com/Finschia/finschia-sdk/version" 12 "github.com/Finschia/finschia-sdk/x/collection" 13 ) 14 15 const ( 16 FlagTokenID = "token-id" 17 ) 18 19 // NewQueryCmd returns the cli query commands for this module 20 func NewQueryCmd() *cobra.Command { 21 queryCmd := &cobra.Command{ 22 Use: collection.ModuleName, 23 Short: fmt.Sprintf("Querying commands for the %s module", collection.ModuleName), 24 Long: "", 25 DisableFlagParsing: true, 26 SuggestionsMinimumDistance: 2, 27 RunE: client.ValidateCmd, 28 } 29 30 queryCmd.AddCommand( 31 NewQueryCmdBalances(), 32 NewQueryCmdFTSupply(), 33 NewQueryCmdFTMinted(), 34 NewQueryCmdFTBurnt(), 35 NewQueryCmdNFTSupply(), 36 NewQueryCmdNFTMinted(), 37 NewQueryCmdNFTBurnt(), 38 NewQueryCmdContract(), 39 NewQueryCmdToken(), 40 NewQueryCmdTokenType(), 41 NewQueryCmdRoot(), 42 NewQueryCmdParent(), 43 NewQueryCmdChildren(), 44 NewQueryCmdGranteeGrants(), 45 NewQueryCmdIsOperatorFor(), 46 NewQueryCmdHoldersByOperator(), 47 ) 48 49 return queryCmd 50 } 51 52 func NewQueryCmdBalances() *cobra.Command { 53 cmd := &cobra.Command{ 54 Use: "balances [contract-id] [address]", 55 Args: cobra.ExactArgs(2), 56 Short: "query for token balances by a given address", 57 Example: fmt.Sprintf(`$ %s query %s balances [contract-id] [address]`, version.AppName, collection.ModuleName), 58 RunE: func(cmd *cobra.Command, args []string) error { 59 clientCtx, err := client.GetClientQueryContext(cmd) 60 if err != nil { 61 return err 62 } 63 64 contractID := args[0] 65 if err := collection.ValidateContractID(contractID); err != nil { 66 return err 67 } 68 69 address := args[1] 70 if _, err := sdk.AccAddressFromBech32(address); err != nil { 71 return err 72 } 73 74 tokenID, err := cmd.Flags().GetString(FlagTokenID) 75 if err != nil { 76 return err 77 } 78 79 queryClient := collection.NewQueryClient(clientCtx) 80 if len(tokenID) == 0 { 81 pageReq, err := client.ReadPageRequest(cmd.Flags()) 82 if err != nil { 83 return err 84 } 85 86 req := &collection.QueryAllBalancesRequest{ 87 ContractId: contractID, 88 Address: address, 89 Pagination: pageReq, 90 } 91 res, err := queryClient.AllBalances(cmd.Context(), req) 92 if err != nil { 93 return err 94 } 95 return clientCtx.PrintProto(res) 96 } 97 98 if err := collection.ValidateTokenID(tokenID); err != nil { 99 return err 100 } 101 102 req := &collection.QueryBalanceRequest{ 103 ContractId: contractID, 104 Address: address, 105 TokenId: tokenID, 106 } 107 res, err := queryClient.Balance(cmd.Context(), req) 108 if err != nil { 109 return err 110 } 111 return clientCtx.PrintProto(res) 112 }, 113 } 114 115 flags.AddQueryFlagsToCmd(cmd) 116 cmd.Flags().String(FlagTokenID, "", "Token ID to query for") 117 flags.AddPaginationFlagsToCmd(cmd, "all balances") 118 119 return cmd 120 } 121 122 func NewQueryCmdFTSupply() *cobra.Command { 123 cmd := &cobra.Command{ 124 Use: "ft-supply [contract-id] [token-id]", 125 Args: cobra.ExactArgs(2), 126 Short: "query the supply of tokens", 127 Example: fmt.Sprintf(`$ %s ft-query %s supply [contract-id] [token-id]`, version.AppName, collection.ModuleName), 128 RunE: func(cmd *cobra.Command, args []string) error { 129 clientCtx, err := client.GetClientQueryContext(cmd) 130 if err != nil { 131 return err 132 } 133 134 contractID := args[0] 135 if err := collection.ValidateContractID(contractID); err != nil { 136 return err 137 } 138 139 tokenID := args[1] 140 if err := collection.ValidateTokenID(tokenID); err != nil { 141 return err 142 } 143 144 queryClient := collection.NewQueryClient(clientCtx) 145 req := &collection.QueryFTSupplyRequest{ 146 ContractId: contractID, 147 TokenId: tokenID, 148 } 149 res, err := queryClient.FTSupply(cmd.Context(), req) 150 if err != nil { 151 return err 152 } 153 return clientCtx.PrintProto(res) 154 }, 155 } 156 157 flags.AddQueryFlagsToCmd(cmd) 158 return cmd 159 } 160 161 func NewQueryCmdFTMinted() *cobra.Command { 162 cmd := &cobra.Command{ 163 Use: "ft-minted [contract-id] [token-id]", 164 Args: cobra.ExactArgs(2), 165 Short: "query the minted tokens", 166 Example: fmt.Sprintf(`$ %s query %s ft-minted [contract-id] [token-id]`, version.AppName, collection.ModuleName), 167 RunE: func(cmd *cobra.Command, args []string) error { 168 clientCtx, err := client.GetClientQueryContext(cmd) 169 if err != nil { 170 return err 171 } 172 173 contractID := args[0] 174 if err := collection.ValidateContractID(contractID); err != nil { 175 return err 176 } 177 178 tokenID := args[1] 179 if err := collection.ValidateTokenID(tokenID); err != nil { 180 return err 181 } 182 183 queryClient := collection.NewQueryClient(clientCtx) 184 req := &collection.QueryFTMintedRequest{ 185 ContractId: contractID, 186 TokenId: tokenID, 187 } 188 res, err := queryClient.FTMinted(cmd.Context(), req) 189 if err != nil { 190 return err 191 } 192 return clientCtx.PrintProto(res) 193 }, 194 } 195 196 flags.AddQueryFlagsToCmd(cmd) 197 return cmd 198 } 199 200 func NewQueryCmdFTBurnt() *cobra.Command { 201 cmd := &cobra.Command{ 202 Use: "ft-burnt [contract-id] [token-id]", 203 Args: cobra.ExactArgs(2), 204 Short: "query the burnt tokens", 205 Example: fmt.Sprintf(`$ %s query %s ft-burnt [contract-id] [token-id]`, version.AppName, collection.ModuleName), 206 RunE: func(cmd *cobra.Command, args []string) error { 207 clientCtx, err := client.GetClientQueryContext(cmd) 208 if err != nil { 209 return err 210 } 211 212 contractID := args[0] 213 if err := collection.ValidateContractID(contractID); err != nil { 214 return err 215 } 216 217 tokenID := args[1] 218 if err := collection.ValidateTokenID(tokenID); err != nil { 219 return err 220 } 221 222 queryClient := collection.NewQueryClient(clientCtx) 223 req := &collection.QueryFTBurntRequest{ 224 ContractId: contractID, 225 TokenId: tokenID, 226 } 227 res, err := queryClient.FTBurnt(cmd.Context(), req) 228 if err != nil { 229 return err 230 } 231 return clientCtx.PrintProto(res) 232 }, 233 } 234 235 flags.AddQueryFlagsToCmd(cmd) 236 return cmd 237 } 238 239 func NewQueryCmdNFTSupply() *cobra.Command { 240 cmd := &cobra.Command{ 241 Use: "nft-supply [contract-id] [token-type]", 242 Args: cobra.ExactArgs(2), 243 Short: "query the supply of tokens", 244 Example: fmt.Sprintf(`$ %s query %s nft-supply [contract-id] [token-type]`, version.AppName, collection.ModuleName), 245 RunE: func(cmd *cobra.Command, args []string) error { 246 clientCtx, err := client.GetClientQueryContext(cmd) 247 if err != nil { 248 return err 249 } 250 251 contractID := args[0] 252 if err := collection.ValidateContractID(contractID); err != nil { 253 return err 254 } 255 256 tokenType := args[1] 257 if err := collection.ValidateClassID(tokenType); err != nil { 258 return err 259 } 260 261 queryClient := collection.NewQueryClient(clientCtx) 262 req := &collection.QueryNFTSupplyRequest{ 263 ContractId: contractID, 264 TokenType: tokenType, 265 } 266 res, err := queryClient.NFTSupply(cmd.Context(), req) 267 if err != nil { 268 return err 269 } 270 return clientCtx.PrintProto(res) 271 }, 272 } 273 274 flags.AddQueryFlagsToCmd(cmd) 275 return cmd 276 } 277 278 func NewQueryCmdNFTMinted() *cobra.Command { 279 cmd := &cobra.Command{ 280 Use: "nft-minted [contract-id] [token-type]", 281 Args: cobra.ExactArgs(2), 282 Short: "query the minted tokens of the class", 283 Example: fmt.Sprintf(`$ %s query %s nft-minted [contract-id] [token-type]`, version.AppName, collection.ModuleName), 284 RunE: func(cmd *cobra.Command, args []string) error { 285 clientCtx, err := client.GetClientQueryContext(cmd) 286 if err != nil { 287 return err 288 } 289 290 contractID := args[0] 291 if err := collection.ValidateContractID(contractID); err != nil { 292 return err 293 } 294 295 tokenType := args[1] 296 if err := collection.ValidateClassID(tokenType); err != nil { 297 return err 298 } 299 300 queryClient := collection.NewQueryClient(clientCtx) 301 req := &collection.QueryNFTMintedRequest{ 302 ContractId: contractID, 303 TokenType: tokenType, 304 } 305 res, err := queryClient.NFTMinted(cmd.Context(), req) 306 if err != nil { 307 return err 308 } 309 return clientCtx.PrintProto(res) 310 }, 311 } 312 313 flags.AddQueryFlagsToCmd(cmd) 314 return cmd 315 } 316 317 func NewQueryCmdNFTBurnt() *cobra.Command { 318 cmd := &cobra.Command{ 319 Use: "nft-burnt [contract-id] [token-type]", 320 Args: cobra.ExactArgs(2), 321 Short: "query the burnt tokens of the class", 322 Example: fmt.Sprintf(`$ %s query %s nft-burnt [contract-id] [token-type]`, version.AppName, collection.ModuleName), 323 RunE: func(cmd *cobra.Command, args []string) error { 324 clientCtx, err := client.GetClientQueryContext(cmd) 325 if err != nil { 326 return err 327 } 328 329 contractID := args[0] 330 if err := collection.ValidateContractID(contractID); err != nil { 331 return err 332 } 333 334 tokenType := args[1] 335 if err := collection.ValidateClassID(tokenType); err != nil { 336 return err 337 } 338 339 queryClient := collection.NewQueryClient(clientCtx) 340 req := &collection.QueryNFTBurntRequest{ 341 ContractId: contractID, 342 TokenType: tokenType, 343 } 344 res, err := queryClient.NFTBurnt(cmd.Context(), req) 345 if err != nil { 346 return err 347 } 348 return clientCtx.PrintProto(res) 349 }, 350 } 351 352 flags.AddQueryFlagsToCmd(cmd) 353 return cmd 354 } 355 356 func NewQueryCmdContract() *cobra.Command { 357 cmd := &cobra.Command{ 358 Use: "contract [contract-id]", 359 Args: cobra.ExactArgs(1), 360 Short: "query token metadata based on its id", 361 Example: fmt.Sprintf(`$ %s query %s contract [contract-id]`, version.AppName, collection.ModuleName), 362 RunE: func(cmd *cobra.Command, args []string) error { 363 clientCtx, err := client.GetClientQueryContext(cmd) 364 if err != nil { 365 return err 366 } 367 368 contractID := args[0] 369 if err := collection.ValidateContractID(contractID); err != nil { 370 return err 371 } 372 373 queryClient := collection.NewQueryClient(clientCtx) 374 req := &collection.QueryContractRequest{ 375 ContractId: contractID, 376 } 377 res, err := queryClient.Contract(cmd.Context(), req) 378 if err != nil { 379 return err 380 } 381 return clientCtx.PrintProto(res) 382 }, 383 } 384 385 flags.AddQueryFlagsToCmd(cmd) 386 return cmd 387 } 388 389 func NewQueryCmdTokenType() *cobra.Command { 390 cmd := &cobra.Command{ 391 Use: "token-type [contract-id] [token-type]", 392 Args: cobra.ExactArgs(2), 393 Short: "query token type", 394 Example: fmt.Sprintf(`$ %s query %s token-type [contract-id] [token-type]`, version.AppName, collection.ModuleName), 395 RunE: func(cmd *cobra.Command, args []string) error { 396 clientCtx, err := client.GetClientQueryContext(cmd) 397 if err != nil { 398 return err 399 } 400 401 contractID := args[0] 402 if err := collection.ValidateContractID(contractID); err != nil { 403 return err 404 } 405 406 classID := args[1] 407 if err := collection.ValidateClassID(classID); err != nil { 408 return err 409 } 410 411 queryClient := collection.NewQueryClient(clientCtx) 412 req := &collection.QueryTokenTypeRequest{ 413 ContractId: contractID, 414 TokenType: classID, 415 } 416 res, err := queryClient.TokenType(cmd.Context(), req) 417 if err != nil { 418 return err 419 } 420 return clientCtx.PrintProto(res) 421 }, 422 } 423 424 flags.AddQueryFlagsToCmd(cmd) 425 return cmd 426 } 427 428 func NewQueryCmdToken() *cobra.Command { 429 cmd := &cobra.Command{ 430 Use: "token [contract-id] [token-id]", 431 Args: cobra.ExactArgs(2), 432 Short: "query token metadata", 433 Example: fmt.Sprintf(`$ %s query %s token [contract-id] [token-id]`, version.AppName, collection.ModuleName), 434 RunE: func(cmd *cobra.Command, args []string) error { 435 clientCtx, err := client.GetClientQueryContext(cmd) 436 if err != nil { 437 return err 438 } 439 440 contractID := args[0] 441 if err := collection.ValidateContractID(contractID); err != nil { 442 return err 443 } 444 445 tokenID := args[1] 446 if err := collection.ValidateTokenID(tokenID); err != nil { 447 return err 448 } 449 450 queryClient := collection.NewQueryClient(clientCtx) 451 req := &collection.QueryTokenRequest{ 452 ContractId: contractID, 453 TokenId: tokenID, 454 } 455 res, err := queryClient.Token(cmd.Context(), req) 456 if err != nil { 457 return err 458 } 459 return clientCtx.PrintProto(res) 460 }, 461 } 462 463 flags.AddQueryFlagsToCmd(cmd) 464 return cmd 465 } 466 467 func NewQueryCmdRoot() *cobra.Command { 468 cmd := &cobra.Command{ 469 Use: "root [contract-id] [token-id]", 470 Args: cobra.ExactArgs(2), 471 Short: "query root of an nft", 472 Example: fmt.Sprintf(`$ %s query %s root [contract-id] [token-id]`, version.AppName, collection.ModuleName), 473 RunE: func(cmd *cobra.Command, args []string) error { 474 clientCtx, err := client.GetClientQueryContext(cmd) 475 if err != nil { 476 return err 477 } 478 479 contractID := args[0] 480 if err := collection.ValidateContractID(contractID); err != nil { 481 return err 482 } 483 484 tokenID := args[1] 485 if err := collection.ValidateNFTID(tokenID); err != nil { 486 return err 487 } 488 489 queryClient := collection.NewQueryClient(clientCtx) 490 req := &collection.QueryRootRequest{ 491 ContractId: contractID, 492 TokenId: tokenID, 493 } 494 res, err := queryClient.Root(cmd.Context(), req) 495 if err != nil { 496 return err 497 } 498 return clientCtx.PrintProto(res) 499 }, 500 } 501 502 flags.AddQueryFlagsToCmd(cmd) 503 return cmd 504 } 505 506 func NewQueryCmdParent() *cobra.Command { 507 cmd := &cobra.Command{ 508 Use: "parent [contract-id] [token-id]", 509 Args: cobra.ExactArgs(2), 510 Short: "query parent of an nft", 511 Example: fmt.Sprintf(`$ %s query %s parent [contract-id] [token-id]`, version.AppName, collection.ModuleName), 512 RunE: func(cmd *cobra.Command, args []string) error { 513 clientCtx, err := client.GetClientQueryContext(cmd) 514 if err != nil { 515 return err 516 } 517 518 contractID := args[0] 519 if err := collection.ValidateContractID(contractID); err != nil { 520 return err 521 } 522 523 tokenID := args[1] 524 if err := collection.ValidateNFTID(tokenID); err != nil { 525 return err 526 } 527 528 queryClient := collection.NewQueryClient(clientCtx) 529 req := &collection.QueryParentRequest{ 530 ContractId: contractID, 531 TokenId: tokenID, 532 } 533 res, err := queryClient.Parent(cmd.Context(), req) 534 if err != nil { 535 return err 536 } 537 return clientCtx.PrintProto(res) 538 }, 539 } 540 541 flags.AddQueryFlagsToCmd(cmd) 542 return cmd 543 } 544 545 func NewQueryCmdChildren() *cobra.Command { 546 cmd := &cobra.Command{ 547 Use: "children [contract-id] [token-id]", 548 Args: cobra.ExactArgs(2), 549 Short: "query children of an nft", 550 Example: fmt.Sprintf(`$ %s query %s children [contract-id] [token-id]`, version.AppName, collection.ModuleName), 551 RunE: func(cmd *cobra.Command, args []string) error { 552 clientCtx, err := client.GetClientQueryContext(cmd) 553 if err != nil { 554 return err 555 } 556 557 contractID := args[0] 558 if err := collection.ValidateContractID(contractID); err != nil { 559 return err 560 } 561 562 tokenID := args[1] 563 if err := collection.ValidateNFTID(tokenID); err != nil { 564 return err 565 } 566 567 queryClient := collection.NewQueryClient(clientCtx) 568 pageReq, err := client.ReadPageRequest(cmd.Flags()) 569 if err != nil { 570 return err 571 } 572 req := &collection.QueryChildrenRequest{ 573 ContractId: contractID, 574 TokenId: tokenID, 575 Pagination: pageReq, 576 } 577 res, err := queryClient.Children(cmd.Context(), req) 578 if err != nil { 579 return err 580 } 581 return clientCtx.PrintProto(res) 582 }, 583 } 584 585 flags.AddQueryFlagsToCmd(cmd) 586 flags.AddPaginationFlagsToCmd(cmd, "children") 587 return cmd 588 } 589 590 func NewQueryCmdGranteeGrants() *cobra.Command { 591 cmd := &cobra.Command{ 592 Use: "grantee-grants [contract-id] [grantee]", 593 Args: cobra.ExactArgs(2), 594 Short: "query grants on a given grantee", 595 Example: fmt.Sprintf(`$ %s query %s grantee-grants [contract-id] [grantee]`, version.AppName, collection.ModuleName), 596 RunE: func(cmd *cobra.Command, args []string) error { 597 clientCtx, err := client.GetClientQueryContext(cmd) 598 if err != nil { 599 return err 600 } 601 602 contractID := args[0] 603 if err := collection.ValidateContractID(contractID); err != nil { 604 return err 605 } 606 607 grantee := args[1] 608 if _, err := sdk.AccAddressFromBech32(grantee); err != nil { 609 return err 610 } 611 612 queryClient := collection.NewQueryClient(clientCtx) 613 req := &collection.QueryGranteeGrantsRequest{ 614 ContractId: contractID, 615 Grantee: grantee, 616 } 617 res, err := queryClient.GranteeGrants(cmd.Context(), req) 618 if err != nil { 619 return err 620 } 621 return clientCtx.PrintProto(res) 622 }, 623 } 624 625 flags.AddQueryFlagsToCmd(cmd) 626 return cmd 627 } 628 629 func NewQueryCmdIsOperatorFor() *cobra.Command { 630 cmd := &cobra.Command{ 631 Use: "approved [contract-id] [operator] [holder]", 632 Args: cobra.ExactArgs(3), 633 Short: "query authorization on its operator and the token holder", 634 Example: fmt.Sprintf(`$ %s query %s approved [contract-id] [operator] [holder]`, version.AppName, collection.ModuleName), 635 RunE: func(cmd *cobra.Command, args []string) error { 636 clientCtx, err := client.GetClientQueryContext(cmd) 637 if err != nil { 638 return err 639 } 640 641 contractID := args[0] 642 if err := collection.ValidateContractID(contractID); err != nil { 643 return err 644 } 645 646 operator := args[1] 647 if _, err := sdk.AccAddressFromBech32(operator); err != nil { 648 return err 649 } 650 651 holder := args[2] 652 if _, err := sdk.AccAddressFromBech32(holder); err != nil { 653 return err 654 } 655 656 queryClient := collection.NewQueryClient(clientCtx) 657 req := &collection.QueryIsOperatorForRequest{ 658 ContractId: contractID, 659 Operator: operator, 660 Holder: holder, 661 } 662 res, err := queryClient.IsOperatorFor(cmd.Context(), req) 663 if err != nil { 664 return err 665 } 666 return clientCtx.PrintProto(res) 667 }, 668 } 669 670 flags.AddQueryFlagsToCmd(cmd) 671 return cmd 672 } 673 674 func NewQueryCmdHoldersByOperator() *cobra.Command { 675 cmd := &cobra.Command{ 676 Use: "approvers [contract-id] [operator]", 677 Args: cobra.ExactArgs(2), 678 Short: "query all authorizations on a given operator", 679 Example: fmt.Sprintf(`$ %s query %s approvers [contract-id] [operator]`, version.AppName, collection.ModuleName), 680 RunE: func(cmd *cobra.Command, args []string) error { 681 clientCtx, err := client.GetClientQueryContext(cmd) 682 if err != nil { 683 return err 684 } 685 686 contractID := args[0] 687 if err := collection.ValidateContractID(contractID); err != nil { 688 return err 689 } 690 691 operator := args[1] 692 if _, err := sdk.AccAddressFromBech32(operator); err != nil { 693 return err 694 } 695 696 queryClient := collection.NewQueryClient(clientCtx) 697 pageReq, err := client.ReadPageRequest(cmd.Flags()) 698 if err != nil { 699 return err 700 } 701 req := &collection.QueryHoldersByOperatorRequest{ 702 ContractId: contractID, 703 Operator: operator, 704 Pagination: pageReq, 705 } 706 res, err := queryClient.HoldersByOperator(cmd.Context(), req) 707 if err != nil { 708 return err 709 } 710 return clientCtx.PrintProto(res) 711 }, 712 } 713 714 flags.AddQueryFlagsToCmd(cmd) 715 flags.AddPaginationFlagsToCmd(cmd, "approvers") 716 return cmd 717 }