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
368}368}
369369
370impl<T: Config> pallet_common::XcmExtensions<T> for NativeFungibleHandle<T> {370impl<T: Config> pallet_common::XcmExtensions<T> for NativeFungibleHandle<T> {
371 fn is_foreign(&self) -> bool {
372 false
373 }
374
375 fn create_item_internal(371 fn create_item_internal(
376 &self,372 &self,
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
1088 read_only: flags.external,1088 read_only: flags.external,
10891089
1090 flags: RpcCollectionFlags {1090 flags: RpcCollectionFlags {
1091 foreign: flags.foreign,
1092 erc721metadata: flags.erc721metadata,1091 erc721metadata: flags.erc721metadata,
1093 },1092 },
1094 })1093 })
1128 /// Create new collection.1127 /// Create new collection.
1129 ///1128 ///
1130 /// * `owner` - The owner of the collection.1129 /// * `owner` - The owner of the collection.
1130 /// * `payer` - If set, the user that will pay a deposit for the collection creation.
1131 /// * `data` - Description of the created collection.1131 /// * `data` - Description of the created collection.
1132 /// * `flags` - Extra flags to store.
1133 pub fn init_collection(1132 pub fn init_collection(
1134 owner: T::CrossAccountId,1133 owner: T::CrossAccountId,
1135 payer: T::CrossAccountId,1134 payer: Option<T::CrossAccountId>,
1136 data: CreateCollectionData<T::CrossAccountId>,1135 data: CreateCollectionData<T::CrossAccountId>,
1137 ) -> Result<CollectionId, DispatchError> {1136 ) -> Result<CollectionId, DispatchError> {
1138 ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);1137 ensure!(data.flags.is_allowed_for_user(), <Error<T>>::NoPermission);
11391138
1140 // Take a (non-refundable) deposit of collection creation1139 // Take a (non-refundable) deposit of collection creation
1141 {1140 if let Some(payer) = payer {
1142 let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();1141 let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
1143 imbalance.subsume(<T as Config>::Currency::deposit(1142 imbalance.subsume(<T as Config>::Currency::deposit(
1144 &T::TreasuryAccountId::get(),1143 &T::TreasuryAccountId::get(),
1155 Self::init_collection_internal(owner, data)1154 Self::init_collection_internal(owner, data)
1156 }1155 }
1157
1158 /// Initializes the collection with ForeignCollection flag. Returns [CollectionId] on success, [DispatchError] otherwise.
1159 pub fn init_foreign_collection(
1160 owner: T::CrossAccountId,
1161 mut data: CreateCollectionData<T::CrossAccountId>,
1162 ) -> Result<CollectionId, DispatchError> {
1163 data.flags.foreign = true;
1164 let id = Self::init_collection_internal(owner, data)?;
1165 Ok(id)
1166 }
11671156
1168 fn init_collection_internal(1157 fn init_collection_internal(
1169 owner: T::CrossAccountId,1158 owner: T::CrossAccountId,
2348where2337where
2349 T: Config,2338 T: Config,
2350{2339{
2351 /// Is the collection a foreign one?
2352 fn is_foreign(&self) -> bool;
2353
2354 /// Does the token have children?2340 /// Does the token have children?
2355 fn token_has_children(&self, _token: TokenId) -> bool {2341 fn token_has_children(&self, _token: TokenId) -> bool {
modifiedpallets/foreign-assets/src/lib.rsdiffbeforeafterboth
304 /// If the `asset_instance` is a part of a local collection,304 /// If the `asset_instance` is a part of a local collection,
305 /// the function will return either `Ok(Some(<token ID>))` or an error if the token is not found.305 /// the function will return either `Ok(Some(<token ID>))` or an error if the token is not found.
306 fn asset_instance_to_token_id(306 fn asset_instance_to_token_id(
307 xcm_ext: &dyn XcmExtensions<T>,
308 collection_id: CollectionId,307 collection_id: CollectionId,
309 asset_instance: &AssetInstance,308 asset_instance: &AssetInstance,
310 ) -> Result<Option<TokenId>, XcmError> {309 ) -> Result<Option<TokenId>, XcmError> {
311 if xcm_ext.is_foreign() {310 if <CollectionToForeignReserveLocation<T>>::contains_key(collection_id) {
312 Ok(Self::foreign_reserve_asset_instance_to_token_id(311 Ok(Self::foreign_reserve_asset_instance_to_token_id(
313 collection_id,312 collection_id,
314 asset_instance,313 asset_instance,
363 to: T::CrossAccountId,362 to: T::CrossAccountId,
364 ) -> XcmResult {363 ) -> XcmResult {
365 let deposit_result = if let Some(token_id) =364 let deposit_result = if let Some(token_id) =
366 Self::asset_instance_to_token_id(xcm_ext, collection_id, asset_instance)?365 Self::asset_instance_to_token_id(collection_id, asset_instance)?
367 {366 {
368 let depositor = &Self::pallet_account();367 let depositor = &Self::pallet_account();
369 let from = depositor;368 let from = depositor;
389 asset_instance: &AssetInstance,388 asset_instance: &AssetInstance,
390 from: T::CrossAccountId,389 from: T::CrossAccountId,
391 ) -> XcmResult {390 ) -> XcmResult {
392 let token_id = Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?391 let token_id = Self::asset_instance_to_token_id(collection_id, &asset_instance)?
393 .ok_or(XcmError::AssetNotFound)?;392 .ok_or(XcmError::AssetNotFound)?;
394393
395 if xcm_ext.token_has_children(token_id) {394 if xcm_ext.token_has_children(token_id) {
518517
519 Fungibility::NonFungible(asset_instance) => {518 Fungibility::NonFungible(asset_instance) => {
520 token_id =519 token_id = Self::asset_instance_to_token_id(collection_id, &asset_instance)?
521 Self::asset_instance_to_token_id(xcm_ext, collection_id, &asset_instance)?
522 .ok_or(XcmError::AssetNotFound)?;520 .ok_or(XcmError::AssetNotFound)?;
523521
524 amount = 1;522 amount = 1;
542 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {540 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {
543 Some(Here.into())541 Some(Here.into())
544 } else {542 } else {
545 let dispatch = T::CollectionDispatch::dispatch(collection_id).ok()?;
546 let collection = dispatch.as_dyn();
547 let xcm_ext = collection.xcm_extensions()?;
548
549 if xcm_ext.is_foreign() {
550 <Pallet<T>>::collection_to_foreign_reserve_location(collection_id)543 <Pallet<T>>::collection_to_foreign_reserve_location(collection_id).or_else(|| {
551 } else {
552 T::SelfLocation::get()544 T::SelfLocation::get()
553 .pushed_with_interior(GeneralIndex(collection_id.0.into()))545 .pushed_with_interior(GeneralIndex(collection_id.0.into()))
554 .ok()546 .ok()
555 }547 })
556 }548 }
557 }549 }
558}550}
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
459}459}
460460
461impl<T: Config> XcmExtensions<T> for FungibleHandle<T> {461impl<T: Config> XcmExtensions<T> for FungibleHandle<T> {
462 fn is_foreign(&self) -> bool {
463 self.flags.foreign
464 }
465
466 fn create_item_internal(462 fn create_item_internal(
467 &self,463 &self,
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
572}572}
573573
574impl<T: Config> XcmExtensions<T> for NonfungibleHandle<T> {574impl<T: Config> XcmExtensions<T> for NonfungibleHandle<T> {
575 fn is_foreign(&self) -> bool {
576 self.flags.foreign
577 }
578
579 fn token_has_children(&self, token: TokenId) -> bool {575 fn token_has_children(&self, token: TokenId) -> bool {
580 <Pallet<T>>::token_has_children(self.id, token)576 <Pallet<T>>::token_has_children(self.id, token)
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
306 payer: T::CrossAccountId,306 payer: T::CrossAccountId,
307 data: CreateCollectionData<T::CrossAccountId>,307 data: CreateCollectionData<T::CrossAccountId>,
308 ) -> Result<CollectionId, DispatchError> {308 ) -> Result<CollectionId, DispatchError> {
309 <PalletCommon<T>>::init_collection(owner, payer, data)309 <PalletCommon<T>>::init_collection(owner, Some(payer), data)
310 }310 }
311311
312 /// Destroy RFT collection312 /// 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
86 _ => {}86 _ => {}
87 };87 };
8888
89 <PalletCommon<T>>::init_collection(sender, payer, data)89 <PalletCommon<T>>::init_collection(sender, Some(payer), data)
90 }90 }
9191
92 fn create_foreign(92 fn create_foreign(
106 _ => {}106 _ => {}
107 };107 };
108108
109 let payer = None;
109 <PalletCommon<T>>::init_foreign_collection(sender, data)110 <PalletCommon<T>>::init_collection(sender, payer, data)
110 }111 }
111112
112 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {113 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {