diff --git a/Cargo.lock b/Cargo.lock index a98283815f..0fbbd13fb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3559,7 +3559,7 @@ dependencies = [ [[package]] name = "framenode" -version = "4.3.2" +version = "4.4.0" dependencies = [ "ahash 0.8.5", "assert_cmd", @@ -3643,7 +3643,7 @@ dependencies = [ [[package]] name = "framenode-chain-spec" -version = "4.3.2" +version = "4.4.0" dependencies = [ "common", "faucet", @@ -3669,7 +3669,7 @@ dependencies = [ [[package]] name = "framenode-runtime" -version = "4.3.2" +version = "4.4.0" dependencies = [ "apollo-platform", "assets", diff --git a/node/Cargo.toml b/node/Cargo.toml index eea2b7e7b0..add8e1538a 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "framenode" -version = "4.3.2" +version = "4.4.0" authors = ["Parity Technologies "] build = "build.rs" edition = "2021" diff --git a/node/chain_spec/Cargo.toml b/node/chain_spec/Cargo.toml index 7db6071d23..0c914a6fa1 100644 --- a/node/chain_spec/Cargo.toml +++ b/node/chain_spec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "framenode-chain-spec" -version = "4.3.2" +version = "4.4.0" authors = ["Parity Technologies "] edition = "2021" diff --git a/pallets/kensetsu/src/lib.rs b/pallets/kensetsu/src/lib.rs index df7f02e094..8f44b6ebe1 100644 --- a/pallets/kensetsu/src/lib.rs +++ b/pallets/kensetsu/src/lib.rs @@ -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 { /// Collateral Risk parameters set by risk management pub risk_parameters: CollateralRiskParameters, @@ -226,7 +226,7 @@ pub mod pallet { pub type CdpId = u128; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(4); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(5); #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] diff --git a/pallets/kensetsu/src/migrations.rs b/pallets/kensetsu/src/migrations.rs index bbfe0d70d2..516c86ae08 100644 --- a/pallets/kensetsu/src/migrations.rs +++ b/pallets/kensetsu/src/migrations.rs @@ -608,3 +608,153 @@ pub mod v3_to_v4 { } } } + +/// Kensetsu version 5 replaces milliseconds to seconds in parameters +pub mod v4_to_v5 { + use crate::{CollateralInfos, Config, Pallet}; + use core::marker::PhantomData; + use frame_support::dispatch::Weight; + use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion}; + use sp_core::Get; + use sp_runtime::traits::Saturating; + use sp_runtime::FixedU128; + + pub struct UpgradeToV5(PhantomData); + + impl OnRuntimeUpgrade for UpgradeToV5 { + fn on_runtime_upgrade() -> Weight { + if Pallet::::on_chain_storage_version() == 4 { + let mut count = 0; + + CollateralInfos::::translate_values::, _>( + |mut value| { + value.risk_parameters.stability_fee_rate = value + .risk_parameters + .stability_fee_rate + .saturating_mul(FixedU128::from_u32(1000u32)); + value.last_fee_update_time /= T::Moment::from(1000u32); + count += 1; + Some(value) + }, + ); + + StorageVersion::new(5).put::>(); + count += 1; + + frame_support::log::info!("Migration to V5 applied"); + T::DbWeight::get().reads_writes(count, count) + } else { + frame_support::log::info!("Migration to V5 already applied, skipping..."); + T::DbWeight::get().reads(1) + } + } + } + + #[cfg(test)] + mod tests { + use crate::migrations::v4_to_v5::UpgradeToV5; + use crate::mock::{new_test_ext, TestRuntime}; + 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::>(); + + CollateralInfos::::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::::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::::on_runtime_upgrade(); + + assert_eq!(CollateralInfos::::iter().count(), 2); + + assert_eq!( + CollateralInfos::::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::::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::::on_chain_storage_version(), 5); + }); + } + } +} diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 394b09e05c..82e18dc4bc 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -5,7 +5,7 @@ license = "BSD-4-Clause" homepage = "https://sora.org" repository = "https://github.com/sora-xor/sora2-network" name = "framenode-runtime" -version = "4.3.2" +version = "4.4.0" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d780d0126f..6b1dddb267 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -255,10 +255,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("sora-substrate"), impl_name: create_runtime_str!("sora-substrate"), authoring_version: 1, - spec_version: 104, + spec_version: 105, impl_version: 1, apis: RUNTIME_API_VERSIONS, - transaction_version: 104, + transaction_version: 105, state_version: 0, }; diff --git a/runtime/src/migrations.rs b/runtime/src/migrations.rs index 097cc016ce..2ea62be41d 100644 --- a/runtime/src/migrations.rs +++ b/runtime/src/migrations.rs @@ -28,4 +28,7 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -pub type Migrations = (xor_fee::migrations::remove_vxor_remint::Migrate,); +pub type Migrations = ( + xor_fee::migrations::remove_vxor_remint::Migrate, + kensetsu::migrations::v4_to_v5::UpgradeToV5, +);