Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.17 upgrade logic #112

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }
Comment on lines +16 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be an overkill just to get the assert_contract_version helper: https://github.com/CosmWasm/cw-plus/pull/858/files#diff-a35715fa1fde56c42db0d8f2845ebc6a571a801699c0705f0080237820d96ee6R59-R83

Would suggest just to just put it in this contract until we have an "official" version of it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Art3miX My PR to cw2 referenced in the comment has been merged. Now we can use the main branch of cw2 instead of mars-protocol fork. Imo this is more elegant than including extra code in this repo which we need to remove later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, so this can be changed to the official cw2 now?

I just didn't like the using some fork for a helper function just to revert back later to the previous package.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it to the main branch (use the latest commit) of the official cw-plus repo

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