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
before · pallets/balances-adapter/src/common.rs
1use alloc::{vec, vec::Vec};2use core::marker::PhantomData;34use frame_support::{5	ensure, fail,6	traits::tokens::{fungible::Mutate, Fortitude, Precision},7	weights::Weight,8};9use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};10use pallet_common::{11	erc::CrossAccountId, CommonCollectionOperations, CommonWeightInfo, Error as CommonError,12};13use up_data_structs::{budget::Budget, TokenId};1415use crate::{Config, NativeFungibleHandle, Pallet};1617pub struct CommonWeights<T: Config>(PhantomData<T>);1819// All implementations with `Weight::default` used in methods that return error `UnsupportedOperation`.20impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {21	fn create_multiple_items(_amount: &[up_data_structs::CreateItemData]) -> Weight {22		Weight::default()23	}2425	fn create_multiple_items_ex(26		_cost: &up_data_structs::CreateItemExData<T::CrossAccountId>,27	) -> Weight {28		Weight::default()29	}3031	fn burn_item() -> Weight {32		Weight::default()33	}3435	fn set_collection_properties(_amount: u32) -> Weight {36		Weight::default()37	}3839	fn set_token_properties(_amount: u32) -> Weight {40		Weight::default()41	}4243	fn set_token_property_permissions(_amount: u32) -> Weight {44		Weight::default()45	}4647	fn transfer() -> Weight {48		<BalancesWeight<T> as WeightInfo>::transfer_allow_death()49	}5051	fn approve() -> Weight {52		Weight::default()53	}5455	fn approve_from() -> Weight {56		Weight::default()57	}5859	fn transfer_from() -> Weight {60		<BalancesWeight<T> as WeightInfo>::transfer_allow_death()61	}6263	fn burn_from() -> Weight {64		Weight::default()65	}6667	fn set_allowance_for_all() -> Weight {68		Weight::default()69	}7071	fn force_repair_item() -> Weight {72		Weight::default()73	}74}7576/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallet77/// methods and adds weight info.78impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {79	fn create_item(80		&self,81		_sender: <T>::CrossAccountId,82		_to: <T>::CrossAccountId,83		_data: up_data_structs::CreateItemData,84		_nesting_budget: &dyn up_data_structs::budget::Budget,85	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {86		fail!(<CommonError<T>>::UnsupportedOperation);87	}8889	fn create_multiple_items(90		&self,91		_sender: <T>::CrossAccountId,92		_to: <T>::CrossAccountId,93		_data: Vec<up_data_structs::CreateItemData>,94		_nesting_budget: &dyn up_data_structs::budget::Budget,95	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {96		fail!(<CommonError<T>>::UnsupportedOperation);97	}9899	fn create_multiple_items_ex(100		&self,101		_sender: <T>::CrossAccountId,102		_data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,103		_nesting_budget: &dyn up_data_structs::budget::Budget,104	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {105		fail!(<CommonError<T>>::UnsupportedOperation);106	}107108	fn burn_item(109		&self,110		_sender: <T>::CrossAccountId,111		_token: TokenId,112		_amount: u128,113	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {114		fail!(<CommonError<T>>::UnsupportedOperation);115	}116117	fn set_collection_properties(118		&self,119		_sender: <T>::CrossAccountId,120		_properties: Vec<up_data_structs::Property>,121	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {122		fail!(<CommonError<T>>::UnsupportedOperation);123	}124125	fn delete_collection_properties(126		&self,127		_sender: &<T>::CrossAccountId,128		_property_keys: Vec<up_data_structs::PropertyKey>,129	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {130		fail!(<CommonError<T>>::UnsupportedOperation);131	}132133	fn set_token_properties(134		&self,135		_sender: <T>::CrossAccountId,136		_token_id: TokenId,137		_properties: Vec<up_data_structs::Property>,138		_budget: &dyn up_data_structs::budget::Budget,139	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {140		fail!(<CommonError<T>>::UnsupportedOperation);141	}142143	fn delete_token_properties(144		&self,145		_sender: <T>::CrossAccountId,146		_token_id: TokenId,147		_property_keys: Vec<up_data_structs::PropertyKey>,148		_budget: &dyn up_data_structs::budget::Budget,149	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {150		fail!(<CommonError<T>>::UnsupportedOperation);151	}152153	fn get_token_properties_raw(154		&self,155		_token_id: TokenId,156	) -> Option<up_data_structs::TokenProperties> {157		// No token properties are defined on fungibles158		None159	}160161	fn set_token_properties_raw(&self, _token_id: TokenId, _map: up_data_structs::TokenProperties) {162		// No token properties are defined on fungibles163	}164165	fn set_token_property_permissions(166		&self,167		_sender: &<T>::CrossAccountId,168		_property_permissions: Vec<up_data_structs::PropertyKeyPermission>,169	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {170		fail!(<CommonError<T>>::UnsupportedOperation);171	}172173	fn transfer(174		&self,175		sender: <T>::CrossAccountId,176		to: <T>::CrossAccountId,177		token: TokenId,178		amount: u128,179		_budget: &dyn up_data_structs::budget::Budget,180	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {181		ensure!(182			token == TokenId::default(),183			<CommonError<T>>::FungibleItemsHaveNoId184		);185186		<Pallet<T>>::transfer(&sender, &to, amount)187	}188189	fn approve(190		&self,191		_sender: <T>::CrossAccountId,192		_spender: <T>::CrossAccountId,193		_token: TokenId,194		_amount: u128,195	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {196		fail!(<CommonError<T>>::UnsupportedOperation);197	}198199	fn approve_from(200		&self,201		_sender: <T>::CrossAccountId,202		_from: <T>::CrossAccountId,203		_to: <T>::CrossAccountId,204		_token: TokenId,205		_amount: u128,206	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {207		fail!(<CommonError<T>>::UnsupportedOperation);208	}209210	fn transfer_from(211		&self,212		sender: <T>::CrossAccountId,213		from: <T>::CrossAccountId,214		to: <T>::CrossAccountId,215		token: TokenId,216		amount: u128,217		budget: &dyn up_data_structs::budget::Budget,218	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {219		ensure!(220			token == TokenId::default(),221			<CommonError<T>>::FungibleItemsHaveNoId222		);223224		<Pallet<T>>::transfer_from(&sender, &from, &to, amount, budget)225	}226227	fn burn_from(228		&self,229		_sender: <T>::CrossAccountId,230		_from: <T>::CrossAccountId,231		_token: TokenId,232		_amount: u128,233		_budget: &dyn up_data_structs::budget::Budget,234	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {235		fail!(<CommonError<T>>::UnsupportedOperation);236	}237238	fn check_nesting(239		&self,240		_sender: &<T>::CrossAccountId,241		_from: (up_data_structs::CollectionId, TokenId),242		_under: TokenId,243		_budget: &dyn up_data_structs::budget::Budget,244	) -> frame_support::sp_runtime::DispatchResult {245		fail!(<CommonError<T>>::UnsupportedOperation);246	}247248	fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}249250	fn unnest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}251252	fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<TokenId> {253		let balance = <Pallet<T>>::total_balance(&account);254		if balance != 0 {255			vec![TokenId::default()]256		} else {257			vec![]258		}259	}260261	fn collection_tokens(&self) -> Vec<TokenId> {262		vec![TokenId::default()]263	}264265	fn token_exists(&self, token: TokenId) -> bool {266		token == TokenId::default()267	}268269	fn last_token_id(&self) -> TokenId {270		TokenId::default()271	}272273	fn token_owner(274		&self,275		_token: TokenId,276	) -> Result<<T>::CrossAccountId, up_data_structs::TokenOwnerError> {277		Err(up_data_structs::TokenOwnerError::MultipleOwners)278	}279280	fn check_token_indirect_owner(281		&self,282		_token: TokenId,283		_maybe_owner: &<T>::CrossAccountId,284		_nesting_budget: &dyn up_data_structs::budget::Budget,285	) -> Result<bool, frame_support::sp_runtime::DispatchError> {286		Ok(false)287	}288289	fn token_owners(&self, _token: TokenId) -> Vec<<T>::CrossAccountId> {290		vec![]291	}292293	fn token_property(294		&self,295		_token_id: TokenId,296		_key: &up_data_structs::PropertyKey,297	) -> Option<up_data_structs::PropertyValue> {298		None299	}300301	fn token_properties(302		&self,303		_token: TokenId,304		_keys: Option<Vec<up_data_structs::PropertyKey>>,305	) -> Vec<up_data_structs::Property> {306		vec![]307	}308309	fn total_supply(&self) -> u32 {310		1311	}312313	fn account_balance(&self, account: T::CrossAccountId) -> u32 {314		let balance = <Pallet<T>>::balance_of(&account);315		(balance != 0).into()316	}317318	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {319		if token != TokenId::default() {320			return 0;321		}322		<Pallet<T>>::balance_of(&account)323	}324325	fn total_pieces(&self, token: TokenId) -> Option<u128> {326		if token != TokenId::default() {327			return None;328		}329		Some(<Pallet<T>>::total_issuance())330	}331332	fn allowance(333		&self,334		_sender: <T>::CrossAccountId,335		_spender: <T>::CrossAccountId,336		_token: TokenId,337	) -> u128 {338		0339	}340341	fn xcm_extensions(&self) -> Option<&dyn pallet_common::XcmExtensions<T>> {342		Some(self)343	}344345	fn set_allowance_for_all(346		&self,347		_owner: <T>::CrossAccountId,348		_operator: <T>::CrossAccountId,349		_approve: bool,350	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {351		fail!(<CommonError<T>>::UnsupportedOperation);352	}353354	fn allowance_for_all(355		&self,356		_owner: <T>::CrossAccountId,357		_operator: <T>::CrossAccountId,358	) -> bool {359		false360	}361362	fn repair_item(363		&self,364		_token: TokenId,365	) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {366		fail!(<CommonError<T>>::UnsupportedOperation);367	}368}369370impl<T: Config> pallet_common::XcmExtensions<T> for NativeFungibleHandle<T> {371	fn is_foreign(&self) -> bool {372		false373	}374375	fn create_item_internal(376		&self,377		_depositor: &<T>::CrossAccountId,378		to: <T>::CrossAccountId,379		data: up_data_structs::CreateItemData,380		_nesting_budget: &dyn Budget,381	) -> Result<TokenId, sp_runtime::DispatchError> {382		match &data {383			up_data_structs::CreateItemData::Fungible(fungible_data) => {384				T::Mutate::mint_into(385					to.as_sub(),386					fungible_data387						.value388						.try_into()389						.map_err(|_| sp_runtime::ArithmeticError::Overflow)?,390				)?;391392				Ok(TokenId::default())393			}394			_ => {395				fail!(<CommonError<T>>::NotFungibleDataUsedToMintFungibleCollectionToken)396			}397		}398	}399400	fn transfer_item_internal(401		&self,402		_depositor: &<T>::CrossAccountId,403		from: &<T>::CrossAccountId,404		to: &<T>::CrossAccountId,405		token: TokenId,406		amount: u128,407		_nesting_budget: &dyn Budget,408	) -> sp_runtime::DispatchResult {409		ensure!(410			token == TokenId::default(),411			<CommonError<T>>::FungibleItemsHaveNoId412		);413414		<Pallet<T>>::transfer(from, to, amount)415			.map(|_| ())416			.map_err(|post_info| post_info.error)417	}418419	fn burn_item_internal(420		&self,421		from: T::CrossAccountId,422		token: TokenId,423		amount: u128,424	) -> sp_runtime::DispatchResult {425		ensure!(426			token == TokenId::default(),427			<CommonError<T>>::FungibleItemsHaveNoId428		);429430		T::Mutate::burn_from(431			from.as_sub(),432			amount433				.try_into()434				.map_err(|_| sp_runtime::ArithmeticError::Overflow)?,435			Precision::Exact,436			Fortitude::Polite,437		)?;438439		Ok(())440	}441}
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
--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -378,9 +378,9 @@
 #[derive(AbiCoderFlags, Bitfields, Clone, Copy, PartialEq, Eq, Debug, Default)]
 #[bondrewd(enforce_bytes = 1)]
 pub struct CollectionFlags {
-	/// Tokens in foreign collections can be transferred, but not burnt
+	/// Reserved flag
 	#[bondrewd(bits = "0..1")]
-	pub foreign: bool,
+	pub reserved_0: bool,
 	/// Supports ERC721Metadata
 	#[bondrewd(bits = "1..2")]
 	pub erc721metadata: bool,
@@ -395,7 +395,7 @@
 
 impl CollectionFlags {
 	pub fn is_allowed_for_user(self) -> bool {
-		!self.foreign && !self.external && self.reserved == 0
+		!self.reserved_0 && !self.external && self.reserved == 0
 	}
 }
 
@@ -461,8 +461,6 @@
 
 #[derive(Debug, Encode, Decode, Clone, PartialEq, TypeInfo, Serialize, Deserialize)]
 pub struct RpcCollectionFlags {
-	/// Is collection is foreign.
-	pub foreign: bool,
 	/// Collection supports ERC721Metadata.
 	pub erc721metadata: bool,
 }
@@ -505,7 +503,7 @@
 	pub read_only: bool,
 
 	/// Extra collection flags
-	#[version(2.., upper(RpcCollectionFlags {foreign: false, erc721metadata: false}))]
+	#[version(2.., upper(RpcCollectionFlags {erc721metadata: false}))]
 	pub flags: RpcCollectionFlags,
 }
 
@@ -542,7 +540,6 @@
 			read_only: true,
 
 			flags: RpcCollectionFlags {
-				foreign: false,
 				erc721metadata: false,
 			},
 		}
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 {