Skip to content

Commit 8914bd7

Browse files
authored
Fix inflated collateral (#354) (#361)
1 parent 3208bb4 commit 8914bd7

File tree

12 files changed

+52
-25
lines changed

12 files changed

+52
-25
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/oracle/wasm/tests/prop_tests.proptest-regressions

Lines changed: 0 additions & 15 deletions
This file was deleted.

contracts/red-bank/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-red-bank"
33
description = "A smart contract that manages asset deposit, borrowing, and liquidations"
4-
version = { workspace = true }
4+
version = "1.2.1"
55
authors = { workspace = true }
66
edition = { workspace = true }
77
license = { workspace = true }

contracts/red-bank/src/contract.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use cosmwasm_std::{
2-
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response,
2+
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response,
33
};
44
use mars_red_bank_types::red_bank::{ExecuteMsg, InstantiateMsg, QueryMsg};
55

6-
use crate::{error::ContractError, execute, query};
6+
use crate::{error::ContractError, execute, migrations, query};
77

88
#[entry_point]
99
pub fn instantiate(
@@ -202,3 +202,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
202202
};
203203
res.map_err(Into::into)
204204
}
205+
206+
#[entry_point]
207+
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
208+
migrations::v1_2_1::migrate(deps)
209+
}

contracts/red-bank/src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub enum ContractError {
2929
#[error("{0}")]
3030
Health(#[from] HealthError),
3131

32+
#[error("{0}")]
33+
Version(#[from] cw2::VersionError),
34+
3235
#[error("Price not found for asset: {denom:?}")]
3336
PriceNotFound {
3437
denom: String,
@@ -64,7 +67,7 @@ pub enum ContractError {
6467
#[error("Cannot have 0 as liquidity index")]
6568
InvalidLiquidityIndex {},
6669

67-
#[error("Borrow amount must be greater than 0 and less or equal available collateral (asset: {denom:?})")]
70+
#[error("Borrow amount must be greater than 0 and less or equal available liquidity (asset: {denom:?})")]
6871
InvalidBorrowAmount {
6972
denom: String,
7073
},

contracts/red-bank/src/execute.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,15 @@ pub fn borrow(
583583
&borrow_market,
584584
env.block.time.seconds(),
585585
)?;
586+
let debt_balance_before = get_underlying_debt_amount(
587+
borrow_market.debt_total_scaled,
588+
&borrow_market,
589+
env.block.time.seconds(),
590+
)?;
586591

587-
// Cannot borrow zero amount or more than available collateral
588-
if borrow_amount.is_zero() || borrow_amount > collateral_balance_before {
592+
// Cannot borrow zero amount or more than available liquidity
593+
let available_liquidity = collateral_balance_before.checked_sub(debt_balance_before)?;
594+
if borrow_amount.is_zero() || borrow_amount > available_liquidity {
589595
return Err(ContractError::InvalidBorrowAmount {
590596
denom,
591597
});

contracts/red-bank/src/interest_rates.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,17 @@ pub fn update_interest_rates(
287287
let total_debt =
288288
get_underlying_debt_amount(market.debt_total_scaled, market, current_timestamp)?;
289289

290-
let current_utilization_rate = if !total_collateral.is_zero() {
290+
let mut current_utilization_rate = if !total_collateral.is_zero() {
291291
Decimal::from_ratio(total_debt, total_collateral)
292292
} else {
293293
Decimal::zero()
294294
};
295295

296+
// Limit utilization_rate to 100%.
297+
// With the current code it should hopefully never happen that it gets calculated to more than 100%,
298+
// but better be safe than sorry.
299+
current_utilization_rate = current_utilization_rate.min(Decimal::one());
300+
296301
market.update_interest_rates(current_utilization_rate)?;
297302

298303
Ok(response.add_event(build_interests_updated_event(&market.denom, market)))

contracts/red-bank/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod error;
44
pub mod execute;
55
pub mod health;
66
pub mod interest_rates;
7+
pub mod migrations;
78
pub mod query;
89
pub mod state;
910
pub mod user;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod v1_2_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+
error::ContractError,
6+
execute::{CONTRACT_NAME, CONTRACT_VERSION},
7+
};
8+
9+
const FROM_VERSION: &str = "1.2.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+
}

0 commit comments

Comments
 (0)