From d1f9838415bf295c6c5cc435d7046c667d4e1792 Mon Sep 17 00:00:00 2001 From: Dusan Morhac <55763425+dudo50@users.noreply.github.com> Date: Thu, 10 Oct 2024 15:35:40 +0200 Subject: [PATCH] Add owneship calls --- templates/parachain/pallets/xcnft/src/lib.rs | 125 +++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/templates/parachain/pallets/xcnft/src/lib.rs b/templates/parachain/pallets/xcnft/src/lib.rs index cfb39f757696..423a88eb3939 100644 --- a/templates/parachain/pallets/xcnft/src/lib.rs +++ b/templates/parachain/pallets/xcnft/src/lib.rs @@ -359,6 +359,38 @@ use frame_system::pallet_prelude::*; owner: T::AccountId, destination: ParaId, }, + + /// Event emitted when cross-chain Collection ownership call transfer fails + ColOwnershipFailedToXCM { + e: SendError, + collection_id: T::CollectionId, + proposed_owner: AccountIdLookupOf, + destination: ParaId, + }, + + /// Event emitted when Collection ownership call is transferred cross-chain + ColOwnershipSent{ + collection_id: T::CollectionId, + proposed_owner: AccountIdLookupOf, + destination: ParaId, + }, + + /// Event emitted when cross-chain NFT ownership call transfer fails + NFTOwnershipFailedToXCM { + e: SendError, + collection_id: T::CollectionId, + asset_id: T::ItemId, + proposed_owner: AccountIdLookupOf, + destination: ParaId, + }, + + /// Event emitted when NFT ownership call is transferred cross-chain + NFTOwnershipSent{ + collection_id: T::CollectionId, + asset_id: T::ItemId, + proposed_owner: AccountIdLookupOf, + destination: ParaId, + }, } /// @@ -1225,5 +1257,98 @@ use frame_system::pallet_prelude::*; Ok(().into()) } + + ///Change collection ownership + #[pallet::call_index(9)] + #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] + pub fn collectionXchangeOwner(origin: OriginFor, destination_collection_id: T::CollectionId, destination_para: ParaId, destination_account: AccountIdLookupOf) -> DispatchResultWithPostInfo { + ensure_signed(origin)?; + + match send_xcm::( + (Parent, Junction::Parachain(destination_para.into())).into(), + Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + Transact { + origin_kind: OriginKind::SovereignAccount, + require_weight_at_most: Weight::from_parts(1_000_000_000, 64 * 1024), + call: >::RuntimeCall::from(pallet_nfts::Call::< + T, + I, + >::transfer_ownership { + collection: destination_collection_id.clone(), + new_owner: destination_account.clone(), + }) + .encode() + .into(), + }, + ]), + ) { + Ok((_hash, _cost)) => { + //Emit event about sucessful metadata send + Self::deposit_event(Event::ColOwnershipSent { + collection_id: destination_collection_id.clone(), + proposed_owner: destination_account.clone(), + destination: destination_para.clone(), + }); + + }, + Err(e) => Self::deposit_event(Event::ColOwnershipFailedToXCM { + e, + collection_id: destination_collection_id.clone(), + proposed_owner: destination_account.clone(), + destination: destination_para.clone(), + }), + } + + Ok(().into()) + } + + ///Change item ownership + #[pallet::call_index(10)] + #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] + pub fn nftXchangeOwner(origin: OriginFor, destination_collection_id: T::CollectionId, destination_asset_id: T::ItemId, destination_para: ParaId, destination_account: AccountIdLookupOf) -> DispatchResultWithPostInfo { + ensure_signed(origin)?; + + match send_xcm::( + (Parent, Junction::Parachain(destination_para.into())).into(), + Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + Transact { + origin_kind: OriginKind::SovereignAccount, + require_weight_at_most: Weight::from_parts(1_000_000_000, 64 * 1024), + call: >::RuntimeCall::from(pallet_nfts::Call::< + T, + I, + >::transfer { + collection: destination_collection_id.clone(), + item: destination_asset_id.clone(), + dest: destination_account.clone(), + }) + .encode() + .into(), + }, + ]), + ) { + Ok((_hash, _cost)) => { + //Emit event about sucessful metadata send + Self::deposit_event(Event::NFTOwnershipSent { + collection_id: destination_collection_id.clone(), + asset_id: destination_asset_id.clone(), + proposed_owner: destination_account.clone(), + destination: destination_para.clone(), + }); + + }, + Err(e) => Self::deposit_event(Event::NFTOwnershipFailedToXCM { + e, + collection_id: destination_collection_id.clone(), + asset_id: destination_asset_id.clone(), + proposed_owner: destination_account.clone(), + destination: destination_para.clone(), + }), + } + + Ok(().into()) + } } }