// This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::*; use frame_support::pallet_prelude::*; impl, I: 'static> Pallet { pub fn do_create_collection( collection: T::CollectionId, owner: T::AccountId, admin: T::AccountId, config: CollectionConfigFor, deposit: DepositBalanceOf, event: Event, ) -> DispatchResult { ensure!(!Collection::::contains_key(collection), Error::::CollectionIdInUse); T::Currency::reserve(&owner, deposit)?; Collection::::insert( collection, CollectionDetails { owner: owner.clone(), owner_deposit: deposit, items: 0, item_metadatas: 0, item_configs: 0, attributes: 0, }, ); CollectionRoleOf::::insert( collection, admin, CollectionRoles( CollectionRole::Admin | CollectionRole::Freezer | CollectionRole::Issuer, ), ); let next_id = collection.increment(); CollectionConfigOf::::insert(&collection, config); CollectionAccount::::insert(&owner, &collection, ()); NextCollectionId::::set(Some(next_id)); Self::deposit_event(Event::NextCollectionIdIncremented { next_id }); Self::deposit_event(event); Ok(()) } pub fn do_destroy_collection( collection: T::CollectionId, witness: DestroyWitness, maybe_check_owner: Option, ) -> Result { Collection::::try_mutate_exists(collection, |maybe_details| { let collection_details = maybe_details.take().ok_or(Error::::UnknownCollection)?; if let Some(check_owner) = maybe_check_owner { ensure!(collection_details.owner == check_owner, Error::::NoPermission); } ensure!(collection_details.items == 0, Error::::CollectionNotEmpty); ensure!(collection_details.attributes == witness.attributes, Error::::BadWitness); ensure!( collection_details.item_metadatas == witness.item_metadatas, Error::::BadWitness ); ensure!( collection_details.item_configs == witness.item_configs, Error::::BadWitness ); for (_, metadata) in ItemMetadataOf::::drain_prefix(&collection) { if let Some(depositor) = metadata.deposit.account { T::Currency::unreserve(&depositor, metadata.deposit.amount); } } CollectionMetadataOf::::remove(&collection); Self::clear_roles(&collection)?; for (_, (_, deposit)) in Attribute::::drain_prefix((&collection,)) { if !deposit.amount.is_zero() { if let Some(account) = deposit.account { T::Currency::unreserve(&account, deposit.amount); } } } CollectionAccount::::remove(&collection_details.owner, &collection); T::Currency::unreserve(&collection_details.owner, collection_details.owner_deposit); CollectionConfigOf::::remove(&collection); let _ = ItemConfigOf::::clear_prefix(&collection, witness.item_configs, None); Self::deposit_event(Event::Destroyed { collection }); Ok(DestroyWitness { item_metadatas: collection_details.item_metadatas, item_configs: collection_details.item_configs, attributes: collection_details.attributes, }) }) } }