Skip to content

Commit

Permalink
Improve migration test
Browse files Browse the repository at this point in the history
  • Loading branch information
vovac12 committed Nov 20, 2024
1 parent abd5757 commit 933d5d6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pallets/kensetsu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub struct CollateralRiskParameters {
}

/// Collateral parameters, includes risk info and additional data for interest rate calculation
#[derive(Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
#[derive(Debug, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Eq)]
pub struct CollateralInfo<Moment> {
/// Collateral Risk parameters set by risk management
pub risk_parameters: CollateralRiskParameters,
Expand Down
94 changes: 91 additions & 3 deletions pallets/kensetsu/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,7 @@ pub mod v4_to_v5 {
.risk_parameters
.stability_fee_rate
.saturating_mul(FixedU128::from_u32(1000u32));
value.last_fee_update_time =
value.last_fee_update_time / T::Moment::from(1000u32);
value.last_fee_update_time /= T::Moment::from(1000u32);
count += 1;
Some(value)
},
Expand All @@ -655,16 +654,105 @@ pub mod v4_to_v5 {
mod tests {
use crate::migrations::v4_to_v5::UpgradeToV5;
use crate::mock::{new_test_ext, TestRuntime};
use crate::Pallet;
use crate::{
CollateralInfo, CollateralInfos, CollateralRiskParameters, Pallet,
StablecoinCollateralIdentifier,
};
use common::{balance, DAI, ETH, KUSD};
use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion};
use sp_runtime::{FixedU128, Perbill};

#[test]
fn test() {
new_test_ext().execute_with(|| {
StorageVersion::new(4).put::<Pallet<TestRuntime>>();

CollateralInfos::<TestRuntime>::insert(
StablecoinCollateralIdentifier {
collateral_asset_id: DAI,
stablecoin_asset_id: KUSD,
},
CollateralInfo {
risk_parameters: CollateralRiskParameters {
hard_cap: balance!(1000),
liquidation_ratio: Perbill::from_rational(50u32, 100u32),
max_liquidation_lot: balance!(1),
stability_fee_rate: FixedU128::from_inner(123_456),
minimal_collateral_deposit: balance!(1),
},
total_collateral: balance!(10),
stablecoin_supply: balance!(20),
last_fee_update_time: 123_456_789,
interest_coefficient: FixedU128::from_u32(1),
},
);

CollateralInfos::<TestRuntime>::insert(
StablecoinCollateralIdentifier {
collateral_asset_id: ETH,
stablecoin_asset_id: KUSD,
},
CollateralInfo {
risk_parameters: CollateralRiskParameters {
hard_cap: balance!(10000),
liquidation_ratio: Perbill::from_rational(75u32, 100u32),
max_liquidation_lot: balance!(1),
stability_fee_rate: FixedU128::from_inner(123_456_789),
minimal_collateral_deposit: balance!(1),
},
total_collateral: balance!(1),
stablecoin_supply: balance!(30),
last_fee_update_time: 123_456,
interest_coefficient: FixedU128::from_u32(1),
},
);

UpgradeToV5::<TestRuntime>::on_runtime_upgrade();

assert_eq!(CollateralInfos::<TestRuntime>::iter().count(), 2);

assert_eq!(
CollateralInfos::<TestRuntime>::get(StablecoinCollateralIdentifier {
collateral_asset_id: DAI,
stablecoin_asset_id: KUSD,
})
.unwrap(),
CollateralInfo {
risk_parameters: CollateralRiskParameters {
hard_cap: balance!(1000),
liquidation_ratio: Perbill::from_rational(50u32, 100u32),
max_liquidation_lot: balance!(1),
stability_fee_rate: FixedU128::from_inner(123_456_000),
minimal_collateral_deposit: balance!(1),
},
total_collateral: balance!(10),
stablecoin_supply: balance!(20),
last_fee_update_time: 123_456,
interest_coefficient: FixedU128::from_u32(1),
},
);

assert_eq!(
CollateralInfos::<TestRuntime>::get(StablecoinCollateralIdentifier {
collateral_asset_id: ETH,
stablecoin_asset_id: KUSD,
})
.unwrap(),
CollateralInfo {
risk_parameters: CollateralRiskParameters {
hard_cap: balance!(10000),
liquidation_ratio: Perbill::from_rational(75u32, 100u32),
max_liquidation_lot: balance!(1),
stability_fee_rate: FixedU128::from_inner(123_456_789_000),
minimal_collateral_deposit: balance!(1),
},
total_collateral: balance!(1),
stablecoin_supply: balance!(30),
last_fee_update_time: 123,
interest_coefficient: FixedU128::from_u32(1),
},
);

assert_eq!(Pallet::<TestRuntime>::on_chain_storage_version(), 5);
});
}
Expand Down

0 comments on commit 933d5d6

Please sign in to comment.