Skip to content

Commit

Permalink
migrating is always possible, no version check needed
Browse files Browse the repository at this point in the history
  • Loading branch information
taitruong committed Mar 19, 2024
1 parent 80ad7b4 commit 78b80f4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
21 changes: 10 additions & 11 deletions contracts/collections/sg721-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cw721::msg::CollectionMetadataMsg;
use cw721::msg::{CollectionMetadataMsg, Cw721MigrateMsg};
use cw721::state::{MAX_COLLECTION_DESCRIPTION_LENGTH, MINTER};
use cw721::traits::{Cw721CustomMsg, Cw721State};
use cw721_base::msg::{CollectionMetadataExtensionMsg, RoyaltyInfoResponse};
Expand Down Expand Up @@ -274,7 +274,11 @@ where
Ok(collection_info)
}

pub fn migrate(mut deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, ContractError> {
pub fn migrate(
mut deps: DepsMut,
env: Env,
msg: Cw721MigrateMsg,
) -> Result<Response, ContractError> {
let prev_contract_version = cw2::get_contract_version(deps.storage)?;

let valid_contract_names = vec![CONTRACT_NAME.to_string()];
Expand All @@ -289,15 +293,10 @@ where

let mut response = Response::new();

#[allow(clippy::cmp_owned)]
if prev_contract_version.version < "3.0.0".to_string() {
response = crate::upgrades::v3_0_0::upgrade(deps.branch(), &env, response)?;
}

#[allow(clippy::cmp_owned)]
if prev_contract_version.version < "3.1.0".to_string() {
response = crate::upgrades::v3_1_0::upgrade(deps.branch(), &env, response)?;
}
// these upgrades can always be called. migration is only executed in case new stores are empty! It is safe calling these on any version.
response = crate::upgrades::v3_0_0_ownable::upgrade(deps.branch(), &env, response, msg)?;
response =
crate::upgrades::v3_1_0_royalty_timestamp::upgrade(deps.branch(), &env, response)?;

cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Expand Down
4 changes: 2 additions & 2 deletions contracts/collections/sg721-base/src/upgrades/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod v3_0_0;
pub mod v3_1_0;
pub mod v3_0_0_ownable;
pub mod v3_1_0_royalty_timestamp;
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@ use cw721::{
};
use cw721_base::execute::Cw721Execute;

pub fn upgrade(deps: DepsMut, env: &Env, response: Response) -> Result<Response, ContractError> {
/// Migrates cw721 states:
/// (1) legacy creator and minter migration, now using cw-ownable
/// - v0.15 and v0.16: dedicated minter store
/// - v0.17 and v0.18: minter stored using cw-ownable
/// (2) legacy contract info migration -> collection metadata
/// - before v0.19 there was only contract info
/// - now we have collection metadata with optional extension
/// (3) optional creator and minter reset (as passed in Cw721MigrateMsg::WithUpdate)
///
/// Note: migration is only executed in case new stores are empty! It is safe calling these on any version.
pub fn upgrade(
deps: DepsMut,
env: &Env,
response: Response,
msg: Cw721MigrateMsg,
) -> Result<Response, ContractError> {
let contract = Sg721Contract::<
DefaultOptionNftMetadataExtension,
DefaultOptionNftMetadataExtensionMsg,
DefaultOptionCollectionMetadataExtension,
DefaultOptionCollectionMetadataExtensionMsg,
Empty,
>::default();
let migrate_msg = Cw721MigrateMsg::WithUpdate {
minter: None,
creator: None,
};
// cw721 migration allows all versions: 0.18. 0.17, 0.16 and older
// cw721 migration covers these versions: 0.18. 0.17, 0.16 and 0.15
let cw721_res = contract
.parent
.migrate(
deps,
env.clone(),
migrate_msg,
CONTRACT_NAME,
CONTRACT_VERSION,
)
.migrate(deps, env.clone(), msg, CONTRACT_NAME, CONTRACT_VERSION)
.map_err(|e| ContractError::MigrationError(e.to_string()))?;

let mut event = Event::new("migrate-3.0.0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@ pub fn upgrade(deps: DepsMut, env: &Env, response: Response) -> Result<Response,
DefaultOptionCollectionMetadataExtensionMsg,
Empty,
>::default();
// check whether royalty timestamp already exists
let royalty_updated_at = contract.royalty_updated_at.may_load(deps.storage)?;
if let Some(_) = royalty_updated_at {
// already migrated
return Ok(response);
}
let royalty_updated_at = env.block.time.minus_seconds(60 * 60 * 24); // 24 hours ago

contract
.royalty_updated_at
.save(deps.storage, &royalty_updated_at)?;

let migrate_msg = Cw721MigrateMsg::WithUpdate {
minter: None,
creator: None,
};
// cw721 migration allows all versions: 0.18. 0.17, 0.16 and older
let cw721_res = contract
.parent
.migrate(
deps,
env.clone(),
migrate_msg,
CONTRACT_NAME,
CONTRACT_VERSION,
)
.map_err(|e| ContractError::MigrationError(e.to_string()))?;

let mut event = Event::new("migrate-3.1.0")
let event = Event::new("migrate-3.1.0")
.add_attribute("royalty-updated", royalty_updated_at.to_string());
event = event.add_attributes(cw721_res.attributes);

Ok(response.add_event(event))
}
20 changes: 10 additions & 10 deletions contracts/collections/sg721-updatable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cosmwasm_std::{Deps, StdResult};
#[cfg(not(feature = "library"))]
use cosmwasm_std::{DepsMut, Env, Event, MessageInfo};
use cw2::set_contract_version;
use cw721::msg::Cw721MigrateMsg;
use cw721::{
DefaultOptionCollectionMetadataExtension, DefaultOptionCollectionMetadataExtensionMsg,
DefaultOptionNftMetadataExtension, DefaultOptionNftMetadataExtensionMsg,
Expand Down Expand Up @@ -173,7 +174,11 @@ pub fn query_frozen_token_metadata(deps: Deps) -> StdResult<FrozenTokenMetadataR
Ok(FrozenTokenMetadataResponse { frozen })
}

pub fn _migrate(mut deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, ContractError> {
pub fn _migrate(
mut deps: DepsMut,
env: Env,
msg: Cw721MigrateMsg,
) -> Result<Response, ContractError> {
let prev_contract_info = cw2::get_contract_version(deps.storage)?;
let prev_contract_name: String = prev_contract_info.contract;
let prev_contract_version: Version = prev_contract_info
Expand Down Expand Up @@ -216,15 +221,10 @@ pub fn _migrate(mut deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, Co

let mut response = Response::new();

#[allow(clippy::cmp_owned)]
if prev_contract_version < Version::new(3, 0, 0) {
response = sg721_base::upgrades::v3_0_0::upgrade(deps.branch(), &env, response)?;
}

#[allow(clippy::cmp_owned)]
if prev_contract_version < Version::new(3, 1, 0) {
response = sg721_base::upgrades::v3_1_0::upgrade(deps.branch(), &env, response)?;
}
// these upgrades can always be called. migration is only executed in case new stores are empty! It is safe calling these on any version.
response = sg721_base::upgrades::v3_0_0_ownable::upgrade(deps.branch(), &env, response, msg)?;
response =
sg721_base::upgrades::v3_1_0_royalty_timestamp::upgrade(deps.branch(), &env, response)?;

cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Expand Down
7 changes: 6 additions & 1 deletion contracts/collections/sg721-updatable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod entry {
};
use cosmwasm_std::{entry_point, to_json_binary, Empty};
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response};
use cw721::msg::Cw721MigrateMsg;
use cw721::{
DefaultOptionCollectionMetadataExtensionMsg, DefaultOptionNftMetadataExtensionMsg,
};
Expand Down Expand Up @@ -70,7 +71,11 @@ pub mod entry {
}

#[entry_point]
pub fn migrate(deps: DepsMut, env: Env, msg: Empty) -> Result<Response, ContractError> {
pub fn migrate(
deps: DepsMut,
env: Env,
msg: Cw721MigrateMsg,
) -> Result<Response, ContractError> {
_migrate(deps, env, msg)
}
}

0 comments on commit 78b80f4

Please sign in to comment.