Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Oak no. 5 - potential outdated configurations stored in the vault #414

Merged
merged 1 commit into from
Jun 24, 2024
Merged
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
26 changes: 18 additions & 8 deletions contracts/vault/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use cosmwasm_std::{
attr, ensure_eq, to_json_binary, Coin, CosmosMsg, Deps, DepsMut, Env, Event, MessageInfo,
Order, Response, StdResult, Uint128, WasmMsg,
Order, Response, StdError, StdResult, Uint128, WasmMsg,
};
use mars_types::{
credit_manager::{self, Action, ActionAmount, ActionCoin, Positions, QueryMsg},
adapters::{account_nft::AccountNftBase, health::HealthContractBase, oracle::OracleBase},
credit_manager::{self, Action, ActionAmount, ActionCoin, ConfigResponse, Positions, QueryMsg},
health::AccountKind,
oracle::ActionKind,
};
Expand All @@ -13,8 +14,8 @@
msg::UnlockState,
performance_fee::PerformanceFeeConfig,
state::{
ACCOUNT_NFT, BASE_TOKEN, COOLDOWN_PERIOD, CREDIT_MANAGER, HEALTH, ORACLE,
PERFORMANCE_FEE_CONFIG, PERFORMANCE_FEE_STATE, UNLOCKS, VAULT_ACC_ID, VAULT_TOKEN,
BASE_TOKEN, COOLDOWN_PERIOD, CREDIT_MANAGER, PERFORMANCE_FEE_CONFIG, PERFORMANCE_FEE_STATE,
UNLOCKS, VAULT_ACC_ID, VAULT_TOKEN,
},
vault_token::{calculate_base_tokens, calculate_vault_tokens},
};
Expand Down Expand Up @@ -362,15 +363,17 @@
let vault_acc_id = VAULT_ACC_ID.load(deps.storage)?;

let cm_acc_kind: AccountKind = deps.querier.query_wasm_smart(
cm_addr,
cm_addr.clone(),
&QueryMsg::AccountKind {
account_id: vault_acc_id.clone(),
},
)?;

let base_token = BASE_TOKEN.load(deps.storage)?;

let health = HEALTH.load(deps.storage)?;
let config: ConfigResponse = deps.querier.query_wasm_smart(cm_addr, &QueryMsg::Config {})?;

let health = HealthContractBase::new(deps.api.addr_validate(&config.health_contract)?);
let health_values = health.query_health_values(
&deps.querier,
&vault_acc_id,
Expand All @@ -380,7 +383,7 @@
let net_value =
health_values.total_collateral_value.checked_sub(health_values.total_debt_value)?;

let oracle = ORACLE.load(deps.storage)?;
let oracle = OracleBase::new(deps.api.addr_validate(&config.oracle)?);
let base_token_price =
oracle.query_price(&deps.querier, &base_token, ActionKind::Default)?.price;

Expand All @@ -399,7 +402,14 @@
return Err(ContractError::VaultAccountNotFound {});
};

let acc_nft = ACCOUNT_NFT.load(deps.storage)?;
let cm_addr = CREDIT_MANAGER.load(deps.storage)?;
let config: ConfigResponse = deps.querier.query_wasm_smart(cm_addr, &QueryMsg::Config {})?;
let Some(acc_nft) = config.account_nft else {
return Err(ContractError::Std(StdError::generic_err(
"Account NFT contract address is not set in Credit Manager".to_string(),
)));

Check warning on line 410 in contracts/vault/src/execute.rs

View check run for this annotation

Codecov / codecov/patch

contracts/vault/src/execute.rs#L408-L410

Added lines #L408 - L410 were not covered by tests
};
let acc_nft = AccountNftBase::new(deps.api.addr_validate(&acc_nft)?);
let vault_acc_owner_addr = acc_nft.query_nft_token_owner(&deps.querier, &vault_acc_id)?;
if vault_acc_owner_addr != info.sender {
return Err(ContractError::NotTokenOwner {
Expand Down
29 changes: 5 additions & 24 deletions contracts/vault/src/instantiate.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdError};
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use mars_owner::OwnerInit;
use mars_types::{
adapters::{account_nft::AccountNftBase, health::HealthContractBase, oracle::OracleBase},
credit_manager::{ConfigResponse, QueryMsg as CreditManagerQueryMsg},
};

use crate::{
error::{ContractError, ContractResult},
error::ContractResult,
msg::InstantiateMsg,
performance_fee::PerformanceFeeState,
state::{
ACCOUNT_NFT, BASE_TOKEN, COOLDOWN_PERIOD, CREDIT_MANAGER, DESCRIPTION, HEALTH, ORACLE,
OWNER, PERFORMANCE_FEE_CONFIG, PERFORMANCE_FEE_STATE, SUBTITLE, TITLE, VAULT_TOKEN,
BASE_TOKEN, COOLDOWN_PERIOD, CREDIT_MANAGER, DESCRIPTION, OWNER, PERFORMANCE_FEE_CONFIG,
PERFORMANCE_FEE_STATE, SUBTITLE, TITLE, VAULT_TOKEN,
},
token_factory::TokenFactoryDenom,
};
Expand All @@ -31,24 +27,9 @@ pub fn init(
},
)?;

// initialize addresses of external contracts
// save credit manager address
let credit_manager = deps.api.addr_validate(&msg.credit_manager)?;
CREDIT_MANAGER.save(deps.storage, &credit_manager.to_string())?;
let config: ConfigResponse = deps
.querier
.query_wasm_smart(credit_manager.to_string(), &CreditManagerQueryMsg::Config {})?;
let oracle = OracleBase::new(config.oracle);
let health = HealthContractBase::new(config.health_contract);
ORACLE.save(deps.storage, &oracle.check(deps.api)?)?;
HEALTH.save(deps.storage, &health.check(deps.api)?)?;
if let Some(acc_nft) = config.account_nft {
let account_nft = AccountNftBase::new(acc_nft);
ACCOUNT_NFT.save(deps.storage, &account_nft.check(deps.api)?)?;
} else {
return Err(ContractError::Std(StdError::generic_err(
"Account NFT contract address is not set in Credit Manager".to_string(),
)));
}

// update contract metadata
if let Some(title) = msg.title {
Expand Down
5 changes: 0 additions & 5 deletions contracts/vault/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use cw_storage_plus::{Item, Map};
use mars_owner::Owner;
use mars_types::adapters::{account_nft::AccountNft, health::HealthContract, oracle::Oracle};

use crate::{
msg::UnlockState,
Expand All @@ -19,10 +18,6 @@ pub const BASE_TOKEN: Item<String> = Item::new("base_token");
pub const CREDIT_MANAGER: Item<String> = Item::new("cm_addr");
pub const VAULT_ACC_ID: Item<String> = Item::new("vault_acc_id");

pub const ORACLE: Item<Oracle> = Item::new("oracle");
pub const HEALTH: Item<HealthContract> = Item::new("health");
pub const ACCOUNT_NFT: Item<AccountNft> = Item::new("account_nft");

pub const TITLE: Item<String> = Item::new("title");
pub const SUBTITLE: Item<String> = Item::new("subtitle");
pub const DESCRIPTION: Item<String> = Item::new("desc");
Expand Down
Loading