Skip to content

Commit

Permalink
Perform proper migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jan 31, 2022
1 parent cb0b944 commit b255685
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
21 changes: 19 additions & 2 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,43 @@ pub fn execute_allow(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let version: Version = CONTRACT_VERSION.parse().map_err(from_semver)?;
let stored = get_contract_version(deps.storage)?;
let storage_version: Version = stored.version.parse().map_err(from_semver)?;

// First, ensure we are working from an equal or older version of this contract
// wrong type
if CONTRACT_NAME != stored.contract {
return Err(ContractError::CannotMigrate {
previous_contract: stored.contract,
});
}
// existing one is newer
if storage_version > version {
return Err(ContractError::CannotMigrateVersion {
previous_version: stored.version,
});
}

// Then, run the proper migration
// unsupported old version
if storage_version < "0.11.1".parse().map_err(from_semver)? {
return Err(ContractError::CannotMigrateVersion {
previous_version: stored.version,
});
}
// for 0.12.0-alpha1 or earlier, migrate from the config.gov_contract to ADMIN
if storage_version <= "0.12.0-alpha1".parse().map_err(from_semver)? {
// DO migration
// Do v1 migration
let old_config = crate::migrations::v1::CONFIG.load(deps.storage)?;
ADMIN.set(deps.branch(), Some(old_config.gov_contract))?;
let config = Config {
default_timeout: old_config.default_timeout,
};
CONFIG.save(deps.storage, &config)?;
}
// otherwise no migration (yet)

if storage_version < version {
// we don't need to save anything if migrating from the same version
Expand Down
1 change: 1 addition & 0 deletions contracts/cw20-ics20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod amount;
pub mod contract;
mod error;
pub mod ibc;
mod migrations;
pub mod msg;
pub mod state;
mod test_helpers;
Expand Down
16 changes: 16 additions & 0 deletions contracts/cw20-ics20/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// v1 format is anything older than 0.12.0
pub mod v1 {
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Addr;
use cw_storage_plus::Item;

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ConfigV1 {
pub default_timeout: u64,
pub gov_contract: Addr,
}

pub const CONFIG: Item<ConfigV1> = Item::new("ics20_config");
}

0 comments on commit b255685

Please sign in to comment.