Skip to content

Commit

Permalink
add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
taramakage committed Dec 22, 2022
1 parent 3634423 commit 4149361
Show file tree
Hide file tree
Showing 17 changed files with 547 additions and 137 deletions.
9 changes: 9 additions & 0 deletions modules/nft/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const (
FlagSymbol = "symbol"
FlagMintRestricted = "mint-restricted"
FlagUpdateRestricted = "update-restricted"

FlagRentable = "rentable"
FlagRentalExpiry = "expiry"
FlagRentalUser = "user"
)

var (
Expand All @@ -29,6 +33,7 @@ var (
FsQuerySupply = flag.NewFlagSet("", flag.ContinueOnError)
FsQueryOwner = flag.NewFlagSet("", flag.ContinueOnError)
FsTransferDenom = flag.NewFlagSet("", flag.ContinueOnError)
FsRental = flag.NewFlagSet("", flag.ContinueOnError)
)

func init() {
Expand All @@ -41,6 +46,7 @@ func init() {
FsIssueDenom.String(FlagData, "", "The data is the app specific metadata of the NFT class. Optional")
FsIssueDenom.Bool(FlagMintRestricted, false, "mint restricted of nft under denom")
FsIssueDenom.Bool(FlagUpdateRestricted, false, "update restricted of nft under denom")
FsIssueDenom.Bool(FlagRentable, false, "rental plugin enable option")

FsMintNFT.String(FlagURI, "", "The uri for supplemental off-chain tokenData (should return a JSON object)")
FsMintNFT.String(FlagURIHash, "", "The uri_hash is a hash of the document pointed by uri. Optional")
Expand All @@ -61,4 +67,7 @@ func init() {
FsQuerySupply.String(FlagOwner, "", "The owner of the nft")

FsQueryOwner.String(FlagDenomID, "", "The name of the collection")

FsRental.String(FlagRentalUser, "", "The user of the rented nft")
FsRental.Int64(FlagRentalExpiry, 0, "The expiry of the rented nft")
}
1 change: 1 addition & 0 deletions modules/nft/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQuerySupply(),
GetCmdQueryOwner(),
GetCmdQueryNFT(),
GetCmdQueryRental(),
)

return queryCmd
Expand Down
188 changes: 188 additions & 0 deletions modules/nft/client/cli/rental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package cli

import (
"context"
"fmt"
"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/irisnet/irismod/modules/nft/types"
"github.com/spf13/cobra"
)

// Tx Command for rental plugin

// GetCmdRental is the CLI tx command for NFT rental plugin.
func GetCmdRental() *cobra.Command {
cmd := &cobra.Command{
Use: "rental",
Long: "NFT rental plugin tx subcommands",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

cmd.AddCommand(
GetCmdRentalSetUser(),
)

return cmd
}

// GetCmdRentalSetUser is the CLI command for setting user for a rentable NFT.
func GetCmdRentalSetUser() *cobra.Command {
cmd := &cobra.Command{
Use: "set-user [denom-id] [nft-id]",
Long: "Set user and expiry for an NFT.",
Example: fmt.Sprintf(
"$ %s tx nft rental set-user <denom-id> <nft-id> "+
"--user=<user> "+
"--expiry=<expiry> "+
"--from=<key-name> "+
"--chain-id=<chain-id> "+
"--fees=<fee>",
version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

sender := clientCtx.GetFromAddress().String()

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

expiry, err := cmd.Flags().GetInt64(FlagRentalExpiry)
if err != nil {
return err
}

msg := types.NewMsgSetUser(
args[0],
args[1],
user,
expiry,
sender,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().AddFlagSet(FsRental)
flags.AddTxFlagsToCmd(cmd)

return cmd
}

// Query Command for rental plugin

// GetCmdQueryRental is the CLI query command for NFT rental plugin.
func GetCmdQueryRental() *cobra.Command {
cmd := &cobra.Command{
Use: "rental",
Long: "NFT rental plugin query subcommands",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

cmd.AddCommand(
GetCmdQueryRentalUserOf(),
GetCmdQueryRentalUserExpires(),
GetCmdQueryRentalHasUser(),
)

return cmd
}

// GetCmdQueryRentalUserOf is the CLI command for query user of a rentable NFT.
func GetCmdQueryRentalUserOf() *cobra.Command {
cmd := &cobra.Command{
Use: "user [denom-id] [nft-id]",
Long: "user of a rented nft",
Example: fmt.Sprintf("$ %s query nft rental user <denom-id> <nft-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
}

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.UserOf(context.Background(), &types.QueryUserOfRequest{
DenomId: args[0],
NftId: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryRentalUserExpires is the CLI command for query expiry of a rentable NFT.
func GetCmdQueryRentalUserExpires() *cobra.Command {
cmd := &cobra.Command{
Use: "user [denom-id] [nft-id]",
Long: "expiry of a rented nft",
Example: fmt.Sprintf("$ %s query nft rental user <denom-id> <nft-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
}

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.UserExpires(context.Background(), &types.QueryUserExpiresRequest{
DenomId: args[0],
NftId: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryRentalHasUser is the CLI command for query does an NFT have a user.
func GetCmdQueryRentalHasUser() *cobra.Command {
cmd := &cobra.Command{
Use: "user [denom-id] [nft-id]",
Long: "does an nft have a user",
Example: fmt.Sprintf("$ %s query nft rental user <denom-id> <nft-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
}

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.HasUser(context.Background(), &types.QueryHasUserRequest{
DenomId: args[0],
NftId: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
104 changes: 104 additions & 0 deletions modules/nft/client/cli/rental_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cli_test

//
//import (
// "fmt"
//
// "github.com/cosmos/cosmos-sdk/client/flags"
// sdk "github.com/cosmos/cosmos-sdk/types"
// "github.com/gogo/protobuf/proto"
// "github.com/irisnet/irismod/modules/nft/client/cli"
// "github.com/irisnet/irismod/modules/nft/client/testutil"
//)
//
//func (s *IntegrationTestSuite) TestRental() {
// owner := s.network.Validators[0]
// user := s.network.Validators[1]
//
// // 1. issue a denom with rental enabled.
// denomID := "denom"
// denomName := "denomName"
// denomSchema := "denomSchema"
// denomSymbol := "dsb"
// mintRestricted := false
// updateRestricted := true
// denomDescription := "denom description"
// denomURI := "denomURI"
// denomURIHash := "denomURIHash"
// denomData := "denomData"
// rentable := true
//
// args := []string{
// fmt.Sprintf("--%s=%s", cli.FlagDenomName, denomName),
// fmt.Sprintf("--%s=%s", cli.FlagSchema, denomSchema),
// fmt.Sprintf("--%s=%s", cli.FlagSymbol, denomSymbol),
// fmt.Sprintf("--%s=%s", cli.FlagURI, denomURI),
// fmt.Sprintf("--%s=%s", cli.FlagURIHash, denomURIHash),
// fmt.Sprintf("--%s=%s", cli.FlagDescription, denomDescription),
// fmt.Sprintf("--%s=%s", cli.FlagData, denomData),
// fmt.Sprintf("--%s=%t", cli.FlagMintRestricted, mintRestricted),
// fmt.Sprintf("--%s=%t", cli.FlagUpdateRestricted, updateRestricted),
// fmt.Sprintf("--%s=%t", cli.FlagRentable, rentable),
//
// fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
// fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
// fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
// }
//
// respType := proto.Message(&sdk.TxResponse{})
// expectedCode := uint32(0)
//
// bz, err := testutil.IssueDenomExec(owner.ClientCtx, owner.Address.String(), denomID, args...)
// s.Require().NoError(err)
// s.Require().NoError(owner.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
// txResp := respType.(*sdk.TxResponse)
// s.Require().Equal(expectedCode, txResp.Code)
//
// // 2. mint an nft
// tokenID := "token"
// tokenName := "tokenName"
// tokenURI := "tokenURI"
// tokenURIHash := "tokenURIHash"
// tokenData := "tokenData"
//
// args = []string{
// fmt.Sprintf("--%s=%s", cli.FlagData, tokenData),
// fmt.Sprintf("--%s=%s", cli.FlagRecipient, owner.Address.String()),
// fmt.Sprintf("--%s=%s", cli.FlagURI, tokenURI),
// fmt.Sprintf("--%s=%s", cli.FlagURIHash, tokenURIHash),
// fmt.Sprintf("--%s=%s", cli.FlagTokenName, tokenName),
//
// fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
// fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
// fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
// }
//
// respType = proto.Message(&sdk.TxResponse{})
//
// bz, err = testutil.MintNFTExec(owner.ClientCtx, owner.Address.String(), denomID, tokenID, args...)
// s.Require().NoError(err)
// s.Require().NoError(owner.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
// txResp = respType.(*sdk.TxResponse)
// s.Require().Equal(expectedCode, txResp.Code)
//
// // 3. set user for that nft
//
// expiry := "2524579200" // 2050-01-01-00:00:00
//
// args = []string{
// fmt.Sprintf("--%s=%s", cli.FlagRentalUser, user.Address.String()),
// fmt.Sprintf("--%s=%s", cli.FlagRentalExpiry, expiry),
// }
//
// respType = proto.Message(&sdk.TxResponse{})
//
// bz, err = testutil.RentalSetUser(owner.ClientCtx, owner.Address.String(), denomID, tokenID, args...)
// //s.Require().NoError(err)
// // s.Require().NoError(owner.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
// owner.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType)
// fmt.Print(respType)
// //txResp = respType.(*sdk.TxResponse)
// //s.Require().Equal(expectedCode, txResp.Code)
//
// // 4. get rental info
//}
9 changes: 8 additions & 1 deletion modules/nft/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewTxCmd() *cobra.Command {
GetCmdTransferNFT(),
GetCmdBurnNFT(),
GetCmdTransferDenom(),
GetCmdRental(),
)

return txCmd
Expand Down Expand Up @@ -107,6 +108,12 @@ func GetCmdIssueDenom() *cobra.Command {
schema = string(optionsContent)
}

// compose plugin config and user data
composeData, err := composeDenomData(cmd, data)
if err != nil {
return err
}

msg := types.NewMsgIssueDenom(
args[0],
denomName,
Expand All @@ -118,7 +125,7 @@ func GetCmdIssueDenom() *cobra.Command {
description,
uri,
uriHash,
data,
composeData,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
Loading

0 comments on commit 4149361

Please sign in to comment.