Skip to content
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
18 changes: 15 additions & 3 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [
"contracts/mock-pyth",
"contracts/mock-red-bank",
"contracts/mock-vault",
"contracts/mock-governance",
"contracts/mock-lst-oracle",

# packages
Expand Down Expand Up @@ -153,6 +154,7 @@ mars-mock-red-bank = { path = "./contracts/mock-red-bank" }
mars-mock-vault = { path = "./contracts/mock-vault" }
mars-mock-lst-oracle = { path = "./contracts/mock-lst-oracle" }
mars-mock-rover-health = { path = "./contracts/mock-health" }
mars-mock-governance = { path = "./contracts/mock-governance" }
mars-swapper-mock = { path = "./contracts/swapper/mock" }
mars-zapper-mock = { path = "./contracts/v2-zapper/mock" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl MockEnvBuilder {
perps: "n/a".to_string(),
keeper_fee_config: Default::default(),
perps_liquidation_bonus_ratio: Decimal::percent(60),
governance: "n/a".to_string(),
},
},
&[],
Expand Down
2 changes: 1 addition & 1 deletion contracts/address-provider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mars-address-provider"
description = "A smart contract that holds addresses of Mars Red Bank contracts"
version = "2.3.2"
version = "2.4.0"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions contracts/address-provider/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, Co
match msg {
MigrateMsg::V2_2_0ToV2_2_2 {} => migrations::v2_2_2::migrate(deps),
MigrateMsg::V2_2_2ToV2_3_2 {} => migrations::v2_3_2::migrate(deps),
MigrateMsg::V2_3_2ToV2_4_0 {} => migrations::v2_4_0::migrate(deps),
}
}
1 change: 1 addition & 0 deletions contracts/address-provider/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod v2_2_2;
pub mod v2_3_2;
pub mod v2_4_0;
22 changes: 22 additions & 0 deletions contracts/address-provider/src/migrations/v2_4_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use cosmwasm_std::{DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};

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

pub const FROM_VERSION: &str = "2.3.2";

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)?;

// this is a standard migration with no state changes
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))
}
1 change: 1 addition & 0 deletions contracts/address-provider/tests/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mod test_addresses;
mod test_instantiate;
mod test_migration_v2;
mod test_migration_v2_3_2;
mod test_migration_v2_4_0;
mod test_update_owner;
73 changes: 73 additions & 0 deletions contracts/address-provider/tests/tests/test_migration_v2_4_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use cosmwasm_std::{attr, testing::mock_env, Event};
use cw2::{ContractVersion, VersionError};
use mars_address_provider::{
contract::{migrate, CONTRACT_NAME, CONTRACT_VERSION},
error::ContractError,
migrations::v2_4_0::FROM_VERSION,
};
use mars_testing::mock_dependencies;
use mars_types::address_provider::MigrateMsg;

#[test]
fn wrong_contract_name() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", FROM_VERSION).unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_3_2ToV2_4_0 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongContract {
expected: format!("crates.io:{CONTRACT_NAME}"),
found: "contract_xyz".to_string()
})
);
}

#[test]
fn wrong_contract_version() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, format!("crates.io:{CONTRACT_NAME}"), "4.1.0")
.unwrap();

let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_3_2ToV2_4_0 {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: FROM_VERSION.to_string(),
found: "4.1.0".to_string()
})
);
}

#[test]
fn successful_migration() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(
deps.as_mut().storage,
format!("crates.io:{CONTRACT_NAME}"),
FROM_VERSION,
)
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_3_2ToV2_4_0 {}).unwrap();

assert_eq!(res.messages, vec![]);
assert_eq!(res.events, vec![] as Vec<Event>);
assert!(res.data.is_none());
assert_eq!(
res.attributes,
vec![
attr("action", "migrate"),
attr("from_version", FROM_VERSION),
attr("to_version", CONTRACT_VERSION)
]
);

let new_contract_version = ContractVersion {
contract: format!("crates.io:{CONTRACT_NAME}"),
version: CONTRACT_VERSION.to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
2 changes: 1 addition & 1 deletion contracts/credit-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mars-credit-manager"
version = "2.3.1"
version = "2.4.0"
authors = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
Expand Down
22 changes: 17 additions & 5 deletions contracts/credit-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use crate::{
migrations,
perp::update_balance_after_deleverage,
query::{
query_accounts, query_all_coin_balances, query_all_debt_shares,
query_all_total_debt_shares, query_all_trigger_orders,
query_account_tier_and_discount, query_accounts, query_all_coin_balances,
query_all_debt_shares, query_all_total_debt_shares, query_all_trigger_orders,
query_all_trigger_orders_for_account, query_all_vault_positions,
query_all_vault_utilizations, query_config, query_positions, query_swap_fee,
query_total_debt_shares, query_vault_bindings, query_vault_position_value,
query_vault_utilization,
query_all_vault_utilizations, query_config, query_fee_tier_config, query_positions,
query_swap_fee, query_total_debt_shares, query_trading_fee, query_vault_bindings,
query_vault_position_value, query_vault_utilization,
},
repay::repay_from_wallet,
state::NEXT_TRIGGER_ID,
Expand Down Expand Up @@ -173,7 +173,15 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
start_after,
limit,
} => to_json_binary(&query_vault_bindings(deps, start_after, limit)?),
QueryMsg::GetAccountTierAndDiscount {
account_id,
} => to_json_binary(&query_account_tier_and_discount(deps, &account_id)?),
QueryMsg::TradingFee {
account_id,
market_type,
} => to_json_binary(&query_trading_fee(deps, &account_id, &market_type)?),
QueryMsg::SwapFeeRate {} => to_json_binary(&query_swap_fee(deps)?),
QueryMsg::FeeTierConfig {} => to_json_binary(&query_fee_tier_config(deps)?),
};
res.map_err(Into::into)
}
Expand All @@ -188,5 +196,9 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, Co
max_trigger_orders,
} => migrations::v2_3_0::migrate(deps, max_trigger_orders),
MigrateMsg::V2_2_0ToV2_2_3 {} => migrations::v2_2_3::migrate(deps),
MigrateMsg::V2_3_1ToV2_4_0 {
fee_tier_config,
governance_address,
} => migrations::v2_4_0::migrate(deps, fee_tier_config, governance_address),
}
}
37 changes: 37 additions & 0 deletions contracts/credit-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ pub enum ContractError {
reason: String,
},

#[error("Fee tier config cannot be empty")]
EmptyFeeTierConfig,

#[error("No tiers present")]
NoTiersPresent,

#[error("Invalid min_voting_power in tier: {voting_power:?}")]
InvalidVotingPower {
voting_power: String,
},

#[error("Tiers must be sorted in descending order")]
TiersNotSortedDescending,

#[error("Duplicate voting power thresholds")]
DuplicateVotingPowerThresholds,

#[error("Discount percentage must be less than or equal to 100%")]
InvalidDiscountPercentage,

#[error("Failed to load governance address from storage")]
FailedToLoadGovernanceAddress,

#[error("Failed to load fee tier config from storage")]
FailedToLoadFeeTierConfig,

#[error("Failed to query voting power: {error:?}")]
FailedToQueryVotingPower {
error: String,
},

#[error("Paying down {debt_coin:?} for {request_coin:?} does not result in a profit for the liquidator")]
LiquidationNotProfitable {
debt_coin: Coin,
Expand Down Expand Up @@ -297,4 +328,10 @@ pub enum ContractError {
},
#[error("Vault has an admin; vaults cannot be managed with an admin set.")]
VaultHasAdmin {},

#[error("Too many tiers. Maximum allowed: {max_tiers:?}, provided: {provided_tiers:?}")]
TooManyTiers {
max_tiers: usize,
provided_tiers: usize,
},
}
8 changes: 5 additions & 3 deletions contracts/credit-manager/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use mars_types::credit_manager::InstantiateMsg;
use crate::{
error::ContractResult,
state::{
DUALITY_SWAPPER, HEALTH_CONTRACT, INCENTIVES, KEEPER_FEE_CONFIG, MAX_SLIPPAGE,
MAX_TRIGGER_ORDERS, MAX_UNLOCKING_POSITIONS, ORACLE, OWNER, PARAMS, PERPS_LB_RATIO,
RED_BANK, SWAPPER, SWAP_FEE, ZAPPER,
DUALITY_SWAPPER, FEE_TIER_CONFIG, GOVERNANCE, HEALTH_CONTRACT, INCENTIVES,
KEEPER_FEE_CONFIG, MAX_SLIPPAGE, MAX_TRIGGER_ORDERS, MAX_UNLOCKING_POSITIONS, ORACLE,
OWNER, PARAMS, PERPS_LB_RATIO, RED_BANK, SWAPPER, SWAP_FEE, ZAPPER,
},
utils::{assert_max_slippage, assert_perps_lb_ratio},
};
Expand Down Expand Up @@ -40,6 +40,8 @@ pub fn store_config(deps: DepsMut, env: Env, msg: &InstantiateMsg) -> ContractRe
INCENTIVES.save(deps.storage, &msg.incentives.check(deps.api, env.contract.address)?)?;
KEEPER_FEE_CONFIG.save(deps.storage, &msg.keeper_fee_config)?;
SWAP_FEE.save(deps.storage, &msg.swap_fee)?;
FEE_TIER_CONFIG.save(deps.storage, &msg.fee_tier_config)?;
GOVERNANCE.save(deps.storage, msg.governance_address.check(deps.api)?.address())?;

Ok(())
}
1 change: 1 addition & 0 deletions contracts/credit-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod reclaim;
pub mod refund;
pub mod repay;
pub mod stake_astro_lp;
pub mod staking;
pub mod state;
pub mod swap;
pub mod trigger;
Expand Down
1 change: 1 addition & 0 deletions contracts/credit-manager/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod v2_2_0;
pub mod v2_2_3;
pub mod v2_3_0;
pub mod v2_3_1;
pub mod v2_4_0;
37 changes: 37 additions & 0 deletions contracts/credit-manager/src/migrations/v2_4_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use cosmwasm_std::{Addr, DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};
use mars_types::fee_tiers::FeeTierConfig;

use crate::{
contract::CONTRACT_NAME,
error::ContractError,
staking::StakingTierManager,
state::{FEE_TIER_CONFIG, GOVERNANCE},
};

const FROM_VERSION: &str = "2.3.1";
const TO_VERSION: &str = "2.4.0";

pub fn migrate(
deps: DepsMut,
fee_tier_config: FeeTierConfig,
governance_address: Addr,
) -> 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)?;

let manager = StakingTierManager::new(fee_tier_config.clone());
manager.validate()?;
// Set the new state items
FEE_TIER_CONFIG.save(deps.storage, &fee_tier_config)?;
GOVERNANCE.save(deps.storage, &governance_address)?;

set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), TO_VERSION)?;

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", FROM_VERSION)
.add_attribute("to_version", TO_VERSION)
.add_attribute("fee_tier_config", "set")
.add_attribute("governance", governance_address))
}
Loading
Loading