Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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/credit-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mars-credit-manager"
version = "2.2.0"
version = "2.2.1"
authors = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
Expand Down
12 changes: 8 additions & 4 deletions contracts/credit-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response,
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response,
};
use cw2::set_contract_version;
use mars_types::{
adapters::vault::VAULT_REQUEST_REPLY_ID,
credit_manager::{ExecuteMsg, InstantiateMsg, QueryMsg},
credit_manager::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
};

use crate::{
Expand Down Expand Up @@ -138,6 +138,10 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v2_2_0::migrate(deps)
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
match msg {
MigrateMsg::V2_0_3ToV2_1_0 {} => migrations::v2_1_0::migrate(deps),
MigrateMsg::V2_1_0ToV2_2_0 {} => migrations::v2_2_0::migrate(deps),
MigrateMsg::V2_2_0ToV2_2_1 {} => migrations::v2_2_1::migrate(deps),
}
}
1 change: 1 addition & 0 deletions contracts/credit-manager/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod v2_1_0;
pub mod v2_2_0;
pub mod v2_2_1;
35 changes: 7 additions & 28 deletions contracts/credit-manager/src/migrations/v2_2_0.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
use cosmwasm_std::{Decimal, DepsMut, Response};
use cw2::{assert_contract_version, get_contract_version, set_contract_version, VersionError};
use cw2::{assert_contract_version, set_contract_version};

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

const FROM_VERSION: &str = "2.1.0";
const TO_VERSION: &str = "2.2.0";
Comment on lines 6 to +7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Migration leaves credit-manager version at 2.2.0

Upgrading a contract still at 2.1.0 with this 2.2.1 binary can only call MigrateMsg::V2_1_0ToV2_2_0, which sets the stored cw2 version to the hard-coded TO_VERSION of "2.2.0". Because a CosmWasm migrate call runs only once per code upload, there is no way to reach 2.2.1 metadata in the same upgrade, leaving the on-chain contract_version behind the deployed code and blocking future migrations that expect 2.2.1 as the starting point.

Useful? React with 👍 / 👎.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this does not affect us as we are on latest versions (2.1.0 for RB, 2.2.0 for CM) I think it is a potential cause of bugs. Perhaps we should think about this pattern and see if there are better alternatives. The problem with using CONTRACT_VERSION is that it breaks old tests


pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
let contract = format!("crates.io:{CONTRACT_NAME}");
let version = get_contract_version(deps.storage)?;
let from_version = version.version;

if version.contract != contract {
return Err(ContractError::Version(VersionError::WrongContract {
expected: contract,
found: version.contract,
}));
}

if from_version != FROM_VERSION {
return Err(ContractError::Version(VersionError::WrongVersion {
expected: FROM_VERSION.to_string(),
found: from_version,
}));
}

assert_contract_version(deps.storage, &contract, FROM_VERSION)?;
assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?;

if SWAP_FEE.may_load(deps.storage)?.is_none() {
SWAP_FEE.save(deps.storage, &Decimal::zero())?;
}

set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?;
set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), TO_VERSION)?;

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", from_version)
.add_attribute("to_version", CONTRACT_VERSION))
.add_attribute("from_version", FROM_VERSION)
.add_attribute("to_version", TO_VERSION))
}
19 changes: 19 additions & 0 deletions contracts/credit-manager/src/migrations/v2_2_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use cosmwasm_std::{DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};

use crate::{contract::CONTRACT_NAME, error::ContractError};

const FROM_VERSION: &str = "2.2.0";
const TO_VERSION: &str = "2.2.1";

pub fn migrate(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)?;

set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), TO_VERSION)?;

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", FROM_VERSION)
.add_attribute("to_version", TO_VERSION))
}
4 changes: 3 additions & 1 deletion contracts/credit-manager/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ pub fn swap_exact_in(
.add_attribute("action", "swapper")
.add_attribute("account_id", account_id)
.add_attribute("coin_in", coin_in_to_trade.to_string())
.add_attribute("denom_out", denom_out))
.add_attribute("denom_out", denom_out)
.add_attribute("rewards_collector", rewards_collector_account)
.add_attribute("rewards_collector_fee", rc_coin.to_string()))
}
2 changes: 1 addition & 1 deletion contracts/credit-manager/tests/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod test_liquidate_lend;
mod test_liquidate_staked_astro_lp;
mod test_liquidate_vault;
mod test_liquidation_pricing;
mod test_migration_v2_2_0;
mod test_migration_v2_2_1;
mod test_no_health_check;
mod test_reclaim;
mod test_reentrancy_guard;
Expand Down
69 changes: 0 additions & 69 deletions contracts/credit-manager/tests/tests/test_migration_v2_2_0.rs

This file was deleted.

119 changes: 119 additions & 0 deletions contracts/credit-manager/tests/tests/test_migration_v2_2_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use cosmwasm_std::{attr, testing::mock_env, Decimal, Event};
use cw2::{ContractVersion, VersionError};
use mars_credit_manager::{contract::migrate, error::ContractError, state::SWAP_FEE};
use mars_testing::mock_dependencies;
use mars_types::credit_manager::MigrateMsg;

const CONTRACT_NAME: &str = "crates.io:mars-credit-manager";

#[test]
fn v2_1_0_to_v2_2_0_wrong_contract_name() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.1.0").unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongContract {
expected: CONTRACT_NAME.to_string(),
found: "contract_xyz".to_string()
})
);
}

#[test]
fn v2_1_0_to_v2_2_0_wrong_contract_version() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.0.3").unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: "2.1.0".to_string(),
found: "2.0.3".to_string()
})
);
}

#[test]
fn v2_1_0_to_v2_2_0_successful_migration() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.1.0").unwrap();

let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).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.1.0"), attr("to_version", "2.2.0")]
);

let new_contract_version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: "2.2.0".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);

// Ensure swap fee exists post-migration (zero by default if absent)
let swap_fee = SWAP_FEE.may_load(deps.as_ref().storage).unwrap().unwrap_or_else(Decimal::zero);
assert!(swap_fee <= Decimal::one());
}

#[test]
fn v2_2_0_to_v2_2_1_wrong_contract_name() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.2.0").unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongContract {
expected: CONTRACT_NAME.to_string(),
found: "contract_xyz".to_string()
})
);
}

#[test]
fn v2_2_0_to_v2_2_1_wrong_contract_version() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.1.0").unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: "2.2.0".to_string(),
found: "2.1.0".to_string()
})
);
}

#[test]
fn v2_2_0_to_v2_2_1_successful_migration() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.2.0").unwrap();

let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).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.2.0"), attr("to_version", "2.2.1")]
);

let new_contract_version = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: "2.2.1".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
2 changes: 1 addition & 1 deletion contracts/red-bank/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mars-red-bank"
description = "A smart contract that manages asset deposit, borrowing, and liquidations"
version = { workspace = true }
version = "2.1.1"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
11 changes: 7 additions & 4 deletions contracts/red-bank/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response,
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response,
};
use mars_types::red_bank::{ExecuteMsg, InstantiateMsg, QueryMsg};
use mars_types::red_bank::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

use crate::{
asset, borrow, collateral, config, deposit, error::ContractError, instantiate, liquidate,
Expand Down Expand Up @@ -244,6 +244,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v2_1_0::migrate(deps)
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
match msg {
MigrateMsg::V2_0_1ToV2_1_0 {} => migrations::v2_1_0::migrate(deps),
MigrateMsg::V2_1_0ToV2_1_1 {} => migrations::v2_1_1::migrate(deps),
}
}
7 changes: 6 additions & 1 deletion contracts/red-bank/src/interest_rates.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str;

use cosmwasm_std::{Addr, Decimal, Env, Event, Response, Storage, Uint128};
use cosmwasm_std::{Addr, Coin, Decimal, Env, Event, Response, Storage, Uint128};
use mars_interest_rate::{
calculate_applied_linear_interest_rate, compute_scaled_amount, compute_underlying_amount,
get_underlying_debt_amount, get_underlying_liquidity_amount, ScalingOperation,
Expand Down Expand Up @@ -90,6 +90,11 @@ pub fn apply_accumulated_interests(
None,
)?;
market.increase_collateral(reward_amount_scaled)?;

let rewards_fee_coin = Coin::new(accrued_protocol_rewards.u128(), market.denom.clone());
response = response
.add_attribute("rewards_collector", rewards_collector_addr.to_string())
.add_attribute("rewards_collector_fee", rewards_fee_coin.to_string());
}

Ok(response)
Expand Down
1 change: 1 addition & 0 deletions contracts/red-bank/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod v2_1_0;
pub mod v2_1_1;
Loading
Loading