Skip to content

Commit

Permalink
feat: add command line for nft module (cosmos#10505)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Add nft module command line and unit test,refer cosmos#9826

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
Zhiqiang Zhang authored and blewater committed Dec 8, 2021
1 parent 8b2e2b4 commit 7494b43
Show file tree
Hide file tree
Showing 13 changed files with 1,645 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7614,7 +7614,7 @@ Query defines the gRPC querier service.

| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `Balance` | [QueryBalanceRequest](#cosmos.nft.v1beta1.QueryBalanceRequest) | [QueryBalanceResponse](#cosmos.nft.v1beta1.QueryBalanceResponse) | Balance queries the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721 | GET|/cosmos/nft/v1beta1/balance/{class_id}/{owner}|
| `Balance` | [QueryBalanceRequest](#cosmos.nft.v1beta1.QueryBalanceRequest) | [QueryBalanceResponse](#cosmos.nft.v1beta1.QueryBalanceResponse) | Balance queries the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721 | GET|/cosmos/nft/v1beta1/balance/{owner}/{class_id}|
| `Owner` | [QueryOwnerRequest](#cosmos.nft.v1beta1.QueryOwnerRequest) | [QueryOwnerResponse](#cosmos.nft.v1beta1.QueryOwnerResponse) | Owner queries the owner of the NFT based on its class and id, same as ownerOf in ERC721 | GET|/cosmos/nft/v1beta1/owner/{class_id}/{id}|
| `Supply` | [QuerySupplyRequest](#cosmos.nft.v1beta1.QuerySupplyRequest) | [QuerySupplyResponse](#cosmos.nft.v1beta1.QuerySupplyResponse) | Supply queries the number of NFTs from the given class, same as totalSupply of ERC721. | GET|/cosmos/nft/v1beta1/supply/{class_id}|
| `NFTsOfClass` | [QueryNFTsOfClassRequest](#cosmos.nft.v1beta1.QueryNFTsOfClassRequest) | [QueryNFTsOfClassResponse](#cosmos.nft.v1beta1.QueryNFTsOfClassResponse) | NFTsOfClass queries all NFTs of a given class or optional owner, similar to tokenByIndex in ERC721Enumerable | GET|/cosmos/nft/v1beta1/nfts/{class_id}|
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/nft/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/nft";
service Query {
// Balance queries the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721
rpc Balance(QueryBalanceRequest) returns (QueryBalanceResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/balance/{class_id}/{owner}";
option (google.api.http).get = "/cosmos/nft/v1beta1/balance/{owner}/{class_id}";
}

// Owner queries the owner of the NFT based on its class and id, same as ownerOf in ERC721
Expand Down
255 changes: 255 additions & 0 deletions x/nft/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/nft"
)

// Flag names and values
const (
FlagOwner = "owner"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
nftQueryCmd := &cobra.Command{
Use: nft.ModuleName,
Short: "Querying commands for the nft module",
Long: "",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

nftQueryCmd.AddCommand(
GetCmdQueryClass(),
GetCmdQueryClasses(),
GetCmdQueryNFT(),
GetCmdQueryNFTs(),
GetCmdQueryOwner(),
GetCmdQueryBalance(),
GetCmdQuerySupply(),
)
return nftQueryCmd
}

// GetCmdQueryClass implements the query class command.
func GetCmdQueryClass() *cobra.Command {
cmd := &cobra.Command{
Use: "class [class-id]",
Args: cobra.ExactArgs(1),
Short: "query an NFT class based on its id",
Example: fmt.Sprintf(`$ %s query %s class <class-id>`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
res, err := queryClient.Class(cmd.Context(), &nft.QueryClassRequest{
ClassId: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryClasses implements the query classes command.
func GetCmdQueryClasses() *cobra.Command {
cmd := &cobra.Command{
Use: "classes",
Short: "query all NFT classes",
Example: fmt.Sprintf(`$ %s query %s classes`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.Classes(cmd.Context(), &nft.QueryClassesRequest{
Pagination: pageReq,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "classes")
return cmd
}

// GetCmdQueryNFT implements the query nft command.
func GetCmdQueryNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "nft [class-id] [nft-id]",
Args: cobra.ExactArgs(2),
Short: "query an NFT based on its class and id.",
Example: fmt.Sprintf(`$ %s query %s nft`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
res, err := queryClient.NFT(cmd.Context(), &nft.QueryNFTRequest{
ClassId: args[0],
Id: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryNFTs implements the query nft command.
func GetCmdQueryNFTs() *cobra.Command {
cmd := &cobra.Command{
Use: "nfts [class-id]",
Args: cobra.ExactArgs(1),
Short: "query all NFTs of a given class or owner address.",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all NFTs of a given class or owner address. If owner
is set, all nfts that belong to the owner are filtered out.
Examples:
$ %s query %s nfts <class-id> --owner=<owner>
`,
version.AppName, nft.ModuleName),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

request := &nft.QueryNFTsOfClassRequest{
ClassId: args[0],
Pagination: pageReq,
}

owner, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}

if len(owner) > 0 {
request.Owner = owner
}
res, err := queryClient.NFTsOfClass(cmd.Context(), request)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "nfts")
cmd.Flags().String(FlagOwner, "", "The owner of the nft")
return cmd
}

// GetCmdQueryOwner implements the query owner command.
func GetCmdQueryOwner() *cobra.Command {
cmd := &cobra.Command{
Use: "owner [class-id] [nft-id]",
Args: cobra.ExactArgs(2),
Short: "query the owner of the NFT based on its class and id.",
Example: fmt.Sprintf(`$ %s query %s owner <class-id> <nft-id>`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
res, err := queryClient.Owner(cmd.Context(), &nft.QueryOwnerRequest{
ClassId: args[0],
Id: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryBalance implements the query balance command.
func GetCmdQueryBalance() *cobra.Command {
cmd := &cobra.Command{
Use: "balance [owner] [class-id]",
Args: cobra.ExactArgs(2),
Short: "query the number of NFTs of a given class owned by the owner.",
Example: fmt.Sprintf(`$ %s query %s balance <owner> <class-id>`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
res, err := queryClient.Balance(cmd.Context(), &nft.QueryBalanceRequest{
ClassId: args[1],
Owner: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQuerySupply implements the query supply command.
func GetCmdQuerySupply() *cobra.Command {
cmd := &cobra.Command{
Use: "supply [class-id]",
Args: cobra.ExactArgs(1),
Short: "query the number of nft based on the class.",
Example: fmt.Sprintf(`$ %s query %s supply <class-id>`, version.AppName, nft.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := nft.NewQueryClient(clientCtx)
res, err := queryClient.Supply(cmd.Context(), &nft.QuerySupplyRequest{
ClassId: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
60 changes: 60 additions & 0 deletions x/nft/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/nft"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
nftTxCmd := &cobra.Command{
Use: nft.ModuleName,
Short: "nft transactions subcommands",
Long: "Provides the most common nft logic for upper-level applications, compatible with Ethereum's erc721 contract",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

nftTxCmd.AddCommand(
NewCmdSend(),
)

return nftTxCmd
}

func NewCmdSend() *cobra.Command {
cmd := &cobra.Command{
Use: "send [class-id] [nft-id] [receiver] --from [sender]",
Args: cobra.ExactArgs(3),
Short: "transfer ownership of nft",
Long: strings.TrimSpace(fmt.Sprintf(`
$ %s tx %s send <class-id> <nft-id> <receiver> --from <sender> --chain-id <chain-id>`, version.AppName, nft.ModuleName),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := nft.MsgSend{
ClassId: args[0],
Id: args[1],
Sender: clientCtx.GetFromAddress().String(),
Receiver: args[2],
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}
15 changes: 15 additions & 0 deletions x/nft/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/testutil/network"
)

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
suite.Run(t, NewIntegrationTestSuite(cfg))
}
Loading

0 comments on commit 7494b43

Please sign in to comment.