diff --git a/Cargo.lock b/Cargo.lock index 24e34f392..76a52df89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -311,6 +311,7 @@ dependencies = [ "cw1-whitelist", "cw2", "schemars", + "semver", "serde", "thiserror", ] @@ -1010,6 +1011,12 @@ dependencies = [ "syn", ] +[[package]] +name = "semver" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" + [[package]] name = "serde" version = "1.0.130" diff --git a/contracts/cw1-subkeys/Cargo.toml b/contracts/cw1-subkeys/Cargo.toml index 37b1abc05..c2c497670 100644 --- a/contracts/cw1-subkeys/Cargo.toml +++ b/contracts/cw1-subkeys/Cargo.toml @@ -27,7 +27,8 @@ cosmwasm-std = { version = "1.0.0-beta", features = ["staking"] } cw-storage-plus = { path = "../../packages/storage-plus", version = "0.10.2" } schemars = "0.8.1" serde = { version = "1.0.103", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.23" } +thiserror = "1.0.23" +semver = "1" [dev-dependencies] cosmwasm-schema = { version = "1.0.0-beta" } diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index e81d58c06..71527bbc4 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -18,8 +18,9 @@ use cw1_whitelist::{ msg::InstantiateMsg, state::ADMIN_LIST, }; -use cw2::set_contract_version; +use cw2::{get_contract_version, set_contract_version}; use cw_storage_plus::Bound; +use semver::Version; use crate::error::ContractError; use crate::msg::{ @@ -455,6 +456,22 @@ pub fn query_all_permissions( Ok(AllPermissionsResponse { permissions: res? }) } +// Migrate contract if version is lower than current version +#[entry_point] +pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result { + let version: Version = CONTRACT_VERSION.parse()?; + let storage_version: Version = get_contract_version(deps.storage)?.version.parse()?; + + if storage_version < version { + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + // If state structure changed in any contract version in the way migration is needed, it + // should occur here + } + + Ok(Response::new()) +} + #[cfg(test)] mod tests { use cosmwasm_std::testing::{ diff --git a/contracts/cw1-subkeys/src/error.rs b/contracts/cw1-subkeys/src/error.rs index 01d10996e..203587e43 100644 --- a/contracts/cw1-subkeys/src/error.rs +++ b/contracts/cw1-subkeys/src/error.rs @@ -42,6 +42,9 @@ pub enum ContractError { #[error("Allowance already expired while setting: {0}")] SettingExpiredAllowance(Expiration), + + #[error("Semver parsing error: {0}")] + SemVer(String), } impl From for ContractError { @@ -52,3 +55,9 @@ impl From for ContractError { } } } + +impl From for ContractError { + fn from(err: semver::Error) -> Self { + Self::SemVer(err.to_string()) + } +}