1234567891011121314151617#![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 77 CorruptedCollectionType,78 NotRmrkCollection,79 RmrkPropertyKeyIsTooLong,80 RmrkPropertyValueIsTooLong,8182 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}