Skip to content

Commit

Permalink
Update migration method with feedback from @larry0x.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xekez committed Feb 8, 2023
1 parent 87e7d89 commit 263036d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 53 deletions.
3 changes: 1 addition & 2 deletions contracts/cw721-base/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use cosmwasm_schema::write_api;
use cosmwasm_std::Empty;

use cw721_base::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use cw721_base::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg<Empty, Empty>,
query: QueryMsg<Empty>,
migrate: MigrateMsg,
}
}
21 changes: 1 addition & 20 deletions contracts/cw721-base/schema/cw721-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -759,26 +759,7 @@
}
}
},
"migrate": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MigrateMsg",
"oneOf": [
{
"description": "Migrates the contract from version 0.16.0 to the latest version. After 0.16.0 the minter was removed in favor of using cw_ownable to track the NFT contract's owner.",
"type": "object",
"required": [
"from016"
],
"properties": {
"from016": {
"type": "object",
"additionalProperties": false
}
},
"additionalProperties": false
}
]
},
"migrate": null,
"sudo": null,
"responses": {
"all_nft_info": {
Expand Down
28 changes: 9 additions & 19 deletions contracts/cw721-base/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cw721::{ContractInfoResponse, Cw721Execute, Cw721ReceiveMsg, Expiration};
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::state::{Approval, Cw721Contract, TokenInfo};
use crate::MigrateMsg;
use crate::upgrades;

// Version info for migration
const CONTRACT_NAME: &str = "crates.io:cw721-base";
Expand Down Expand Up @@ -134,26 +134,16 @@ where
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

pub fn migrate(deps: DepsMut, msg: MigrateMsg) -> Result<Response<C>, ContractError> {
/// Migrates the contract from the previous version to the current
/// version.
pub fn migrate(deps: DepsMut, _env: Env) -> Result<Response<C>, ContractError> {
let ContractVersion { version, .. } = get_contract_version(deps.storage)?;
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
match msg {
MigrateMsg::From016 {} => {
if version != "0.16.0" {
Err(ContractError::WrongMigrateVersion(version))
} else {
use cw721_base_016 as v16;
let tract16 = v16::Cw721Contract::<T, C, E, Q>::default();
let minter = tract16.minter.load(deps.storage)?;
tract16.minter.remove(deps.storage);
cw_ownable::initialize_owner(deps.storage, deps.api, Some(minter.as_str()))?;
let ownership = cw_ownable::get_ownership(deps.storage)?;
Ok(Response::new()
.add_attribute("method", "migrate_from_016")
.add_attribute("old_minter", minter)
.add_attributes(ownership.into_attributes()))
}
}

if version != "0.16.0" {
Err(ContractError::WrongMigrateVersion(version))
} else {
upgrades::v0_16::migrate::<T, C, E, Q>(deps)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions contracts/cw721-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ pub mod helpers;
pub mod msg;
mod query;
pub mod state;
mod upgrades;

#[cfg(test)]
mod contract_tests;
#[cfg(test)]
mod multi_tests;

pub use crate::error::ContractError;
pub use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, MinterResponse, QueryMsg};
pub use crate::msg::{ExecuteMsg, InstantiateMsg, MinterResponse, QueryMsg};
pub use crate::state::Cw721Contract;

// These types are re-exported so that contracts interacting with this
Expand Down Expand Up @@ -64,7 +65,7 @@ pub mod entry {
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
Cw721Contract::<Extension, Empty, Empty, Empty>::migrate(deps, msg)
pub fn migrate(deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, ContractError> {
Cw721Contract::<Extension, Empty, Empty, Empty>::migrate(deps, env)
}
}
8 changes: 0 additions & 8 deletions contracts/cw721-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,3 @@ pub enum QueryMsg<Q: JsonSchema> {
pub struct MinterResponse {
pub minter: Option<String>,
}

#[cw_serde]
pub enum MigrateMsg {
/// Migrates the contract from version 0.16.0 to the latest
/// version. After 0.16.0 the minter was removed in favor of using
/// cw_ownable to track the NFT contract's owner.
From016 {},
}
2 changes: 1 addition & 1 deletion contracts/cw721-base/src/multi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn test_016_017_migration() {
WasmMsg::Migrate {
contract_addr: cw721.to_string(),
new_code_id: code_id_017,
msg: to_binary(&crate::MigrateMsg::From016 {}).unwrap(),
msg: to_binary(&Empty::default()).unwrap(),
}
.into(),
)
Expand Down

0 comments on commit 263036d

Please sign in to comment.