Skip to content

Commit

Permalink
add nft royalty client_test
Browse files Browse the repository at this point in the history
  • Loading branch information
aofengli committed Jan 12, 2023
1 parent 39e228c commit 79c277c
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 2 deletions.
177 changes: 177 additions & 0 deletions modules/nft/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cli_test

import (
"fmt"
"math/big"
"testing"

sdkmath "cosmossdk.io/math"

"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -327,4 +330,178 @@ func (s *IntegrationTestSuite) TestNft() {
s.Require().Equal(symbol, denomItem2.Symbol)
s.Require().Equal(mintRestricted, denomItem2.MintRestricted)
s.Require().Equal(updateRestricted, denomItem2.UpdateRestricted)

}

func (s *IntegrationTestSuite) TestRoyalty() {
val := s.network.Validators[0]

// ---------------------------------------------------------------------------
from := val.Address
tokenName := "Kitty Token1"
uri := "uri1"
uriHash := "uriHash1"
description := "description1"
data := "{\"key1\":\"value1\",\"key2\":\"value2\"}"
tokenID := "kitty1"
//owner := "owner"
denomName := "name1"
denom := "denom1"
schema := "schema"
symbol := "symbol"
mintRestricted := true
updateRestricted := false

denomDefaultFraction := sdkmath.NewUintFromBigInt(new(big.Int).SetUint64(1000))
tokenFraction := sdkmath.NewUintFromBigInt(new(big.Int).SetUint64(100))
tokenSalePrice := sdkmath.NewUintFromBigInt(new(big.Int).SetUint64(1000))
tokenRoyaltyAmountFromDenom := sdkmath.NewUintFromBigInt(new(big.Int).SetUint64(100))
tokenRoyaltyAmount := sdkmath.NewUintFromBigInt(new(big.Int).SetUint64(10))

//------test GetCmdIssueDenom()-------------
args := []string{
fmt.Sprintf("--%s=%s", nftcli.FlagDenomName, denomName),
fmt.Sprintf("--%s=%s", nftcli.FlagSchema, schema),
fmt.Sprintf("--%s=%s", nftcli.FlagSymbol, symbol),
fmt.Sprintf("--%s=%s", nftcli.FlagURI, uri),
fmt.Sprintf("--%s=%s", nftcli.FlagURIHash, uriHash),
fmt.Sprintf("--%s=%s", nftcli.FlagDescription, description),
fmt.Sprintf("--%s=%s", nftcli.FlagData, data),
fmt.Sprintf("--%s=%t", nftcli.FlagMintRestricted, mintRestricted),
fmt.Sprintf("--%s=%t", nftcli.FlagUpdateRestricted, updateRestricted),

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 := nfttestutil.IssueDenomExec(val.ClientCtx, from.String(), denom, args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp := respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

denomID := gjson.Get(txResp.RawLog, "0.events.0.attributes.0.value").String()

// test Royalty cases
//------test GetCmdSetDefaultRoyalty()-------------
args = []string{
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 = nfttestutil.SetDefaultRoyaltyExec(val.ClientCtx, from.String(), val.Address.String(), denomID, denomDefaultFraction.String(), args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

//------test GetCmdQueryDefaultRoyalty()-------------
respType = proto.Message(&nfttypes.MsgDefaultRoyaltyInfoResponse{})
bz, err = nfttestutil.QueryDefaultRoyaltyExec(val.ClientCtx, denomID)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))

defaultRoyaltyInfo := respType.(*nfttypes.MsgDefaultRoyaltyInfoResponse)
s.Require().Equal(denomDefaultFraction, defaultRoyaltyInfo.RoyaltyFraction)
s.Require().Equal(val.Address.String(), defaultRoyaltyInfo.Receiver)

//------test GetCmdMintNFT()-------------
args = []string{
fmt.Sprintf("--%s=%s", nftcli.FlagData, data),
fmt.Sprintf("--%s=%s", nftcli.FlagRecipient, from.String()),
fmt.Sprintf("--%s=%s", nftcli.FlagURI, uri),
fmt.Sprintf("--%s=%s", nftcli.FlagURIHash, uriHash),
fmt.Sprintf("--%s=%s", nftcli.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 = nfttestutil.MintNFTExec(val.ClientCtx, from.String(), denomID, tokenID, args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

//------test GetCmdSetTokenRoyalty()-------------
args = []string{
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 = nfttestutil.SetTokenRoyaltyExec(val.ClientCtx, from.String(), val.Address.String(), denomID, tokenID, tokenFraction.String(), args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

//------test GetCmdQueryTokenRoyalty()-------------
respType = proto.Message(&nfttypes.MsgTokenRoyaltyInfoResponse{})
bz, err = nfttestutil.QueryTokenRoyaltyExec(val.ClientCtx, denomID, tokenID)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))

tokenRoyaltyInfo := respType.(*nfttypes.MsgTokenRoyaltyInfoResponse)
s.Require().Equal(tokenFraction, tokenRoyaltyInfo.RoyaltyFraction)
s.Require().Equal(val.Address.String(), tokenRoyaltyInfo.Receiver)

//------test GetCmdQueryRoyaltyInfo()-------------
respType = proto.Message(&nfttypes.MsgRoyaltyInfoResponse{})
bz, err = nfttestutil.QueryRoyaltyInfoExec(val.ClientCtx, denomID, tokenID, tokenSalePrice.String())
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))

royaltyInfo := respType.(*nfttypes.MsgRoyaltyInfoResponse)
s.Require().Equal(tokenRoyaltyAmount, royaltyInfo.RoyaltyAmount)
s.Require().Equal(val.Address.String(), royaltyInfo.Receiver)

//------test GetCmdResetTokenRoyalty()-------------
args = []string{
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 = nfttestutil.ResetTokenRoyaltyExec(val.ClientCtx, from.String(), denomID, tokenID, args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

//------test GetCmdQueryRoyaltyInfo()-------------
respType = proto.Message(&nfttypes.MsgRoyaltyInfoResponse{})
bz, err = nfttestutil.QueryRoyaltyInfoExec(val.ClientCtx, denomID, tokenID, tokenSalePrice.String())
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))

royaltyInfo = respType.(*nfttypes.MsgRoyaltyInfoResponse)
s.Require().Equal(tokenRoyaltyAmountFromDenom, royaltyInfo.RoyaltyAmount)
s.Require().Equal(val.Address.String(), royaltyInfo.Receiver)

//------test GetCmdDeleteDefaultRoyalty()-------------
args = []string{
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 = nfttestutil.DeleteDefaultRoyaltyExec(val.ClientCtx, from.String(), denomID, args...)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String())
txResp = respType.(*sdk.TxResponse)
s.Require().Equal(expectedCode, txResp.Code)

}
111 changes: 111 additions & 0 deletions modules/nft/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

sdkmath "cosmossdk.io/math"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -29,6 +31,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQuerySupply(),
GetCmdQueryOwner(),
GetCmdQueryNFT(),
GetCmdQueryDefaultRoyalty(),
)

return queryCmd
Expand Down Expand Up @@ -247,3 +250,111 @@ func GetCmdQueryNFT() *cobra.Command {

return cmd
}

// GetCmdQueryDefaultRoyalty queries a single NFTs from a collection
func GetCmdQueryDefaultRoyalty() *cobra.Command {
cmd := &cobra.Command{
Use: "denom royalty [denom-id] ",
Long: "Query the default royalty information of a denom",
Example: fmt.Sprintf("$ %s query nft denom royalty <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.DefaultRoyaltyInfo(context.Background(), &types.MsgDefaultRoyaltyInfoRequest{
DenomId: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdQueryTokenRoyalty queries a single NFTs from a collection
func GetCmdQueryTokenRoyalty() *cobra.Command {
cmd := &cobra.Command{
Use: "token royalty [denom-id] [token-id]",
Long: "Query the default royalty information of a denom",
Example: fmt.Sprintf("$ %s query nft token royalty <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.ValidateDenomID(args[1]); err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.TokenRoyaltyInfo(context.Background(), &types.MsgTokenRoyaltyInfoRequest{
DenomId: args[0],
NftId: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdQueryRoyaltyInfo queries a single NFTs from a collection
func GetCmdQueryRoyaltyInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "token royalty-info [denom-id] [token-id] [sale-price]",
Long: "Query the default royalty information of a denom",
Example: fmt.Sprintf("$ %s query nft token royalty-info <denom-id> <token-id> ", version.AppName),
Args: cobra.ExactArgs(3),
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.ValidateDenomID(args[1]); err != nil {
return err
}

salePrice := sdkmath.NewUintFromString(args[2])

queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.RoyaltyInfo(context.Background(), &types.MsgRoyaltyInfoRequest{
DenomId: args[0],
NftId: args[1],
SalePrice: salePrice,
})
if err != nil {
return err
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit 79c277c

Please sign in to comment.