git.delta.rocks / unique-network / refs/commits / 332bbaac859c

difftreelog

fix use AssetId in the mapping

Daniel Shiposha2023-11-07parent: #0880691.patch.diff
in: master

2 files changed

modifiedpallets/foreign-assets/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/foreign-assets/src/benchmarking.rs
+++ b/pallets/foreign-assets/src/benchmarking.rs
@@ -19,7 +19,7 @@
 use frame_benchmarking::v2::*;
 use frame_system::RawOrigin;
 use pallet_common::benchmarking::{create_data, create_u16_data};
-use sp_std::vec;
+use sp_std::{vec, boxed::Box};
 use staging_xcm::prelude::*;
 use up_data_structs::{MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH};
 
@@ -41,7 +41,7 @@
 		#[extrinsic_call]
 		_(
 			RawOrigin::Root,
-			Box::new(location),
+			Box::new(location.into()),
 			name,
 			token_prefix,
 			mode,
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
99 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 }
106106
107 /// 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>;
112112
113 /// 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>;
118118
119 /// The correponding NFT token id of reserve NFTs119 /// The correponding NFT token id of reserve NFTs
120 #[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())?;
158158
159 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 }
162163
163 let foreign_collection_owner = Self::pallet_account();164 let foreign_collection_owner = Self::pallet_account();
164165
182183
183 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_id
186 .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 )?;
206207
207 <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);
209210
210 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 });
214215
215 Ok(())216 Ok(())
237 .expect("key length < max property key length; qed")238 .expect("key length < max property key length; qed")
238 }239 }
239240
240 /// 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 };
257
255 let self_location = T::SelfLocation::get();258 let self_location = T::SelfLocation::get();
256259
264 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()?);
266269
267 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 };
293
294 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 }
299298
570 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()))