diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index c3c3631ff7..e19c230905 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -9,5 +9,80 @@ // 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. +use codec::{Decode, Encode}; -pub type UpgradeCentrifuge1021 = (); +use crate::{PoolSystem, Runtime, Weight}; + +pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; + +/// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC, +/// native on Polkadot's AssetHub. +mod anemoy_pool { + + use cfg_primitives::PoolId; + use cfg_types::tokens::CurrencyId; + use frame_support::traits::OnRuntimeUpgrade; + use pallet_pool_system::PoolDetailsOf; + #[cfg(feature = "try-runtime")] + use sp_std::vec::Vec; + #[cfg(feature = "try-runtime")] + use frame_support::ensure; + + use super::*; + + const ANEMOY_POOL_ID: PoolId = 4_139_607_887; + const LP_ETH_USDC: CurrencyId = CurrencyId::ForeignAsset(100_001); + const DOT_NATIVE_USDC: CurrencyId = CurrencyId::ForeignAsset(6); + + pub struct Migration; + + impl OnRuntimeUpgrade for Migration { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + let pool_details: PoolDetailsOf = + PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?; + + ensure!( + pool_details.currency == LP_ETH_USDC, + "anemoy_pool::Migration: pre_upgrade failing as Anemoy's currency should be LpEthUSDC" + ); + + Ok(pool_details.encode()) + } + + fn on_runtime_upgrade() -> Weight { + // To be executed at 1021, reject higher spec_versions + if crate::VERSION.spec_version >= 1022 { + log::info!("anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022"); + return Weight::zero(); + } + + pallet_pool_system::Pool::::mutate(ANEMOY_POOL_ID, |details| { + let details = details.as_mut().unwrap(); + details.currency = DOT_NATIVE_USDC; + log::info!("anemoy_pool::Migration: Finished mutating currency to USDC"); + }); + + ::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(old_state: Vec) -> Result<(), &'static str> { + let mut old_pool_details = PoolDetailsOf::::decode(&mut old_state.as_ref()) + .map_err(|_| "Error decoding pre-upgrade state")?; + + let pool_details: PoolDetailsOf = + PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?; + + // Ensure the currency set to USDC is the only mutation performed + old_pool_details.currency = DOT_NATIVE_USDC; + ensure!( + old_pool_details == pool_details, + "Corrupted migration: Only the currency of the Anemoy pool should have changed" + ); + + log::info!("anemoy_pool::Migration: post_upgrade succeeded"); + Ok(()) + } + } +}