Skip to content

Commit

Permalink
add migration function
Browse files Browse the repository at this point in the history
  • Loading branch information
amityadav0 committed Aug 23, 2023
1 parent 9e21caf commit 55561ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 19 additions & 1 deletion contracts/ics100/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cw2::set_contract_version;
use crate::error::ContractError;
use crate::msg::{
AtomicSwapPacketData, CancelSwapMsg, DetailsResponse, ExecuteMsg, InstantiateMsg, ListResponse,
MakeSwapMsg, QueryMsg, SwapMessageType, TakeSwapMsg,
MakeSwapMsg, QueryMsg, SwapMessageType, TakeSwapMsg, MigrateMsg,
};
use crate::state::{
AtomicSwapOrder,
Expand Down Expand Up @@ -238,6 +238,24 @@ pub fn execute_cancel_swap(
return Ok(res);
}

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let ver = cw2::get_contract_version(deps.storage)?;
// ensure we are migrating from an allowed contract
if ver.contract != CONTRACT_NAME {
return Err(StdError::generic_err("Can only upgrade from same type").into());
}
// note: better to do proper semver compare, but string compare *usually* works
if ver.version >= CONTRACT_VERSION.to_string() {
return Err(StdError::generic_err("Cannot upgrade from a newer version").into());
}

// set the new version
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
Expand Down
4 changes: 4 additions & 0 deletions contracts/ics100/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub fn is_valid_name(name: &str) -> bool {
true
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct MigrateMsg {
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum SwapMessageType {
#[serde(rename = "TYPE_UNSPECIFIED")]
Expand Down

0 comments on commit 55561ca

Please sign in to comment.