Fungibles and Non-Fungibles Create and Destroy Traits + Assets and Uniques Implementation (#9844)

* refactor `do_destroy`

* destroy trait

* refactor do_force_create

* impl create trait

* do not bleed weight into api

* Do the same for uniques

* add docs
This commit is contained in:
Shawn Tabrizi
2021-09-26 18:05:01 -04:00
committed by GitHub
parent 0daa2b66ec
commit f8a228859e
8 changed files with 243 additions and 88 deletions
+35
View File
@@ -80,6 +80,41 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(())
}
pub(super) fn do_destroy_class(
class: T::ClassId,
witness: DestroyWitness,
maybe_check_owner: Option<T::AccountId>,
) -> Result<DestroyWitness, DispatchError> {
Class::<T, I>::try_mutate_exists(class, |maybe_details| {
let class_details = maybe_details.take().ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(class_details.owner == check_owner, Error::<T, I>::NoPermission);
}
ensure!(class_details.instances == witness.instances, Error::<T, I>::BadWitness);
ensure!(
class_details.instance_metadatas == witness.instance_metadatas,
Error::<T, I>::BadWitness
);
ensure!(class_details.attributes == witness.attributes, Error::<T, I>::BadWitness);
for (instance, details) in Asset::<T, I>::drain_prefix(&class) {
Account::<T, I>::remove((&details.owner, &class, &instance));
}
InstanceMetadataOf::<T, I>::remove_prefix(&class, None);
ClassMetadataOf::<T, I>::remove(&class);
Attribute::<T, I>::remove_prefix((&class,), None);
T::Currency::unreserve(&class_details.owner, class_details.total_deposit);
Self::deposit_event(Event::Destroyed(class));
Ok(DestroyWitness {
instances: class_details.instances,
instance_metadatas: class_details.instance_metadatas,
attributes: class_details.attributes,
})
})
}
pub(super) fn do_mint(
class: T::ClassId,
instance: T::InstanceId,
@@ -19,13 +19,10 @@
use super::*;
use frame_support::{
traits::{
tokens::nonfungibles::{Create, Inspect, InspectEnumerable, Mutate, Transfer},
Get,
},
traits::{tokens::nonfungibles::*, Get},
BoundedSlice,
};
use sp_runtime::DispatchResult;
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::convert::TryFrom;
impl<T: Config<I>, I: 'static> Inspect<<T as SystemConfig>::AccountId> for Pallet<T, I> {
@@ -106,6 +103,22 @@ impl<T: Config<I>, I: 'static> Create<<T as SystemConfig>::AccountId> for Pallet
}
}
impl<T: Config<I>, I: 'static> Destroy<<T as SystemConfig>::AccountId> for Pallet<T, I> {
type DestroyWitness = DestroyWitness;
fn get_destroy_witness(class: &Self::ClassId) -> Option<DestroyWitness> {
Class::<T, I>::get(class).map(|a| a.destroy_witness())
}
fn destroy(
class: Self::ClassId,
witness: Self::DestroyWitness,
maybe_check_owner: Option<T::AccountId>,
) -> Result<Self::DestroyWitness, DispatchError> {
Self::do_destroy_class(class, witness, maybe_check_owner)
}
}
impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
fn mint_into(
class: &Self::ClassId,
+8 -26
View File
@@ -381,37 +381,19 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
witness: DestroyWitness,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
Class::<T, I>::try_mutate_exists(class, |maybe_details| {
let class_details = maybe_details.take().ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(class_details.owner == check_owner, Error::<T, I>::NoPermission);
}
ensure!(class_details.instances == witness.instances, Error::<T, I>::BadWitness);
ensure!(
class_details.instance_metadatas == witness.instance_metadatas,
Error::<T, I>::BadWitness
);
ensure!(class_details.attributes == witness.attributes, Error::<T, I>::BadWitness);
let details = Self::do_destroy_class(class, witness, maybe_check_owner)?;
for (instance, details) in Asset::<T, I>::drain_prefix(&class) {
Account::<T, I>::remove((&details.owner, &class, &instance));
}
InstanceMetadataOf::<T, I>::remove_prefix(&class, None);
ClassMetadataOf::<T, I>::remove(&class);
Attribute::<T, I>::remove_prefix((&class,), None);
T::Currency::unreserve(&class_details.owner, class_details.total_deposit);
Self::deposit_event(Event::Destroyed(class));
// NOTE: could use postinfo to reflect the actual number of
// accounts/sufficient/approvals
Ok(())
})
Ok(Some(T::WeightInfo::destroy(
details.instances,
details.instance_metadatas,
details.attributes,
))
.into())
}
/// Mint an asset instance of a particular class.