diff --git a/pallets/stable-asset/src/lib.rs b/pallets/stable-asset/src/lib.rs index 73adc29821..62f75750ac 100644 --- a/pallets/stable-asset/src/lib.rs +++ b/pallets/stable-asset/src/lib.rs @@ -30,6 +30,8 @@ mod tests; pub mod weights; +pub mod migration; + pub use crate::traits::StableAsset; use frame_support::{dispatch::DispatchResult, ensure, traits::Get, weights::Weight}; use frame_system::pallet_prelude::BlockNumberFor; diff --git a/pallets/stable-asset/src/migration.rs b/pallets/stable-asset/src/migration.rs new file mode 100644 index 0000000000..b9f9fc73eb --- /dev/null +++ b/pallets/stable-asset/src/migration.rs @@ -0,0 +1,114 @@ +// This file is part of Bifrost. + +// Copyright (C) Liebi Technologies PTE. LTD. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#![cfg_attr(not(feature = "std"), no_std)] + +use super::{Config, Weight, *}; +use frame_support::traits::Get; + +pub fn update_pallet_id() -> Weight { + let pool_count: u32 = PoolCount::::get(); + + for pool_id in 0..pool_count { + Pools::::try_mutate_exists(pool_id, |maybe_pool_info| -> DispatchResult { + let to: T::AccountId = T::PalletId::get().into_sub_account_truncating(pool_id); + let pool_info = maybe_pool_info.as_mut().ok_or(Error::::PoolNotFound)?; + + for (_, &asset_id) in pool_info.assets.iter().enumerate() { + let balance = T::Assets::free_balance(asset_id, &pool_info.account_id); + if balance == Zero::zero() { + continue; + } + T::Assets::transfer(asset_id, &pool_info.account_id, &to, balance)?; + } + let pool_asset_balance = + T::Assets::free_balance(pool_info.pool_asset, &pool_info.account_id); + T::Assets::transfer( + pool_info.pool_asset, + &pool_info.account_id, + &to, + pool_asset_balance, + )?; + + pool_info.account_id = to; + Ok(()) + }) + .ok(); + } + + let count: u64 = (pool_count * 3).into(); + Weight::from(T::DbWeight::get().reads_writes(count, count)) +} + +use frame_support::{pallet_prelude::PhantomData, traits::OnRuntimeUpgrade}; +pub struct StableAssetOnRuntimeUpgrade(PhantomData); +impl OnRuntimeUpgrade for StableAssetOnRuntimeUpgrade { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::PalletId; + log::info!("Bifrost `pre_upgrade`..."); + + Ok(vec![]) + } + + fn on_runtime_upgrade() -> Weight { + log::info!("Bifrost `on_runtime_upgrade`..."); + + let weight = super::migration::update_pallet_id::(); + + log::info!("Bifrost `on_runtime_upgrade finished`"); + + weight + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: sp_std::prelude::Vec) -> Result<(), sp_runtime::DispatchError> { + #[allow(unused_imports)] + use frame_support::PalletId; + log::info!("Bifrost `post_upgrade`..."); + let old_pallet_id: PalletId = PalletId(*b"nuts/sta"); + + let pool_count: u32 = PoolCount::::get(); + for pool_id in 0..pool_count { + if let Some(pool_info) = Pools::::get(pool_id) { + let old_account_id: T::AccountId = + old_pallet_id.into_sub_account_truncating(pool_id); + for (_, &asset_id) in pool_info.assets.iter().enumerate() { + let old_balance = T::Assets::free_balance(asset_id, &old_account_id); + assert_eq!(old_balance, Zero::zero()); + + let balance = T::Assets::free_balance(asset_id, &pool_info.account_id); + if balance != Zero::zero() { + continue; + } else { + log::info!( + "New pool {:?} asset_id {:?} free_balance is zero.", + pool_id, + asset_id + ); + } + } + } else { + log::info!("Pool {:?} not found", pool_id); + } + } + + Ok(()) + } +} diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index 0d2ee5f1a0..db656c80ed 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -325,6 +325,7 @@ parameter_types! { pub const FarmingBoostPalletId: PalletId = PalletId(*b"bf/fmbst"); pub const LendMarketPalletId: PalletId = PalletId(*b"bf/ldmkt"); pub const OraclePalletId: PalletId = PalletId(*b"bf/oracl"); + pub const StableAssetPalletId: PalletId = PalletId(*b"bf/stabl"); } impl frame_system::Config for Runtime { @@ -1640,9 +1641,6 @@ impl bifrost_stable_asset::traits::ValidateAssetId for EnsurePoolAss true } } -parameter_types! { - pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta"); -} /// Configure the pallet bifrost_stable_asset in pallets/bifrost_stable_asset. impl bifrost_stable_asset::Config for Runtime { @@ -1901,11 +1899,11 @@ pub type Migrations = migrations::Unreleased; /// The runtime migrations per release. pub mod migrations { - #[allow(unused)] - use super::*; + use crate::Runtime; + use bifrost_stable_asset::migration::StableAssetOnRuntimeUpgrade; /// Unreleased migrations. Add new ones here: - pub type Unreleased = (); + pub type Unreleased = StableAssetOnRuntimeUpgrade; } /// Executive: handles dispatch to the various modules. diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index ccf117c986..b73584025d 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -309,6 +309,7 @@ parameter_types! { pub const FarmingBoostPalletId: PalletId = PalletId(*b"bf/fmbst"); pub const LendMarketPalletId: PalletId = PalletId(*b"bf/ldmkt"); pub const OraclePalletId: PalletId = PalletId(*b"bf/oracl"); + pub const StableAssetPalletId: PalletId = PalletId(*b"bf/stabl"); } impl frame_system::Config for Runtime { @@ -1311,9 +1312,6 @@ impl bifrost_stable_asset::traits::ValidateAssetId for EnsurePoolAss true } } -parameter_types! { - pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta"); -} /// Configure the pallet bifrost_stable_asset in pallets/bifrost_stable_asset. impl bifrost_stable_asset::Config for Runtime { @@ -1705,8 +1703,12 @@ pub type Migrations = migrations::Unreleased; /// The runtime migrations per release. pub mod migrations { use crate::Runtime; + use bifrost_stable_asset::migration::StableAssetOnRuntimeUpgrade; /// Unreleased migrations. Add new ones here: - pub type Unreleased = bifrost_asset_registry::migration::InsertBNCMetadata; + pub type Unreleased = ( + bifrost_asset_registry::migration::InsertBNCMetadata, + StableAssetOnRuntimeUpgrade, + ); } /// Executive: handles dispatch to the various modules.