Skip to content

Commit

Permalink
add migrate to base-factory and vending-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
yubrew committed Jul 5, 2023
1 parent 49bb44d commit 0a606aa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
36 changes: 34 additions & 2 deletions contracts/factories/base-factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
ensure, ensure_eq, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, StdResult, WasmMsg,
ensure, ensure_eq, to_binary, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, StdError,
StdResult, WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::must_pay;
Expand All @@ -16,7 +17,7 @@ use crate::msg::{
BaseMinterCreateMsg, BaseSudoMsg, BaseUpdateParamsMsg, ExecuteMsg, InstantiateMsg,
ParamsResponse, SudoMsg,
};
use crate::state::SUDO_PARAMS;
use crate::state::{EARLIEST_VERSION, SUDO_PARAMS, TO_VERSION};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:sg-base-factory";
Expand Down Expand Up @@ -200,3 +201,34 @@ fn query_allowed_collection_code_id(
let allowed = code_ids.contains(&code_id);
Ok(AllowedCollectionCodeIdResponse { allowed })
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, env: Env, msg: Empty) -> StdResult<Response> {
try_migrate(deps, env, msg)
}

pub fn try_migrate(deps: DepsMut, _env: Env, _msg: Empty) -> StdResult<Response> {
// make sure the correct contract is being upgraded, and it's being
// upgraded from the correct version.

if CONTRACT_VERSION < EARLIEST_VERSION {
return Err(StdError::generic_err("Cannot upgrade to a previous contract version").into());
}
if CONTRACT_VERSION > TO_VERSION {
return Err(StdError::generic_err("Cannot upgrade to a previous contract version").into());
}
// if same version return
if CONTRACT_VERSION == TO_VERSION {
return Ok(Response::new());
}

// update contract version
cw2::set_contract_version(deps.storage, CONTRACT_NAME, TO_VERSION)?;

let event = Event::new("migrate")
.add_attribute("from_name", CONTRACT_NAME)
.add_attribute("to_version", TO_VERSION)
.add_attribute("from_version", CONTRACT_VERSION);

Ok(Response::new().add_event(event))
}
5 changes: 5 additions & 0 deletions contracts/factories/base-factory/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ pub type Extension = Option<Empty>;
pub type BaseMinterParams = MinterParams<Extension>;

pub const SUDO_PARAMS: Item<BaseMinterParams> = Item::new("sudo-params");

// migration version constants
// TODO fix
pub const EARLIEST_VERSION: &str = "0.25.0";
pub const TO_VERSION: &str = "3.0.0";
12 changes: 10 additions & 2 deletions contracts/factories/vending-factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use base_factory::contract::{must_be_allowed_collection, must_not_be_frozen, update_params};
use base_factory::contract::{
must_be_allowed_collection, must_not_be_frozen, try_migrate, update_params,
};
use base_factory::ContractError as BaseContractError;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
ensure, ensure_eq, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, StdResult, WasmMsg,
ensure, ensure_eq, to_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, StdResult,
WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::must_pay;
Expand Down Expand Up @@ -115,6 +118,11 @@ pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractE
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, env: Env, msg: Empty) -> StdResult<Response> {
try_migrate(deps, env, msg)
}

/// Only governance can update contract params
pub fn sudo_update_params(
deps: DepsMut,
Expand Down
5 changes: 5 additions & 0 deletions scripts/migrate-0.25.0-to-v3/00-store.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# general wasm store tx
ADMIN=stars10w5eulj60qp3cfqa0hkmke78qdy2feq6x9xdmd
starsd tx wasm store vending_factory-yubrew:sg-955.wasm --from $ADMIN \
--gas-prices 0.025ustars --gas-adjustment 1.7 \
--gas auto -y -b block -o json | jq '.logs' | grep -A 1 code_id
10 changes: 10 additions & 0 deletions scripts/migrate-0.25.0-to-v3/04-migrate-factory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ADMIN=stars10w5eulj60qp3cfqa0hkmke78qdy2feq6x9xdmd
FACTORY=stars18xydeup6yzht7app47vju0npv9fsy4ea4gy0rudx2xzjh3t3s95sa8snj9
# COLLECTION=stars16w3v5dn5m6nu0d6wnyju4jr78ldg6hys3344n4j6vljakvympueq2d3uaz
INCREASE_ROYALTIES_FACTORY_CODE_ID=2631

starsd tx wasm migrate $FACTORY $INCREASE_ROYALTIES_FACTORY_CODE_ID "{}" \
--gas-prices 0.025ustars --gas 2000000 --gas-adjustment 1.9 \
--from $ADMIN -y -b block -o json | jq .

# test minting after factory migration

0 comments on commit 0a606aa

Please sign in to comment.