difftreelog
fix use AssetId in the mapping
in: master
2 files changed
pallets/foreign-assets/src/benchmarking.rsdiffbeforeafterboth19use frame_benchmarking::v2::*;19use frame_benchmarking::v2::*;20use frame_system::RawOrigin;20use frame_system::RawOrigin;21use pallet_common::benchmarking::{create_data, create_u16_data};21use pallet_common::benchmarking::{create_data, create_u16_data};22use sp_std::vec;22use sp_std::{vec, boxed::Box};23use staging_xcm::prelude::*;23use staging_xcm::prelude::*;24use up_data_structs::{MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH};24use up_data_structs::{MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH};252541 #[extrinsic_call]41 #[extrinsic_call]42 _(42 _(43 RawOrigin::Root,43 RawOrigin::Root,44 Box::new(location),44 Box::new(location.into()),45 name,45 name,46 token_prefix,46 token_prefix,47 mode,47 mode,pallets/foreign-assets/src/lib.rsdiffbeforeafterboth99 pub enum Event<T: Config> {99 pub enum Event<T: Config> {100 /// The foreign asset registered.100 /// The foreign asset registered.101 ForeignAssetRegistered {101 ForeignAssetRegistered {102 asset_id: CollectionId,102 collection_id: CollectionId,103 reserve_location: Box<MultiLocation>,103 asset_id: Box<AssetId>,104 },104 },105 }105 }106106107 /// The corresponding collections of reserve locations.107 /// The corresponding collections of foreign assets.108 #[pallet::storage]108 #[pallet::storage]109 #[pallet::getter(fn foreign_reserve_location_to_collection)]109 #[pallet::getter(fn foreign_asset_to_collection)]110 pub type ForeignReserveLocationToCollection<T: Config> =110 pub type ForeignAssetToCollection<T: Config> =111 StorageMap<_, Twox64Concat, staging_xcm::v3::MultiLocation, CollectionId, OptionQuery>;111 StorageMap<_, Twox64Concat, staging_xcm::v3::AssetId, CollectionId, OptionQuery>;112112113 /// The corresponding reserve location of collections.113 /// The corresponding foreign assets of collections.114 #[pallet::storage]114 #[pallet::storage]115 #[pallet::getter(fn collection_to_foreign_reserve_location)]115 #[pallet::getter(fn collection_to_foreign_asset)]116 pub type CollectionToForeignReserveLocation<T: Config> =116 pub type CollectionToForeignAsset<T: Config> =117 StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::MultiLocation, OptionQuery>;117 StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::AssetId, OptionQuery>;118118119 /// The correponding NFT token id of reserve NFTs119 /// The correponding NFT token id of reserve NFTs120 #[pallet::storage]120 #[pallet::storage]149 #[pallet::weight(<T as Config>::WeightInfo::register_foreign_asset())]149 #[pallet::weight(<T as Config>::WeightInfo::register_foreign_asset())]150 pub fn force_register_foreign_asset(150 pub fn force_register_foreign_asset(151 origin: OriginFor<T>,151 origin: OriginFor<T>,152 reserve_location: Box<MultiLocation>,152 asset_id: Box<AssetId>,153 name: CollectionName,153 name: CollectionName,154 token_prefix: CollectionTokenPrefix,154 token_prefix: CollectionTokenPrefix,155 mode: ForeignCollectionMode,155 mode: ForeignCollectionMode,156 ) -> DispatchResult {156 ) -> DispatchResult {157 T::ForceRegisterOrigin::ensure_origin(origin.clone())?;157 T::ForceRegisterOrigin::ensure_origin(origin.clone())?;158158159 ensure!(159 if <ForeignReserveLocationToCollection<T>>::contains_key(*reserve_location) {160 !<ForeignAssetToCollection<T>>::contains_key(*asset_id),161 <Error<T>>::ForeignAssetAlreadyRegistered,160 return Err(<Error<T>>::ForeignAssetAlreadyRegistered.into());162 );161 }162163163 let foreign_collection_owner = Self::pallet_account();164 let foreign_collection_owner = Self::pallet_account();164165182183183 properties: vec![Property {184 properties: vec![Property {184 key: Self::reserve_location_property_key(),185 key: Self::reserve_location_property_key(),185 value: reserve_location186 value: asset_id186 .encode()187 .encode()187 .try_into()188 .try_into()188 .expect("multilocation is less than 32k; qed"),189 .expect("multilocation is less than 32k; qed"),204 },205 },205 )?;206 )?;206207207 <ForeignReserveLocationToCollection<T>>::insert(*reserve_location, collection_id);208 <ForeignAssetToCollection<T>>::insert(*asset_id, collection_id);208 <CollectionToForeignReserveLocation<T>>::insert(collection_id, *reserve_location);209 <CollectionToForeignAsset<T>>::insert(collection_id, *asset_id);209210210 Self::deposit_event(Event::<T>::ForeignAssetRegistered {211 Self::deposit_event(Event::<T>::ForeignAssetRegistered {211 asset_id: collection_id,212 collection_id,212 reserve_location,213 asset_id,213 });214 });214215215 Ok(())216 Ok(())237 .expect("key length < max property key length; qed")238 .expect("key length < max property key length; qed")238 }239 }239240240 /// Converts a multilocation to a local collection on Unique Network.241 /// Converts a concrete asset ID (the asset multilocation) to a local collection on Unique Network.241 ///242 ///242 /// The multilocation corresponds to a local collection if:243 /// The multilocation corresponds to a local collection if:243 /// * It is `Here` location that corresponds to the native token of this parachain.244 /// * It is `Here` location that corresponds to the native token of this parachain.249 /// If the multilocation doesn't match the patterns listed above,250 /// If the multilocation doesn't match the patterns listed above,250 /// or the `<Collection ID>` points to a foreign collection,251 /// or the `<Collection ID>` points to a foreign collection,251 /// `None` is returned, identifying that the given multilocation doesn't correspond to a local collection.252 /// `None` is returned, identifying that the given multilocation doesn't correspond to a local collection.252 fn local_asset_location_to_collection(253 fn local_asset_id_to_collection(asset_id: &AssetId) -> Option<CollectionLocality> {253 asset_location: &MultiLocation,254 let AssetId::Concrete(asset_location) = asset_id else {254 ) -> Option<CollectionLocality> {255 return None;256 };257255 let self_location = T::SelfLocation::get();258 let self_location = T::SelfLocation::get();256259264 Some(GeneralIndex(collection_id)) => {267 Some(GeneralIndex(collection_id)) => {265 let collection_id = CollectionId((*collection_id).try_into().ok()?);268 let collection_id = CollectionId((*collection_id).try_into().ok()?);266269267 Self::collection_to_foreign_reserve_location(collection_id)270 Self::collection_to_foreign_asset(collection_id)268 .is_none()271 .is_none()269 .then_some(CollectionLocality::Local(collection_id))272 .then_some(CollectionLocality::Local(collection_id))270 }273 }287 ///290 ///288 /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned.291 /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned.289 fn asset_to_collection(asset_id: &AssetId) -> Result<CollectionLocality, XcmError> {292 fn asset_to_collection(asset_id: &AssetId) -> Result<CollectionLocality, XcmError> {290 let AssetId::Concrete(asset_reserve_location) = asset_id else {291 return Err(XcmExecutorError::AssetNotHandled.into());292 };293294 Self::foreign_reserve_location_to_collection(asset_reserve_location)293 Self::foreign_asset_to_collection(asset_id)295 .map(CollectionLocality::Foreign)294 .map(CollectionLocality::Foreign)296 .or_else(|| Self::local_asset_location_to_collection(asset_reserve_location))295 .or_else(|| Self::local_asset_id_to_collection(asset_id))297 .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into())296 .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into())298 }297 }299298570 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {569 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {571 Some(T::SelfLocation::get())570 Some(T::SelfLocation::get())572 } else {571 } else {573 <Pallet<T>>::collection_to_foreign_reserve_location(collection_id).or_else(|| {572 <Pallet<T>>::collection_to_foreign_asset(collection_id)573 .and_then(|asset_id| match asset_id {574 AssetId::Concrete(location) => Some(location),575 _ => None,576 })577 .or_else(|| {574 T::SelfLocation::get()578 T::SelfLocation::get()575 .pushed_with_interior(GeneralIndex(collection_id.0.into()))579 .pushed_with_interior(GeneralIndex(collection_id.0.into()))