forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command line for nft module (cosmos#10505)
<!-- 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
Showing
13 changed files
with
1,645 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.