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

difftreelog

source

pallets/proxy-rmrk-core/src/lib.rs8.8 KiBsourcehistory
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    pub 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    pub fn get_collection_property(collection_id: CollectionId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {240        let collection_property = <PalletCommon<T>>::collection_properties(collection_id)241            .get(&rmrk_property!(Config=T, key)?)242            .ok_or(<Error<T>>::CollectionUnknown)?243            .clone();244245        Ok(collection_property)246    }247248    pub fn get_collection_type(collection_id: CollectionId) -> Result<CollectionType, DispatchError> {249        let value = Self::get_collection_property(collection_id, RmrkProperty::CollectionType)?;250        let collection_type: CollectionType = (&value)251            .try_into()252            .map_err(<Error<T>>::from)?;253254        Ok(collection_type)255    }256257    pub fn get_nft_property(collection_id: CollectionId, nft_id: TokenId, key: RmrkProperty) -> Result<PropertyValue, DispatchError> {258        let nft_property = <PalletNft<T>>::token_properties((collection_id, nft_id))259            .get(&rmrk_property!(Config=T, key)?)260            .ok_or(<Error<T>>::CollectionUnknown)? // todo is that right?261            .clone();262263        Ok(nft_property)264    }265266    pub fn check_collection_type(collection_id: CollectionId, collection_type: CollectionType) -> DispatchResult {267        let actual_type = Self::get_collection_type(collection_id)?;268        ensure!(actual_type == collection_type, <CommonError<T>>::NoPermission);269270        Ok(())271    }272}