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
--- 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
before · runtime/common/dispatch.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{dispatch::DispatchResult, ensure, fail};18use pallet_balances_adapter::NativeFungibleHandle;19pub use pallet_common::dispatch::CollectionDispatch;20use pallet_common::{21	erc::CommonEvmHandler, eth::map_eth_to_id, unsupported, CollectionById, CollectionHandle,22	CommonCollectionOperations, Pallet as PalletCommon,23};24use pallet_evm::{PrecompileHandle, PrecompileResult};25use pallet_fungible::{FungibleHandle, Pallet as PalletFungible};26use pallet_nonfungible::{NonfungibleHandle, Pallet as PalletNonfungible};27use pallet_refungible::{28	erc_token::RefungibleTokenHandle, Pallet as PalletRefungible, RefungibleHandle,29};30use sp_core::H160;31use sp_runtime::DispatchError;32use sp_std::{borrow::ToOwned, vec::Vec};33use up_data_structs::{34	mapping::TokenAddressMapping, CollectionId, CollectionMode, CreateCollectionData,35	MAX_DECIMAL_POINTS,36};3738pub enum CollectionDispatchT<T>39where40	T: pallet_fungible::Config41		+ pallet_nonfungible::Config42		+ pallet_refungible::Config43		+ pallet_balances_adapter::Config,44{45	Fungible(FungibleHandle<T>),46	Nonfungible(NonfungibleHandle<T>),47	Refungible(RefungibleHandle<T>),48	NativeFungible(NativeFungibleHandle<T>),49}5051impl<T> CollectionDispatch<T> for CollectionDispatchT<T>52where53	T: pallet_common::Config54		+ pallet_unique::Config55		+ pallet_fungible::Config56		+ pallet_nonfungible::Config57		+ pallet_refungible::Config58		+ pallet_balances_adapter::Config,59{60	fn check_is_internal(&self) -> DispatchResult {61		match self {62			Self::Fungible(h) => h.check_is_internal(),63			Self::Nonfungible(h) => h.check_is_internal(),64			Self::Refungible(h) => h.check_is_internal(),65			Self::NativeFungible(h) => h.check_is_internal(),66		}67	}6869	fn create(70		sender: T::CrossAccountId,71		payer: T::CrossAccountId,72		data: CreateCollectionData<T::CrossAccountId>,73	) -> Result<CollectionId, DispatchError> {74		match data.mode {75			CollectionMode::Fungible(decimal_points) => {76				// check params77				ensure!(78					decimal_points <= MAX_DECIMAL_POINTS,79					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded80				);81			}8283			#[cfg(not(feature = "refungible"))]84			CollectionMode::ReFungible => return unsupported!(T),8586			_ => {}87		};8889		<PalletCommon<T>>::init_collection(sender, payer, data)90	}9192	fn create_foreign(93		sender: <T>::CrossAccountId,94		data: CreateCollectionData<<T>::CrossAccountId>,95	) -> Result<CollectionId, DispatchError> {96		match data.mode {97			CollectionMode::Fungible(decimal_points) => {98				// check params99				ensure!(100					decimal_points <= MAX_DECIMAL_POINTS,101					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded102				);103			}104105			CollectionMode::ReFungible => return unsupported!(T),106			_ => {}107		};108109		<PalletCommon<T>>::init_foreign_collection(sender, data)110	}111112	fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {113		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {114			fail!(<pallet_common::Error<T>>::UnsupportedOperation);115		}116117		let collection = <CollectionHandle<T>>::try_get(collection_id)?;118119		match collection.mode {120			CollectionMode::ReFungible => {121				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?122			}123			CollectionMode::Fungible(_) => {124				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?125			}126			CollectionMode::NFT => {127				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?128			}129		}130		Ok(())131	}132133	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {134		if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {135			return Ok(Self::NativeFungible(136				NativeFungibleHandle::new_with_gas_limit(u64::MAX),137			));138		}139140		let handle = <CollectionHandle<T>>::try_get(collection_id)?;141		Ok(match handle.mode {142			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),143			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),144			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),145		})146	}147148	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {149		match self {150			Self::Fungible(h) => h,151			Self::Nonfungible(h) => h,152			Self::Refungible(h) => h,153			Self::NativeFungible(h) => h,154		}155	}156}157158impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>159where160	T: pallet_common::Config161		+ pallet_unique::Config162		+ pallet_fungible::Config163		+ pallet_nonfungible::Config164		+ pallet_refungible::Config165		+ pallet_balances_adapter::Config,166	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,167{168	fn is_reserved(target: &H160) -> bool {169		map_eth_to_id(target).is_some()170	}171	fn is_used(target: &H160) -> bool {172		map_eth_to_id(target)173			.map(<CollectionById<T>>::contains_key)174			.unwrap_or(false)175	}176	fn get_code(target: &H160) -> Option<Vec<u8>> {177		if let Some(collection_id) = map_eth_to_id(target) {178			let collection = <CollectionById<T>>::get(collection_id)?;179			Some(180				match collection.mode {181					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,182					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,183					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,184				}185				.to_owned(),186			)187		} else if let Some((collection_id, _token_id)) =188			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)189		{190			let collection = <CollectionById<T>>::get(collection_id)?;191			if collection.mode != CollectionMode::ReFungible {192				return None;193			}194			// TODO: check token existence195			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())196		} else {197			None198		}199	}200	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {201		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {202			if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {203				<NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)204			} else {205				let collection = <CollectionHandle<T>>::new_with_gas_limit(206					collection_id,207					handle.remaining_gas(),208				)?;209210				match collection.mode {211					CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),212					CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),213					CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),214				}215			}216		} else if let Some((collection_id, token_id)) =217			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(218				&handle.code_address(),219			) {220			let collection =221				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;222			if collection.mode != CollectionMode::ReFungible {223				return None;224			}225226			let h = RefungibleHandle::cast(collection);227			// TODO: check token existence228			RefungibleTokenHandle(h, token_id).call(handle)229		} else {230			None231		}232	}233}