Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Add a field "TokenURI" in MsgTransferNFT #4

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions incubator/nft/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {

// GetCmdTransferNFT is the CLI command for sending a TransferNFT transaction
func GetCmdTransferNFT(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "transfer [sender] [recipient] [denom] [tokenID]",
Short: "transfer a NFT to a recipient",
Long: strings.TrimSpace(
Expand Down Expand Up @@ -76,11 +76,14 @@ crypto-kitties d04b98f48e8f8bcc15c6ae5ac050801cd6dcfd428fb5f9e65c4e16e7807340fa

denom := args[2]
tokenID := args[3]
tokenURI := viper.GetString(flagTokenURI)

msg := types.NewMsgTransferNFT(sender, recipient, denom, tokenID)
msg := types.NewMsgTransferNFT(sender, recipient, denom, tokenID, tokenURI)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd.Flags().String(flagTokenURI, "[do-not-modify]", "Extra properties available for querying")
return cmd
}

// GetCmdEditNFTMetadata is the CLI command for sending an EditMetadata transaction
Expand Down
3 changes: 2 additions & 1 deletion incubator/nft/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type transferNFTReq struct {
Denom string `json:"denom"`
ID string `json:"id"`
Recipient string `json:"recipient"`
TokenURI string `json:"tokenURI"`
}

func transferNFTHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
Expand All @@ -65,7 +66,7 @@ func transferNFTHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.Handle
return
}
// create the message
msg := types.NewMsgTransferNFT(cliCtx.GetFromAddress(), recipient, req.Denom, req.ID)
msg := types.NewMsgTransferNFT(cliCtx.GetFromAddress(), recipient, req.Denom, req.ID, req.TokenURI)

utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
Expand Down
3 changes: 3 additions & 0 deletions incubator/nft/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func HandleMsgTransferNFT(ctx sdk.Context, msg types.MsgTransferNFT, k keeper.Ke
}
// update NFT owner
nft.SetOwner(msg.Recipient)
if msg.TokenURI != types.DoNotModify {
nft.EditMetadata(msg.TokenURI)
}
// update the NFT (owners are updated within the keeper)
err = k.UpdateNFT(ctx, msg.Denom, nft)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions incubator/nft/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestTransferNFTMsg(t *testing.T) {
nft := types.NewBaseNFT(id, address, "TokenURI")

// Define MsgTransferNft
transferNftMsg := types.NewMsgTransferNFT(address, address2, denom, id)
transferNftMsg := types.NewMsgTransferNFT(address, address2, denom, id, tokenURI)

// handle should fail trying to transfer NFT that doesn't exist
res := h(ctx, transferNftMsg)
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestTransferNFTMsg(t *testing.T) {
require.NoError(t, err)
require.True(t, nftAfterwards.GetOwner().Equals(address2))

transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom, id)
transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom, id, tokenURI)

// handle should succeed when nft exists and is transferred by owner
res = h(ctx, transferNftMsg)
Expand All @@ -92,7 +92,7 @@ func TestTransferNFTMsg(t *testing.T) {
require.Nil(t, err)
require.True(t, CheckInvariants(app.NFTKeeper, ctx))

transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom2, id)
transferNftMsg = types.NewMsgTransferNFT(address2, address3, denom2, id, tokenURI)

// handle should succeed when nft exists and is transferred by owner
res = h(ctx, transferNftMsg)
Expand Down
7 changes: 6 additions & 1 deletion incubator/nft/internal/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// constant used to indicate that some field should not be updated
const DoNotModify = "[do-not-modify]"

/* --------------------------------------------------------------------------- */
// MsgTransferNFT
/* --------------------------------------------------------------------------- */
Expand All @@ -14,17 +17,19 @@ import (
type MsgTransferNFT struct {
Sender sdk.AccAddress
Recipient sdk.AccAddress
TokenURI string
Denom string
ID string
}

// NewMsgTransferNFT is a constructor function for MsgSetName
func NewMsgTransferNFT(sender, recipient sdk.AccAddress, denom, id string) MsgTransferNFT {
func NewMsgTransferNFT(sender, recipient sdk.AccAddress, denom, id, tokenURI string) MsgTransferNFT {
return MsgTransferNFT{
Sender: sender,
Recipient: recipient,
Denom: strings.TrimSpace(denom),
ID: strings.TrimSpace(id),
TokenURI: strings.TrimSpace(tokenURI),
}
}

Expand Down
20 changes: 10 additions & 10 deletions incubator/nft/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@ import (
func TestNewMsgTransferNFT(t *testing.T) {
newMsgTransferNFT := NewMsgTransferNFT(address, address2,
fmt.Sprintf(" %s ", denom),
fmt.Sprintf(" %s ", id))
fmt.Sprintf(" %s ", id), tokenURI)
require.Equal(t, newMsgTransferNFT.Sender, address)
require.Equal(t, newMsgTransferNFT.Recipient, address2)
require.Equal(t, newMsgTransferNFT.Denom, denom)
require.Equal(t, newMsgTransferNFT.ID, id)
}

func TestMsgTransferNFTValidateBasicMethod(t *testing.T) {
newMsgTransferNFT := NewMsgTransferNFT(address, address2, "", id)
newMsgTransferNFT := NewMsgTransferNFT(address, address2, "", id, tokenURI)
err := newMsgTransferNFT.ValidateBasic()
require.Error(t, err)

newMsgTransferNFT = NewMsgTransferNFT(address, address2, denom, "")
newMsgTransferNFT = NewMsgTransferNFT(address, address2, denom, "", tokenURI)
err = newMsgTransferNFT.ValidateBasic()
require.Error(t, err)

newMsgTransferNFT = NewMsgTransferNFT(nil, address2, denom, "")
newMsgTransferNFT = NewMsgTransferNFT(nil, address2, denom, "", tokenURI)
err = newMsgTransferNFT.ValidateBasic()
require.Error(t, err)

newMsgTransferNFT = NewMsgTransferNFT(address, nil, denom, "")
newMsgTransferNFT = NewMsgTransferNFT(address, nil, denom, "", tokenURI)
err = newMsgTransferNFT.ValidateBasic()
require.Error(t, err)

newMsgTransferNFT = NewMsgTransferNFT(address, address2, denom, id)
newMsgTransferNFT = NewMsgTransferNFT(address, address2, denom, id, tokenURI)
err = newMsgTransferNFT.ValidateBasic()
require.NoError(t, err)
}

func TestMsgTransferNFTGetSignBytesMethod(t *testing.T) {
newMsgTransferNFT := NewMsgTransferNFT(address, address2, denom, id)
newMsgTransferNFT := NewMsgTransferNFT(address, address2, denom, id, tokenURI)
sortedBytes := newMsgTransferNFT.GetSignBytes()
require.Equal(t, string(sortedBytes), fmt.Sprintf(`{"type":"cosmos-sdk/MsgTransferNFT","value":{"Denom":"%s","ID":"%s","Recipient":"%s","Sender":"%s"}}`,
denom, id, address2, address,
require.Equal(t, string(sortedBytes), fmt.Sprintf(`{"type":"cosmos-sdk/MsgTransferNFT","value":{"Denom":"%s","ID":"%s","Recipient":"%s","Sender":"%s","TokenURI":"%s"}}`,
denom, id, address2, address, tokenURI,
))
}

func TestMsgTransferNFTGetSignersMethod(t *testing.T) {
newMsgTransferNFT := NewMsgTransferNFT(address, address2, denom, id)
newMsgTransferNFT := NewMsgTransferNFT(address, address2, denom, id, tokenURI)
signers := newMsgTransferNFT.GetSigners()
require.Equal(t, 1, len(signers))
require.Equal(t, address.String(), signers[0].String())
Expand Down
1 change: 1 addition & 0 deletions incubator/nft/simulation/operations/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func SimulateMsgTransferNFT(k keeper.Keeper) simulation.Operation {
simulation.RandomAcc(r, accs).Address, // recipient
denom,
nftID,
"",
)

if msg.ValidateBasic() != nil {
Expand Down