From 69d502d80778bdc5c43226af06af5e811fa5b68e Mon Sep 17 00:00:00 2001 From: Axvn Date: Sun, 6 Feb 2022 10:49:38 +0100 Subject: [PATCH 1/2] [cw721-fixed-price] Fix receiving cw20 msg --- contracts/cw721-fixed-price/src/contract.rs | 35 +++++++++++++-------- contracts/cw721-fixed-price/src/msg.rs | 3 +- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/contracts/cw721-fixed-price/src/contract.rs b/contracts/cw721-fixed-price/src/contract.rs index 83b88b007..1f43f0bdd 100644 --- a/contracts/cw721-fixed-price/src/contract.rs +++ b/contracts/cw721-fixed-price/src/contract.rs @@ -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, @@ -124,9 +125,11 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { 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), } } @@ -135,6 +138,7 @@ pub fn execute_receive( info: MessageInfo, sender: String, amount: Uint128, + _msg: Binary, ) -> Result { let mut config = CONFIG.load(deps.storage)?; if config.cw20_address != info.sender { @@ -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(); @@ -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 @@ -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(); @@ -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(); @@ -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(); diff --git a/contracts/cw721-fixed-price/src/msg.rs b/contracts/cw721-fixed-price/src/msg.rs index 1f344c6b1..6ef12e171 100644 --- a/contracts/cw721-fixed-price/src/msg.rs +++ b/contracts/cw721-fixed-price/src/msg.rs @@ -1,4 +1,5 @@ use cosmwasm_std::{Addr, Uint128}; +use cw20::Cw20ReceiveMsg; use cw721_base::Extension; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -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)] From cc97715124264ba8f6c530185abeb3f705c8f4a4 Mon Sep 17 00:00:00 2001 From: Axvn Date: Sun, 6 Feb 2022 16:20:33 +0100 Subject: [PATCH 2/2] refresh schema --- .../cw721-fixed-price/schema/execute_msg.json | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/contracts/cw721-fixed-price/schema/execute_msg.json b/contracts/cw721-fixed-price/schema/execute_msg.json index 1893491c1..dc39f5520 100644 --- a/contracts/cw721-fixed-price/schema/execute_msg.json +++ b/contracts/cw721-fixed-price/schema/execute_msg.json @@ -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 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", + "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"