Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

WIP: [NFTs] Simplify collection creation and item mint #13572

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions frame/nfts/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ impl<T: Config<I>, I: 'static> Inspect<<T as SystemConfig>::AccountId> for Palle
impl<T: Config<I>, I: 'static> Create<<T as SystemConfig>::AccountId, CollectionConfigFor<T, I>>
for Pallet<T, I>
{
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin`.
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin` with
/// collection settings provided in `config`.
fn create_collection(
who: &T::AccountId,
admin: &T::AccountId,
Expand All @@ -159,6 +160,27 @@ impl<T: Config<I>, I: 'static> Create<<T as SystemConfig>::AccountId, Collection
Ok(collection)
}
}
impl<T: Config<I>, I: 'static> CreateSimplified<<T as SystemConfig>::AccountId> for Pallet<T, I> {
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin` with
/// default collection settings.
fn create_collection(
who: &T::AccountId,
admin: &T::AccountId,
) -> Result<T::CollectionId, DispatchError> {
let collection =
NextCollectionId::<T, I>::get().unwrap_or(T::CollectionId::initial_value());

Self::do_create_collection(
collection,
who.clone(),
admin.clone(),
CollectionConfigFor::<T, I>::default(),
T::CollectionDeposit::get(),
Event::Created { collection, creator: who.clone(), owner: admin.clone() },
)?;
Ok(collection)
}
}

impl<T: Config<I>, I: 'static> Destroy<<T as SystemConfig>::AccountId> for Pallet<T, I> {
type DestroyWitness = DestroyWitness;
Expand All @@ -181,9 +203,13 @@ impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId, ItemConfig
collection: &Self::CollectionId,
item: &Self::ItemId,
who: &T::AccountId,
item_config: &ItemConfig,
item_config: Option<&ItemConfig>,
deposit_collection_owner: bool,
) -> DispatchResult {
let item_config = match item_config {
Some(item_config) => *item_config,
None => ItemConfig { settings: Self::get_default_item_settings(&collection)? },
};
Self::do_mint(
*collection,
*item,
Expand All @@ -192,7 +218,7 @@ impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId, ItemConfig
false => Some(who.clone()),
},
who.clone(),
*item_config,
item_config,
|_, _| Ok(()),
)
}
Expand Down
26 changes: 20 additions & 6 deletions frame/nfts/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ pub enum PalletAttributes<CollectionId> {
}

/// Collection's configuration.
#[derive(
Clone, Copy, Decode, Default, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo,
)]
#[derive(Clone, Copy, Decode, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo)]
pub struct CollectionConfig<Price, BlockNumber, CollectionId> {
/// Collection's settings.
pub settings: CollectionSettings,
Expand All @@ -359,6 +357,18 @@ pub struct CollectionConfig<Price, BlockNumber, CollectionId> {
pub mint_settings: MintSettings<Price, BlockNumber, CollectionId>,
}

impl<Price, BlockNumber, CollectionId> Default
for CollectionConfig<Price, BlockNumber, CollectionId>
{
fn default() -> Self {
Self {
settings: CollectionSettings::all_enabled(),
max_supply: None,
mint_settings: MintSettings::default(),
}
}
}

impl<Price, BlockNumber, CollectionId> CollectionConfig<Price, BlockNumber, CollectionId> {
pub fn is_setting_enabled(&self, setting: CollectionSetting) -> bool {
!self.settings.is_disabled(setting)
Expand Down Expand Up @@ -409,14 +419,18 @@ impl ItemSettings {
impl_codec_bitflags!(ItemSettings, u64, ItemSetting);

/// Item's configuration.
#[derive(
Encode, Decode, Default, PartialEq, RuntimeDebug, Clone, Copy, MaxEncodedLen, TypeInfo,
)]
#[derive(Encode, Decode, PartialEq, RuntimeDebug, Clone, Copy, MaxEncodedLen, TypeInfo)]
pub struct ItemConfig {
/// Item's settings.
pub settings: ItemSettings,
}

impl Default for ItemConfig {
fn default() -> Self {
Self { settings: ItemSettings::all_enabled() }
}
}

impl ItemConfig {
pub fn is_setting_enabled(&self, setting: ItemSetting) -> bool {
!self.settings.is_disabled(setting)
Expand Down
5 changes: 3 additions & 2 deletions frame/support/src/traits/tokens/nonfungible_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {
/// attributes set on them.
pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
/// Mint some `item` to be owned by `who`.
/// When `config` set to `None` it will use the collection's default_item_settings.
///
/// By default, this is not a supported operation.
fn mint_into(
_item: &Self::ItemId,
_who: &AccountId,
_config: &ItemConfig,
_config: Option<&ItemConfig>,
_deposit_collection_owner: bool,
) -> DispatchResult {
Err(TokenError::Unsupported.into())
Expand Down Expand Up @@ -257,7 +258,7 @@ impl<
fn mint_into(
item: &Self::ItemId,
who: &AccountId,
config: &ItemConfig,
config: Option<&ItemConfig>,
deposit_collection_owner: bool,
) -> DispatchResult {
<F as nonfungibles::Mutate<AccountId, ItemConfig>>::mint_into(
Expand Down
16 changes: 14 additions & 2 deletions frame/support/src/traits/tokens/nonfungibles_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,25 @@ pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {

/// Trait for providing the ability to create collections of nonfungible items.
pub trait Create<AccountId, CollectionConfig>: Inspect<AccountId> {
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin`.
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin` with
/// collection settings provided in `config`.
fn create_collection(
who: &AccountId,
admin: &AccountId,
config: &CollectionConfig,
) -> Result<Self::CollectionId, DispatchError>;
}

/// A simplified trait for providing the ability to create collections of nonfungible items.
pub trait CreateSimplified<AccountId>: Inspect<AccountId> {
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin` with
/// default collection settings.
fn create_collection(
who: &AccountId,
admin: &AccountId,
) -> Result<Self::CollectionId, DispatchError>;
}

/// Trait for providing the ability to destroy collections of nonfungible items.
pub trait Destroy<AccountId>: Inspect<AccountId> {
/// The witness data needed to destroy an item.
Expand Down Expand Up @@ -219,13 +230,14 @@ pub trait Destroy<AccountId>: Inspect<AccountId> {
/// minted, burned and/or have attributes set on them.
pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
/// Mint some `item` of `collection` to be owned by `who`.
/// When `config` set to `None` it will use the collection's default_item_settings.
///
/// By default, this is not a supported operation.
fn mint_into(
_collection: &Self::CollectionId,
_item: &Self::ItemId,
_who: &AccountId,
_config: &ItemConfig,
_config: Option<&ItemConfig>,
_deposit_collection_owner: bool,
) -> DispatchResult {
Err(TokenError::Unsupported.into())
Expand Down