Skip to content

Commit

Permalink
Fix incentives config migration. (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab authored Jan 13, 2025
1 parent 06a510b commit 85e2c33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 22 additions & 1 deletion contracts/incentives/src/migrations/v2_1_0.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
use cosmwasm_std::{DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};
use mars_types::incentives::Config;

use crate::{
contract::{CONTRACT_NAME, CONTRACT_VERSION},
error::ContractError,
state::CONFIG,
};

const FROM_VERSION: &str = "2.0.0";

pub mod v1_state {
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Decimal, DepsMut, Uint128};
use cw_storage_plus::Map;
use cw_storage_plus::{Item, Map};

#[cw_serde]
pub struct Config {
pub address_provider: Addr,
pub max_whitelisted_denoms: u8,
pub mars_denom: String,
}

pub const CONFIG: Item<Config> = Item::new("config");

/// Don't care about the actual types, just use some dummy types to clear the storage
pub const ASSET_INCENTIVES: Map<&str, String> = Map::new("incentives");
Expand All @@ -31,6 +43,15 @@ pub fn migrate(mut deps: DepsMut) -> Result<Response, ContractError> {
// Make sure we're migrating the correct contract and from the correct version
assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?;

// Migrate old config to new config
let old_config = v1_state::CONFIG.load(deps.storage)?;
let new_config = Config {
address_provider: old_config.address_provider,
max_whitelisted_denoms: old_config.max_whitelisted_denoms,
};
v1_state::CONFIG.remove(deps.storage);
CONFIG.save(deps.storage, &new_config)?;

// Clear old state
v1_state::clear_state(&mut deps);

Expand Down
27 changes: 25 additions & 2 deletions contracts/incentives/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use cosmwasm_std::{attr, testing::mock_env, Empty, Event};
use cosmwasm_std::{attr, testing::mock_env, Addr, Empty, Event};
use cw2::{ContractVersion, VersionError};
use mars_incentives::{contract::migrate, ContractError};
use mars_incentives::{
contract::migrate, migrations::v2_1_0::v1_state, state::CONFIG, ContractError,
};
use mars_testing::mock_dependencies;
use mars_types::incentives::Config;

#[test]
fn wrong_contract_name() {
Expand Down Expand Up @@ -40,8 +43,28 @@ fn successful_migration() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-incentives", "2.0.0").unwrap();

v1_state::CONFIG
.save(
deps.as_mut().storage,
&v1_state::Config {
address_provider: Addr::unchecked("addr_provider".to_string()),
max_whitelisted_denoms: 15,
mars_denom: "mars".to_string(),
},
)
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap();

let config = CONFIG.load(deps.as_ref().storage).unwrap();
assert_eq!(
config,
Config {
address_provider: Addr::unchecked("addr_provider".to_string()),
max_whitelisted_denoms: 15,
}
);

assert_eq!(res.messages, vec![]);
assert_eq!(res.events, vec![] as Vec<Event>);
assert!(res.data.is_none());
Expand Down

0 comments on commit 85e2c33

Please sign in to comment.