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

Auto-incremental CollectionId #11796

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6020e33
autoincrementing CollectionId
Szegoo Jul 7, 2022
995ee51
fix
Szegoo Jul 7, 2022
d109bb1
benchmarking fix
Szegoo Jul 7, 2022
bc1544d
fmt
Szegoo Jul 7, 2022
6d75a4a
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 8, 2022
56bbfa7
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 11, 2022
dd1be35
fix
Szegoo Jul 11, 2022
9945968
Merge branch 'autoincrement-collectionId' of https://github.com/Szego…
Szegoo Jul 11, 2022
6756619
update before checking
Szegoo Jul 11, 2022
72c5014
fmt
Szegoo Jul 11, 2022
3a26210
fix
Szegoo Jul 11, 2022
b974c52
fmt
Szegoo Jul 11, 2022
95c2aaf
commit
Szegoo Jul 11, 2022
3acff15
tests & fix
Jul 12, 2022
364f01b
fix
Jul 12, 2022
d75b565
commit
Szegoo Jul 12, 2022
0ac9b35
docs
Szegoo Jul 12, 2022
9001ca7
safe math
Szegoo Jul 12, 2022
83d4233
unexpose function
Szegoo Jul 12, 2022
036a494
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 12, 2022
8d76e7d
benchmark
Szegoo Jul 12, 2022
1a05a4e
fmt
Szegoo Jul 12, 2022
4c03867
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 12, 2022
3e4d3c3
better naming
Szegoo Jul 13, 2022
a0d4c88
Merge branch 'autoincrement-collectionId' of https://github.com/Szego…
Szegoo Jul 13, 2022
c946847
fix?
Szegoo Jul 13, 2022
ab21c59
Merge branch 'master' into autoincrement-collectionId
Szegoo Jul 14, 2022
1191946
merge fixes
Szegoo Jul 14, 2022
8fb3fbc
fmt
Szegoo Jul 14, 2022
2a991e7
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jul 15, 2022
f51f9f6
".git/.scripts/bench-bot.sh" pallet dev pallet_uniques
Jul 15, 2022
16e2267
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 15, 2022
47ff81a
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 21, 2022
0b4dce4
wrong weight
Szegoo Jul 21, 2022
45a21e0
Update frame/uniques/src/lib.rs
Szegoo Jul 25, 2022
7066000
Update frame/uniques/src/lib.rs
Szegoo Jul 25, 2022
e37673d
using substrate trait instead of num-traits
Szegoo Jul 25, 2022
2342b07
remove unnecessary trait
Szegoo Jul 25, 2022
576b21c
emit NextCollectionIdIncremented in do_create_collection
Szegoo Jul 25, 2022
167c84c
fix in benchmarks
Szegoo Jul 25, 2022
9319f76
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 25, 2022
01f0f20
check for event & group import
Szegoo Jul 25, 2022
fb33c9a
Merge branch 'autoincrement-collectionId' of https://github.com/Szego…
Szegoo Jul 25, 2022
87effd9
Merge branch 'paritytech:master' into autoincrement-collectionId
Szegoo Jul 30, 2022
bf61196
docs
Szegoo Jul 30, 2022
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
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frame/uniques/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ frame-support = { version = "4.0.0-dev", default-features = false, path = "../su
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" }
num-traits = { version = "0.2.15", default-features = false }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
Expand Down
15 changes: 6 additions & 9 deletions frame/uniques/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ fn create_collection<T: Config<I>, I: 'static>(
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
assert!(Uniques::<T, I>::force_create(
SystemOrigin::Root.into(),
collection,
caller_lookup.clone(),
false,
)
.is_ok());
assert!(
Uniques::<T, I>::force_create(SystemOrigin::Root.into(), caller_lookup.clone(), false,)
.is_ok()
);
(collection, caller, caller_lookup)
}

Expand Down Expand Up @@ -143,7 +140,7 @@ 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 { collection, admin };
let call = Call::<T, I>::create { admin };
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_last_event::<T, I>(Event::Created { collection: T::Helper::collection(0), creator: caller.clone(), owner: caller }.into());
Expand All @@ -152,7 +149,7 @@ benchmarks_instance_pallet! {
force_create {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
}: _(SystemOrigin::Root, T::Helper::collection(0), caller_lookup, true)
}: _(SystemOrigin::Root, caller_lookup, true)
verify {
assert_last_event::<T, I>(Event::ForceCreated { collection: T::Helper::collection(0), owner: caller }.into());
}
Expand Down
14 changes: 12 additions & 2 deletions frame/uniques/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
free_holding: bool,
event: Event<T, I>,
) -> DispatchResult {
ensure!(!Collection::<T, I>::contains_key(collection), Error::<T, I>::InUse);

T::Currency::reserve(&owner, deposit)?;

Collection::<T, I>::insert(
Expand All @@ -85,10 +83,22 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
);

CollectionAccount::<T, I>::insert(&owner, &collection, ());

Self::deposit_event(event);
Ok(())
}

pub fn try_increment_id(
last_collection: T::CollectionId,
) -> Result<T::CollectionId, Error<T, I>> {
let new_collection = last_collection + T::CollectionId::one();
Szegoo marked this conversation as resolved.
Show resolved Hide resolved

CollectionsCount::<T, I>::set(new_collection);
ensure!(!Collection::<T, I>::contains_key(new_collection), Error::<T, I>::InUse);
Szegoo marked this conversation as resolved.
Show resolved Hide resolved

Ok(new_collection)
}

pub fn do_destroy_collection(
collection: T::CollectionId,
witness: DestroyWitness,
Expand Down
25 changes: 20 additions & 5 deletions frame/uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ use sp_runtime::{
};
use sp_std::prelude::*;

use num_traits::{FromPrimitive, One, Unsigned};

pub use pallet::*;
pub use types::*;
pub use weights::WeightInfo;
Expand Down Expand Up @@ -88,7 +90,13 @@ pub mod pallet {
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;

/// Identifier for the collection of item.
type CollectionId: Member + Parameter + MaxEncodedLen + Copy;
type CollectionId: Member
+ Parameter
+ MaxEncodedLen
+ Copy
+ Default
+ Unsigned
+ FromPrimitive;

/// The type used to identify a unique item within a collection.
type ItemId: Member + Parameter + MaxEncodedLen + Copy;
Expand Down Expand Up @@ -250,6 +258,11 @@ pub mod pallet {
pub(super) type CollectionMaxSupply<T: Config<I>, I: 'static = ()> =
StorageMap<_, Blake2_128Concat, T::CollectionId, u32, OptionQuery>;

#[pallet::storage]
/// Stores the last CollectionId of the collections.
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
pub(super) type CollectionsCount<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::CollectionId, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
Expand Down Expand Up @@ -400,7 +413,6 @@ pub mod pallet {
/// `ItemDeposit` funds of sender are reserved.
///
/// Parameters:
/// - `collection`: The identifier of the new collection. This must not be currently in use.
/// - `admin`: The admin of this collection. The admin is the initial address of each
/// member of the collection's admin team.
///
Expand All @@ -410,9 +422,11 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::create())]
pub fn create(
origin: OriginFor<T>,
collection: T::CollectionId,
admin: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let last_collection = CollectionsCount::<T, I>::get();
let collection = Self::try_increment_id(last_collection)?;

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

Expand All @@ -434,7 +448,6 @@ pub mod pallet {
///
/// Unlike `create`, no funds are reserved.
///
/// - `collection`: The identifier of the new item. This must not be currently in use.
/// - `owner`: The owner of this collection of items. The owner has full superuser
/// permissions
/// over this item, but may later change and configure the permissions using
Expand All @@ -446,13 +459,15 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force_create())]
pub fn force_create(
origin: OriginFor<T>,
collection: T::CollectionId,
owner: <T::Lookup as StaticLookup>::Source,
free_holding: bool,
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
let owner = T::Lookup::lookup(owner)?;

let last_collection = CollectionsCount::<T, I>::get();
let collection = Self::try_increment_id(last_collection)?;

Self::do_create_collection(
collection,
owner.clone(),
Expand Down
Loading