git.delta.rocks / unique-network / refs/commits / f6360ccd4957

difftreelog

fix use CollectionUnknown instead of NotRmrkCollection

Daniel Shiposha2022-05-20parent: #fbd9bd5.patch.diff
in: master

2 files changed

modifiedpallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth
before · pallets/proxy-rmrk-core/src/lib.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/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::{DispatchError, traits::StaticLookup};22use up_data_structs::*;23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};24use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};25use pallet_evm::account::CrossAccountId;2627pub use pallet::*;2829pub mod misc;30pub mod property;3132use misc::*;33pub use property::*;3435#[frame_support::pallet]36pub mod pallet {37    use super::*;38    use pallet_evm::account;3940	#[pallet::config]41	pub trait Config: frame_system::Config42                    + pallet_common::Config43                    + pallet_nonfungible::Config44                    + account::Config {45		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;46	}4748	#[pallet::pallet]49	#[pallet::generate_store(pub(super) trait Store)]50	pub struct Pallet<T>(_);5152	#[pallet::event]53	#[pallet::generate_deposit(pub(super) fn deposit_event)]54	pub enum Event<T: Config> {55        CollectionCreated {56			issuer: T::AccountId,57			collection_id: RmrkCollectionId,58		},59        CollectionDestroyed {60			issuer: T::AccountId,61			collection_id: RmrkCollectionId,62		},63        IssuerChanged {64			old_issuer: T::AccountId,65			new_issuer: T::AccountId,66			collection_id: RmrkCollectionId,67		},68        CollectionLocked {69			issuer: T::AccountId,70			collection_id: RmrkCollectionId,71		},72	}7374	#[pallet::error]75	pub enum Error<T> {76        /* Unique-specific events */77        CorruptedCollectionType,78        NotRmrkCollection,79        RmrkPropertyKeyIsTooLong,80        RmrkPropertyValueIsTooLong,8182        /* RMRK compatible events */83        CollectionNotEmpty,84        NoAvailableCollectionId,85        CollectionUnknown,86	}8788	#[pallet::call]89	impl<T: Config> Pallet<T> {90        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]91		#[transactional]92		pub fn create_collection(93			origin: OriginFor<T>,94			metadata: RmrkString,95			max: Option<u32>,96			symbol: RmrkCollectionSymbol,97		) -> DispatchResult {98            let sender = ensure_signed(origin)?;99100            let limits = max.map(|max| CollectionLimits {101                token_limit: Some(max),102                ..Default::default()103            });104105            let data = CreateCollectionData {106                limits,107                token_prefix: symbol.into_inner()108                    .try_into()109                    .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,110                ..Default::default()111            };112113            let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);114115            if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {116                return Err(<Error<T>>::NoAvailableCollectionId.into());117            }118119            let collection_id = collection_id_res?;120121            let collection = Self::get_nft_collection(collection_id)?.into_inner();122123            <PalletCommon<T>>::set_scoped_collection_properties(124                &collection,125                PropertyScope::Rmrk,126                [127                    rmrk_property!(Config=T, Metadata: metadata)?,128                    rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?,129                ].into_iter()130            )?;131132            Self::deposit_event(Event::CollectionCreated {133                issuer: sender,134                collection_id: collection_id.0135            });136137            Ok(())138        }139140        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]141		#[transactional]142		pub fn destroy_collection(143			origin: OriginFor<T>,144			collection_id: RmrkCollectionId,145		) -> DispatchResult {146            let sender = ensure_signed(origin)?;147            let cross_sender = T::CrossAccountId::from_sub(sender.clone());148149            let unique_collection_id = collection_id.into();150151            let collection = Self::get_nft_collection(unique_collection_id)?;152153            Self::check_collection_type(unique_collection_id, CollectionType::Regular)?;154155            ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);156157            <PalletNft<T>>::destroy_collection(collection, &cross_sender)?;158159            Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });160161            Ok(())162        }163164        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]165		#[transactional]166		pub fn change_collection_issuer(167			origin: OriginFor<T>,168			collection_id: RmrkCollectionId,169			new_issuer: <T::Lookup as StaticLookup>::Source,170		) -> DispatchResult {171            let sender = ensure_signed(origin)?;172173            let new_issuer = T::Lookup::lookup(new_issuer)?;174175            Self::change_collection_owner(176                collection_id.into(),177                CollectionType::Regular,178                sender.clone(),179                new_issuer.clone()180            )?;181182            Self::deposit_event(Event::IssuerChanged {183				old_issuer: sender,184				new_issuer,185				collection_id,186			});187188            Ok(())189        }190191        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]192		#[transactional]193		pub fn lock_collection(194			origin: OriginFor<T>,195			collection_id: RmrkCollectionId,196		) -> DispatchResult {197            let sender = ensure_signed(origin)?;198            let cross_sender = T::CrossAccountId::from_sub(sender.clone());199200            let collection = Self::get_nft_collection(collection_id.into())?;201            collection.check_is_owner(&cross_sender)?;202203            let token_count = collection.total_supply();204205            let mut collection = collection.into_inner();206            collection.limits.token_limit = Some(token_count);207            collection.save()?;208209			Self::deposit_event(Event::CollectionLocked { issuer: sender, collection_id });210211            Ok(())212        }213	}214}215216impl<T: Config> Pallet<T> {217    fn change_collection_owner(218        collection_id: CollectionId,219        collection_type: CollectionType,220        sender: T::AccountId,221        new_owner: T::AccountId,222    ) -> DispatchResult {223        let mut collection = Self::get_nft_collection(collection_id)?.into_inner();224        collection.check_is_owner(&T::CrossAccountId::from_sub(sender))?;225226        Self::check_collection_type(collection_id, collection_type)?;227228        collection.owner = new_owner;229        collection.save()230    }231232    fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {233        let collection = <CollectionHandle<T>>::try_get(collection_id)234            .map_err(|_| <Error<T>>::CollectionUnknown)?235            .into_nft_collection()?;236237        Ok(collection)238    }239240    fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {241        let collection_type: CollectionType = <PalletCommon<T>>::collection_properties(collection_id)242            .get(&rmrk_property!(Config=T, CollectionType)?)243            .ok_or(<Error<T>>::NotRmrkCollection)?244            .try_into()245            .map_err(<Error<T>>::from)?;246247        Ok(collection_type)248    }249250    fn check_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {251        let actual_type = Self::get_collection_type(collection_id)?;252        ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);253254        Ok(())255    }256}
after · pallets/proxy-rmrk-core/src/lib.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/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use frame_support::{pallet_prelude::*, transactional, BoundedVec, dispatch::DispatchResult};20use frame_system::{pallet_prelude::*, ensure_signed};21use sp_runtime::{DispatchError, traits::StaticLookup};22use up_data_structs::*;23use pallet_common::{Pallet as PalletCommon, Error as CommonError, CollectionHandle, CommonCollectionOperations};24use pallet_nonfungible::{Pallet as PalletNft, NonfungibleHandle};25use pallet_evm::account::CrossAccountId;2627pub use pallet::*;2829pub mod misc;30pub mod property;3132use misc::*;33pub use property::*;3435#[frame_support::pallet]36pub mod pallet {37    use super::*;38    use pallet_evm::account;3940	#[pallet::config]41	pub trait Config: frame_system::Config42                    + pallet_common::Config43                    + pallet_nonfungible::Config44                    + account::Config {45		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;46	}4748	#[pallet::pallet]49	#[pallet::generate_store(pub(super) trait Store)]50	pub struct Pallet<T>(_);5152	#[pallet::event]53	#[pallet::generate_deposit(pub(super) fn deposit_event)]54	pub enum Event<T: Config> {55        CollectionCreated {56			issuer: T::AccountId,57			collection_id: RmrkCollectionId,58		},59        CollectionDestroyed {60			issuer: T::AccountId,61			collection_id: RmrkCollectionId,62		},63        IssuerChanged {64			old_issuer: T::AccountId,65			new_issuer: T::AccountId,66			collection_id: RmrkCollectionId,67		},68        CollectionLocked {69			issuer: T::AccountId,70			collection_id: RmrkCollectionId,71		},72	}7374	#[pallet::error]75	pub enum Error<T> {76        /* Unique-specific events */77        CorruptedCollectionType,78        RmrkPropertyKeyIsTooLong,79        RmrkPropertyValueIsTooLong,8081        /* RMRK compatible events */82        CollectionNotEmpty,83        NoAvailableCollectionId,84        CollectionUnknown,85	}8687	#[pallet::call]88	impl<T: Config> Pallet<T> {89        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]90		#[transactional]91		pub fn create_collection(92			origin: OriginFor<T>,93			metadata: RmrkString,94			max: Option<u32>,95			symbol: RmrkCollectionSymbol,96		) -> DispatchResult {97            let sender = ensure_signed(origin)?;9899            let limits = max.map(|max| CollectionLimits {100                token_limit: Some(max),101                ..Default::default()102            });103104            let data = CreateCollectionData {105                limits,106                token_prefix: symbol.into_inner()107                    .try_into()108                    .map_err(|_| <CommonError<T>>::CollectionTokenPrefixLimitExceeded)?,109                ..Default::default()110            };111112            let collection_id_res = <PalletNft<T>>::init_collection(sender.clone(), data);113114            if let Err(DispatchError::Arithmetic(_)) = &collection_id_res {115                return Err(<Error<T>>::NoAvailableCollectionId.into());116            }117118            let collection_id = collection_id_res?;119120            let collection = Self::get_nft_collection(collection_id)?.into_inner();121122            <PalletCommon<T>>::set_scoped_collection_properties(123                &collection,124                PropertyScope::Rmrk,125                [126                    rmrk_property!(Config=T, Metadata: metadata)?,127                    rmrk_property!(Config=T, CollectionType: CollectionType::Regular)?,128                ].into_iter()129            )?;130131            Self::deposit_event(Event::CollectionCreated {132                issuer: sender,133                collection_id: collection_id.0134            });135136            Ok(())137        }138139        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]140		#[transactional]141		pub fn destroy_collection(142			origin: OriginFor<T>,143			collection_id: RmrkCollectionId,144		) -> DispatchResult {145            let sender = ensure_signed(origin)?;146            let cross_sender = T::CrossAccountId::from_sub(sender.clone());147148            let unique_collection_id = collection_id.into();149150            let collection = Self::get_nft_collection(unique_collection_id)?;151152            Self::check_collection_type(unique_collection_id, CollectionType::Regular)?;153154            ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);155156            <PalletNft<T>>::destroy_collection(collection, &cross_sender)?;157158            Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });159160            Ok(())161        }162163        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]164		#[transactional]165		pub fn change_collection_issuer(166			origin: OriginFor<T>,167			collection_id: RmrkCollectionId,168			new_issuer: <T::Lookup as StaticLookup>::Source,169		) -> DispatchResult {170            let sender = ensure_signed(origin)?;171172            let new_issuer = T::Lookup::lookup(new_issuer)?;173174            Self::change_collection_owner(175                collection_id.into(),176                CollectionType::Regular,177                sender.clone(),178                new_issuer.clone()179            )?;180181            Self::deposit_event(Event::IssuerChanged {182				old_issuer: sender,183				new_issuer,184				collection_id,185			});186187            Ok(())188        }189190        #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]191		#[transactional]192		pub fn lock_collection(193			origin: OriginFor<T>,194			collection_id: RmrkCollectionId,195		) -> DispatchResult {196            let sender = ensure_signed(origin)?;197            let cross_sender = T::CrossAccountId::from_sub(sender.clone());198199            let collection = Self::get_nft_collection(collection_id.into())?;200            collection.check_is_owner(&cross_sender)?;201202            let token_count = collection.total_supply();203204            let mut collection = collection.into_inner();205            collection.limits.token_limit = Some(token_count);206            collection.save()?;207208			Self::deposit_event(Event::CollectionLocked { issuer: sender, collection_id });209210            Ok(())211        }212	}213}214215impl<T: Config> Pallet<T> {216    fn change_collection_owner(217        collection_id: CollectionId,218        collection_type: CollectionType,219        sender: T::AccountId,220        new_owner: T::AccountId,221    ) -> DispatchResult {222        let mut collection = Self::get_nft_collection(collection_id)?.into_inner();223        collection.check_is_owner(&T::CrossAccountId::from_sub(sender))?;224225        Self::check_collection_type(collection_id, collection_type)?;226227        collection.owner = new_owner;228        collection.save()229    }230231    fn get_nft_collection(collection_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {232        let collection = <CollectionHandle<T>>::try_get(collection_id)233            .map_err(|_| <Error<T>>::CollectionUnknown)?234            .into_nft_collection()?;235236        Ok(collection)237    }238239    fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {240        let collection_type: CollectionType = <PalletCommon<T>>::collection_properties(collection_id)241            .get(&rmrk_property!(Config=T, CollectionType)?)242            .ok_or(<Error<T>>::CollectionUnknown)?243            .try_into()244            .map_err(<Error<T>>::from)?;245246        Ok(collection_type)247    }248249    fn check_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {250        let actual_type = Self::get_collection_type(collection_id)?;251        ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);252253        Ok(())254    }255}
modifiedpallets/proxy-rmrk-core/src/misc.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-core/src/misc.rs
+++ b/pallets/proxy-rmrk-core/src/misc.rs
@@ -48,7 +48,7 @@
     fn into_nft_collection(self) -> Result<NonfungibleHandle<T>, Error<T>> {
         match self.mode {
             CollectionMode::NFT => Ok(NonfungibleHandle::cast(self)),
-            _ => Err(<Error<T>>::NotRmrkCollection)
+            _ => Err(<Error<T>>::CollectionUnknown)
         }
     }
 }