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

[cw721-fixed-price] Fix receiving cw20 msg #49

Merged
merged 4 commits into from
Jul 18, 2022
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
42 changes: 27 additions & 15 deletions contracts/cw721-fixed-price/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@
{
"type": "object",
"required": [
"cw20_receive_msg"
"receive"
],
"properties": {
"cw20_receive_msg": {
"type": "object",
"required": [
"amount",
"sender"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"sender": {
"type": "string"
}
}
"receive": {
"$ref": "#/definitions/Cw20ReceiveMsg"
}
},
"additionalProperties": false
}
],
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"Cw20ReceiveMsg": {
"description": "Cw20ReceiveMsg should be de/serialized under `Receive()` variant in a ExecuteMsg",
"type": "object",
"required": [
"amount",
"msg",
"sender"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"msg": {
"$ref": "#/definitions/Binary"
},
"sender": {
"type": "string"
}
}
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
Expand Down
35 changes: 22 additions & 13 deletions contracts/cw721-fixed-price/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cosmwasm_std::{
StdResult, SubMsg, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw20::Cw20ReceiveMsg;
use cw721_base::{
msg::ExecuteMsg as Cw721ExecuteMsg, msg::InstantiateMsg as Cw721InstantiateMsg, Extension,
MintMsg,
Expand Down Expand Up @@ -124,9 +125,11 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Cw20ReceiveMsg { sender, amount } => {
execute_receive(deps, info, sender, amount)
}
ExecuteMsg::Receive(Cw20ReceiveMsg {
sender,
amount,
msg,
}) => execute_receive(deps, info, sender, amount, msg),
}
}

Expand All @@ -135,6 +138,7 @@ pub fn execute_receive(
info: MessageInfo,
sender: String,
amount: Uint128,
_msg: Binary,
) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;
if config.cw20_address != info.sender {
Expand Down Expand Up @@ -353,10 +357,11 @@ mod tests {
};
reply(deps.as_mut(), mock_env(), reply_msg).unwrap();

let msg = ExecuteMsg::Cw20ReceiveMsg {
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("minter"),
amount: Uint128::new(1),
};
msg: [].into(),
});

let info = mock_info(MOCK_CONTRACT_ADDR, &[]);
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand Down Expand Up @@ -503,10 +508,11 @@ mod tests {
};
reply(deps.as_mut(), mock_env(), reply_msg).unwrap();

let msg = ExecuteMsg::Cw20ReceiveMsg {
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("minter"),
amount: Uint128::new(1),
};
msg: [].into(),
});
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);

// Max mint is 1, so second mint request should fail
Expand Down Expand Up @@ -540,10 +546,11 @@ mod tests {

// Test token transfer when nft contract has not been linked

let msg = ExecuteMsg::Cw20ReceiveMsg {
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("minter"),
amount: Uint128::new(1),
};
msg: [].into(),
});
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);

let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
Expand Down Expand Up @@ -593,10 +600,11 @@ mod tests {
reply(deps.as_mut(), mock_env(), reply_msg).unwrap();

// Test token transfer from invalid token contract
let msg = ExecuteMsg::Cw20ReceiveMsg {
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("minter"),
amount: Uint128::new(1),
};
msg: [].into(),
});
let info = mock_info("unauthorized-token", &[]);
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();

Expand Down Expand Up @@ -646,10 +654,11 @@ mod tests {
reply(deps.as_mut(), mock_env(), reply_msg).unwrap();

// Test token transfer from invalid token contract
let msg = ExecuteMsg::Cw20ReceiveMsg {
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("minter"),
amount: Uint128::new(100),
};
msg: [].into(),
});
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();

Expand Down
3 changes: 2 additions & 1 deletion contracts/cw721-fixed-price/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{Addr, Uint128};
use cw20::Cw20ReceiveMsg;
use cw721_base::Extension;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -19,7 +20,7 @@ pub struct InstantiateMsg {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Cw20ReceiveMsg { sender: String, amount: Uint128 },
Receive(Cw20ReceiveMsg),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down