Skip to content

Commit

Permalink
Merge pull request #10 from FigureTechnologies/tsilva/migrate-subs
Browse files Browse the repository at this point in the history
require sub code id on migrate, update it in state, migrate all sub c…
  • Loading branch information
tsilva-figure authored Feb 25, 2022
2 parents 4d6a2fc + c03caf6 commit ebb8c85
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "marketpalace-raise-contract"
version = "0.1.1"
version = "1.0.0"
authors = ["Thomas Silva <[email protected]>"]
edition = "2018"

Expand Down
3 changes: 2 additions & 1 deletion examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use marketpalace_raise_contract::msg::{HandleMsg, InstantiateMsg, QueryMsg};
use marketpalace_raise_contract::msg::{HandleMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use marketpalace_raise_contract::state::State;

fn main() {
Expand All @@ -13,6 +13,7 @@ fn main() {
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(MigrateMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(State), &out_dir);
Expand Down
15 changes: 15 additions & 0 deletions schema/migrate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MigrateMsg",
"type": "object",
"required": [
"subscription_code_id"
],
"properties": {
"subscription_code_id": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
}
}
71 changes: 69 additions & 2 deletions src/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
use crate::contract::ContractResponse;
use crate::error::ContractError;
use crate::msg::MigrateMsg;
use crate::state::config;
use crate::state::config_read;
use crate::version::CONTRACT_NAME;
use crate::version::CONTRACT_VERSION;
use cosmwasm_std::entry_point;
use cosmwasm_std::to_binary;
use cosmwasm_std::DepsMut;
use cosmwasm_std::Env;
use cosmwasm_std::Response;
use cw2::set_contract_version;
use serde::Serialize;

#[derive(Serialize)]
struct EmptyArgs {}

#[entry_point]
pub fn migrate(deps: DepsMut, _: Env, _: MigrateMsg) -> ContractResponse {
pub fn migrate(deps: DepsMut, _: Env, msg: MigrateMsg) -> ContractResponse {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Ok(Response::default())
let state = config_read(deps.storage).load()?;

if state.subscription_code_id != msg.subscription_code_id {
config(deps.storage).update(|mut state| -> Result<_, ContractError> {
state.subscription_code_id = msg.subscription_code_id;
Ok(state)
})?;

let sub_migrations = state
.pending_review_subs
.union(&state.accepted_subs)
.map(|addr| cosmwasm_std::WasmMsg::Migrate {
contract_addr: addr.to_string(),
new_code_id: msg.subscription_code_id,
msg: to_binary(&EmptyArgs {}).unwrap(),
});

Ok(Response::default().add_messages(sub_migrations))
} else {
Ok(Response::default())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::contract::tests::default_deps;
use crate::state::State;
use crate::state::Status;
use crate::state::Withdrawal;
use cosmwasm_std::testing::mock_dependencies;
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::Addr;
use cosmwasm_storage::{singleton, singleton_read};
use schemars::JsonSchema;
Expand Down Expand Up @@ -50,6 +81,42 @@ mod tests {

pub static CONFIG_KEY: &[u8] = b"config";

#[test]
fn new_sub_code_migration() {
let mut deps = default_deps(Some(|state| {
state.accepted_subs = vec![Addr::unchecked("sub_1")].into_iter().collect();
}));

let res = migrate(
deps.as_mut(),
mock_env(),
MigrateMsg {
subscription_code_id: 1,
},
)
.unwrap();

assert_eq!(1, res.messages.len());
}

#[test]
fn same_sub_code_migration() {
let mut deps = default_deps(Some(|state| {
state.accepted_subs = vec![Addr::unchecked("sub_1")].into_iter().collect();
}));

let res = migrate(
deps.as_mut(),
mock_env(),
MigrateMsg {
subscription_code_id: 0,
},
)
.unwrap();

assert_eq!(0, res.messages.len());
}

#[test]
fn read_from_old_state() {
let mut deps = mock_dependencies(&[]);
Expand Down
4 changes: 3 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub struct InstantiateMsg {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MigrateMsg {}
pub struct MigrateMsg {
pub subscription_code_id: u64,
}

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

0 comments on commit ebb8c85

Please sign in to comment.