Skip to content

Commit

Permalink
fix(profit): Added storage migration
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilMihaylov authored and Gancho Manev committed May 17, 2023
1 parent 3087417 commit bd134e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion contracts/profit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "profit"
version = "0.3.0"
version = "0.3.1"
edition.workspace = true
authors.workspace = true
license.workspace = true
Expand Down
40 changes: 35 additions & 5 deletions contracts/profit/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
};

// version info for migration info
// const CONTRACT_STORAGE_VERSION_FROM: VersionSegment = 0;
const CONTRACT_STORAGE_VERSION: VersionSegment = 0;
const CONTRACT_STORAGE_VERSION_FROM: VersionSegment = 0;
const CONTRACT_STORAGE_VERSION: VersionSegment = 1;

#[cfg_attr(feature = "contract-with-bindings", entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -57,9 +57,39 @@ pub fn instantiate(
}

#[cfg_attr(feature = "contract-with-bindings", entry_point)]
pub fn migrate(deps: DepsMut<'_>, _env: Env, _msg: MigrateMsg) -> ContractResult<CwResponse> {
versioning::update_software::<ContractError>(deps.storage, version!(CONTRACT_STORAGE_VERSION))
.and_then(response::response)
pub fn migrate(deps: DepsMut<'_>, _env: Env, msg: MigrateMsg) -> ContractResult<CwResponse> {
use sdk::{
cosmwasm_std::{Addr, Storage},
cw_storage_plus::Item,
};

#[derive(serde::Serialize, serde::Deserialize)]
struct OldConfig {
cadence_hours: u16,
treasury: Addr,
}

let migration_closure = move |storage: &mut dyn Storage| {
let old_config: Item<'_, OldConfig> = Item::new("profit_config");
let OldConfig {
cadence_hours,
treasury,
} = old_config.load(storage)?;
old_config.remove(storage);

let oracle: OracleRef = OracleRef::try_from(msg.oracle, &deps.querier)?;
let time_alarms: TimeAlarmsRef = TimeAlarmsRef::new(msg.timealarms, &deps.querier)?;

State::new(Config::new(cadence_hours, treasury, oracle, time_alarms)).store(storage)
};

versioning::update_software_and_storage::<CONTRACT_STORAGE_VERSION_FROM, _, _, ContractError>(
deps.storage,
version!(CONTRACT_STORAGE_VERSION),
migration_closure,
)
.map(|(label, _)| label)
.and_then(response::response)
}

#[cfg_attr(feature = "contract-with-bindings", entry_point)]
Expand Down
6 changes: 5 additions & 1 deletion contracts/profit/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub struct InstantiateMsg {
}

#[derive(Serialize, Deserialize)]
pub struct MigrateMsg {}
pub struct MigrateMsg {
pub owner: Addr,
pub oracle: Addr,
pub timealarms: Addr,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down

0 comments on commit bd134e0

Please sign in to comment.