From 332bbaac859c102616666327832989fe38739beb Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Tue, 07 Nov 2023 13:31:20 +0000 Subject: [PATCH] fix: use AssetId in the mapping --- --- 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, --- a/pallets/foreign-assets/src/lib.rs +++ b/pallets/foreign-assets/src/lib.rs @@ -99,22 +99,22 @@ pub enum Event { /// The foreign asset registered. ForeignAssetRegistered { - asset_id: CollectionId, - reserve_location: Box, + collection_id: CollectionId, + asset_id: Box, }, } - /// The corresponding collections of reserve locations. + /// The corresponding collections of foreign assets. #[pallet::storage] - #[pallet::getter(fn foreign_reserve_location_to_collection)] - pub type ForeignReserveLocationToCollection = - StorageMap<_, Twox64Concat, staging_xcm::v3::MultiLocation, CollectionId, OptionQuery>; + #[pallet::getter(fn foreign_asset_to_collection)] + pub type ForeignAssetToCollection = + StorageMap<_, Twox64Concat, staging_xcm::v3::AssetId, CollectionId, OptionQuery>; - /// The corresponding reserve location of collections. + /// The corresponding foreign assets of collections. #[pallet::storage] - #[pallet::getter(fn collection_to_foreign_reserve_location)] - pub type CollectionToForeignReserveLocation = - StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::MultiLocation, OptionQuery>; + #[pallet::getter(fn collection_to_foreign_asset)] + pub type CollectionToForeignAsset = + StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::AssetId, OptionQuery>; /// The correponding NFT token id of reserve NFTs #[pallet::storage] @@ -149,16 +149,17 @@ #[pallet::weight(::WeightInfo::register_foreign_asset())] pub fn force_register_foreign_asset( origin: OriginFor, - reserve_location: Box, + asset_id: Box, name: CollectionName, token_prefix: CollectionTokenPrefix, mode: ForeignCollectionMode, ) -> DispatchResult { T::ForceRegisterOrigin::ensure_origin(origin.clone())?; - if >::contains_key(*reserve_location) { - return Err(>::ForeignAssetAlreadyRegistered.into()); - } + ensure!( + !>::contains_key(*asset_id), + >::ForeignAssetAlreadyRegistered, + ); let foreign_collection_owner = Self::pallet_account(); @@ -182,7 +183,7 @@ properties: vec![Property { key: Self::reserve_location_property_key(), - value: reserve_location + value: asset_id .encode() .try_into() .expect("multilocation is less than 32k; qed"), @@ -204,12 +205,12 @@ }, )?; - >::insert(*reserve_location, collection_id); - >::insert(collection_id, *reserve_location); + >::insert(*asset_id, collection_id); + >::insert(collection_id, *asset_id); Self::deposit_event(Event::::ForeignAssetRegistered { - asset_id: collection_id, - reserve_location, + collection_id, + asset_id, }); Ok(()) @@ -237,7 +238,7 @@ .expect("key length < max property key length; qed") } - /// Converts a multilocation to a local collection on Unique Network. + /// Converts a concrete asset ID (the asset multilocation) to a local collection on Unique Network. /// /// The multilocation corresponds to a local collection if: /// * It is `Here` location that corresponds to the native token of this parachain. @@ -249,9 +250,11 @@ /// If the multilocation doesn't match the patterns listed above, /// or the `` points to a foreign collection, /// `None` is returned, identifying that the given multilocation doesn't correspond to a local collection. - fn local_asset_location_to_collection( - asset_location: &MultiLocation, - ) -> Option { + fn local_asset_id_to_collection(asset_id: &AssetId) -> Option { + let AssetId::Concrete(asset_location) = asset_id else { + return None; + }; + let self_location = T::SelfLocation::get(); if *asset_location == Here.into() || *asset_location == self_location { @@ -264,7 +267,7 @@ Some(GeneralIndex(collection_id)) => { let collection_id = CollectionId((*collection_id).try_into().ok()?); - Self::collection_to_foreign_reserve_location(collection_id) + Self::collection_to_foreign_asset(collection_id) .is_none() .then_some(CollectionLocality::Local(collection_id)) } @@ -287,13 +290,9 @@ /// /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned. fn asset_to_collection(asset_id: &AssetId) -> Result { - let AssetId::Concrete(asset_reserve_location) = asset_id else { - return Err(XcmExecutorError::AssetNotHandled.into()); - }; - - Self::foreign_reserve_location_to_collection(asset_reserve_location) + Self::foreign_asset_to_collection(asset_id) .map(CollectionLocality::Foreign) - .or_else(|| Self::local_asset_location_to_collection(asset_reserve_location)) + .or_else(|| Self::local_asset_id_to_collection(asset_id)) .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into()) } @@ -570,11 +569,16 @@ if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID { Some(T::SelfLocation::get()) } else { - >::collection_to_foreign_reserve_location(collection_id).or_else(|| { - T::SelfLocation::get() - .pushed_with_interior(GeneralIndex(collection_id.0.into())) - .ok() - }) + >::collection_to_foreign_asset(collection_id) + .and_then(|asset_id| match asset_id { + AssetId::Concrete(location) => Some(location), + _ => None, + }) + .or_else(|| { + T::SelfLocation::get() + .pushed_with_interior(GeneralIndex(collection_id.0.into())) + .ok() + }) } } } -- gitstuff