Skip to content

Commit

Permalink
Migrate swapper to new version. (#387)
Browse files Browse the repository at this point in the history
* Migrate swapper to new version.

* Handle different CosmWasm pool deserialization.
  • Loading branch information
piobab authored May 22, 2024
1 parent 86b121e commit f1167f0
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 23 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/swapper/osmosis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mars-swapper-osmosis"
version = "2.0.2"
version = "2.0.3"
authors = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion contracts/swapper/osmosis/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
match msg {
MigrateMsg::V1_0_0ToV2_0_0 {} => migrations::v2_0_0::migrate(deps),
MigrateMsg::V2_0_1ToV2_0_2 {} => migrations::v2_0_2::migrate(deps),
MigrateMsg::V2_0_2ToV2_0_3 {} => migrations::v2_0_3::migrate(deps),
}
}
2 changes: 1 addition & 1 deletion contracts/swapper/osmosis/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod v2_0_0;
pub mod v2_0_2;
pub mod v2_0_3;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use mars_swapper_base::ContractError;

use crate::contract::{CONTRACT_NAME, CONTRACT_VERSION};

const FROM_VERSION: &str = "2.0.1";
const FROM_VERSION: &str = "2.0.2";

pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
// make sure we're migrating the correct contract and from the correct version
Expand Down
12 changes: 6 additions & 6 deletions contracts/swapper/osmosis/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ fn successful_migration() {
assert!(res.data.is_none());
assert_eq!(
res.attributes,
vec![attr("action", "migrate"), attr("from_version", "1.0.0"), attr("to_version", "2.0.2")] // to_version should be 2.0.0 but because of global current version in Cargo.toml is different
vec![attr("action", "migrate"), attr("from_version", "1.0.0"), attr("to_version", "2.0.3")] // to_version should be 2.0.0 but because of global current version in Cargo.toml is different
);

let new_contract_version = ContractVersion {
contract: "crates.io:mars-swapper-osmosis".to_string(),
version: "2.0.2".to_string(), // should be 2.0.0 but global current version in Cargo.toml is different
version: "2.0.3".to_string(), // should be 2.0.0 but global current version in Cargo.toml is different
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);

Expand All @@ -85,22 +85,22 @@ fn successful_migration() {
#[test]
fn successful_migration_to_v2_0_2() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-swapper-osmosis", "2.0.1")
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-swapper-osmosis", "2.0.2")
.unwrap();

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

assert_eq!(res.messages, vec![]);
assert_eq!(res.events, vec![] as Vec<Event>);
assert!(res.data.is_none());
assert_eq!(
res.attributes,
vec![attr("action", "migrate"), attr("from_version", "2.0.1"), attr("to_version", "2.0.2")]
vec![attr("action", "migrate"), attr("from_version", "2.0.2"), attr("to_version", "2.0.3")]
);

let new_contract_version = ContractVersion {
contract: "crates.io:mars-swapper-osmosis".to_string(),
version: "2.0.2".to_string(),
version: "2.0.3".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
22 changes: 12 additions & 10 deletions packages/chains/osmosis/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ impl TryFrom<osmosis_std::shim::Any> for Pool {
}));
}

return Err(StdError::parse_err(
"Pool",
"Failed to parse CosmWasm pool instantiate message.",
));
// There could be many versions of the CosmWasm pool. Every time a new version is released,
// we need to add a new `if` block here to handle the new version. Instead of doing that,
// we can just return only CosmWasm pool with pool id and let the caller handle it.
// The caller can then query the pool contract to get the pool asset configs (if needed).
return Ok(Pool::CosmWasm(CosmWasmPool {
id: pool.pool_id,
pool_asset_configs: vec![],
}));
}

Err(StdError::parse_err(
Expand Down Expand Up @@ -553,7 +557,7 @@ mod tests {
}

#[test]
fn cosmwasm_pool_error_handled() {
fn unknown_cosmwasm_pool_handled() {
let msg = InstantiateMsg {
pool_asset_denoms: vec![
"ibc/D189335C6E4A68B513C10AB227BF1C1D38C746766278BA3EEB4FB14124F1D858".to_string(),
Expand All @@ -568,11 +572,9 @@ mod tests {
};

let any_pool = cosmwasm_pool.to_any();
let pool: Result<Pool, StdError> = any_pool.try_into();
let pool: Pool = any_pool.try_into().unwrap();

assert_eq!(
pool.unwrap_err(),
StdError::parse_err("Pool", "Failed to parse CosmWasm pool instantiate message.",)
);
assert_eq!(cosmwasm_pool.pool_id, pool.get_pool_id());
assert_eq!(Vec::<String>::new(), pool.get_pool_denoms());
}
}
2 changes: 1 addition & 1 deletion packages/types/src/swapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ pub struct EstimateExactInSwapResponse {
#[cw_serde]
pub enum MigrateMsg {
V1_0_0ToV2_0_0 {},
V2_0_1ToV2_0_2 {},
V2_0_2ToV2_0_3 {},
}
2 changes: 1 addition & 1 deletion schemas/mars-swapper-osmosis/mars-swapper-osmosis.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "mars-swapper-osmosis",
"contract_version": "2.0.2",
"contract_version": "2.0.3",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit f1167f0

Please sign in to comment.