Skip to content

Commit

Permalink
Merge pull request #112 from CosmWasm/v0.17-upgrade
Browse files Browse the repository at this point in the history
v0.17 upgrade logic
  • Loading branch information
JakeHartnell authored Apr 3, 2023
2 parents c1ece55 + 2a0906c commit 5179de0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
25 changes: 19 additions & 6 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ rust-version = "1.65"
[workspace.dependencies]
cosmwasm-schema = "1.2.1"
cosmwasm-std = "1.2.1"
cw2 = "1.0.1"
# FIXME: change back to the "official" crates.io release after this PR is merged:
# https://github.com/CosmWasm/cw-plus/pull/858
cw2 = { git = "https://github.com/mars-protocol/cw-plus", rev = "1a3a944" }
cw20 = "1.0.1"
cw721 = { version = "0.17.0", path = "./packages/cw721" }
cw721-base = { version = "0.17.0", path = "./contracts/cw721-base" }
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw721-base/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub enum ContractError {
#[error(transparent)]
Ownership(#[from] OwnershipError),

#[error(transparent)]
Version(#[from] cw2::VersionError),

#[error("token_id already claimed")]
Claimed {},

Expand All @@ -18,7 +21,4 @@ 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),
}
16 changes: 0 additions & 16 deletions contracts/cw721-base/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ use serde::Serialize;

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

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::upgrades;
use crate::{CONTRACT_NAME, CONTRACT_VERSION};

impl<'a, T, C, E, Q> Cw721Contract<'a, T, C, E, Q>
where
Expand Down Expand Up @@ -130,19 +127,6 @@ where
let ownership = cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?;
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

/// 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)?;

if version != "0.16.0" {
Err(ContractError::WrongMigrateVersion(version))
} else {
upgrades::v0_16::migrate::<T, C, E, Q>(deps)
}
}
}

impl<'a, T, C, E, Q> Cw721Execute<T, C> for Cw721Contract<'a, T, C, E, Q>
Expand Down
18 changes: 16 additions & 2 deletions contracts/cw721-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub type Extension = Option<Empty>;
pub const CONTRACT_NAME: &str = "crates.io:cw721-base";
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

// currently we only support migrating from 0.16.0. this is ok for now because
// we have not released any 0.16.x where x != 0
//
// TODO: parse semvar so that any version 0.16.x can be migrated from
pub const EXPECTED_FROM_VERSION: &str = "0.16.0";

pub mod entry {
use super::*;

Expand Down Expand Up @@ -71,8 +77,16 @@ pub mod entry {
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, env: Env, _msg: Empty) -> Result<Response, ContractError> {
Cw721Contract::<Extension, Empty, Empty, Empty>::migrate(deps, env)
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
// make sure the correct contract is being upgraded, and it's being
// upgraded from the correct version.
cw2::assert_contract_version(deps.as_ref().storage, CONTRACT_NAME, EXPECTED_FROM_VERSION)?;

// update contract version
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

// perform the upgrade
upgrades::v0_17::migrate::<Extension, Empty, Empty, Empty>(deps)
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/cw721-base/src/upgrades/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod v0_16;
pub mod v0_17;
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ where
Q: CustomMsg,
E: CustomMsg,
{
// remove old minter info
let tract16 = v16::Cw721Contract::<T, C, E, Q>::default();
let minter = tract16.minter.load(deps.storage)?;
tract16.minter.remove(deps.storage);

// save new ownership info
let ownership = cw_ownable::initialize_owner(deps.storage, deps.api, Some(minter.as_str()))?;

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", "0.16.0")
Expand Down

0 comments on commit 5179de0

Please sign in to comment.