Skip to content

Commit

Permalink
Perps migration (#1)
Browse files Browse the repository at this point in the history
* add migration for staked astro incentives (#187)

* add migration for staked astro incentives

* tidy

* MP-2780. Perps migration (#188)

* Update address-provider migration.

* Update migration for account-nft.

* Update credit-manager migration.

* Update health migration.

* Update incentives migration.

* Update oracle migration.

* Update red-bank migration.

* Update rewards-collector migration.

* Update swapper migration.

* Update zapper migration.

* Prepare params migration.

* Update incentives state migration.

* Update how we handle withdraw_enabled for RB.

* Fix for missing init for next order id. (#189)

* Fix for missing init for next order id.

* Update audit.

---------

Co-authored-by: Mark Watney <[email protected]>
  • Loading branch information
piobab and markonmars authored Dec 11, 2024
1 parent f59d3a7 commit 90ae9b0
Show file tree
Hide file tree
Showing 73 changed files with 1,103 additions and 1,273 deletions.
2 changes: 1 addition & 1 deletion .cargo/audit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# Ignore the following advisory IDs.
# Reported vulnerabilities relate to test-tube which is only used for testing.
# RUSTSEC-2024-0344 - no newer dependency fixing the issue in cosmwasm
ignore = ["RUSTSEC-2024-0003", "RUSTSEC-2024-0006", "RUSTSEC-2024-0019", "RUSTSEC-2024-0332", "RUSTSEC-2024-0336", "RUSTSEC-2024-0344"]
ignore = ["RUSTSEC-2024-0003", "RUSTSEC-2024-0006", "RUSTSEC-2024-0019", "RUSTSEC-2024-0332", "RUSTSEC-2024-0336", "RUSTSEC-2024-0344", "RUSTSEC-2024-0421"]
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions contracts/account-nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ cw-multi-test = { workspace = true }
mars-mock-credit-manager = { workspace = true }
mars-mock-rover-health = { workspace = true }
mars-owner = { workspace = true }
mars-testing = { workspace = true }
serde_json = { workspace = true }
6 changes: 6 additions & 0 deletions contracts/account-nft/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use mars_types::account_nft::{ExecuteMsg, InstantiateMsg, NftConfig, QueryMsg};
use crate::{
error::ContractError,
execute::{burn, mint, update_config},
migrations,
query::{query_config, query_next_id},
state::{CONFIG, NEXT_ID},
};
Expand Down Expand Up @@ -79,3 +80,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
_ => Parent::default().query(deps, env, msg.try_into()?),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v2_2_0::migrate(deps)
}
1 change: 1 addition & 0 deletions contracts/account-nft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod contract;
pub mod error;
pub mod execute;
pub mod migrations;
pub mod query;
pub mod state;
1 change: 1 addition & 0 deletions contracts/account-nft/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v2_2_0;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::ContractError,
};

const FROM_VERSION: &str = "1.2.0";
const FROM_VERSION: &str = "2.1.0";

pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> {
// make sure we're migrating the correct contract and from the correct version
Expand Down
1 change: 1 addition & 0 deletions contracts/account-nft/tests/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod helpers;

mod test_burn_allowance;
mod test_instantiate;
mod test_migration_v2;
mod test_mint;
mod test_proposed_minter;
mod test_update_config;
60 changes: 60 additions & 0 deletions contracts/account-nft/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use cosmwasm_std::{attr, testing::mock_env, Empty, Event};
use cw2::{ContractVersion, VersionError};
use mars_account_nft::{contract::migrate, error::ContractError};
use mars_testing::mock_dependencies;

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

let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongContract {
expected: "crates.io:mars-account-nft".to_string(),
found: "contract_xyz".to_string()
})
);
}

#[test]
fn wrong_contract_version() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-account-nft", "4.1.0")
.unwrap();

let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: "2.1.0".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, "crates.io:mars-account-nft", "2.1.0")
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), Empty {}).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", "2.1.0"), attr("to_version", "2.2.0")]
);

let new_contract_version = ContractVersion {
contract: "crates.io:mars-account-nft".to_string(),
version: "2.2.0".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
2 changes: 1 addition & 1 deletion contracts/address-provider/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ fn query_all_addresses(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v2_0_0::migrate(deps)
migrations::v2_2_0::migrate(deps)
}
2 changes: 1 addition & 1 deletion contracts/address-provider/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod v2_0_0;
pub mod v2_2_0;
21 changes: 21 additions & 0 deletions contracts/address-provider/src/migrations/v2_2_0.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::{
contract::{CONTRACT_NAME, CONTRACT_VERSION},
error::ContractError,
};

const FROM_VERSION: &str = "2.1.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))
}
8 changes: 4 additions & 4 deletions contracts/address-provider/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mars_testing::mock_dependencies;
#[test]
fn wrong_contract_name() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "1.2.0").unwrap();
cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.1.0").unwrap();

let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err();

Expand All @@ -30,7 +30,7 @@ fn wrong_contract_version() {
assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: "1.2.0".to_string(),
expected: "2.1.0".to_string(),
found: "4.1.0".to_string()
})
);
Expand All @@ -39,7 +39,7 @@ fn wrong_contract_version() {
#[test]
fn successful_migration() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-address-provider", "1.2.0")
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-address-provider", "2.1.0")
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap();
Expand All @@ -49,7 +49,7 @@ fn successful_migration() {
assert!(res.data.is_none());
assert_eq!(
res.attributes,
vec![attr("action", "migrate"), attr("from_version", "1.2.0"), attr("to_version", "2.2.0")]
vec![attr("action", "migrate"), attr("from_version", "2.1.0"), attr("to_version", "2.2.0")]
);

let new_contract_version = ContractVersion {
Expand Down
8 changes: 7 additions & 1 deletion contracts/credit-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response,
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response,
};
use cw2::set_contract_version;
use mars_types::{
Expand All @@ -12,6 +12,7 @@ use crate::{
error::{ContractError, ContractResult},
execute::{create_credit_account, dispatch_actions, execute_callback},
instantiate::store_config,
migrations,
perp::update_balance_after_deleverage,
query::{
query_accounts, query_all_coin_balances, query_all_debt_shares,
Expand Down Expand Up @@ -174,3 +175,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
};
res.map_err(Into::into)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
migrations::v2_2_0::migrate(deps)
}
1 change: 1 addition & 0 deletions contracts/credit-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod liquidate;
pub mod liquidate_astro_lp;
pub mod liquidate_deposit;
pub mod liquidate_lend;
pub mod migrations;
pub mod perp;
pub mod perp_vault;
pub mod query;
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
@@ -0,0 +1 @@
pub mod v2_2_0;
24 changes: 24 additions & 0 deletions contracts/credit-manager/src/migrations/v2_2_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cosmwasm_std::{DepsMut, Response};
use cw2::{assert_contract_version, set_contract_version};

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

const FROM_VERSION: &str = "2.1.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)?;

NEXT_TRIGGER_ID.save(deps.storage, &1)?;

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/credit-manager/tests/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod test_liquidate_lend;
mod test_liquidate_staked_astro_lp;
mod test_liquidate_vault;
mod test_liquidation_pricing;
mod test_migration_v2;
mod test_no_health_check;
mod test_perp;
mod test_perp_vault;
Expand Down
63 changes: 63 additions & 0 deletions contracts/credit-manager/tests/tests/test_migration_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use cosmwasm_std::{attr, testing::mock_env, Empty, Event};
use cw2::{ContractVersion, VersionError};
use mars_credit_manager::{contract::migrate, error::ContractError, state::NEXT_TRIGGER_ID};
use mars_testing::mock_dependencies;

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

let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongContract {
expected: "crates.io:mars-credit-manager".to_string(),
found: "contract_xyz".to_string()
})
);
}

#[test]
fn wrong_contract_version() {
let mut deps = mock_dependencies(&[]);
cw2::set_contract_version(deps.as_mut().storage, "crates.io:mars-credit-manager", "4.1.0")
.unwrap();

let err = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap_err();

assert_eq!(
err,
ContractError::Version(VersionError::WrongVersion {
expected: "2.1.0".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, "crates.io:mars-credit-manager", "2.1.0")
.unwrap();

let res = migrate(deps.as_mut(), mock_env(), Empty {}).unwrap();

let order_id = NEXT_TRIGGER_ID.load(deps.as_ref().storage).unwrap();
assert_eq!(order_id, 1);

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", "2.1.0"), attr("to_version", "2.2.0")]
);

let new_contract_version = ContractVersion {
contract: "crates.io:mars-credit-manager".to_string(),
version: "2.2.0".to_string(),
};
assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version);
}
3 changes: 2 additions & 1 deletion contracts/health/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ cw-utils = { workspace = true }
cw-vault-standard = { workspace = true }
mars-mock-credit-manager = { workspace = true }
mars-mock-oracle = { workspace = true }
mars-params = { workspace = true }
mars-mock-vault = { workspace = true }
mars-params = { workspace = true }
mars-testing = { workspace = true }
12 changes: 9 additions & 3 deletions contracts/health/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
use cw2::set_contract_version;
use mars_owner::OwnerInit::SetInitialOwner;
use mars_types::health::{ConfigResponse, ExecuteMsg, HealthResult, InstantiateMsg, QueryMsg};

use crate::{
compute::{health_state, health_values},
migrations,
state::{CREDIT_MANAGER, OWNER},
update_config::update_config,
};

const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -79,3 +80,8 @@ pub fn query_config(deps: Deps) -> HealthResult<ConfigResponse> {
owner_response,
})
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> HealthResult<Response> {
migrations::v2_2_0::migrate(deps)
}
1 change: 1 addition & 0 deletions contracts/health/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod compute;
pub mod contract;
pub mod migrations;
pub mod querier;
pub mod state;
pub mod update_config;
1 change: 1 addition & 0 deletions contracts/health/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v2_2_0;
Loading

0 comments on commit 90ae9b0

Please sign in to comment.