difftreelog
fix destroy only not empty collections
in: master
4 files changed
pallets/common/src/lib.rsdiffbeforeafterboth1// 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)]1819extern crate alloc;2021use core::ops::{Deref, DerefMut};22use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};23use sp_std::vec::Vec;24use pallet_evm::account::CrossAccountId;25use frame_support::{26 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},27 ensure,28 traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},29 BoundedVec,30 weights::Pays,31 transactional,32};33use pallet_evm::GasWeightMapping;34use up_data_structs::{35 COLLECTION_NUMBER_LIMIT,36 Collection,37 RpcCollection,38 CollectionId,39 CreateItemData,40 MAX_TOKEN_PREFIX_LENGTH,41 COLLECTION_ADMINS_LIMIT,42 TokenId,43 CollectionStats,44 MAX_TOKEN_OWNERSHIP,45 CollectionMode,46 NFT_SPONSOR_TRANSFER_TIMEOUT,47 FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,48 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,49 MAX_SPONSOR_TIMEOUT,50 CUSTOM_DATA_LIMIT,51 CollectionLimits,52 CreateCollectionData,53 SponsorshipState,54 CreateItemExData,55 SponsoringRateLimit,56 budget::Budget,57 COLLECTION_FIELD_LIMIT,58 PhantomType,59 Property,60 Properties,61 PropertiesPermissionMap,62 PropertyKey,63 PropertyValue,64 PropertyPermission,65 PropertiesError,66 PropertyKeyPermission,67 TokenData,68 TrySetProperty,69 PropertyScope,70 // RMRK71 RmrkCollectionInfo,72 RmrkInstanceInfo,73 RmrkResourceInfo,74 RmrkPropertyInfo,75 RmrkBaseInfo,76 RmrkPartType,77 RmrkTheme,78 RmrkNftChild,79 CollectionPermissions,80 SchemaVersion,81};8283pub use pallet::*;84use sp_core::H160;85use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};86#[cfg(feature = "runtime-benchmarks")]87pub mod benchmarking;88pub mod dispatch;89pub mod erc;90pub mod eth;91pub mod weights;9293pub type SelfWeightOf<T> = <T as Config>::WeightInfo;9495#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]96pub struct CollectionHandle<T: Config> {97 pub id: CollectionId,98 collection: Collection<T::AccountId>,99 pub recorder: SubstrateRecorder<T>,100}101impl<T: Config> WithRecorder<T> for CollectionHandle<T> {102 fn recorder(&self) -> &SubstrateRecorder<T> {103 &self.recorder104 }105 fn into_recorder(self) -> SubstrateRecorder<T> {106 self.recorder107 }108}109impl<T: Config> CollectionHandle<T> {110 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {111 <CollectionById<T>>::get(id).map(|collection| Self {112 id,113 collection,114 recorder: SubstrateRecorder::new(gas_limit),115 })116 }117 pub fn new(id: CollectionId) -> Option<Self> {118 Self::new_with_gas_limit(id, u64::MAX)119 }120 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {121 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)122 }123 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {124 self.recorder125 .consume_gas(T::GasWeightMapping::weight_to_gas(126 <T as frame_system::Config>::DbWeight::get()127 .read128 .saturating_mul(reads),129 ))130 }131 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {132 self.recorder133 .consume_gas(T::GasWeightMapping::weight_to_gas(134 <T as frame_system::Config>::DbWeight::get()135 .write136 .saturating_mul(writes),137 ))138 }139 pub fn save(self) -> DispatchResult {140 <CollectionById<T>>::insert(self.id, self.collection);141 Ok(())142 }143}144impl<T: Config> Deref for CollectionHandle<T> {145 type Target = Collection<T::AccountId>;146147 fn deref(&self) -> &Self::Target {148 &self.collection149 }150}151152impl<T: Config> DerefMut for CollectionHandle<T> {153 fn deref_mut(&mut self) -> &mut Self::Target {154 &mut self.collection155 }156}157158impl<T: Config> CollectionHandle<T> {159 pub fn check_is_owner(&self, subject: &T::CrossAccountId) -> DispatchResult {160 ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);161 Ok(())162 }163 pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {164 *subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))165 }166 pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {167 ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);168 Ok(())169 }170 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {171 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)172 }173 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {174 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)175 }176 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {177 ensure!(178 <Allowlist<T>>::get((self.id, user)),179 <Error<T>>::AddressNotInAllowlist180 );181 Ok(())182 }183}184185#[frame_support::pallet]186pub mod pallet {187 use super::*;188 use pallet_evm::account;189 use dispatch::CollectionDispatch;190 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};191 use frame_system::pallet_prelude::*;192 use frame_support::traits::Currency;193 use up_data_structs::{TokenId, mapping::TokenAddressMapping};194 use scale_info::TypeInfo;195 use weights::WeightInfo;196197 #[pallet::config]198 pub trait Config:199 frame_system::Config + pallet_evm_coder_substrate::Config + TypeInfo + account::Config200 {201 type WeightInfo: WeightInfo;202 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;203204 type Currency: Currency<Self::AccountId>;205206 #[pallet::constant]207 type CollectionCreationPrice: Get<208 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,209 >;210 type CollectionDispatch: CollectionDispatch<Self>;211212 type TreasuryAccountId: Get<Self::AccountId>;213214 type EvmTokenAddressMapping: TokenAddressMapping<H160>;215 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;216 }217218 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);219220 #[pallet::pallet]221 #[pallet::storage_version(STORAGE_VERSION)]222 #[pallet::generate_store(pub(super) trait Store)]223 pub struct Pallet<T>(_);224225 #[pallet::extra_constants]226 impl<T: Config> Pallet<T> {227 pub fn collection_admins_limit() -> u32 {228 COLLECTION_ADMINS_LIMIT229 }230 }231232 #[pallet::event]233 #[pallet::generate_deposit(pub fn deposit_event)]234 pub enum Event<T: Config> {235 /// New collection was created236 ///237 /// # Arguments238 ///239 /// * collection_id: Globally unique identifier of newly created collection.240 ///241 /// * mode: [CollectionMode] converted into u8.242 ///243 /// * account_id: Collection owner.244 CollectionCreated(CollectionId, u8, T::AccountId),245246 /// New collection was destroyed247 ///248 /// # Arguments249 ///250 /// * collection_id: Globally unique identifier of collection.251 CollectionDestroyed(CollectionId),252253 /// New item was created.254 ///255 /// # Arguments256 ///257 /// * collection_id: Id of the collection where item was created.258 ///259 /// * item_id: Id of an item. Unique within the collection.260 ///261 /// * recipient: Owner of newly created item262 ///263 /// * amount: Always 1 for NFT264 ItemCreated(CollectionId, TokenId, T::CrossAccountId, u128),265266 /// Collection item was burned.267 ///268 /// # Arguments269 ///270 /// * collection_id.271 ///272 /// * item_id: Identifier of burned NFT.273 ///274 /// * owner: which user has destroyed its tokens275 ///276 /// * amount: Always 1 for NFT277 ItemDestroyed(CollectionId, TokenId, T::CrossAccountId, u128),278279 /// Item was transferred280 ///281 /// * collection_id: Id of collection to which item is belong282 ///283 /// * item_id: Id of an item284 ///285 /// * sender: Original owner of item286 ///287 /// * recipient: New owner of item288 ///289 /// * amount: Always 1 for NFT290 Transfer(291 CollectionId,292 TokenId,293 T::CrossAccountId,294 T::CrossAccountId,295 u128,296 ),297298 /// * collection_id299 ///300 /// * item_id301 ///302 /// * sender303 ///304 /// * spender305 ///306 /// * amount307 Approved(308 CollectionId,309 TokenId,310 T::CrossAccountId,311 T::CrossAccountId,312 u128,313 ),314315 CollectionPropertySet(CollectionId, PropertyKey),316317 CollectionPropertyDeleted(CollectionId, PropertyKey),318319 TokenPropertySet(CollectionId, TokenId, PropertyKey),320321 TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),322323 PropertyPermissionSet(CollectionId, PropertyKey),324 }325326 #[pallet::error]327 pub enum Error<T> {328 /// This collection does not exist.329 CollectionNotFound,330 /// Sender parameter and item owner must be equal.331 MustBeTokenOwner,332 /// No permission to perform action333 NoPermission,334 /// Collection is not in mint mode.335 PublicMintingNotAllowed,336 /// Address is not in allow list.337 AddressNotInAllowlist,338339 /// Collection name can not be longer than 63 char.340 CollectionNameLimitExceeded,341 /// Collection description can not be longer than 255 char.342 CollectionDescriptionLimitExceeded,343 /// Token prefix can not be longer than 15 char.344 CollectionTokenPrefixLimitExceeded,345 /// Total collections bound exceeded.346 TotalCollectionsLimitExceeded,347 /// Exceeded max admin count348 CollectionAdminCountExceeded,349 /// Collection limit bounds per collection exceeded350 CollectionLimitBoundsExceeded,351 /// Tried to enable permissions which are only permitted to be disabled352 OwnerPermissionsCantBeReverted,353 /// Collection settings not allowing items transferring354 TransferNotAllowed,355 /// Account token limit exceeded per collection356 AccountTokenLimitExceeded,357 /// Collection token limit exceeded358 CollectionTokenLimitExceeded,359 /// Metadata flag frozen360 MetadataFlagFrozen,361362 /// Item not exists.363 TokenNotFound,364 /// Item balance not enough.365 TokenValueTooLow,366 /// Requested value more than approved.367 ApprovedValueTooLow,368 /// Tried to approve more than owned369 CantApproveMoreThanOwned,370371 /// Can't transfer tokens to ethereum zero address372 AddressIsZero,373 /// Target collection doesn't supports this operation374 UnsupportedOperation,375376 /// Not sufficient founds to perform action377 NotSufficientFounds,378379 /// Collection has nesting disabled380 NestingIsDisabled,381 /// Only owner may nest tokens under this collection382 OnlyOwnerAllowedToNest,383 /// Only tokens from specific collections may nest tokens under this384 SourceCollectionIsNotAllowedToNest,385386 /// Tried to store more data than allowed in collection field387 CollectionFieldSizeExceeded,388389 /// Tried to store more property data than allowed390 NoSpaceForProperty,391392 /// Tried to store more property keys than allowed393 PropertyLimitReached,394395 /// Property key is too long396 PropertyKeyIsTooLong,397398 /// Only ASCII letters, digits, and '_', '-' are allowed399 InvalidCharacterInPropertyKey,400401 /// Empty property keys are forbidden402 EmptyPropertyKey,403 }404405 #[pallet::storage]406 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;407 #[pallet::storage]408 pub type DestroyedCollectionCount<T> =409 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;410411 /// Collection info412 #[pallet::storage]413 pub type CollectionById<T> = StorageMap<414 Hasher = Blake2_128Concat,415 Key = CollectionId,416 Value = Collection<<T as frame_system::Config>::AccountId>,417 QueryKind = OptionQuery,418 >;419420 /// Collection properties421 #[pallet::storage]422 #[pallet::getter(fn collection_properties)]423 pub type CollectionProperties<T> = StorageMap<424 Hasher = Blake2_128Concat,425 Key = CollectionId,426 Value = Properties,427 QueryKind = ValueQuery,428 OnEmpty = up_data_structs::CollectionProperties,429 >;430431 #[pallet::storage]432 #[pallet::getter(fn property_permissions)]433 pub type CollectionPropertyPermissions<T> = StorageMap<434 Hasher = Blake2_128Concat,435 Key = CollectionId,436 Value = PropertiesPermissionMap,437 QueryKind = ValueQuery,438 >;439440 #[pallet::storage]441 pub type AdminAmount<T> = StorageMap<442 Hasher = Blake2_128Concat,443 Key = CollectionId,444 Value = u32,445 QueryKind = ValueQuery,446 >;447448 /// List of collection admins449 #[pallet::storage]450 pub type IsAdmin<T: Config> = StorageNMap<451 Key = (452 Key<Blake2_128Concat, CollectionId>,453 Key<Blake2_128Concat, T::CrossAccountId>,454 ),455 Value = bool,456 QueryKind = ValueQuery,457 >;458459 /// Allowlisted collection users460 #[pallet::storage]461 pub type Allowlist<T: Config> = StorageNMap<462 Key = (463 Key<Blake2_128Concat, CollectionId>,464 Key<Blake2_128Concat, T::CrossAccountId>,465 ),466 Value = bool,467 QueryKind = ValueQuery,468 >;469470 /// Not used by code, exists only to provide some types to metadata471 #[pallet::storage]472 pub type DummyStorageValue<T: Config> = StorageValue<473 Value = (474 CollectionStats,475 CollectionId,476 TokenId,477 PhantomType<TokenData<T::CrossAccountId>>,478 PhantomType<RpcCollection<T::AccountId>>,479 // RMRK480 PhantomType<RmrkCollectionInfo<T::AccountId>>,481 PhantomType<RmrkInstanceInfo<T::AccountId>>,482 PhantomType<RmrkResourceInfo>,483 PhantomType<RmrkPropertyInfo>,484 PhantomType<RmrkBaseInfo<T::AccountId>>,485 PhantomType<RmrkPartType>,486 PhantomType<RmrkTheme>,487 PhantomType<RmrkNftChild>,488 ),489 QueryKind = OptionQuery,490 >;491492 #[pallet::hooks]493 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {494 fn on_runtime_upgrade() -> Weight {495 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {496 use up_data_structs::{CollectionVersion1, CollectionVersion2};497 <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {498 let mut props = Vec::new();499 if !v.offchain_schema.is_empty() {500 props.push(Property {501 key: b"_old_offchainSchema".to_vec().try_into().unwrap(),502 value: v.offchain_schema.clone().into_inner().try_into().expect("offchain schema too big"),503 });504 }505 if !v.variable_on_chain_schema.is_empty() {506 props.push(Property {507 key: b"_old_variableOnChainSchema".to_vec().try_into().unwrap(),508 value: v.variable_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),509 });510 }511 if !v.const_on_chain_schema.is_empty() {512 props.push(Property {513 key: b"_old_constOnChainSchema".to_vec().try_into().unwrap(),514 value: v.const_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),515 });516 }517 props.push(Property {518 key: b"_old_schemaVersion".to_vec().try_into().unwrap(),519 value: match v.schema_version {520 SchemaVersion::ImageURL => b"ImageUrl".as_slice(),521 SchemaVersion::Unique => b"Unique".as_slice(),522 }.to_vec().try_into().unwrap(),523 });524 Self::set_scoped_collection_properties(525 id,526 PropertyScope::None,527 props.into_iter(),528 ).expect("existing data larger than properties");529 let mut new = CollectionVersion2::from(v.clone());530 new.permissions.access = Some(v.access);531 new.permissions.mint_mode = Some(v.mint_mode);532 Some(new)533 });534 }535536 0537 }538 }539}540541impl<T: Config> Pallet<T> {542 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens543 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {544 ensure!(545 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,546 <Error<T>>::AddressIsZero547 );548 Ok(())549 }550 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {551 <IsAdmin<T>>::iter_prefix((collection,))552 .map(|(a, _)| a)553 .collect()554 }555 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {556 <Allowlist<T>>::iter_prefix((collection,))557 .map(|(a, _)| a)558 .collect()559 }560 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {561 <Allowlist<T>>::get((collection, user))562 }563 pub fn collection_stats() -> CollectionStats {564 let created = <CreatedCollectionCount<T>>::get();565 let destroyed = <DestroyedCollectionCount<T>>::get();566 CollectionStats {567 created: created.0,568 destroyed: destroyed.0,569 alive: created.0 - destroyed.0,570 }571 }572573 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {574 let collection = <CollectionById<T>>::get(collection);575 if collection.is_none() {576 return None;577 }578579 let collection = collection.unwrap();580 let limits = collection.limits;581 let effective_limits = CollectionLimits {582 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),583 sponsored_data_size: Some(limits.sponsored_data_size()),584 sponsored_data_rate_limit: Some(585 limits586 .sponsored_data_rate_limit587 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),588 ),589 token_limit: Some(limits.token_limit()),590 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(591 match collection.mode {592 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,593 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,594 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,595 },596 )),597 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),598 owner_can_transfer: Some(limits.owner_can_transfer()),599 owner_can_destroy: Some(limits.owner_can_destroy()),600 transfers_enabled: Some(limits.transfers_enabled()),601 };602603 Some(effective_limits)604 }605606 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {607 let Collection {608 name,609 description,610 owner,611 mode,612 token_prefix,613 sponsorship,614 limits,615 permissions,616 } = <CollectionById<T>>::get(collection)?;617618 let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)619 .into_iter()620 .map(|(key, permission)| PropertyKeyPermission {621 key,622 permission,623 })624 .collect();625626 let properties = <CollectionProperties<T>>::get(collection)627 .into_iter()628 .map(|(key, value)| Property {629 key,630 value,631 })632 .collect();633634 Some(RpcCollection {635 name: name.into_inner(),636 description: description.into_inner(),637 owner,638 mode,639 token_prefix: token_prefix.into_inner(),640 sponsorship,641 limits,642 permissions,643 token_property_permissions,644 properties,645 })646 }647}648649macro_rules! limit_default {650 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{651 $(652 if let Some($new) = $new.$field {653 let $old = $old.$field($($arg)?);654 let _ = $new;655 let _ = $old;656 $check657 } else {658 $new.$field = $old.$field659 }660 )*661 }};662}663macro_rules! limit_default_clone {664 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{665 $(666 if let Some($new) = $new.$field.clone() {667 let $old = $old.$field($($arg)?);668 let _ = $new;669 let _ = $old;670 $check671 } else {672 $new.$field = $old.$field.clone()673 }674 )*675 }};676}677678impl<T: Config> Pallet<T> {679 pub fn init_collection(680 owner: T::AccountId,681 data: CreateCollectionData<T::AccountId>,682 ) -> Result<CollectionId, DispatchError> {683 {684 ensure!(685 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,686 Error::<T>::CollectionTokenPrefixLimitExceeded687 );688 }689690 let created_count = <CreatedCollectionCount<T>>::get()691 .0692 .checked_add(1)693 .ok_or(ArithmeticError::Overflow)?;694 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;695 let id = CollectionId(created_count);696697 // bound Total number of collections698 ensure!(699 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,700 <Error<T>>::TotalCollectionsLimitExceeded701 );702703 // =========704705 let collection = Collection {706 owner: owner.clone(),707 name: data.name,708 mode: data.mode.clone(),709 description: data.description,710 token_prefix: data.token_prefix,711 sponsorship: data712 .pending_sponsor713 .map(SponsorshipState::Unconfirmed)714 .unwrap_or_default(),715 limits: data716 .limits717 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))718 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,719 permissions: data720 .permissions721 .map(|permissions| Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions))722 .unwrap_or_else(|| Ok(CollectionPermissions::default()))?,723 };724725 let mut collection_properties = up_data_structs::CollectionProperties::get();726 collection_properties727 .try_set_from_iter(data.properties.into_iter())728 .map_err(<Error<T>>::from)?;729730 CollectionProperties::<T>::insert(id, collection_properties);731732 let mut token_props_permissions = PropertiesPermissionMap::new();733 token_props_permissions734 .try_set_from_iter(data.token_property_permissions.into_iter())735 .map_err(<Error<T>>::from)?;736737 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);738739 // Take a (non-refundable) deposit of collection creation740 {741 let mut imbalance =742 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();743 imbalance.subsume(744 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(745 &T::TreasuryAccountId::get(),746 T::CollectionCreationPrice::get(),747 ),748 );749 <T as Config>::Currency::settle(750 &owner,751 imbalance,752 WithdrawReasons::TRANSFER,753 ExistenceRequirement::KeepAlive,754 )755 .map_err(|_| Error::<T>::NotSufficientFounds)?;756 }757758 <CreatedCollectionCount<T>>::put(created_count);759 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));760 <CollectionById<T>>::insert(id, collection);761 Ok(id)762 }763764 pub fn destroy_collection(765 collection: CollectionHandle<T>,766 sender: &T::CrossAccountId,767 ) -> DispatchResult {768 ensure!(769 collection.limits.owner_can_destroy(),770 <Error<T>>::NoPermission,771 );772 collection.check_is_owner(sender)?;773774 let destroyed_collections = <DestroyedCollectionCount<T>>::get()775 .0776 .checked_add(1)777 .ok_or(ArithmeticError::Overflow)?;778779 // =========780781 <DestroyedCollectionCount<T>>::put(destroyed_collections);782 <CollectionById<T>>::remove(collection.id);783 <AdminAmount<T>>::remove(collection.id);784 <IsAdmin<T>>::remove_prefix((collection.id,), None);785 <Allowlist<T>>::remove_prefix((collection.id,), None);786 <CollectionProperties<T>>::remove(collection.id);787788 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));789 Ok(())790 }791792 pub fn set_collection_property(793 collection: &CollectionHandle<T>,794 sender: &T::CrossAccountId,795 property: Property,796 ) -> DispatchResult {797 collection.check_is_owner_or_admin(sender)?;798799 CollectionProperties::<T>::try_mutate(collection.id, |properties| {800 let property = property.clone();801 properties.try_set(property.key, property.value)802 })803 .map_err(<Error<T>>::from)?;804805 Self::deposit_event(Event::CollectionPropertySet(collection.id, property.key));806807 Ok(())808 }809810 pub fn set_scoped_collection_property(811 collection_id: CollectionId,812 scope: PropertyScope,813 property: Property,814 ) -> DispatchResult {815 CollectionProperties::<T>::try_mutate(collection_id, |properties| {816 properties.try_scoped_set(scope, property.key, property.value)817 })818 .map_err(<Error<T>>::from)?;819820 Ok(())821 }822823 pub fn set_scoped_collection_properties(824 collection_id: CollectionId,825 scope: PropertyScope,826 properties: impl Iterator<Item = Property>,827 ) -> DispatchResult {828 CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {829 stored_properties.try_scoped_set_from_iter(scope, properties)830 })831 .map_err(<Error<T>>::from)?;832833 Ok(())834 }835836 #[transactional]837 pub fn set_collection_properties(838 collection: &CollectionHandle<T>,839 sender: &T::CrossAccountId,840 properties: Vec<Property>,841 ) -> DispatchResult {842 for property in properties {843 Self::set_collection_property(collection, sender, property)?;844 }845846 Ok(())847 }848849 pub fn delete_collection_property(850 collection: &CollectionHandle<T>,851 sender: &T::CrossAccountId,852 property_key: PropertyKey,853 ) -> DispatchResult {854 collection.check_is_owner_or_admin(sender)?;855856 CollectionProperties::<T>::try_mutate(collection.id, |properties| {857 properties.remove(&property_key)858 })859 .map_err(<Error<T>>::from)?;860861 Self::deposit_event(Event::CollectionPropertyDeleted(862 collection.id,863 property_key,864 ));865866 Ok(())867 }868869 #[transactional]870 pub fn delete_collection_properties(871 collection: &CollectionHandle<T>,872 sender: &T::CrossAccountId,873 property_keys: Vec<PropertyKey>,874 ) -> DispatchResult {875 for key in property_keys {876 Self::delete_collection_property(collection, sender, key)?;877 }878879 Ok(())880 }881882 // For migrations883 pub fn set_property_permission_unchecked(884 collection: CollectionId,885 property_permission: PropertyKeyPermission,886 ) -> DispatchResult {887 <CollectionPropertyPermissions<T>>::try_mutate(collection, |permissions| {888 permissions.try_set(property_permission.key, property_permission.permission)889 })890 .map_err(<Error<T>>::from)?;891 Ok(())892 }893894 pub fn set_property_permission(895 collection: &CollectionHandle<T>,896 sender: &T::CrossAccountId,897 property_permission: PropertyKeyPermission,898 ) -> DispatchResult {899 collection.check_is_owner_or_admin(sender)?;900901 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);902 let current_permission = all_permissions.get(&property_permission.key);903 if matches![904 current_permission,905 Some(PropertyPermission { mutable: false, .. })906 ] {907 return Err(<Error<T>>::NoPermission.into());908 }909910 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {911 let property_permission = property_permission.clone();912 permissions.try_set(property_permission.key, property_permission.permission)913 })914 .map_err(<Error<T>>::from)?;915916 Self::deposit_event(Event::PropertyPermissionSet(917 collection.id,918 property_permission.key,919 ));920921 Ok(())922 }923924 #[transactional]925 pub fn set_property_permissions(926 collection: &CollectionHandle<T>,927 sender: &T::CrossAccountId,928 property_permissions: Vec<PropertyKeyPermission>,929 ) -> DispatchResult {930 for prop_pemission in property_permissions {931 Self::set_property_permission(collection, sender, prop_pemission)?;932 }933934 Ok(())935 }936937 pub fn get_collection_property(938 collection_id: CollectionId,939 key: &PropertyKey,940 ) -> Option<PropertyValue> {941 Self::collection_properties(collection_id).get(key).cloned()942 }943944 pub fn bytes_keys_to_property_keys(945 keys: Vec<Vec<u8>>,946 ) -> Result<Vec<PropertyKey>, DispatchError> {947 keys.into_iter()948 .map(|key| -> Result<PropertyKey, DispatchError> {949 key.try_into()950 .map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())951 })952 .collect::<Result<Vec<PropertyKey>, DispatchError>>()953 }954955 pub fn filter_collection_properties(956 collection_id: CollectionId,957 keys: Option<Vec<PropertyKey>>,958 ) -> Result<Vec<Property>, DispatchError> {959 let properties = Self::collection_properties(collection_id);960961 let properties = keys962 .map(|keys| {963 keys.into_iter()964 .filter_map(|key| {965 properties.get(&key).map(|value| Property {966 key,967 value: value.clone(),968 })969 })970 .collect()971 })972 .unwrap_or_else(|| {973 properties974 .into_iter()975 .map(|(key, value)| Property {976 key,977 value,978 })979 .collect()980 });981982 Ok(properties)983 }984985 pub fn filter_property_permissions(986 collection_id: CollectionId,987 keys: Option<Vec<PropertyKey>>,988 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {989 let permissions = Self::property_permissions(collection_id);990991 let key_permissions = keys992 .map(|keys| {993 keys.into_iter()994 .filter_map(|key| {995 permissions996 .get(&key)997 .map(|permission| PropertyKeyPermission {998 key,999 permission: permission.clone(),1000 })1001 })1002 .collect()1003 })1004 .unwrap_or_else(|| {1005 permissions1006 .into_iter()1007 .map(|(key, permission)| PropertyKeyPermission {1008 key,1009 permission,1010 })1011 .collect()1012 });10131014 Ok(key_permissions)1015 }10161017 pub fn toggle_allowlist(1018 collection: &CollectionHandle<T>,1019 sender: &T::CrossAccountId,1020 user: &T::CrossAccountId,1021 allowed: bool,1022 ) -> DispatchResult {1023 collection.check_is_owner_or_admin(sender)?;10241025 // =========10261027 if allowed {1028 <Allowlist<T>>::insert((collection.id, user), true);1029 } else {1030 <Allowlist<T>>::remove((collection.id, user));1031 }10321033 Ok(())1034 }10351036 pub fn toggle_admin(1037 collection: &CollectionHandle<T>,1038 sender: &T::CrossAccountId,1039 user: &T::CrossAccountId,1040 admin: bool,1041 ) -> DispatchResult {1042 collection.check_is_owner_or_admin(sender)?;10431044 let was_admin = <IsAdmin<T>>::get((collection.id, user));1045 if was_admin == admin {1046 return Ok(());1047 }1048 let amount = <AdminAmount<T>>::get(collection.id);10491050 if admin {1051 let amount = amount1052 .checked_add(1)1053 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1054 ensure!(1055 amount <= Self::collection_admins_limit(),1056 <Error<T>>::CollectionAdminCountExceeded,1057 );10581059 // =========10601061 <AdminAmount<T>>::insert(collection.id, amount);1062 <IsAdmin<T>>::insert((collection.id, user), true);1063 } else {1064 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));1065 <IsAdmin<T>>::remove((collection.id, user));1066 }10671068 Ok(())1069 }10701071 pub fn clamp_limits(1072 mode: CollectionMode,1073 old_limit: &CollectionLimits,1074 mut new_limit: CollectionLimits,1075 ) -> Result<CollectionLimits, DispatchError> {1076 limit_default!(old_limit, new_limit,1077 account_token_ownership_limit => ensure!(1078 new_limit <= MAX_TOKEN_OWNERSHIP,1079 <Error<T>>::CollectionLimitBoundsExceeded,1080 ),1081 sponsor_transfer_timeout(match mode {1082 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1083 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1084 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1085 }) => ensure!(1086 new_limit <= MAX_SPONSOR_TIMEOUT,1087 <Error<T>>::CollectionLimitBoundsExceeded,1088 ),1089 sponsored_data_size => ensure!(1090 new_limit <= CUSTOM_DATA_LIMIT,1091 <Error<T>>::CollectionLimitBoundsExceeded,1092 ),1093 token_limit => ensure!(1094 old_limit >= new_limit && new_limit > 0,1095 <Error<T>>::CollectionTokenLimitExceeded1096 ),1097 owner_can_transfer => ensure!(1098 old_limit || !new_limit,1099 <Error<T>>::OwnerPermissionsCantBeReverted,1100 ),1101 owner_can_destroy => ensure!(1102 old_limit || !new_limit,1103 <Error<T>>::OwnerPermissionsCantBeReverted,1104 ),1105 sponsored_data_rate_limit => {},1106 transfers_enabled => {},1107 );1108 Ok(new_limit)1109 }1110 pub fn clamp_permissions(1111 mode: CollectionMode,1112 old_limit: &CollectionPermissions,1113 mut new_limit: CollectionPermissions,1114 ) -> Result<CollectionPermissions, DispatchError> {1115 limit_default_clone!(old_limit, new_limit,1116 );1117 Ok(new_limit)1118 }1119}11201121#[macro_export]1122macro_rules! unsupported {1123 () => {1124 Err(<Error<T>>::UnsupportedOperation.into())1125 };1126}11271128/// Worst cases1129pub trait CommonWeightInfo<CrossAccountId> {1130 fn create_item() -> Weight;1131 fn create_multiple_items(amount: &[CreateItemData]) -> Weight;1132 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;1133 fn burn_item() -> Weight;1134 fn set_collection_properties(amount: u32) -> Weight;1135 fn delete_collection_properties(amount: u32) -> Weight;1136 fn set_token_properties(amount: u32) -> Weight;1137 fn delete_token_properties(amount: u32) -> Weight;1138 fn set_property_permissions(amount: u32) -> Weight;1139 fn transfer() -> Weight;1140 fn approve() -> Weight;1141 fn transfer_from() -> Weight;1142 fn burn_from() -> Weight;1143}11441145pub trait CommonCollectionOperations<T: Config> {1146 fn create_item(1147 &self,1148 sender: T::CrossAccountId,1149 to: T::CrossAccountId,1150 data: CreateItemData,1151 nesting_budget: &dyn Budget,1152 ) -> DispatchResultWithPostInfo;1153 fn create_multiple_items(1154 &self,1155 sender: T::CrossAccountId,1156 to: T::CrossAccountId,1157 data: Vec<CreateItemData>,1158 nesting_budget: &dyn Budget,1159 ) -> DispatchResultWithPostInfo;1160 fn create_multiple_items_ex(1161 &self,1162 sender: T::CrossAccountId,1163 data: CreateItemExData<T::CrossAccountId>,1164 nesting_budget: &dyn Budget,1165 ) -> DispatchResultWithPostInfo;1166 fn burn_item(1167 &self,1168 sender: T::CrossAccountId,1169 token: TokenId,1170 amount: u128,1171 ) -> DispatchResultWithPostInfo;1172 fn set_collection_properties(1173 &self,1174 sender: T::CrossAccountId,1175 properties: Vec<Property>,1176 ) -> DispatchResultWithPostInfo;1177 fn delete_collection_properties(1178 &self,1179 sender: &T::CrossAccountId,1180 property_keys: Vec<PropertyKey>,1181 ) -> DispatchResultWithPostInfo;1182 fn set_token_properties(1183 &self,1184 sender: T::CrossAccountId,1185 token_id: TokenId,1186 property: Vec<Property>,1187 ) -> DispatchResultWithPostInfo;1188 fn delete_token_properties(1189 &self,1190 sender: T::CrossAccountId,1191 token_id: TokenId,1192 property_keys: Vec<PropertyKey>,1193 ) -> DispatchResultWithPostInfo;1194 fn set_property_permissions(1195 &self,1196 sender: &T::CrossAccountId,1197 property_permissions: Vec<PropertyKeyPermission>,1198 ) -> DispatchResultWithPostInfo;1199 fn transfer(1200 &self,1201 sender: T::CrossAccountId,1202 to: T::CrossAccountId,1203 token: TokenId,1204 amount: u128,1205 nesting_budget: &dyn Budget,1206 ) -> DispatchResultWithPostInfo;1207 fn approve(1208 &self,1209 sender: T::CrossAccountId,1210 spender: T::CrossAccountId,1211 token: TokenId,1212 amount: u128,1213 ) -> DispatchResultWithPostInfo;1214 fn transfer_from(1215 &self,1216 sender: T::CrossAccountId,1217 from: T::CrossAccountId,1218 to: T::CrossAccountId,1219 token: TokenId,1220 amount: u128,1221 nesting_budget: &dyn Budget,1222 ) -> DispatchResultWithPostInfo;1223 fn burn_from(1224 &self,1225 sender: T::CrossAccountId,1226 from: T::CrossAccountId,1227 token: TokenId,1228 amount: u128,1229 nesting_budget: &dyn Budget,1230 ) -> DispatchResultWithPostInfo;12311232 fn check_nesting(1233 &self,1234 sender: T::CrossAccountId,1235 from: (CollectionId, TokenId),1236 under: TokenId,1237 budget: &dyn Budget,1238 ) -> DispatchResult;12391240 fn nest(1241 &self,1242 under: TokenId,1243 to_nest: (CollectionId, TokenId)1244 );12451246 fn unnest(1247 &self,1248 under: TokenId,1249 to_nest: (CollectionId, TokenId)1250 );12511252 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;1253 fn collection_tokens(&self) -> Vec<TokenId>;1254 fn token_exists(&self, token: TokenId) -> bool;1255 fn last_token_id(&self) -> TokenId;12561257 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1258 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue>;1259 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property>;1260 /// Amount of unique collection tokens1261 fn total_supply(&self) -> u32;1262 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1263 fn account_balance(&self, account: T::CrossAccountId) -> u32;1264 /// Amount of specific token account have (Applicable to fungible/refungible)1265 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;1266 fn allowance(1267 &self,1268 sender: T::CrossAccountId,1269 spender: T::CrossAccountId,1270 token: TokenId,1271 ) -> u128;1272}12731274// Flexible enough for implementing CommonCollectionOperations1275pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {1276 let post_info = PostDispatchInfo {1277 actual_weight: Some(weight),1278 pays_fee: Pays::Yes,1279 };1280 match res {1281 Ok(()) => Ok(post_info),1282 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),1283 }1284}12851286impl<T: Config> From<PropertiesError> for Error<T> {1287 fn from(error: PropertiesError) -> Self {1288 match error {1289 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,1290 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,1291 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,1292 PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,1293 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,1294 }1295 }1296}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)]1819extern crate alloc;2021use core::ops::{Deref, DerefMut};22use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};23use sp_std::vec::Vec;24use pallet_evm::account::CrossAccountId;25use frame_support::{26 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},27 ensure,28 traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},29 BoundedVec,30 weights::Pays,31 transactional,32};33use pallet_evm::GasWeightMapping;34use up_data_structs::{35 COLLECTION_NUMBER_LIMIT,36 Collection,37 RpcCollection,38 CollectionId,39 CreateItemData,40 MAX_TOKEN_PREFIX_LENGTH,41 COLLECTION_ADMINS_LIMIT,42 TokenId,43 CollectionStats,44 MAX_TOKEN_OWNERSHIP,45 CollectionMode,46 NFT_SPONSOR_TRANSFER_TIMEOUT,47 FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,48 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,49 MAX_SPONSOR_TIMEOUT,50 CUSTOM_DATA_LIMIT,51 CollectionLimits,52 CreateCollectionData,53 SponsorshipState,54 CreateItemExData,55 SponsoringRateLimit,56 budget::Budget,57 COLLECTION_FIELD_LIMIT,58 PhantomType,59 Property,60 Properties,61 PropertiesPermissionMap,62 PropertyKey,63 PropertyValue,64 PropertyPermission,65 PropertiesError,66 PropertyKeyPermission,67 TokenData,68 TrySetProperty,69 PropertyScope,70 // RMRK71 RmrkCollectionInfo,72 RmrkInstanceInfo,73 RmrkResourceInfo,74 RmrkPropertyInfo,75 RmrkBaseInfo,76 RmrkPartType,77 RmrkTheme,78 RmrkNftChild,79 CollectionPermissions,80 SchemaVersion,81};8283pub use pallet::*;84use sp_core::H160;85use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};86#[cfg(feature = "runtime-benchmarks")]87pub mod benchmarking;88pub mod dispatch;89pub mod erc;90pub mod eth;91pub mod weights;9293pub type SelfWeightOf<T> = <T as Config>::WeightInfo;9495#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]96pub struct CollectionHandle<T: Config> {97 pub id: CollectionId,98 collection: Collection<T::AccountId>,99 pub recorder: SubstrateRecorder<T>,100}101impl<T: Config> WithRecorder<T> for CollectionHandle<T> {102 fn recorder(&self) -> &SubstrateRecorder<T> {103 &self.recorder104 }105 fn into_recorder(self) -> SubstrateRecorder<T> {106 self.recorder107 }108}109impl<T: Config> CollectionHandle<T> {110 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {111 <CollectionById<T>>::get(id).map(|collection| Self {112 id,113 collection,114 recorder: SubstrateRecorder::new(gas_limit),115 })116 }117 pub fn new(id: CollectionId) -> Option<Self> {118 Self::new_with_gas_limit(id, u64::MAX)119 }120 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {121 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)122 }123 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {124 self.recorder125 .consume_gas(T::GasWeightMapping::weight_to_gas(126 <T as frame_system::Config>::DbWeight::get()127 .read128 .saturating_mul(reads),129 ))130 }131 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {132 self.recorder133 .consume_gas(T::GasWeightMapping::weight_to_gas(134 <T as frame_system::Config>::DbWeight::get()135 .write136 .saturating_mul(writes),137 ))138 }139 pub fn save(self) -> DispatchResult {140 <CollectionById<T>>::insert(self.id, self.collection);141 Ok(())142 }143}144impl<T: Config> Deref for CollectionHandle<T> {145 type Target = Collection<T::AccountId>;146147 fn deref(&self) -> &Self::Target {148 &self.collection149 }150}151152impl<T: Config> DerefMut for CollectionHandle<T> {153 fn deref_mut(&mut self) -> &mut Self::Target {154 &mut self.collection155 }156}157158impl<T: Config> CollectionHandle<T> {159 pub fn check_is_owner(&self, subject: &T::CrossAccountId) -> DispatchResult {160 ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);161 Ok(())162 }163 pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {164 *subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))165 }166 pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {167 ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);168 Ok(())169 }170 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {171 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)172 }173 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {174 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)175 }176 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {177 ensure!(178 <Allowlist<T>>::get((self.id, user)),179 <Error<T>>::AddressNotInAllowlist180 );181 Ok(())182 }183}184185#[frame_support::pallet]186pub mod pallet {187 use super::*;188 use pallet_evm::account;189 use dispatch::CollectionDispatch;190 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};191 use frame_system::pallet_prelude::*;192 use frame_support::traits::Currency;193 use up_data_structs::{TokenId, mapping::TokenAddressMapping};194 use scale_info::TypeInfo;195 use weights::WeightInfo;196197 #[pallet::config]198 pub trait Config:199 frame_system::Config + pallet_evm_coder_substrate::Config + TypeInfo + account::Config200 {201 type WeightInfo: WeightInfo;202 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;203204 type Currency: Currency<Self::AccountId>;205206 #[pallet::constant]207 type CollectionCreationPrice: Get<208 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,209 >;210 type CollectionDispatch: CollectionDispatch<Self>;211212 type TreasuryAccountId: Get<Self::AccountId>;213214 type EvmTokenAddressMapping: TokenAddressMapping<H160>;215 type CrossTokenAddressMapping: TokenAddressMapping<Self::CrossAccountId>;216 }217218 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);219220 #[pallet::pallet]221 #[pallet::storage_version(STORAGE_VERSION)]222 #[pallet::generate_store(pub(super) trait Store)]223 pub struct Pallet<T>(_);224225 #[pallet::extra_constants]226 impl<T: Config> Pallet<T> {227 pub fn collection_admins_limit() -> u32 {228 COLLECTION_ADMINS_LIMIT229 }230 }231232 #[pallet::event]233 #[pallet::generate_deposit(pub fn deposit_event)]234 pub enum Event<T: Config> {235 /// New collection was created236 ///237 /// # Arguments238 ///239 /// * collection_id: Globally unique identifier of newly created collection.240 ///241 /// * mode: [CollectionMode] converted into u8.242 ///243 /// * account_id: Collection owner.244 CollectionCreated(CollectionId, u8, T::AccountId),245246 /// New collection was destroyed247 ///248 /// # Arguments249 ///250 /// * collection_id: Globally unique identifier of collection.251 CollectionDestroyed(CollectionId),252253 /// New item was created.254 ///255 /// # Arguments256 ///257 /// * collection_id: Id of the collection where item was created.258 ///259 /// * item_id: Id of an item. Unique within the collection.260 ///261 /// * recipient: Owner of newly created item262 ///263 /// * amount: Always 1 for NFT264 ItemCreated(CollectionId, TokenId, T::CrossAccountId, u128),265266 /// Collection item was burned.267 ///268 /// # Arguments269 ///270 /// * collection_id.271 ///272 /// * item_id: Identifier of burned NFT.273 ///274 /// * owner: which user has destroyed its tokens275 ///276 /// * amount: Always 1 for NFT277 ItemDestroyed(CollectionId, TokenId, T::CrossAccountId, u128),278279 /// Item was transferred280 ///281 /// * collection_id: Id of collection to which item is belong282 ///283 /// * item_id: Id of an item284 ///285 /// * sender: Original owner of item286 ///287 /// * recipient: New owner of item288 ///289 /// * amount: Always 1 for NFT290 Transfer(291 CollectionId,292 TokenId,293 T::CrossAccountId,294 T::CrossAccountId,295 u128,296 ),297298 /// * collection_id299 ///300 /// * item_id301 ///302 /// * sender303 ///304 /// * spender305 ///306 /// * amount307 Approved(308 CollectionId,309 TokenId,310 T::CrossAccountId,311 T::CrossAccountId,312 u128,313 ),314315 CollectionPropertySet(CollectionId, PropertyKey),316317 CollectionPropertyDeleted(CollectionId, PropertyKey),318319 TokenPropertySet(CollectionId, TokenId, PropertyKey),320321 TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),322323 PropertyPermissionSet(CollectionId, PropertyKey),324 }325326 #[pallet::error]327 pub enum Error<T> {328 /// This collection does not exist.329 CollectionNotFound,330 /// Sender parameter and item owner must be equal.331 MustBeTokenOwner,332 /// No permission to perform action333 NoPermission,334 /// Destroying only empty collections is allowed335 CantDestroyNotEmptyCollection,336 /// Collection is not in mint mode.337 PublicMintingNotAllowed,338 /// Address is not in allow list.339 AddressNotInAllowlist,340341 /// Collection name can not be longer than 63 char.342 CollectionNameLimitExceeded,343 /// Collection description can not be longer than 255 char.344 CollectionDescriptionLimitExceeded,345 /// Token prefix can not be longer than 15 char.346 CollectionTokenPrefixLimitExceeded,347 /// Total collections bound exceeded.348 TotalCollectionsLimitExceeded,349 /// Exceeded max admin count350 CollectionAdminCountExceeded,351 /// Collection limit bounds per collection exceeded352 CollectionLimitBoundsExceeded,353 /// Tried to enable permissions which are only permitted to be disabled354 OwnerPermissionsCantBeReverted,355 /// Collection settings not allowing items transferring356 TransferNotAllowed,357 /// Account token limit exceeded per collection358 AccountTokenLimitExceeded,359 /// Collection token limit exceeded360 CollectionTokenLimitExceeded,361 /// Metadata flag frozen362 MetadataFlagFrozen,363364 /// Item not exists.365 TokenNotFound,366 /// Item balance not enough.367 TokenValueTooLow,368 /// Requested value more than approved.369 ApprovedValueTooLow,370 /// Tried to approve more than owned371 CantApproveMoreThanOwned,372373 /// Can't transfer tokens to ethereum zero address374 AddressIsZero,375 /// Target collection doesn't supports this operation376 UnsupportedOperation,377378 /// Not sufficient founds to perform action379 NotSufficientFounds,380381 /// Collection has nesting disabled382 NestingIsDisabled,383 /// Only owner may nest tokens under this collection384 OnlyOwnerAllowedToNest,385 /// Only tokens from specific collections may nest tokens under this386 SourceCollectionIsNotAllowedToNest,387388 /// Tried to store more data than allowed in collection field389 CollectionFieldSizeExceeded,390391 /// Tried to store more property data than allowed392 NoSpaceForProperty,393394 /// Tried to store more property keys than allowed395 PropertyLimitReached,396397 /// Property key is too long398 PropertyKeyIsTooLong,399400 /// Only ASCII letters, digits, and '_', '-' are allowed401 InvalidCharacterInPropertyKey,402403 /// Empty property keys are forbidden404 EmptyPropertyKey,405 }406407 #[pallet::storage]408 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;409 #[pallet::storage]410 pub type DestroyedCollectionCount<T> =411 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;412413 /// Collection info414 #[pallet::storage]415 pub type CollectionById<T> = StorageMap<416 Hasher = Blake2_128Concat,417 Key = CollectionId,418 Value = Collection<<T as frame_system::Config>::AccountId>,419 QueryKind = OptionQuery,420 >;421422 /// Collection properties423 #[pallet::storage]424 #[pallet::getter(fn collection_properties)]425 pub type CollectionProperties<T> = StorageMap<426 Hasher = Blake2_128Concat,427 Key = CollectionId,428 Value = Properties,429 QueryKind = ValueQuery,430 OnEmpty = up_data_structs::CollectionProperties,431 >;432433 #[pallet::storage]434 #[pallet::getter(fn property_permissions)]435 pub type CollectionPropertyPermissions<T> = StorageMap<436 Hasher = Blake2_128Concat,437 Key = CollectionId,438 Value = PropertiesPermissionMap,439 QueryKind = ValueQuery,440 >;441442 #[pallet::storage]443 pub type AdminAmount<T> = StorageMap<444 Hasher = Blake2_128Concat,445 Key = CollectionId,446 Value = u32,447 QueryKind = ValueQuery,448 >;449450 /// List of collection admins451 #[pallet::storage]452 pub type IsAdmin<T: Config> = StorageNMap<453 Key = (454 Key<Blake2_128Concat, CollectionId>,455 Key<Blake2_128Concat, T::CrossAccountId>,456 ),457 Value = bool,458 QueryKind = ValueQuery,459 >;460461 /// Allowlisted collection users462 #[pallet::storage]463 pub type Allowlist<T: Config> = StorageNMap<464 Key = (465 Key<Blake2_128Concat, CollectionId>,466 Key<Blake2_128Concat, T::CrossAccountId>,467 ),468 Value = bool,469 QueryKind = ValueQuery,470 >;471472 /// Not used by code, exists only to provide some types to metadata473 #[pallet::storage]474 pub type DummyStorageValue<T: Config> = StorageValue<475 Value = (476 CollectionStats,477 CollectionId,478 TokenId,479 PhantomType<TokenData<T::CrossAccountId>>,480 PhantomType<RpcCollection<T::AccountId>>,481 // RMRK482 PhantomType<RmrkCollectionInfo<T::AccountId>>,483 PhantomType<RmrkInstanceInfo<T::AccountId>>,484 PhantomType<RmrkResourceInfo>,485 PhantomType<RmrkPropertyInfo>,486 PhantomType<RmrkBaseInfo<T::AccountId>>,487 PhantomType<RmrkPartType>,488 PhantomType<RmrkTheme>,489 PhantomType<RmrkNftChild>,490 ),491 QueryKind = OptionQuery,492 >;493494 #[pallet::hooks]495 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {496 fn on_runtime_upgrade() -> Weight {497 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {498 use up_data_structs::{CollectionVersion1, CollectionVersion2};499 <CollectionById<T>>::translate::<CollectionVersion1<T::AccountId>, _>(|id, v| {500 let mut props = Vec::new();501 if !v.offchain_schema.is_empty() {502 props.push(Property {503 key: b"_old_offchainSchema".to_vec().try_into().unwrap(),504 value: v.offchain_schema.clone().into_inner().try_into().expect("offchain schema too big"),505 });506 }507 if !v.variable_on_chain_schema.is_empty() {508 props.push(Property {509 key: b"_old_variableOnChainSchema".to_vec().try_into().unwrap(),510 value: v.variable_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),511 });512 }513 if !v.const_on_chain_schema.is_empty() {514 props.push(Property {515 key: b"_old_constOnChainSchema".to_vec().try_into().unwrap(),516 value: v.const_on_chain_schema.clone().into_inner().try_into().expect("offchain schema too big"),517 });518 }519 props.push(Property {520 key: b"_old_schemaVersion".to_vec().try_into().unwrap(),521 value: match v.schema_version {522 SchemaVersion::ImageURL => b"ImageUrl".as_slice(),523 SchemaVersion::Unique => b"Unique".as_slice(),524 }.to_vec().try_into().unwrap(),525 });526 Self::set_scoped_collection_properties(527 id,528 PropertyScope::None,529 props.into_iter(),530 ).expect("existing data larger than properties");531 let mut new = CollectionVersion2::from(v.clone());532 new.permissions.access = Some(v.access);533 new.permissions.mint_mode = Some(v.mint_mode);534 Some(new)535 });536 }537538 0539 }540 }541}542543impl<T: Config> Pallet<T> {544 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens545 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {546 ensure!(547 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,548 <Error<T>>::AddressIsZero549 );550 Ok(())551 }552 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {553 <IsAdmin<T>>::iter_prefix((collection,))554 .map(|(a, _)| a)555 .collect()556 }557 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {558 <Allowlist<T>>::iter_prefix((collection,))559 .map(|(a, _)| a)560 .collect()561 }562 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {563 <Allowlist<T>>::get((collection, user))564 }565 pub fn collection_stats() -> CollectionStats {566 let created = <CreatedCollectionCount<T>>::get();567 let destroyed = <DestroyedCollectionCount<T>>::get();568 CollectionStats {569 created: created.0,570 destroyed: destroyed.0,571 alive: created.0 - destroyed.0,572 }573 }574575 pub fn effective_collection_limits(collection: CollectionId) -> Option<CollectionLimits> {576 let collection = <CollectionById<T>>::get(collection);577 if collection.is_none() {578 return None;579 }580581 let collection = collection.unwrap();582 let limits = collection.limits;583 let effective_limits = CollectionLimits {584 account_token_ownership_limit: Some(limits.account_token_ownership_limit()),585 sponsored_data_size: Some(limits.sponsored_data_size()),586 sponsored_data_rate_limit: Some(587 limits588 .sponsored_data_rate_limit589 .unwrap_or(SponsoringRateLimit::SponsoringDisabled),590 ),591 token_limit: Some(limits.token_limit()),592 sponsor_transfer_timeout: Some(limits.sponsor_transfer_timeout(593 match collection.mode {594 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,595 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,596 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,597 },598 )),599 sponsor_approve_timeout: Some(limits.sponsor_approve_timeout()),600 owner_can_transfer: Some(limits.owner_can_transfer()),601 owner_can_destroy: Some(limits.owner_can_destroy()),602 transfers_enabled: Some(limits.transfers_enabled()),603 };604605 Some(effective_limits)606 }607608 pub fn rpc_collection(collection: CollectionId) -> Option<RpcCollection<T::AccountId>> {609 let Collection {610 name,611 description,612 owner,613 mode,614 token_prefix,615 sponsorship,616 limits,617 permissions,618 } = <CollectionById<T>>::get(collection)?;619620 let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)621 .into_iter()622 .map(|(key, permission)| PropertyKeyPermission {623 key,624 permission,625 })626 .collect();627628 let properties = <CollectionProperties<T>>::get(collection)629 .into_iter()630 .map(|(key, value)| Property {631 key,632 value,633 })634 .collect();635636 Some(RpcCollection {637 name: name.into_inner(),638 description: description.into_inner(),639 owner,640 mode,641 token_prefix: token_prefix.into_inner(),642 sponsorship,643 limits,644 permissions,645 token_property_permissions,646 properties,647 })648 }649}650651macro_rules! limit_default {652 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{653 $(654 if let Some($new) = $new.$field {655 let $old = $old.$field($($arg)?);656 let _ = $new;657 let _ = $old;658 $check659 } else {660 $new.$field = $old.$field661 }662 )*663 }};664}665macro_rules! limit_default_clone {666 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{667 $(668 if let Some($new) = $new.$field.clone() {669 let $old = $old.$field($($arg)?);670 let _ = $new;671 let _ = $old;672 $check673 } else {674 $new.$field = $old.$field.clone()675 }676 )*677 }};678}679680impl<T: Config> Pallet<T> {681 pub fn init_collection(682 owner: T::AccountId,683 data: CreateCollectionData<T::AccountId>,684 ) -> Result<CollectionId, DispatchError> {685 {686 ensure!(687 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,688 Error::<T>::CollectionTokenPrefixLimitExceeded689 );690 }691692 let created_count = <CreatedCollectionCount<T>>::get()693 .0694 .checked_add(1)695 .ok_or(ArithmeticError::Overflow)?;696 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;697 let id = CollectionId(created_count);698699 // bound Total number of collections700 ensure!(701 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,702 <Error<T>>::TotalCollectionsLimitExceeded703 );704705 // =========706707 let collection = Collection {708 owner: owner.clone(),709 name: data.name,710 mode: data.mode.clone(),711 description: data.description,712 token_prefix: data.token_prefix,713 sponsorship: data714 .pending_sponsor715 .map(SponsorshipState::Unconfirmed)716 .unwrap_or_default(),717 limits: data718 .limits719 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))720 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,721 permissions: data722 .permissions723 .map(|permissions| Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions))724 .unwrap_or_else(|| Ok(CollectionPermissions::default()))?,725 };726727 let mut collection_properties = up_data_structs::CollectionProperties::get();728 collection_properties729 .try_set_from_iter(data.properties.into_iter())730 .map_err(<Error<T>>::from)?;731732 CollectionProperties::<T>::insert(id, collection_properties);733734 let mut token_props_permissions = PropertiesPermissionMap::new();735 token_props_permissions736 .try_set_from_iter(data.token_property_permissions.into_iter())737 .map_err(<Error<T>>::from)?;738739 CollectionPropertyPermissions::<T>::insert(id, token_props_permissions);740741 // Take a (non-refundable) deposit of collection creation742 {743 let mut imbalance =744 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();745 imbalance.subsume(746 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(747 &T::TreasuryAccountId::get(),748 T::CollectionCreationPrice::get(),749 ),750 );751 <T as Config>::Currency::settle(752 &owner,753 imbalance,754 WithdrawReasons::TRANSFER,755 ExistenceRequirement::KeepAlive,756 )757 .map_err(|_| Error::<T>::NotSufficientFounds)?;758 }759760 <CreatedCollectionCount<T>>::put(created_count);761 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));762 <CollectionById<T>>::insert(id, collection);763 Ok(id)764 }765766 pub fn destroy_collection(767 collection: CollectionHandle<T>,768 sender: &T::CrossAccountId,769 ) -> DispatchResult {770 ensure!(771 collection.limits.owner_can_destroy(),772 <Error<T>>::NoPermission,773 );774 collection.check_is_owner(sender)?;775776 let destroyed_collections = <DestroyedCollectionCount<T>>::get()777 .0778 .checked_add(1)779 .ok_or(ArithmeticError::Overflow)?;780781 // =========782783 <DestroyedCollectionCount<T>>::put(destroyed_collections);784 <CollectionById<T>>::remove(collection.id);785 <AdminAmount<T>>::remove(collection.id);786 <IsAdmin<T>>::remove_prefix((collection.id,), None);787 <Allowlist<T>>::remove_prefix((collection.id,), None);788 <CollectionProperties<T>>::remove(collection.id);789790 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));791 Ok(())792 }793794 pub fn set_collection_property(795 collection: &CollectionHandle<T>,796 sender: &T::CrossAccountId,797 property: Property,798 ) -> DispatchResult {799 collection.check_is_owner_or_admin(sender)?;800801 CollectionProperties::<T>::try_mutate(collection.id, |properties| {802 let property = property.clone();803 properties.try_set(property.key, property.value)804 })805 .map_err(<Error<T>>::from)?;806807 Self::deposit_event(Event::CollectionPropertySet(collection.id, property.key));808809 Ok(())810 }811812 pub fn set_scoped_collection_property(813 collection_id: CollectionId,814 scope: PropertyScope,815 property: Property,816 ) -> DispatchResult {817 CollectionProperties::<T>::try_mutate(collection_id, |properties| {818 properties.try_scoped_set(scope, property.key, property.value)819 })820 .map_err(<Error<T>>::from)?;821822 Ok(())823 }824825 pub fn set_scoped_collection_properties(826 collection_id: CollectionId,827 scope: PropertyScope,828 properties: impl Iterator<Item = Property>,829 ) -> DispatchResult {830 CollectionProperties::<T>::try_mutate(collection_id, |stored_properties| {831 stored_properties.try_scoped_set_from_iter(scope, properties)832 })833 .map_err(<Error<T>>::from)?;834835 Ok(())836 }837838 #[transactional]839 pub fn set_collection_properties(840 collection: &CollectionHandle<T>,841 sender: &T::CrossAccountId,842 properties: Vec<Property>,843 ) -> DispatchResult {844 for property in properties {845 Self::set_collection_property(collection, sender, property)?;846 }847848 Ok(())849 }850851 pub fn delete_collection_property(852 collection: &CollectionHandle<T>,853 sender: &T::CrossAccountId,854 property_key: PropertyKey,855 ) -> DispatchResult {856 collection.check_is_owner_or_admin(sender)?;857858 CollectionProperties::<T>::try_mutate(collection.id, |properties| {859 properties.remove(&property_key)860 })861 .map_err(<Error<T>>::from)?;862863 Self::deposit_event(Event::CollectionPropertyDeleted(864 collection.id,865 property_key,866 ));867868 Ok(())869 }870871 #[transactional]872 pub fn delete_collection_properties(873 collection: &CollectionHandle<T>,874 sender: &T::CrossAccountId,875 property_keys: Vec<PropertyKey>,876 ) -> DispatchResult {877 for key in property_keys {878 Self::delete_collection_property(collection, sender, key)?;879 }880881 Ok(())882 }883884 // For migrations885 pub fn set_property_permission_unchecked(886 collection: CollectionId,887 property_permission: PropertyKeyPermission,888 ) -> DispatchResult {889 <CollectionPropertyPermissions<T>>::try_mutate(collection, |permissions| {890 permissions.try_set(property_permission.key, property_permission.permission)891 })892 .map_err(<Error<T>>::from)?;893 Ok(())894 }895896 pub fn set_property_permission(897 collection: &CollectionHandle<T>,898 sender: &T::CrossAccountId,899 property_permission: PropertyKeyPermission,900 ) -> DispatchResult {901 collection.check_is_owner_or_admin(sender)?;902903 let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);904 let current_permission = all_permissions.get(&property_permission.key);905 if matches![906 current_permission,907 Some(PropertyPermission { mutable: false, .. })908 ] {909 return Err(<Error<T>>::NoPermission.into());910 }911912 CollectionPropertyPermissions::<T>::try_mutate(collection.id, |permissions| {913 let property_permission = property_permission.clone();914 permissions.try_set(property_permission.key, property_permission.permission)915 })916 .map_err(<Error<T>>::from)?;917918 Self::deposit_event(Event::PropertyPermissionSet(919 collection.id,920 property_permission.key,921 ));922923 Ok(())924 }925926 #[transactional]927 pub fn set_property_permissions(928 collection: &CollectionHandle<T>,929 sender: &T::CrossAccountId,930 property_permissions: Vec<PropertyKeyPermission>,931 ) -> DispatchResult {932 for prop_pemission in property_permissions {933 Self::set_property_permission(collection, sender, prop_pemission)?;934 }935936 Ok(())937 }938939 pub fn get_collection_property(940 collection_id: CollectionId,941 key: &PropertyKey,942 ) -> Option<PropertyValue> {943 Self::collection_properties(collection_id).get(key).cloned()944 }945946 pub fn bytes_keys_to_property_keys(947 keys: Vec<Vec<u8>>,948 ) -> Result<Vec<PropertyKey>, DispatchError> {949 keys.into_iter()950 .map(|key| -> Result<PropertyKey, DispatchError> {951 key.try_into()952 .map_err(|_| <Error<T>>::PropertyKeyIsTooLong.into())953 })954 .collect::<Result<Vec<PropertyKey>, DispatchError>>()955 }956957 pub fn filter_collection_properties(958 collection_id: CollectionId,959 keys: Option<Vec<PropertyKey>>,960 ) -> Result<Vec<Property>, DispatchError> {961 let properties = Self::collection_properties(collection_id);962963 let properties = keys964 .map(|keys| {965 keys.into_iter()966 .filter_map(|key| {967 properties.get(&key).map(|value| Property {968 key,969 value: value.clone(),970 })971 })972 .collect()973 })974 .unwrap_or_else(|| {975 properties976 .into_iter()977 .map(|(key, value)| Property {978 key,979 value,980 })981 .collect()982 });983984 Ok(properties)985 }986987 pub fn filter_property_permissions(988 collection_id: CollectionId,989 keys: Option<Vec<PropertyKey>>,990 ) -> Result<Vec<PropertyKeyPermission>, DispatchError> {991 let permissions = Self::property_permissions(collection_id);992993 let key_permissions = keys994 .map(|keys| {995 keys.into_iter()996 .filter_map(|key| {997 permissions998 .get(&key)999 .map(|permission| PropertyKeyPermission {1000 key,1001 permission: permission.clone(),1002 })1003 })1004 .collect()1005 })1006 .unwrap_or_else(|| {1007 permissions1008 .into_iter()1009 .map(|(key, permission)| PropertyKeyPermission {1010 key,1011 permission,1012 })1013 .collect()1014 });10151016 Ok(key_permissions)1017 }10181019 pub fn toggle_allowlist(1020 collection: &CollectionHandle<T>,1021 sender: &T::CrossAccountId,1022 user: &T::CrossAccountId,1023 allowed: bool,1024 ) -> DispatchResult {1025 collection.check_is_owner_or_admin(sender)?;10261027 // =========10281029 if allowed {1030 <Allowlist<T>>::insert((collection.id, user), true);1031 } else {1032 <Allowlist<T>>::remove((collection.id, user));1033 }10341035 Ok(())1036 }10371038 pub fn toggle_admin(1039 collection: &CollectionHandle<T>,1040 sender: &T::CrossAccountId,1041 user: &T::CrossAccountId,1042 admin: bool,1043 ) -> DispatchResult {1044 collection.check_is_owner_or_admin(sender)?;10451046 let was_admin = <IsAdmin<T>>::get((collection.id, user));1047 if was_admin == admin {1048 return Ok(());1049 }1050 let amount = <AdminAmount<T>>::get(collection.id);10511052 if admin {1053 let amount = amount1054 .checked_add(1)1055 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;1056 ensure!(1057 amount <= Self::collection_admins_limit(),1058 <Error<T>>::CollectionAdminCountExceeded,1059 );10601061 // =========10621063 <AdminAmount<T>>::insert(collection.id, amount);1064 <IsAdmin<T>>::insert((collection.id, user), true);1065 } else {1066 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));1067 <IsAdmin<T>>::remove((collection.id, user));1068 }10691070 Ok(())1071 }10721073 pub fn clamp_limits(1074 mode: CollectionMode,1075 old_limit: &CollectionLimits,1076 mut new_limit: CollectionLimits,1077 ) -> Result<CollectionLimits, DispatchError> {1078 limit_default!(old_limit, new_limit,1079 account_token_ownership_limit => ensure!(1080 new_limit <= MAX_TOKEN_OWNERSHIP,1081 <Error<T>>::CollectionLimitBoundsExceeded,1082 ),1083 sponsor_transfer_timeout(match mode {1084 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,1085 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1086 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,1087 }) => ensure!(1088 new_limit <= MAX_SPONSOR_TIMEOUT,1089 <Error<T>>::CollectionLimitBoundsExceeded,1090 ),1091 sponsored_data_size => ensure!(1092 new_limit <= CUSTOM_DATA_LIMIT,1093 <Error<T>>::CollectionLimitBoundsExceeded,1094 ),1095 token_limit => ensure!(1096 old_limit >= new_limit && new_limit > 0,1097 <Error<T>>::CollectionTokenLimitExceeded1098 ),1099 owner_can_transfer => ensure!(1100 old_limit || !new_limit,1101 <Error<T>>::OwnerPermissionsCantBeReverted,1102 ),1103 owner_can_destroy => ensure!(1104 old_limit || !new_limit,1105 <Error<T>>::OwnerPermissionsCantBeReverted,1106 ),1107 sponsored_data_rate_limit => {},1108 transfers_enabled => {},1109 );1110 Ok(new_limit)1111 }1112 pub fn clamp_permissions(1113 mode: CollectionMode,1114 old_limit: &CollectionPermissions,1115 mut new_limit: CollectionPermissions,1116 ) -> Result<CollectionPermissions, DispatchError> {1117 limit_default_clone!(old_limit, new_limit,1118 );1119 Ok(new_limit)1120 }1121}11221123#[macro_export]1124macro_rules! unsupported {1125 () => {1126 Err(<Error<T>>::UnsupportedOperation.into())1127 };1128}11291130/// Worst cases1131pub trait CommonWeightInfo<CrossAccountId> {1132 fn create_item() -> Weight;1133 fn create_multiple_items(amount: &[CreateItemData]) -> Weight;1134 fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;1135 fn burn_item() -> Weight;1136 fn set_collection_properties(amount: u32) -> Weight;1137 fn delete_collection_properties(amount: u32) -> Weight;1138 fn set_token_properties(amount: u32) -> Weight;1139 fn delete_token_properties(amount: u32) -> Weight;1140 fn set_property_permissions(amount: u32) -> Weight;1141 fn transfer() -> Weight;1142 fn approve() -> Weight;1143 fn transfer_from() -> Weight;1144 fn burn_from() -> Weight;1145}11461147pub trait CommonCollectionOperations<T: Config> {1148 fn create_item(1149 &self,1150 sender: T::CrossAccountId,1151 to: T::CrossAccountId,1152 data: CreateItemData,1153 nesting_budget: &dyn Budget,1154 ) -> DispatchResultWithPostInfo;1155 fn create_multiple_items(1156 &self,1157 sender: T::CrossAccountId,1158 to: T::CrossAccountId,1159 data: Vec<CreateItemData>,1160 nesting_budget: &dyn Budget,1161 ) -> DispatchResultWithPostInfo;1162 fn create_multiple_items_ex(1163 &self,1164 sender: T::CrossAccountId,1165 data: CreateItemExData<T::CrossAccountId>,1166 nesting_budget: &dyn Budget,1167 ) -> DispatchResultWithPostInfo;1168 fn burn_item(1169 &self,1170 sender: T::CrossAccountId,1171 token: TokenId,1172 amount: u128,1173 ) -> DispatchResultWithPostInfo;1174 fn set_collection_properties(1175 &self,1176 sender: T::CrossAccountId,1177 properties: Vec<Property>,1178 ) -> DispatchResultWithPostInfo;1179 fn delete_collection_properties(1180 &self,1181 sender: &T::CrossAccountId,1182 property_keys: Vec<PropertyKey>,1183 ) -> DispatchResultWithPostInfo;1184 fn set_token_properties(1185 &self,1186 sender: T::CrossAccountId,1187 token_id: TokenId,1188 property: Vec<Property>,1189 ) -> DispatchResultWithPostInfo;1190 fn delete_token_properties(1191 &self,1192 sender: T::CrossAccountId,1193 token_id: TokenId,1194 property_keys: Vec<PropertyKey>,1195 ) -> DispatchResultWithPostInfo;1196 fn set_property_permissions(1197 &self,1198 sender: &T::CrossAccountId,1199 property_permissions: Vec<PropertyKeyPermission>,1200 ) -> DispatchResultWithPostInfo;1201 fn transfer(1202 &self,1203 sender: T::CrossAccountId,1204 to: T::CrossAccountId,1205 token: TokenId,1206 amount: u128,1207 nesting_budget: &dyn Budget,1208 ) -> DispatchResultWithPostInfo;1209 fn approve(1210 &self,1211 sender: T::CrossAccountId,1212 spender: T::CrossAccountId,1213 token: TokenId,1214 amount: u128,1215 ) -> DispatchResultWithPostInfo;1216 fn transfer_from(1217 &self,1218 sender: T::CrossAccountId,1219 from: T::CrossAccountId,1220 to: T::CrossAccountId,1221 token: TokenId,1222 amount: u128,1223 nesting_budget: &dyn Budget,1224 ) -> DispatchResultWithPostInfo;1225 fn burn_from(1226 &self,1227 sender: T::CrossAccountId,1228 from: T::CrossAccountId,1229 token: TokenId,1230 amount: u128,1231 nesting_budget: &dyn Budget,1232 ) -> DispatchResultWithPostInfo;12331234 fn check_nesting(1235 &self,1236 sender: T::CrossAccountId,1237 from: (CollectionId, TokenId),1238 under: TokenId,1239 budget: &dyn Budget,1240 ) -> DispatchResult;12411242 fn nest(1243 &self,1244 under: TokenId,1245 to_nest: (CollectionId, TokenId)1246 );12471248 fn unnest(1249 &self,1250 under: TokenId,1251 to_nest: (CollectionId, TokenId)1252 );12531254 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;1255 fn collection_tokens(&self) -> Vec<TokenId>;1256 fn token_exists(&self, token: TokenId) -> bool;1257 fn last_token_id(&self) -> TokenId;12581259 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;1260 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue>;1261 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property>;1262 /// Amount of unique collection tokens1263 fn total_supply(&self) -> u32;1264 /// Amount of different tokens account has (Applicable to nonfungible/refungible)1265 fn account_balance(&self, account: T::CrossAccountId) -> u32;1266 /// Amount of specific token account have (Applicable to fungible/refungible)1267 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;1268 fn allowance(1269 &self,1270 sender: T::CrossAccountId,1271 spender: T::CrossAccountId,1272 token: TokenId,1273 ) -> u128;1274}12751276// Flexible enough for implementing CommonCollectionOperations1277pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {1278 let post_info = PostDispatchInfo {1279 actual_weight: Some(weight),1280 pays_fee: Pays::Yes,1281 };1282 match res {1283 Ok(()) => Ok(post_info),1284 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),1285 }1286}12871288impl<T: Config> From<PropertiesError> for Error<T> {1289 fn from(error: PropertiesError) -> Self {1290 match error {1291 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,1292 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,1293 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,1294 PropertiesError::PropertyKeyIsTooLong => Self::PropertyKeyIsTooLong,1295 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,1296 }1297 }1298}pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -145,6 +145,10 @@
) -> DispatchResult {
let id = collection.id;
+ if Self::collection_has_tokens(id) {
+ return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());
+ }
+
// =========
PalletCommon::destroy_collection(collection.0, sender)?;
@@ -155,6 +159,10 @@
Ok(())
}
+ fn collection_has_tokens(collection_id: CollectionId) -> bool {
+ <TotalSupply<T>>::get(collection_id) != 0
+ }
+
pub fn burn(
collection: &FungibleHandle<T>,
owner: &T::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -79,8 +79,6 @@
NonfungibleItemsHaveNoAmount,
/// Unable to burn NFT with children
CantBurnNftWithChildren,
- /// Unable to burn a collection containing NFTs that have children
- CantBurnCollectionWithNestedTokens
}
#[pallet::config]
@@ -295,8 +293,8 @@
) -> DispatchResult {
let id = collection.id;
- if Self::collection_has_nested_tokens(id) {
- return Err(<Error<T>>::CantBurnCollectionWithNestedTokens.into());
+ if Self::collection_has_tokens(id) {
+ return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());
}
// =========
@@ -972,8 +970,8 @@
);
}
- fn collection_has_nested_tokens(collection_id: CollectionId) -> bool {
- <TokenChildren<T>>::iter_prefix((collection_id,)).next().is_some()
+ fn collection_has_tokens(collection_id: CollectionId) -> bool {
+ <TokenData<T>>::iter_prefix((collection_id,)).next().is_some()
}
fn token_has_children(collection_id: CollectionId, token_id: TokenId) -> bool {
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -210,6 +210,10 @@
) -> DispatchResult {
let id = collection.id;
+ if Self::collection_has_tokens(id) {
+ return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());
+ }
+
// =========
PalletCommon::destroy_collection(collection.0, sender)?;
@@ -225,6 +229,10 @@
Ok(())
}
+ fn collection_has_tokens(collection_id: CollectionId) -> bool {
+ <TokenData<T>>::iter_prefix((collection_id,)).next().is_some()
+ }
+
pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {
let burnt = <TokensBurnt<T>>::get(collection.id)
.checked_add(1)