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

Fix SendNft in Cw721-base #132

Merged
merged 2 commits into from
Oct 28, 2020
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
55 changes: 32 additions & 23 deletions contracts/cw721-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use cosmwasm_std::{
attr, from_binary, to_binary, Api, Binary, BlockInfo, CosmosMsg, Env, Extern, HandleResponse,
HumanAddr, InitResponse, MessageInfo, Order, Querier, StdError, StdResult, Storage, KV,
attr, to_binary, Api, Binary, BlockInfo, Env, Extern, HandleResponse, HumanAddr, InitResponse,
MessageInfo, Order, Querier, StdError, StdResult, Storage, KV,
};

use cw0::maybe_canonical;
use cw2::set_contract_version;
use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, Expiration, NftInfoResponse,
NumTokensResponse, OwnerOfResponse, TokensResponse,
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, Cw721ReceiveMsg, Expiration,
NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
};

use crate::error::ContractError;
Expand Down Expand Up @@ -139,18 +139,18 @@ pub fn handle_send_nft<S: Storage, A: Api, Q: Querier>(
token_id: String,
msg: Option<Binary>,
) -> Result<HandleResponse, ContractError> {
// Unwrap message first
let msgs: Vec<CosmosMsg> = match &msg {
None => vec![],
Some(msg) => vec![from_binary(msg)?],
};

maurolacy marked this conversation as resolved.
Show resolved Hide resolved
// Transfer token
_transfer_nft(deps, &env, &info, &contract, &token_id)?;

let send = Cw721ReceiveMsg {
sender: info.sender.clone(),
token_id: token_id.clone(),
msg,
};

// Send message
Ok(HandleResponse {
messages: msgs,
messages: vec![send.into_cosmos_msg(contract.clone())?],
attributes: vec![
attr("action", "send_nft"),
attr("sender", info.sender),
Expand Down Expand Up @@ -584,7 +584,7 @@ fn humanize_approval<A: Api>(api: A, approval: &Approval) -> StdResult<cw721::Ap
#[cfg(test)]
mod tests {
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::WasmMsg;
use cosmwasm_std::{from_binary, CosmosMsg, WasmMsg};

use super::*;
use cw721::ApprovedForAllResponse;
Expand Down Expand Up @@ -798,18 +798,12 @@ mod tests {
let minter = mock_info(MINTER, &[]);
handle(&mut deps, mock_env(), minter, mint_msg).unwrap();

// random cannot send
let inner_msg = WasmMsg::Execute {
contract_addr: "another_contract".into(),
msg: to_binary("You now have the melting power").unwrap(),
send: vec![],
};
let msg: CosmosMsg = CosmosMsg::Wasm(inner_msg);

let msg = to_binary("You now have the melting power").unwrap();
let target = HumanAddr::from("another_contract");
let send_msg = HandleMsg::SendNft {
contract: "another_contract".into(),
contract: target.clone(),
token_id: token_id.clone(),
msg: Some(to_binary(&msg).unwrap()),
msg: Some(msg.clone()),
};

let random = mock_info("random", &[]);
Expand All @@ -822,10 +816,25 @@ mod tests {
// but owner can
let random = mock_info("venus", &[]);
let res = handle(&mut deps, mock_env(), random, send_msg).unwrap();

let payload = Cw721ReceiveMsg {
sender: "venus".into(),
token_id: token_id.clone(),
msg: Some(msg),
};
let expected = payload.into_cosmos_msg(target.clone()).unwrap();
// ensure expected serializes as we think it should
match &expected {
CosmosMsg::Wasm(WasmMsg::Execute { contract_addr, .. }) => {
assert_eq!(contract_addr, &target)
}
m => panic!("Unexpected message type: {:?}", m),
}
// and make sure this is the request sent by the contract
assert_eq!(
res,
HandleResponse {
messages: vec![msg],
messages: vec![expected],
attributes: vec![
attr("action", "send_nft"),
attr("sender", "venus"),
Expand Down
13 changes: 5 additions & 8 deletions packages/cw721/schema/cw721_receive_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
"description": "Cw721ReceiveMsg should be de/serialized under `Receive()` variant in a HandleMsg",
"type": "object",
"required": [
"amount",
"sender"
"sender",
"token_id"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"msg": {
"anyOf": [
{
Expand All @@ -23,6 +20,9 @@
},
"sender": {
"$ref": "#/definitions/HumanAddr"
},
"token_id": {
"type": "string"
}
},
"definitions": {
Expand All @@ -32,9 +32,6 @@
},
"HumanAddr": {
"type": "string"
},
"Uint128": {
"type": "string"
}
}
}
4 changes: 2 additions & 2 deletions packages/cw721/src/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{to_binary, Binary, CosmosMsg, HumanAddr, StdResult, Uint128, WasmMsg};
use cosmwasm_std::{to_binary, Binary, CosmosMsg, HumanAddr, StdResult, WasmMsg};

/// Cw721ReceiveMsg should be de/serialized under `Receive()` variant in a HandleMsg
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct Cw721ReceiveMsg {
pub sender: HumanAddr,
pub amount: Uint128,
pub token_id: String,
pub msg: Option<Binary>,
}

Expand Down