// This file is part of Bizinikiwi. // 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 core::marker::PhantomData; use crate::{ asset_strategies::{Attribute, WithCollectionConfig}, Collection as CollectionStorage, *, }; use pezframe_support::{ ensure, traits::{ tokens::asset_ops::{ common_strategies::{ Bytes, CheckOrigin, CheckState, ConfigValue, IfOwnedBy, Owner, WithConfig, WithWitness, }, AssetDefinition, Create, Destroy, Inspect, }, EnsureOrigin, Get, }, BoundedSlice, }; use pezframe_system::ensure_signed; use pezsp_runtime::{DispatchError, DispatchResult}; pub struct Collection(PhantomData); impl, I: 'static> AssetDefinition for Collection> { type Id = T::CollectionId; } impl, I: 'static> Inspect> for Collection> { fn inspect( collection: &Self::Id, _ownership: Owner, ) -> Result { CollectionStorage::::get(collection) .map(|a| a.owner) .ok_or(Error::::UnknownCollection.into()) } } impl, I: 'static> Inspect for Collection> { fn inspect(collection: &Self::Id, _bytes: Bytes) -> Result, DispatchError> { CollectionMetadataOf::::get(collection) .map(|m| m.data.into()) .ok_or(Error::::NoMetadata.into()) } } impl<'a, T: Config, I: 'static> Inspect>> for Collection> { fn inspect( collection: &Self::Id, strategy: Bytes, ) -> Result, DispatchError> { let Bytes(Attribute(attribute)) = strategy; let attribute = BoundedSlice::try_from(attribute).map_err(|_| Error::::WrongAttribute)?; crate::Attribute::::get((collection, Option::::None, attribute)) .map(|a| a.0.into()) .ok_or(Error::::AttributeNotFound.into()) } } impl, I: 'static> Create> for Collection> { fn create(strategy: WithCollectionConfig) -> Result { let WithConfig { config, extra: id_assignment } = strategy; let collection = id_assignment.params; let (ConfigValue(owner), ConfigValue(admin)) = config; >::do_create_collection( collection.clone(), owner.clone(), admin.clone(), T::CollectionDeposit::get(), false, Event::Created { collection: collection.clone(), creator: owner, owner: admin }, )?; Ok(collection) } } impl, I: 'static> Create>> for Collection> { fn create( strategy: CheckOrigin>, ) -> Result { let CheckOrigin(origin, creation) = strategy; let WithConfig { config, extra: id_assignment } = &creation; let collection = &id_assignment.params; let (ConfigValue(owner), ..) = config; let maybe_check_signer = T::ForceOrigin::try_origin(origin).map(|_| None).or_else(|origin| { T::CreateOrigin::ensure_origin(origin, collection) .map(Some) .map_err(DispatchError::from) })?; if let Some(signer) = maybe_check_signer { ensure!(signer == *owner, Error::::NoPermission); } Self::create(creation) } } impl, I: 'static> Destroy> for Collection> { fn destroy(collection: &Self::Id, strategy: WithWitness) -> DispatchResult { let CheckState(witness, _) = strategy; >::do_destroy_collection(collection.clone(), witness, None).map(|_witness| ()) } } impl, I: 'static> Destroy>> for Collection> { fn destroy( collection: &Self::Id, strategy: IfOwnedBy>, ) -> DispatchResult { let CheckState(owner, CheckState(witness, _)) = strategy; >::do_destroy_collection(collection.clone(), witness, Some(owner)) .map(|_witness| ()) } } impl, I: 'static> Destroy>> for Collection> { fn destroy( collection: &Self::Id, strategy: CheckOrigin>, ) -> DispatchResult { let CheckOrigin(origin, CheckState(witness, _)) = strategy; let maybe_check_owner = match T::ForceOrigin::try_origin(origin) { Ok(_) => None, Err(origin) => Some(ensure_signed(origin)?), }; >::do_destroy_collection(collection.clone(), witness, maybe_check_owner) .map(|_witness| ()) } }