diff --git a/Cargo.lock b/Cargo.lock index fb9485a98e..db14921565 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9620,9 +9620,11 @@ dependencies = [ "frame-support", "frame-system", "log", + "pallet-asset-manager", "pallet-assets", "pallet-balances", "pallet-evm", + "pallet-moonbeam-foreign-assets", "pallet-scheduler", "pallet-timestamp", "parity-scale-codec", @@ -9632,6 +9634,8 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std", + "staging-xcm", + "xcm-primitives 0.1.1", ] [[package]] diff --git a/pallets/moonbeam-lazy-migrations/Cargo.toml b/pallets/moonbeam-lazy-migrations/Cargo.toml index cf03633392..4e00e0999e 100644 --- a/pallets/moonbeam-lazy-migrations/Cargo.toml +++ b/pallets/moonbeam-lazy-migrations/Cargo.toml @@ -13,13 +13,16 @@ frame-support = { workspace = true } frame-system = { workspace = true } pallet-scheduler = { workspace = true } pallet-assets = { workspace = true } +pallet-asset-manager = { workspace = true } pallet-balances = { workspace = true } +pallet-moonbeam-foreign-assets = { workspace = true } parity-scale-codec = { workspace = true } scale-info = { workspace = true, features = ["derive"] } sp-core = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } +xcm-primitives = { workspace = true } # Frontier pallet-evm = { workspace = true, features = ["forbid-evm-reentrancy"] } @@ -52,7 +55,11 @@ std = [ "pallet-evm/std", "pallet-timestamp/std", "pallet-assets/std", + "pallet-moonbeam-foreign-assets/std", + "pallet-asset-manager/std", "cumulus-primitives-storage-weight-reclaim/std", "rlp/std", + "xcm/std", + "xcm-primitives/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/moonbeam-lazy-migrations/src/lib.rs b/pallets/moonbeam-lazy-migrations/src/lib.rs index 58875eefee..222e4a74a0 100644 --- a/pallets/moonbeam-lazy-migrations/src/lib.rs +++ b/pallets/moonbeam-lazy-migrations/src/lib.rs @@ -40,8 +40,11 @@ pub mod pallet { use super::*; use cumulus_primitives_storage_weight_reclaim::get_proof_size; use frame_support::pallet_prelude::*; + use frame_support::traits::fungibles::metadata::Inspect; use frame_system::pallet_prelude::*; use sp_core::H160; + use xcm::latest::Location; + use xcm_primitives::AssetTypeGetter; pub const ARRAY_LIMIT: u32 = 1000; pub type GetArrayLimit = ConstU32; @@ -76,7 +79,16 @@ pub mod pallet { /// Configuration trait of this pallet. #[pallet::config] - pub trait Config: frame_system::Config + pallet_evm::Config + pallet_balances::Config { + pub trait Config: + frame_system::Config + + pallet_evm::Config + + pallet_balances::Config + + pallet_assets::Config + + pallet_asset_manager::Config + + pallet_moonbeam_foreign_assets::Config + { + // Origin that is allowed to freeze foreign assets for migration + type ForeignAssetFreezerOrigin: EnsureOrigin; type WeightInfo: WeightInfo; } @@ -94,6 +106,12 @@ pub mod pallet { ContractNotExist, /// The key lengths exceeds the maximum allowed KeyTooLong, + /// The symbol length exceeds the maximum allowed + SymbolTooLong, + /// The name length exceeds the maximum allowed + NameTooLong, + /// The asset type was not found + AssetTypeNotFound, } pub(crate) const MAX_ITEM_PROOF_SIZE: u64 = 30 * 1024; // 30 KB @@ -322,7 +340,8 @@ pub mod pallet { } #[pallet::call] - impl Pallet { + impl Pallet + where ::ForeignAssetType: Into>{ // TODO(rodrigo): This extrinsic should be removed once the storage of destroyed contracts // has been removed #[pallet::call_index(1)] @@ -412,6 +431,39 @@ pub mod pallet { ) .into()) } + + // TODO update weights + #[pallet::call_index(3)] + #[pallet::weight(0)] + pub fn freeze_foreign_asset( + origin: OriginFor, + asset_id: u128, + ) -> DispatchResultWithPostInfo { + ::ForeignAssetFreezerOrigin::ensure_origin(origin.clone())?; + + // Freeze the asset + pallet_assets::Pallet::::freeze_asset(origin.clone(), asset_id.into())?; + + let decimals = pallet_assets::Pallet::::decimals(asset_id); + let symbol = pallet_assets::Pallet::::symbol(asset_id) + .try_into() + .map_err(|_| Error::::SymbolTooLong)?; + let name = as Inspect<_>>::name(asset_id) + .try_into() + .map_err(|_| Error::::NameTooLong)?; + + let location: Option = + pallet_asset_manager::Pallet::::get_asset_type(asset_id) + .ok_or(Error::::AssetTypeNotFound)? + .into(); + + // Create the SC for the asset with moonbeam foreign assets pallet + pallet_moonbeam_foreign_assets::Pallet::::create_foreign_asset( + origin, asset_id, location.unwrap(), decimals, symbol, name, + )?; + + Ok(Pays::No.into()) + } } impl Pallet { diff --git a/runtime/moonbase/src/lib.rs b/runtime/moonbase/src/lib.rs index b7677f7159..17b3988ec1 100644 --- a/runtime/moonbase/src/lib.rs +++ b/runtime/moonbase/src/lib.rs @@ -120,6 +120,7 @@ use sp_std::{ use sp_version::NativeVersion; use sp_version::RuntimeVersion; use xcm::{VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm}; +use xcm_config::ForeignAssetManagerOrigin; use xcm_runtime_apis::{ dry_run::{CallDryRunEffects, Error as XcmDryRunApiError, XcmDryRunEffects}, fees::Error as XcmPaymentApiError, @@ -1168,6 +1169,7 @@ impl pallet_migrations::Config for Runtime { } impl pallet_moonbeam_lazy_migrations::Config for Runtime { + type ForeignAssetFreezerOrigin = ForeignAssetManagerOrigin; type WeightInfo = moonbase_weights::pallet_moonbeam_lazy_migrations::WeightInfo; } diff --git a/runtime/moonbeam/src/lib.rs b/runtime/moonbeam/src/lib.rs index 05a79d2811..d32e1f2d20 100644 --- a/runtime/moonbeam/src/lib.rs +++ b/runtime/moonbeam/src/lib.rs @@ -1158,7 +1158,15 @@ impl pallet_migrations::Config for Runtime { type XcmExecutionManager = XcmExecutionManager; } +pub type ForeignAssetFreezerOrigin = EitherOfDiverse< + EnsureRoot, + EitherOfDiverse< + pallet_collective::EnsureProportionMoreThan, + governance::custom_origins::GeneralAdmin, + >, +>; impl pallet_moonbeam_lazy_migrations::Config for Runtime { + type ForeignAssetFreezerOrigin = ForeignAssetFreezerOrigin; type WeightInfo = moonbeam_weights::pallet_moonbeam_lazy_migrations::WeightInfo; } diff --git a/runtime/moonriver/src/lib.rs b/runtime/moonriver/src/lib.rs index 69fdba085e..ed75ae4ee6 100644 --- a/runtime/moonriver/src/lib.rs +++ b/runtime/moonriver/src/lib.rs @@ -1162,7 +1162,16 @@ impl pallet_migrations::Config for Runtime { type XcmExecutionManager = XcmExecutionManager; } +pub type ForeignAssetFreezerOrigin = EitherOfDiverse< + EnsureRoot, + EitherOfDiverse< + pallet_collective::EnsureProportionMoreThan, + governance::custom_origins::GeneralAdmin, + >, +>; + impl pallet_moonbeam_lazy_migrations::Config for Runtime { + type ForeignAssetFreezerOrigin = ForeignAssetFreezerOrigin; type WeightInfo = moonriver_weights::pallet_moonbeam_lazy_migrations::WeightInfo; }