Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove incrementable trait #406

Open
wants to merge 1 commit into
base: chungquantin/feat-nfts_cross_chain
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions pallets/nfts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn create_collection<T: Config<I>, I: 'static>(
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
assert_ok!(Nfts::<T, I>::force_create(
SystemOrigin::Root.into(),
collection.clone(),
caller_lookup.clone(),
default_collection_config::<T, I>()
));
Expand Down Expand Up @@ -230,19 +231,14 @@ benchmarks_instance_pallet! {
whitelist_account!(caller);
let admin = T::Lookup::unlookup(caller.clone());
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
let call = Call::<T, I>::create { admin, config: default_collection_config::<T, I>() };
let call = Call::<T, I>::create { admin, collection, config: default_collection_config::<T, I>() };
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_last_event::<T, I>(Event::NextCollectionIdIncremented { next_id: Some(T::Helper::collection(1)) }.into());
}

force_create {
let collection = T::Helper::collection(0);
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
}: _(SystemOrigin::Root, caller_lookup, default_collection_config::<T, I>())
verify {
assert_last_event::<T, I>(Event::NextCollectionIdIncremented { next_id: Some(T::Helper::collection(1)) }.into());
}
}: _(SystemOrigin::Root, collection, caller_lookup, default_collection_config::<T, I>())

destroy {
let m in 0 .. 1_000;
Expand Down Expand Up @@ -881,6 +877,7 @@ benchmarks_instance_pallet! {
let item = T::Helper::item(0);
assert_ok!(Nfts::<T, I>::force_create(
SystemOrigin::Root.into(),
collection.clone(),
caller_lookup.clone(),
default_collection_config::<T, I>()
));
Expand Down
19 changes: 0 additions & 19 deletions pallets/nfts/src/common_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,4 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

Ok(())
}

pub(crate) fn set_next_collection_id(collection: T::CollectionId) {
let next_id = collection.increment();
NextCollectionId::<T, I>::set(next_id.clone());
Self::deposit_event(Event::NextCollectionIdIncremented { next_id });
}

#[allow(missing_docs)]
#[cfg(any(test, feature = "runtime-benchmarks"))]
pub fn set_next_id(id: T::CollectionId) {
NextCollectionId::<T, I>::set(Some(id));
}

#[cfg(test)]
pub fn get_next_id() -> T::CollectionId {
NextCollectionId::<T, I>::get()
.or(T::CollectionId::initial_value())
.expect("Failed to get next collection ID")
}
}
33 changes: 4 additions & 29 deletions pallets/nfts/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,36 +151,11 @@ impl<T: Config<I>, I: 'static> Create<<T as SystemConfig>::AccountId, Collection
{
/// Create a `collection` of nonfungible items to be owned by `who` and managed by `admin`.
fn create_collection(
who: &T::AccountId,
admin: &T::AccountId,
config: &CollectionConfigFor<T, I>,
_who: &T::AccountId,
_admin: &T::AccountId,
_config: &CollectionConfigFor<T, I>,
) -> Result<T::CollectionId, DispatchError> {
// DepositRequired can be disabled by calling the force_create() only
ensure!(
!config.has_disabled_setting(CollectionSetting::DepositRequired),
Error::<T, I>::WrongSetting
);

let collection = NextCollectionId::<T, I>::get()
.or(T::CollectionId::initial_value())
.ok_or(Error::<T, I>::UnknownCollection)?;

Self::do_create_collection(
collection.clone(),
who.clone(),
admin.clone(),
config.clone(),
T::CollectionDeposit::get(),
Event::Created {
collection: collection.clone(),
creator: who.clone(),
owner: admin.clone(),
},
)?;

Self::set_next_collection_id(collection.clone());

Ok(collection)
unimplemented!("This function can be used with XCM location.");
}

/// Create a collection of nonfungible items with `collection` Id to be owned by `who` and
Expand Down
29 changes: 5 additions & 24 deletions pallets/nfts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ use core::cmp::Ordering;

use codec::{Decode, Encode};
use frame_support::traits::{
tokens::Locker, BalanceStatus::Reserved, Currency, EnsureOriginWithArg, Incrementable,
ReservableCurrency,
tokens::Locker, BalanceStatus::Reserved, Currency, EnsureOriginWithArg, ReservableCurrency,
};
use frame_system::Config as SystemConfig;
pub use pallet::*;
Expand Down Expand Up @@ -140,15 +139,7 @@ pub mod pallet {
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Identifier for the collection of item.
///
/// SAFETY: The functions in the `Incrementable` trait are fallible. If the functions
/// of the implementation both return `None`, the automatic CollectionId generation
/// should not be used. So the `create` and `force_create` extrinsics and the
/// `create_collection` function will return an `UnknownCollection` Error. Instead use
/// the `create_collection_with_id` function. However, if the `Incrementable` trait
/// implementation has an incremental order, the `create_collection_with_id` function
/// should not be used as it can claim a value in the ID sequence.
type CollectionId: Member + Parameter + MaxEncodedLen + Clone + Incrementable;
type CollectionId: Member + Parameter + MaxEncodedLen + Clone;

/// The type used to identify a unique item within a collection.
type ItemId: Member + Parameter + MaxEncodedLen + Copy;
Expand Down Expand Up @@ -569,8 +560,6 @@ pub mod pallet {
CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 },
/// Mint settings for a collection had changed.
CollectionMintSettingsUpdated { collection: T::CollectionId },
/// Event gets emitted when the `NextCollectionId` gets incremented.
NextCollectionIdIncremented { next_id: Option<T::CollectionId> },
/// The price was set for the item.
ItemPriceSet {
collection: T::CollectionId,
Expand Down Expand Up @@ -774,13 +763,10 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::create())]
pub fn create(
origin: OriginFor<T>,
collection: T::CollectionId,
admin: AccountIdLookupOf<T>,
config: CollectionConfigFor<T, I>,
) -> DispatchResult {
let collection = NextCollectionId::<T, I>::get()
.or(T::CollectionId::initial_value())
.ok_or(Error::<T, I>::UnknownCollection)?;

let owner = T::CreateOrigin::ensure_origin(origin, &collection)?;
let admin = T::Lookup::lookup(admin)?;

Expand All @@ -799,7 +785,6 @@ pub mod pallet {
Event::Created { collection: collection.clone(), creator: owner, owner: admin },
)?;

Self::set_next_collection_id(collection);
Ok(())
}

Expand All @@ -822,26 +807,22 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force_create())]
pub fn force_create(
origin: OriginFor<T>,
collection: T::CollectionId,
owner: AccountIdLookupOf<T>,
config: CollectionConfigFor<T, I>,
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
let owner = T::Lookup::lookup(owner)?;

let collection = NextCollectionId::<T, I>::get()
.or(T::CollectionId::initial_value())
.ok_or(Error::<T, I>::UnknownCollection)?;

Self::do_create_collection(
collection.clone(),
owner.clone(),
owner.clone(),
config,
Zero::zero(),
Event::ForceCreated { collection: collection.clone(), owner },
Event::ForceCreated { collection, owner },
)?;

Self::set_next_collection_id(collection);
Ok(())
}

Expand Down
Loading
Loading