Skip to content

Commit 9be34ae

Browse files
authored
Remove debt from total deposit. (#351)
* Remove debt from total deposit. * Add migration and use proper version.
1 parent 4068ad1 commit 9be34ae

File tree

12 files changed

+105
-23
lines changed

12 files changed

+105
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/params/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "mars-params"
33
description = "Contract storing the asset params for Credit Manager and Red Bank."
4-
version = { workspace = true }
4+
version = "2.0.1"
55
authors = { workspace = true }
66
license = { workspace = true }
77
edition = { workspace = true }

contracts/params/src/contract.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(not(feature = "library"))]
22
use cosmwasm_std::entry_point;
3-
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response};
3+
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
44
use cw2::set_contract_version;
55
use mars_owner::OwnerInit::SetInitialOwner;
66
use mars_types::params::{
@@ -15,15 +15,16 @@ use crate::{
1515
assert_thf, update_asset_params, update_config, update_target_health_factor,
1616
update_vault_config,
1717
},
18+
migrations,
1819
query::{
1920
query_all_asset_params, query_all_vault_configs, query_config, query_total_deposit,
2021
query_vault_config,
2122
},
2223
state::{ADDRESS_PROVIDER, ASSET_PARAMS, OWNER, TARGET_HEALTH_FACTOR},
2324
};
2425

25-
const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
26-
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
26+
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
27+
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
2728

2829
#[cfg_attr(not(feature = "library"), entry_point)]
2930
pub fn instantiate(
@@ -109,3 +110,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
109110
};
110111
res.map_err(Into::into)
111112
}
113+
114+
#[cfg_attr(not(feature = "library"), entry_point)]
115+
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> ContractResult<Response> {
116+
migrations::v2_0_1::migrate(deps)
117+
}

contracts/params/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cosmwasm_std::{DecimalRangeExceeded, StdError};
2+
use cw2::VersionError;
23
use mars_owner::OwnerError;
34
use mars_types::error::MarsError;
45
use mars_utils::error::ValidationError;
@@ -22,4 +23,7 @@ pub enum ContractError {
2223

2324
#[error("{0}")]
2425
Mars(#[from] MarsError),
26+
27+
#[error("{0}")]
28+
Version(#[from] VersionError),
2529
}

contracts/params/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod contract;
22
pub mod emergency_powers;
33
pub mod error;
44
pub mod execute;
5+
pub mod migrations;
56
pub mod query;
67
pub mod state;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod v2_0_1;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use cosmwasm_std::{DepsMut, Response};
2+
use cw2::{assert_contract_version, set_contract_version};
3+
4+
use crate::{
5+
contract::{CONTRACT_NAME, CONTRACT_VERSION},
6+
error::ContractError,
7+
};
8+
9+
const FROM_VERSION: &str = "2.0.0";
10+
11+
pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
12+
// make sure we're migrating the correct contract and from the correct version
13+
assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?;
14+
15+
set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?;
16+
17+
Ok(Response::new()
18+
.add_attribute("action", "migrate")
19+
.add_attribute("from_version", FROM_VERSION)
20+
.add_attribute("to_version", CONTRACT_VERSION))
21+
}

contracts/params/src/query.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use mars_interest_rate::get_underlying_liquidity_amount;
44
use mars_types::{
55
address_provider::{self, MarsAddressType},
66
params::{AssetParams, ConfigResponse, TotalDepositResponse, VaultConfig},
7-
red_bank::{self, Market, UserDebtResponse},
7+
red_bank::{self, Market},
88
};
99

1010
use crate::state::{ADDRESS_PROVIDER, ASSET_PARAMS, VAULT_CONFIGS};
@@ -115,26 +115,13 @@ pub fn query_total_deposit(
115115
.transpose()?
116116
.unwrap_or_else(Uint128::zero);
117117

118-
// amount of debt in this asset the Credit Manager owes to Red Bank
119-
// this query returns zero if no debt is owed
120-
let cm_debt = deps
121-
.querier
122-
.query_wasm_smart::<UserDebtResponse>(
123-
red_bank_addr,
124-
&red_bank::QueryMsg::UserDebt {
125-
user: credit_manager_addr.into(),
126-
denom: denom.clone(),
127-
},
128-
)?
129-
.amount;
130-
131118
// amount of this asset deposited into Credit Manager
132119
// this is simply the coin balance of the CM contract
133120
// note that this way, we don't include LP tokens or vault positions
134121
let cm_deposit = deps.querier.query_balance(credit_manager_addr, &denom)?.amount;
135122

136123
// total deposited amount
137-
let amount = rb_deposit.checked_add(cm_deposit)?.checked_sub(cm_debt)?;
124+
let amount = rb_deposit.checked_add(cm_deposit)?;
138125

139126
// additionally, we include the deposit cap in the response
140127
let asset_params = ASSET_PARAMS.load(deps.storage, &denom)?;

contracts/params/tests/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod helpers;
33
mod test_asset_validation;
44
mod test_deposit_cap;
55
mod test_emergency_powers;
6+
mod test_migration;
67
mod test_owner;
78
mod test_target_health_factor;
89
mod test_update_asset_params;

contracts/params/tests/tests/test_deposit_cap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn querying_total_deposit(rb_market: Market, rb_debt: UserDebtResponse, cm_balan
6262

6363
// setup
6464
deps.querier.set_redbank_market(rb_market.clone());
65-
deps.querier.set_red_bank_user_debt(CREDIT_MANAGER, rb_debt.clone());
65+
deps.querier.set_red_bank_user_debt(CREDIT_MANAGER, rb_debt);
6666
deps.querier.update_balances(CREDIT_MANAGER, coins(cm_balance.u128(), MOCK_DENOM));
6767
ADDRESS_PROVIDER.save(deps.as_mut().storage, &Addr::unchecked("address_provider")).unwrap();
6868
ASSET_PARAMS.save(deps.as_mut().storage, MOCK_DENOM, &params).unwrap();
@@ -71,7 +71,7 @@ fn querying_total_deposit(rb_market: Market, rb_debt: UserDebtResponse, cm_balan
7171
let rb_deposit =
7272
get_underlying_liquidity_amount(rb_market.collateral_total_scaled, &rb_market, TIMESTAMP)
7373
.unwrap();
74-
let exp_total_deposit = rb_deposit + cm_balance - rb_debt.amount;
74+
let exp_total_deposit = rb_deposit + cm_balance;
7575

7676
// query total deposit
7777
let res = query_total_deposit(deps.as_ref(), &env, MOCK_DENOM.into()).unwrap();

0 commit comments

Comments
 (0)