Skip to content

Commit

Permalink
fix: celo usdc migration
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Feb 28, 2024
1 parent 6c27fe5 commit cd983fc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
7 changes: 5 additions & 2 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ pub type UpgradeCentrifuge1025 = (
LocalCurrencyIdUsdc,
>,
// Register new canonical USDC on Celo
runtime_common::migrations::update_celo_usdcs::Migration<super::Runtime>,
// Init local representation for all assets
runtime_common::migrations::update_celo_usdcs::AddNewCeloUsdc<super::Runtime>,
// Update custom metadata by initiating local representation for all assets
runtime_common::migrations::local_currency::translate_metadata::Migration<
super::Runtime,
UsdcVariants,
LocalAssetIdUsdc,
>,
// Change name and symbol of Celo Wormhole USDC
// NOTE: Needs to happen after metadata translation because expects new CustomMetadata
runtime_common::migrations::update_celo_usdcs::UpdateWormholeUsdc<super::Runtime>,
// Switch pool currency from Polkadot USDC to Local USDC
runtime_common::migrations::local_currency::migrate_pool_currency::Migration<
super::Runtime,
Expand Down
70 changes: 55 additions & 15 deletions runtime/common/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ pub mod update_celo_usdcs {
NetworkId::Ethereum,
};

const LOG_PREFIX: &str = "UpdateCeloUsdcs";
const LOG_PREFIX_ADD: &str = "AddNewCeloUsdc";
const LOG_PREFIX_UPDATE: &str = "UpdateCeloWormholeUsdc";

pub struct Migration<T>(sp_std::marker::PhantomData<T>);
pub struct AddNewCeloUsdc<T>(sp_std::marker::PhantomData<T>);

impl<T> OnRuntimeUpgrade for Migration<T>
impl<T> OnRuntimeUpgrade for AddNewCeloUsdc<T>
where
T: orml_asset_registry::Config<
CustomMetadata = CustomMetadata,
Expand Down Expand Up @@ -82,14 +83,50 @@ pub mod update_celo_usdcs {
)
.map_err(|e| {
log::error!(
"{LOG_PREFIX} Failed to register new canonical Celo USDC due to error {:?}",
"{LOG_PREFIX_ADD} Failed to register new canonical Celo USDC due to error {:?}",
e
);
})
.ok();

log::info!("{LOG_PREFIX} Done registering new canonical Celo USDC currency");
log::info!("{LOG_PREFIX_ADD} Done registering new canonical Celo USDC currency");

T::DbWeight::get().writes(1)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
assert!(!orml_asset_registry::Metadata::<T>::contains_key(
CURRENCY_ID_LP_CELO
));

log::info!("{LOG_PREFIX_ADD} PRE UPGRADE: Finished");

Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
assert!(orml_asset_registry::Metadata::<T>::contains_key(
CURRENCY_ID_LP_CELO
));

log::info!("{LOG_PREFIX_ADD} POST UPGRADE: Finished");
Ok(())
}
}

pub struct UpdateWormholeUsdc<T>(sp_std::marker::PhantomData<T>);

impl<T> OnRuntimeUpgrade for UpdateWormholeUsdc<T>
where
T: orml_asset_registry::Config<
CustomMetadata = CustomMetadata,
AssetId = CurrencyId,
Balance = Balance,
>,
{
fn on_runtime_upgrade() -> Weight {
<orml_asset_registry::Pallet<T> as Mutate>::update_asset(
CURRENCY_ID_LP_CELO_WORMHOLE,
None,
Expand All @@ -101,35 +138,38 @@ pub mod update_celo_usdcs {
)
.map_err(|e| {
log::error!(
"{LOG_PREFIX} Failed to update wormhole Celo USDC due to error {:?}",
"{LOG_PREFIX_UPDATE} Failed to update wormhole Celo USDC due to error {:?}",
e
);
})
.ok();

log::info!("{LOG_PREFIX} Done updating wormhole Celo USDC currency");
log::info!("{LOG_PREFIX_UPDATE} Done updating wormhole Celo USDC currency");

T::DbWeight::get().writes(2)
T::DbWeight::get().writes(1)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
assert!(!orml_asset_registry::Metadata::<T>::contains_key(
CURRENCY_ID_LP_CELO
assert!(orml_asset_registry::Metadata::<T>::contains_key(
CURRENCY_ID_LP_CELO_WORMHOLE
));

log::info!("{LOG_PREFIX} PRE UPGRADE: Finished");
log::info!("{LOG_PREFIX_UPDATE} PRE UPGRADE: Finished");

Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
assert!(orml_asset_registry::Metadata::<T>::contains_key(
CURRENCY_ID_LP_CELO
));
assert_eq!(
orml_asset_registry::Metadata::<T>::get(CURRENCY_ID_LP_CELO_WORMHOLE)
.expect("Wormhole Celo USDC exists; qed")
.name,
"LP Celo Wrapped Wormhole USDC ".as_bytes().to_vec()
);

log::info!("{LOG_PREFIX} POST UPGRADE: Finished");
log::info!("{LOG_PREFIX_UPDATE} POST UPGRADE: Finished");
Ok(())
}
}
Expand Down

0 comments on commit cd983fc

Please sign in to comment.