Skip to content

Commit 85e2c33

Browse files
authored
Fix incentives config migration. (#441)
1 parent 06a510b commit 85e2c33

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

contracts/incentives/src/migrations/v2_1_0.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
use cosmwasm_std::{DepsMut, Response};
22
use cw2::{assert_contract_version, set_contract_version};
3+
use mars_types::incentives::Config;
34

45
use crate::{
56
contract::{CONTRACT_NAME, CONTRACT_VERSION},
67
error::ContractError,
8+
state::CONFIG,
79
};
810

911
const FROM_VERSION: &str = "2.0.0";
1012

1113
pub mod v1_state {
14+
use cosmwasm_schema::cw_serde;
1215
use cosmwasm_std::{Addr, Decimal, DepsMut, Uint128};
13-
use cw_storage_plus::Map;
16+
use cw_storage_plus::{Item, Map};
17+
18+
#[cw_serde]
19+
pub struct Config {
20+
pub address_provider: Addr,
21+
pub max_whitelisted_denoms: u8,
22+
pub mars_denom: String,
23+
}
24+
25+
pub const CONFIG: Item<Config> = Item::new("config");
1426

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

46+
// Migrate old config to new config
47+
let old_config = v1_state::CONFIG.load(deps.storage)?;
48+
let new_config = Config {
49+
address_provider: old_config.address_provider,
50+
max_whitelisted_denoms: old_config.max_whitelisted_denoms,
51+
};
52+
v1_state::CONFIG.remove(deps.storage);
53+
CONFIG.save(deps.storage, &new_config)?;
54+
3455
// Clear old state
3556
v1_state::clear_state(&mut deps);
3657

contracts/incentives/tests/tests/test_migration_v2.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use cosmwasm_std::{attr, testing::mock_env, Empty, Event};
1+
use cosmwasm_std::{attr, testing::mock_env, Addr, Empty, Event};
22
use cw2::{ContractVersion, VersionError};
3-
use mars_incentives::{contract::migrate, ContractError};
3+
use mars_incentives::{
4+
contract::migrate, migrations::v2_1_0::v1_state, state::CONFIG, ContractError,
5+
};
46
use mars_testing::mock_dependencies;
7+
use mars_types::incentives::Config;
58

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

46+
v1_state::CONFIG
47+
.save(
48+
deps.as_mut().storage,
49+
&v1_state::Config {
50+
address_provider: Addr::unchecked("addr_provider".to_string()),
51+
max_whitelisted_denoms: 15,
52+
mars_denom: "mars".to_string(),
53+
},
54+
)
55+
.unwrap();
56+
4357
let res = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap();
4458

59+
let config = CONFIG.load(deps.as_ref().storage).unwrap();
60+
assert_eq!(
61+
config,
62+
Config {
63+
address_provider: Addr::unchecked("addr_provider".to_string()),
64+
max_whitelisted_denoms: 15,
65+
}
66+
);
67+
4568
assert_eq!(res.messages, vec![]);
4669
assert_eq!(res.events, vec![] as Vec<Event>);
4770
assert!(res.data.is_none());

0 commit comments

Comments
 (0)