1#[cfg(not(feature = "library"))]
2use cosmwasm_std::entry_point;
3use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response};
4use cw2::set_contract_version;
5use mars_owner::OwnerInit::SetInitialOwner;
6
7use crate::{
8 emergency_powers::{disable_borrowing, disallow_coin, set_zero_deposit_cap, set_zero_max_ltv},
9 error::ContractResult,
10 execute::{assert_thf, update_asset_params, update_target_health_factor, update_vault_config},
11 msg::{
12 CmEmergencyUpdate, EmergencyUpdate, ExecuteMsg, InstantiateMsg, QueryMsg,
13 RedBankEmergencyUpdate,
14 },
15 query::{query_all_asset_params, query_all_vault_configs, query_vault_config},
16 state::{ASSET_PARAMS, OWNER, TARGET_HEALTH_FACTOR},
17};
18
19const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
20const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
21
22#[cfg_attr(not(feature = "library"), entry_point)]
23pub fn instantiate(
24 deps: DepsMut,
25 _: Env,
26 _: MessageInfo,
27 msg: InstantiateMsg,
28) -> ContractResult<Response> {
29 set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?;
30
31 OWNER.initialize(
32 deps.storage,
33 deps.api,
34 SetInitialOwner {
35 owner: msg.owner,
36 },
37 )?;
38
39 assert_thf(msg.target_health_factor)?;
40 TARGET_HEALTH_FACTOR.save(deps.storage, &msg.target_health_factor)?;
41
42 Ok(Response::default())
43}
44
45#[cfg_attr(not(feature = "library"), entry_point)]
46pub fn execute(
47 deps: DepsMut,
48 _: Env,
49 info: MessageInfo,
50 msg: ExecuteMsg,
51) -> ContractResult<Response> {
52 match msg {
53 ExecuteMsg::UpdateOwner(update) => Ok(OWNER.update(deps, info, update)?),
54 ExecuteMsg::UpdateAssetParams(update) => update_asset_params(deps, info, update),
55 ExecuteMsg::UpdateTargetHealthFactor(mcf) => update_target_health_factor(deps, info, mcf),
56 ExecuteMsg::UpdateVaultConfig(update) => update_vault_config(deps, info, update),
57 ExecuteMsg::EmergencyUpdate(update) => match update {
58 EmergencyUpdate::RedBank(rb_u) => match rb_u {
59 RedBankEmergencyUpdate::DisableBorrowing(denom) => {
60 disable_borrowing(deps, info, &denom)
61 }
62 },
63 EmergencyUpdate::CreditManager(rv_u) => match rv_u {
64 CmEmergencyUpdate::DisallowCoin(denom) => disallow_coin(deps, info, &denom),
65 CmEmergencyUpdate::SetZeroMaxLtvOnVault(v) => set_zero_max_ltv(deps, info, &v),
66 CmEmergencyUpdate::SetZeroDepositCapOnVault(v) => {
67 set_zero_deposit_cap(deps, info, &v)
68 }
69 },
70 },
71 }
72}
73
74#[cfg_attr(not(feature = "library"), entry_point)]
75pub fn query(deps: Deps, _: Env, msg: QueryMsg) -> ContractResult<Binary> {
76 let res = match msg {
77 QueryMsg::Owner {} => to_binary(&OWNER.query(deps.storage)?),
78 QueryMsg::AssetParams {
79 denom,
80 } => to_binary(&ASSET_PARAMS.load(deps.storage, &denom)?),
81 QueryMsg::AllAssetParams {
82 start_after,
83 limit,
84 } => to_binary(&query_all_asset_params(deps, start_after, limit)?),
85 QueryMsg::VaultConfig {
86 address,
87 } => to_binary(&query_vault_config(deps, &address)?),
88 QueryMsg::AllVaultConfigs {
89 start_after,
90 limit,
91 } => to_binary(&query_all_vault_configs(deps, start_after, limit)?),
92 QueryMsg::TargetHealthFactor {} => to_binary(&TARGET_HEALTH_FACTOR.load(deps.storage)?),
93 };
94 res.map_err(Into::into)
95}