Skip to content

Commit

Permalink
Fix inflated collateral (#354) (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab authored Feb 22, 2024
1 parent 3208bb4 commit 8914bd7
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions contracts/oracle/wasm/tests/prop_tests.proptest-regressions

This file was deleted.

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 = "1.2.1"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
9 changes: 7 additions & 2 deletions contracts/red-bank/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response,
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response,
};
use mars_red_bank_types::red_bank::{ExecuteMsg, InstantiateMsg, QueryMsg};

use crate::{error::ContractError, execute, query};
use crate::{error::ContractError, execute, migrations, query};

#[entry_point]
pub fn instantiate(
Expand Down Expand Up @@ -202,3 +202,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
};
res.map_err(Into::into)
}

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v1_2_1::migrate(deps)
}
5 changes: 4 additions & 1 deletion contracts/red-bank/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum ContractError {
#[error("{0}")]
Health(#[from] HealthError),

#[error("{0}")]
Version(#[from] cw2::VersionError),

#[error("Price not found for asset: {denom:?}")]
PriceNotFound {
denom: String,
Expand Down Expand Up @@ -64,7 +67,7 @@ pub enum ContractError {
#[error("Cannot have 0 as liquidity index")]
InvalidLiquidityIndex {},

#[error("Borrow amount must be greater than 0 and less or equal available collateral (asset: {denom:?})")]
#[error("Borrow amount must be greater than 0 and less or equal available liquidity (asset: {denom:?})")]
InvalidBorrowAmount {
denom: String,
},
Expand Down
10 changes: 8 additions & 2 deletions contracts/red-bank/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,15 @@ pub fn borrow(
&borrow_market,
env.block.time.seconds(),
)?;
let debt_balance_before = get_underlying_debt_amount(
borrow_market.debt_total_scaled,
&borrow_market,
env.block.time.seconds(),
)?;

// Cannot borrow zero amount or more than available collateral
if borrow_amount.is_zero() || borrow_amount > collateral_balance_before {
// Cannot borrow zero amount or more than available liquidity
let available_liquidity = collateral_balance_before.checked_sub(debt_balance_before)?;
if borrow_amount.is_zero() || borrow_amount > available_liquidity {
return Err(ContractError::InvalidBorrowAmount {
denom,
});
Expand Down
7 changes: 6 additions & 1 deletion contracts/red-bank/src/interest_rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,17 @@ pub fn update_interest_rates(
let total_debt =
get_underlying_debt_amount(market.debt_total_scaled, market, current_timestamp)?;

let current_utilization_rate = if !total_collateral.is_zero() {
let mut current_utilization_rate = if !total_collateral.is_zero() {
Decimal::from_ratio(total_debt, total_collateral)
} else {
Decimal::zero()
};

// Limit utilization_rate to 100%.
// With the current code it should hopefully never happen that it gets calculated to more than 100%,
// but better be safe than sorry.
current_utilization_rate = current_utilization_rate.min(Decimal::one());

market.update_interest_rates(current_utilization_rate)?;

Ok(response.add_event(build_interests_updated_event(&market.denom, market)))
Expand Down
1 change: 1 addition & 0 deletions contracts/red-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod error;
pub mod execute;
pub mod health;
pub mod interest_rates;
pub mod migrations;
pub mod query;
pub mod state;
pub mod user;
1 change: 1 addition & 0 deletions contracts/red-bank/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1_2_1;
21 changes: 21 additions & 0 deletions contracts/red-bank/src/migrations/v1_2_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use cosmwasm_std::{DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};

use crate::{
error::ContractError,
execute::{CONTRACT_NAME, CONTRACT_VERSION},
};

const FROM_VERSION: &str = "1.2.0";

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}"), CONTRACT_VERSION)?;

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", FROM_VERSION)
.add_attribute("to_version", CONTRACT_VERSION))
}
2 changes: 1 addition & 1 deletion schemas/mars-oracle-wasm/mars-oracle-wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "mars-oracle-wasm",
"contract_version": "1.2.0",
"contract_version": "1.2.1",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
2 changes: 1 addition & 1 deletion schemas/mars-red-bank/mars-red-bank.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "mars-red-bank",
"contract_version": "1.2.0",
"contract_version": "1.2.1",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit 8914bd7

Please sign in to comment.