difftreelog
refactor remove redundant foreign flag, use foreign-assets pallet instead
in: master
8 files changed
pallets/balances-adapter/src/common.rsdiffbeforeafterboth368}368}369369370impl<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 false373 }374375 fn create_item_internal(371 fn create_item_internal(376 &self,372 &self,pallets/common/src/lib.rsdiffbeforeafterboth1088 read_only: flags.external,1088 read_only: flags.external,108910891090 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);113911381140 // Take a (non-refundable) deposit of collection creation1139 // Take a (non-refundable) deposit of collection creation1141 {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 }11571158 /// 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 }116711561168 fn init_collection_internal(1157 fn init_collection_internal(1169 owner: T::CrossAccountId,1158 owner: T::CrossAccountId,2348where2337where2349 T: Config,2338 T: Config,2350{2339{2351 /// Is the collection a foreign one?2352 fn is_foreign(&self) -> bool;23532354 /// 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 {pallets/foreign-assets/src/lib.rsdiffbeforeafterboth304 /// 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)?;394393395 if xcm_ext.token_has_children(token_id) {394 if xcm_ext.token_has_children(token_id) {518517519 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)?;523521524 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()?;548549 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}pallets/fungible/src/common.rsdiffbeforeafterboth459}459}460460461impl<T: Config> XcmExtensions<T> for FungibleHandle<T> {461impl<T: Config> XcmExtensions<T> for FungibleHandle<T> {462 fn is_foreign(&self) -> bool {463 self.flags.foreign464 }465466 fn create_item_internal(462 fn create_item_internal(467 &self,463 &self,pallets/nonfungible/src/common.rsdiffbeforeafterboth572}572}573573574impl<T: Config> XcmExtensions<T> for NonfungibleHandle<T> {574impl<T: Config> XcmExtensions<T> for NonfungibleHandle<T> {575 fn is_foreign(&self) -> bool {576 self.flags.foreign577 }578579 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)pallets/refungible/src/lib.rsdiffbeforeafterboth306 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 }311311312 /// Destroy RFT collection312 /// Destroy RFT collectionprimitives/data-structs/src/lib.rsdiffbeforeafterboth378#[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 flag382 #[bondrewd(bits = "0..1")]382 #[bondrewd(bits = "0..1")]383 pub foreign: bool,383 pub reserved_0: bool,384 /// Supports ERC721Metadata384 /// Supports ERC721Metadata385 #[bondrewd(bits = "1..2")]385 #[bondrewd(bits = "1..2")]386 pub erc721metadata: bool,386 pub erc721metadata: bool,395395396impl 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 == 0399 }399 }400}400}401401461461462#[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,506504507 /// Extra collection flags505 /// Extra collection flags508 #[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}511509542 read_only: true,540 read_only: true,543541544 flags: RpcCollectionFlags {542 flags: RpcCollectionFlags {545 foreign: false,546 erc721metadata: false,543 erc721metadata: false,547 },544 },548 }545 }runtime/common/dispatch.rsdiffbeforeafterboth86 _ => {}86 _ => {}87 };87 };888889 <PalletCommon<T>>::init_collection(sender, payer, data)89 <PalletCommon<T>>::init_collection(sender, Some(payer), data)90 }90 }919192 fn create_foreign(92 fn create_foreign(106 _ => {}106 _ => {}107 };107 };108108109 let payer = None;109 <PalletCommon<T>>::init_foreign_collection(sender, data)110 <PalletCommon<T>>::init_collection(sender, payer, data)110 }111 }111112112 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {113 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {