From e688635cb46a5ea7d833230824d0dd1263669b91 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Wed, 15 Feb 2023 17:12:38 +0000 Subject: [PATCH 1/2] move cw2 into the entry point --- contracts/cw2981-royalties/src/lib.rs | 11 ++++------- contracts/cw721-base/src/execute.rs | 9 +++------ contracts/cw721-base/src/lib.rs | 6 ++++++ contracts/cw721-metadata-onchain/src/lib.rs | 11 ++++------- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/contracts/cw2981-royalties/src/lib.rs b/contracts/cw2981-royalties/src/lib.rs index fa63d02b6..4fe26e2d8 100644 --- a/contracts/cw2981-royalties/src/lib.rs +++ b/contracts/cw2981-royalties/src/lib.rs @@ -5,7 +5,6 @@ pub use query::{check_royalties, query_royalties_info}; use cosmwasm_schema::cw_serde; use cosmwasm_std::{to_binary, Empty}; -use cw2::set_contract_version; use cw721_base::Cw721Contract; pub use cw721_base::{ContractError, InstantiateMsg, MinterResponse}; @@ -65,12 +64,10 @@ pub mod entry { env: Env, info: MessageInfo, msg: InstantiateMsg, - ) -> Result { - let res = Cw2981Contract::default().instantiate(deps.branch(), env, info, msg)?; - // Explicitly set contract name and version, otherwise set to cw721-base info - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION) - .map_err(ContractError::Std)?; - Ok(res) + ) -> StdResult { + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + Cw2981Contract::default().instantiate(deps.branch(), env, info, msg) } #[entry_point] diff --git a/contracts/cw721-base/src/execute.rs b/contracts/cw721-base/src/execute.rs index 2f7cd214a..ab2caa312 100644 --- a/contracts/cw721-base/src/execute.rs +++ b/contracts/cw721-base/src/execute.rs @@ -11,10 +11,7 @@ use crate::error::ContractError; use crate::msg::{ExecuteMsg, InstantiateMsg}; use crate::state::{Approval, Cw721Contract, TokenInfo}; use crate::upgrades; - -// Version info for migration -const CONTRACT_NAME: &str = "crates.io:cw721-base"; -const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); +use crate::{CONTRACT_NAME, CONTRACT_VERSION}; impl<'a, T, C, E, Q> Cw721Contract<'a, T, C, E, Q> where @@ -30,14 +27,14 @@ where _info: MessageInfo, msg: InstantiateMsg, ) -> StdResult> { - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; - let info = ContractInfoResponse { name: msg.name, symbol: msg.symbol, }; self.contract_info.save(deps.storage, &info)?; + cw_ownable::initialize_owner(deps.storage, deps.api, Some(&msg.minter))?; + Ok(Response::default()) } diff --git a/contracts/cw721-base/src/lib.rs b/contracts/cw721-base/src/lib.rs index 57084aa2a..eaeb4121b 100644 --- a/contracts/cw721-base/src/lib.rs +++ b/contracts/cw721-base/src/lib.rs @@ -28,6 +28,10 @@ use cosmwasm_std::Empty; // This is a simple type to let us handle empty extensions pub type Extension = Option; +// Version info for migration +const CONTRACT_NAME: &str = "crates.io:cw721-base"; +const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); + pub mod entry { use super::*; @@ -43,6 +47,8 @@ pub mod entry { info: MessageInfo, msg: InstantiateMsg, ) -> StdResult { + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + let tract = Cw721Contract::::default(); tract.instantiate(deps, env, info, msg) } diff --git a/contracts/cw721-metadata-onchain/src/lib.rs b/contracts/cw721-metadata-onchain/src/lib.rs index 1ba51a9ee..2c8bba260 100644 --- a/contracts/cw721-metadata-onchain/src/lib.rs +++ b/contracts/cw721-metadata-onchain/src/lib.rs @@ -1,6 +1,5 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::Empty; -use cw2::set_contract_version; pub use cw721_base::{ContractError, InstantiateMsg, MinterResponse}; // Version info for migration @@ -49,12 +48,10 @@ pub mod entry { env: Env, info: MessageInfo, msg: InstantiateMsg, - ) -> Result { - let res = Cw721MetadataContract::default().instantiate(deps.branch(), env, info, msg)?; - // Explicitly set contract name and version, otherwise set to cw721-base info - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION) - .map_err(ContractError::Std)?; - Ok(res) + ) -> StdResult { + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + Cw721MetadataContract::default().instantiate(deps.branch(), env, info, msg) } #[entry_point] From f4d3f17621a990807cca0ae4ca2d3d1948ed33b9 Mon Sep 17 00:00:00 2001 From: larry <26318510+larry0x@users.noreply.github.com> Date: Wed, 15 Feb 2023 17:42:19 +0000 Subject: [PATCH 2/2] add unit tests --- contracts/cw721-base/src/lib.rs | 39 +++++++++++++++++++-- contracts/cw721-metadata-onchain/src/lib.rs | 23 ++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/contracts/cw721-base/src/lib.rs b/contracts/cw721-base/src/lib.rs index eaeb4121b..cbc4dcf27 100644 --- a/contracts/cw721-base/src/lib.rs +++ b/contracts/cw721-base/src/lib.rs @@ -29,8 +29,8 @@ use cosmwasm_std::Empty; pub type Extension = Option; // Version info for migration -const CONTRACT_NAME: &str = "crates.io:cw721-base"; -const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const CONTRACT_NAME: &str = "crates.io:cw721-base"; +pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod entry { use super::*; @@ -75,3 +75,38 @@ pub mod entry { Cw721Contract::::migrate(deps, env) } } + +#[cfg(test)] +mod tests { + use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cw2::ContractVersion; + + use super::*; + + /// Make sure cw2 version info is properly initialized during instantiation. + #[test] + fn proper_cw2_initialization() { + let mut deps = mock_dependencies(); + + entry::instantiate( + deps.as_mut(), + mock_env(), + mock_info("larry", &[]), + InstantiateMsg { + name: "".into(), + symbol: "".into(), + minter: "larry".into(), + }, + ) + .unwrap(); + + let version = cw2::get_contract_version(deps.as_ref().storage).unwrap(); + assert_eq!( + version, + ContractVersion { + contract: CONTRACT_NAME.into(), + version: CONTRACT_VERSION.into(), + }, + ); + } +} diff --git a/contracts/cw721-metadata-onchain/src/lib.rs b/contracts/cw721-metadata-onchain/src/lib.rs index 2c8bba260..8de57e145 100644 --- a/contracts/cw721-metadata-onchain/src/lib.rs +++ b/contracts/cw721-metadata-onchain/src/lib.rs @@ -79,6 +79,29 @@ mod tests { const CREATOR: &str = "creator"; + /// Make sure cw2 version info is properly initialized during instantiation, + /// and NOT overwritten by the base contract. + #[test] + fn proper_cw2_initialization() { + let mut deps = mock_dependencies(); + + entry::instantiate( + deps.as_mut(), + mock_env(), + mock_info("larry", &[]), + InstantiateMsg { + name: "".into(), + symbol: "".into(), + minter: "larry".into(), + }, + ) + .unwrap(); + + let version = cw2::get_contract_version(deps.as_ref().storage).unwrap(); + assert_eq!(version.contract, CONTRACT_NAME); + assert_ne!(version.contract, cw721_base::CONTRACT_NAME); + } + #[test] fn use_metadata_extension() { let mut deps = mock_dependencies();