git.delta.rocks / unique-network / refs/commits / 383b7efb354e

difftreelog

refactor remove redundant foreign flag, use foreign-assets pallet instead

Daniel Shiposha2023-10-18parent: #68fcead.patch.diff
in: master

8 files changed

modifiedpallets/balances-adapter/src/common.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/common.rs
+++ b/pallets/balances-adapter/src/common.rs
@@ -368,10 +368,6 @@
 }
 
 impl<T: Config> pallet_common::XcmExtensions<T> for NativeFungibleHandle<T> {
-	fn is_foreign(&self) -> bool {
-		false
-	}
-
 	fn create_item_internal(
 		&self,
 		_depositor: &<T>::CrossAccountId,
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1088,7 +1088,6 @@
 			read_only: flags.external,
 
 			flags: RpcCollectionFlags {
-				foreign: flags.foreign,
 				erc721metadata: flags.erc721metadata,
 			},
 		})
@@ -1128,17 +1127,17 @@
 	/// Create new collection.
 	///
 	/// * `owner` - The owner of the collection.
+	/// * `payer` - If set, the user that will pay a deposit for the collection creation.
 	/// * `data` - Description of the created collection.
-	/// * `flags` - Extra flags to store.
 	pub fn init_collection(
 		owner: T::CrossAccountId,
-		payer: T::CrossAccountId,
+		payer: Option<T::CrossAccountId>,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError> {
 		ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
 
 		// Take a (non-refundable) deposit of collection creation
-		{
+		if let Some(payer) = payer {
 			let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
 			imbalance.subsume(<T as Config>::Currency::deposit(
 				&T::TreasuryAccountId::get(),
@@ -1153,16 +1152,6 @@
 		}
 
 		Self::init_collection_internal(owner, data)
-	}
-
-	/// Initializes the collection with ForeignCollection flag. Returns [CollectionId] on success, [DispatchError] otherwise.
-	pub fn init_foreign_collection(
-		owner: T::CrossAccountId,
-		mut data: CreateCollectionData<T::CrossAccountId>,
-	) -> Result<CollectionId, DispatchError> {
-		data.flags.foreign = true;
-		let id = Self::init_collection_internal(owner, data)?;
-		Ok(id)
 	}
 
 	fn init_collection_internal(
@@ -2348,9 +2337,6 @@
 where
 	T: Config,
 {
-	/// Is the collection a foreign one?
-	fn is_foreign(&self) -> bool;
-
 	/// Does the token have children?
 	fn token_has_children(&self, _token: TokenId) -> bool {
 		false
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
--- a/pallets/foreign-assets/src/lib.rs
+++ b/pallets/foreign-assets/src/lib.rs
@@ -304,11 +304,10 @@
 	/// If the `asset_instance` is a part of a local collection,
 	/// the function will return either `Ok(Some(<token ID>))` or an error if the token is not found.
 	fn asset_instance_to_token_id(
-		xcm_ext: &dyn XcmExtensions<T>,
 		collection_id: CollectionId,
 		asset_instance: &AssetInstance,
 	) -> Result<Option<TokenId>, XcmError> {
-		if xcm_ext.is_foreign() {
+		if <CollectionToForeignReserveLocation<T>>::contains_key(collection_id) {
 			Ok(Self::foreign_reserve_asset_instance_to_token_id(
 				collection_id,
 				asset_instance,
@@ -363,7 +362,7 @@
 		to: T::CrossAccountId,
 	) -> XcmResult {
 		let deposit_result = if let Some(token_id) =
-			Self::asset_instance_to_token_id(xcm_ext, collection_id, asset_instance)?
+			Self::asset_instance_to_token_id(collection_id, asset_instance)?
 		{
 			let depositor = &Self::pallet_account();
 			let from = depositor;
@@ -389,7 +388,7 @@
 		asset_instance: &AssetInstance,
 		from: T::CrossAccountId,
 	) -> XcmResult {
-		let token_id = Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?
+		let token_id = Self::asset_instance_to_token_id(collection_id, &asset_instance)?
 			.ok_or(XcmError::AssetNotFound)?;
 
 		if xcm_ext.token_has_children(token_id) {
@@ -517,9 +516,8 @@
 			}
 
 			Fungibility::NonFungible(asset_instance) => {
-				token_id =
-					Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?
-						.ok_or(XcmError::AssetNotFound)?;
+				token_id = Self::asset_instance_to_token_id(collection_id, &asset_instance)?
+					.ok_or(XcmError::AssetNotFound)?;
 
 				amount = 1;
 				map_error = |_| XcmError::FailedToTransactAsset("nonfungible item transfer failed")
@@ -542,17 +540,11 @@
 		if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {
 			Some(Here.into())
 		} else {
-			let dispatch = T::CollectionDispatch::dispatch(collection_id).ok()?;
-			let collection = dispatch.as_dyn();
-			let xcm_ext = collection.xcm_extensions()?;
-
-			if xcm_ext.is_foreign() {
-				<Pallet<T>>::collection_to_foreign_reserve_location(collection_id)
-			} else {
+			<Pallet<T>>::collection_to_foreign_reserve_location(collection_id).or_else(|| {
 				T::SelfLocation::get()
 					.pushed_with_interior(GeneralIndex(collection_id.0.into()))
 					.ok()
-			}
+			})
 		}
 	}
 }
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -459,10 +459,6 @@
 }
 
 impl<T: Config> XcmExtensions<T> for FungibleHandle<T> {
-	fn is_foreign(&self) -> bool {
-		self.flags.foreign
-	}
-
 	fn create_item_internal(
 		&self,
 		depositor: &<T>::CrossAccountId,
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -572,10 +572,6 @@
 }
 
 impl<T: Config> XcmExtensions<T> for NonfungibleHandle<T> {
-	fn is_foreign(&self) -> bool {
-		self.flags.foreign
-	}
-
 	fn token_has_children(&self, token: TokenId) -> bool {
 		<Pallet<T>>::token_has_children(self.id, token)
 	}
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -306,7 +306,7 @@
 		payer: T::CrossAccountId,
 		data: CreateCollectionData<T::CrossAccountId>,
 	) -> Result<CollectionId, DispatchError> {
-		<PalletCommon<T>>::init_collection(owner, payer, data)
+		<PalletCommon<T>>::init_collection(owner, Some(payer), data)
 	}
 
 	/// Destroy RFT collection
modifiedprimitives/data-structs/src/lib.rsdiffbeforeafterboth
378#[derive(AbiCoderFlags, Bitfields, Clone, Copy, PartialEq, Eq, Debug, Default)]378#[derive(AbiCoderFlags, Bitfields, Clone, Copy, PartialEq, Eq, Debug, Default)]
379#[bondrewd(enforce_bytes = 1)]379#[bondrewd(enforce_bytes = 1)]
380pub struct CollectionFlags {380pub struct CollectionFlags {
381 /// Tokens in foreign collections can be transferred, but not burnt381 /// Reserved flag
382 #[bondrewd(bits = "0..1")]382 #[bondrewd(bits = "0..1")]
383 pub foreign: bool,383 pub reserved_0: bool,
384 /// Supports ERC721Metadata384 /// Supports ERC721Metadata
385 #[bondrewd(bits = "1..2")]385 #[bondrewd(bits = "1..2")]
386 pub erc721metadata: bool,386 pub erc721metadata: bool,
395395
396impl CollectionFlags {396impl CollectionFlags {
397 pub fn is_allowed_for_user(self) -> bool {397 pub fn is_allowed_for_user(self) -> bool {
398 !self.foreign && !self.external && self.reserved == 0398 !self.reserved_0 && !self.external && self.reserved == 0
399 }399 }
400}400}
401401
461461
462#[derive(Debug, Encode, Decode, Clone, PartialEq, TypeInfo, Serialize, Deserialize)]462#[derive(Debug, Encode, Decode, Clone, PartialEq, TypeInfo, Serialize, Deserialize)]
463pub struct RpcCollectionFlags {463pub struct RpcCollectionFlags {
464 /// Is collection is foreign.
465 pub foreign: bool,
466 /// Collection supports ERC721Metadata.464 /// Collection supports ERC721Metadata.
467 pub erc721metadata: bool,465 pub erc721metadata: bool,
468}466}
505 pub read_only: bool,503 pub read_only: bool,
506504
507 /// Extra collection flags505 /// Extra collection flags
508 #[version(2.., upper(RpcCollectionFlags {foreign: false, erc721metadata: false}))]506 #[version(2.., upper(RpcCollectionFlags {erc721metadata: false}))]
509 pub flags: RpcCollectionFlags,507 pub flags: RpcCollectionFlags,
510}508}
511509
542 read_only: true,540 read_only: true,
543541
544 flags: RpcCollectionFlags {542 flags: RpcCollectionFlags {
545 foreign: false,
546 erc721metadata: false,543 erc721metadata: false,
547 },544 },
548 }545 }
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
--- a/runtime/common/dispatch.rs
+++ b/runtime/common/dispatch.rs
@@ -86,7 +86,7 @@
 			_ => {}
 		};
 
-		<PalletCommon<T>>::init_collection(sender, payer, data)
+		<PalletCommon<T>>::init_collection(sender, Some(payer), data)
 	}
 
 	fn create_foreign(
@@ -106,7 +106,8 @@
 			_ => {}
 		};
 
-		<PalletCommon<T>>::init_foreign_collection(sender, data)
+		let payer = None;
+		<PalletCommon<T>>::init_collection(sender, payer, data)
 	}
 
 	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {