Skip to content

Commit

Permalink
Add migration logic for 0.16 upgrades.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekez committed Feb 7, 2023
1 parent 153fe5c commit 3178a72
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 19 deletions.
94 changes: 84 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ rust-version = "1.65"
[workspace.dependencies]
cosmwasm-schema = "1.1.5"
cosmwasm-std = "1.1.5"
cw-multi-test = "0.16.0"
cw-ownable = "0.4.0"
cw-storage-plus = "0.16.0"
cw-utils = "0.16.0"
cw2 = "0.16.0"
cw20 = "0.16.0"
cw721 = { version = "0.16.0", path = "./packages/cw721" }
cw721-base = { version = "0.16.0", path = "./contracts/cw721-base" }
cw-storage-plus = "0.16.0"
cw-utils = "0.16.0"
cw721-base-016 = { package = "cw721-base", version = "0.16.0" }
schemars = "0.8.10"
serde = { version = "1.0.140", default-features = false, features = ["derive"] }
thiserror = "1.0.31"
cw-ownable = "0.4.0"

[profile.release.package.cw721-base]
codegen-units = 1
Expand Down
6 changes: 5 additions & 1 deletion contracts/cw721-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ library = []
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-ownable = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
cw2 = { workspace = true }
cw721 = { workspace = true }
cw-storage-plus = { workspace = true }
cw721-base-016 = { workspace = true, features = ["library"] }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
3 changes: 2 additions & 1 deletion contracts/cw721-base/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use cosmwasm_schema::write_api;
use cosmwasm_std::Empty;

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

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg<Empty, Empty>,
query: QueryMsg<Empty>,
migrate: MigrateMsg,
}
}
21 changes: 20 additions & 1 deletion contracts/cw721-base/schema/cw721-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,26 @@
}
}
},
"migrate": null,
"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's owner.",
"type": "object",
"required": [
"from016"
],
"properties": {
"from016": {
"type": "object",
"additionalProperties": false
}
},
"additionalProperties": false
}
]
},
"sudo": null,
"responses": {
"all_nft_info": {
Expand Down
3 changes: 3 additions & 0 deletions contracts/cw721-base/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ pub enum ContractError {

#[error("Approval not found for: {spender}")]
ApprovalNotFound { spender: String },

#[error("found version ({0}) while attempting to migrate from 0.16.0")]
WrongMigrateVersion(String),
}
26 changes: 25 additions & 1 deletion contracts/cw721-base/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use serde::Serialize;

use cosmwasm_std::{Binary, CustomMsg, Deps, DepsMut, Env, MessageInfo, Response, StdResult};

use cw2::set_contract_version;
use cw2::{get_contract_version, set_contract_version, ContractVersion};
use cw721::{ContractInfoResponse, Cw721Execute, Cw721ReceiveMsg, Expiration};

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::state::{Approval, Cw721Contract, TokenInfo};
use crate::MigrateMsg;

// Version info for migration
const CONTRACT_NAME: &str = "crates.io:cw721-base";
Expand Down Expand Up @@ -132,6 +133,29 @@ where
let ownership = cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?;
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

pub fn migrate(deps: DepsMut, msg: MigrateMsg) -> 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()))
}
}
}
}
}

impl<'a, T, C, E, Q> Cw721Execute<T, C> for Cw721Contract<'a, T, C, E, Q>
Expand Down
13 changes: 11 additions & 2 deletions contracts/cw721-base/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
mod contract_tests;
mod error;
mod execute;
pub mod helpers;
pub mod msg;
mod query;
pub mod state;

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

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

// These types are re-exported so that contracts interacting with this
Expand Down Expand Up @@ -58,4 +62,9 @@ pub mod entry {
let tract = Cw721Contract::<Extension, Empty, Empty, Empty>::default();
tract.query(deps, env, msg)
}

#[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)
}
}
8 changes: 8 additions & 0 deletions contracts/cw721-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ 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 {},
}
Loading

0 comments on commit 3178a72

Please sign in to comment.