@@ -51,7 +51,7 @@ use frame_support::{
51
51
} ;
52
52
use frame_system:: Config as SystemConfig ;
53
53
use sp_runtime:: {
54
- traits:: { Saturating , StaticLookup , Zero } ,
54
+ traits:: { AtLeast32BitUnsigned , Saturating , StaticLookup , Zero } ,
55
55
ArithmeticError , RuntimeDebug ,
56
56
} ;
57
57
use sp_std:: prelude:: * ;
@@ -92,7 +92,12 @@ pub mod pallet {
92
92
type Event : From < Event < Self , I > > + IsType < <Self as frame_system:: Config >:: Event > ;
93
93
94
94
/// Identifier for the collection of item.
95
- type CollectionId : Member + Parameter + MaxEncodedLen + Copy ;
95
+ type CollectionId : Member
96
+ + Parameter
97
+ + MaxEncodedLen
98
+ + Copy
99
+ + Default
100
+ + AtLeast32BitUnsigned ;
96
101
97
102
/// The type used to identify a unique item within a collection.
98
103
type ItemId : Member + Parameter + MaxEncodedLen + Copy ;
@@ -266,6 +271,12 @@ pub mod pallet {
266
271
pub ( super ) type CollectionMaxSupply < T : Config < I > , I : ' static = ( ) > =
267
272
StorageMap < _ , Blake2_128Concat , T :: CollectionId , u32 , OptionQuery > ;
268
273
274
+ #[ pallet:: storage]
275
+ /// Stores the `CollectionId` that is going to be used for the next collection.
276
+ /// This gets incremented by 1 whenever a new collection is created.
277
+ pub ( super ) type NextCollectionId < T : Config < I > , I : ' static = ( ) > =
278
+ StorageValue < _ , T :: CollectionId , ValueQuery > ;
279
+
269
280
#[ pallet:: event]
270
281
#[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
271
282
pub enum Event < T : Config < I > , I : ' static = ( ) > {
@@ -357,6 +368,8 @@ pub mod pallet {
357
368
OwnershipAcceptanceChanged { who : T :: AccountId , maybe_collection : Option < T :: CollectionId > } ,
358
369
/// Max supply has been set for a collection.
359
370
CollectionMaxSupplySet { collection : T :: CollectionId , max_supply : u32 } ,
371
+ /// Event gets emmited when the `NextCollectionId` gets incremented.
372
+ NextCollectionIdIncremented { next_id : T :: CollectionId } ,
360
373
/// The price was set for the instance.
361
374
ItemPriceSet {
362
375
collection : T :: CollectionId ,
@@ -408,6 +421,10 @@ pub mod pallet {
408
421
MaxSupplyAlreadySet ,
409
422
/// The provided max supply is less to the amount of items a collection already has.
410
423
MaxSupplyTooSmall ,
424
+ /// The `CollectionId` in `NextCollectionId` is not being used.
425
+ ///
426
+ /// This means that you can directly proceed to call `create`.
427
+ NextIdNotUsed ,
411
428
/// The given item ID is unknown.
412
429
UnknownItem ,
413
430
/// Item is not for sale.
@@ -439,7 +456,6 @@ pub mod pallet {
439
456
/// `ItemDeposit` funds of sender are reserved.
440
457
///
441
458
/// Parameters:
442
- /// - `collection`: The identifier of the new collection. This must not be currently in use.
443
459
/// - `admin`: The admin of this collection. The admin is the initial address of each
444
460
/// member of the collection's admin team.
445
461
///
@@ -449,9 +465,10 @@ pub mod pallet {
449
465
#[ pallet:: weight( T :: WeightInfo :: create( ) ) ]
450
466
pub fn create (
451
467
origin : OriginFor < T > ,
452
- collection : T :: CollectionId ,
453
468
admin : <T :: Lookup as StaticLookup >:: Source ,
454
469
) -> DispatchResult {
470
+ let collection = NextCollectionId :: < T , I > :: get ( ) ;
471
+
455
472
let owner = T :: CreateOrigin :: ensure_origin ( origin, & collection) ?;
456
473
let admin = T :: Lookup :: lookup ( admin) ?;
457
474
@@ -473,7 +490,6 @@ pub mod pallet {
473
490
///
474
491
/// Unlike `create`, no funds are reserved.
475
492
///
476
- /// - `collection`: The identifier of the new item. This must not be currently in use.
477
493
/// - `owner`: The owner of this collection of items. The owner has full superuser
478
494
/// permissions
479
495
/// over this item, but may later change and configure the permissions using
@@ -485,13 +501,14 @@ pub mod pallet {
485
501
#[ pallet:: weight( T :: WeightInfo :: force_create( ) ) ]
486
502
pub fn force_create (
487
503
origin : OriginFor < T > ,
488
- collection : T :: CollectionId ,
489
504
owner : <T :: Lookup as StaticLookup >:: Source ,
490
505
free_holding : bool ,
491
506
) -> DispatchResult {
492
507
T :: ForceOrigin :: ensure_origin ( origin) ?;
493
508
let owner = T :: Lookup :: lookup ( owner) ?;
494
509
510
+ let collection = NextCollectionId :: < T , I > :: get ( ) ;
511
+
495
512
Self :: do_create_collection (
496
513
collection,
497
514
owner. clone ( ) ,
@@ -502,6 +519,31 @@ pub mod pallet {
502
519
)
503
520
}
504
521
522
+ /// Increments the `CollectionId` stored in `NextCollectionId`.
523
+ ///
524
+ /// This is only callable when the next `CollectionId` is already being
525
+ /// used for some other collection.
526
+ ///
527
+ /// The origin must be Signed and the sender must have sufficient funds
528
+ /// free.
529
+ ///
530
+ /// Emits `NextCollectionIdIncremented` event when successful.
531
+ ///
532
+ /// Weight: `O(1)`
533
+ #[ pallet:: weight( T :: WeightInfo :: try_increment_id( ) ) ]
534
+ pub fn try_increment_id ( origin : OriginFor < T > ) -> DispatchResult {
535
+ ensure_signed ( origin) ?;
536
+ ensure ! (
537
+ Collection :: <T , I >:: contains_key( NextCollectionId :: <T , I >:: get( ) ) ,
538
+ Error :: <T , I >:: NextIdNotUsed
539
+ ) ;
540
+
541
+ let next_id = NextCollectionId :: < T , I > :: get ( ) . saturating_add ( 1u32 . into ( ) ) ;
542
+ NextCollectionId :: < T , I > :: set ( next_id) ;
543
+ Self :: deposit_event ( Event :: NextCollectionIdIncremented { next_id } ) ;
544
+ Ok ( ( ) )
545
+ }
546
+
505
547
/// Destroy a collection of fungible items.
506
548
///
507
549
/// The origin must conform to `ForceOrigin` or must be `Signed` and the sender must be the
0 commit comments