From 6020e33adcf76e9456fc7bd7a5a7348dd5392514 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 7 Jul 2022 08:20:17 +0200 Subject: [PATCH 01/32] autoincrementing CollectionId --- Cargo.lock | 5 ++-- frame/uniques/Cargo.toml | 1 + frame/uniques/src/functions.rs | 4 ++-- frame/uniques/src/lib.rs | 23 +++++++++++++++---- frame/uniques/src/tests.rs | 42 +++++++++++++++++----------------- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d68330cfb15c2..58a79ffb71a79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5123,9 +5123,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg 1.0.1", "libm", @@ -6521,6 +6521,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "num-traits", "pallet-balances", "parity-scale-codec", "scale-info", diff --git a/frame/uniques/Cargo.toml b/frame/uniques/Cargo.toml index 19b0790947f84..ef30c3f39e776 100644 --- a/frame/uniques/Cargo.toml +++ b/frame/uniques/Cargo.toml @@ -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 = "0.2.15" [dev-dependencies] pallet-balances = { version = "4.0.0-dev", path = "../balances" } diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 155fb35ef0999..de38780ca3610 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -64,8 +64,6 @@ impl, I: 'static> Pallet { free_holding: bool, event: Event, ) -> DispatchResult { - ensure!(!Collection::::contains_key(collection), Error::::InUse); - T::Currency::reserve(&owner, deposit)?; Collection::::insert( @@ -85,6 +83,8 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); + CollectionsCount::::set(collection + T::CollectionId::one()); + Self::deposit_event(event); Ok(()) } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 5c7adeac0eff2..22564fe42b3a6 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -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; @@ -88,7 +90,13 @@ pub mod pallet { type Event: From> + IsType<::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; @@ -250,6 +258,11 @@ pub mod pallet { pub(super) type CollectionMaxSupply, I: 'static = ()> = StorageMap<_, Blake2_128Concat, T::CollectionId, u32, OptionQuery>; + #[pallet::storage] + /// Stores the last CollectionId of the collections. + pub(super) type CollectionsCount, I: 'static = ()> = + StorageValue<_, T::CollectionId, ValueQuery>; + #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event, I: 'static = ()> { @@ -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. /// @@ -410,9 +422,10 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::create())] pub fn create( origin: OriginFor, - collection: T::CollectionId, admin: ::Source, ) -> DispatchResult { + let collection = CollectionsCount::::get(); + let owner = T::CreateOrigin::ensure_origin(origin, &collection)?; let admin = T::Lookup::lookup(admin)?; @@ -434,7 +447,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 @@ -446,13 +458,14 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::force_create())] pub fn force_create( origin: OriginFor, - collection: T::CollectionId, owner: ::Source, free_holding: bool, ) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; + let collection = CollectionsCount::::get(); + Self::do_create_collection( collection, owner.clone(), diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index ef28a2143e84a..133925711dd1c 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -92,12 +92,12 @@ fn basic_setup_works() { #[test] fn basic_minting_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_eq!(collections(), vec![(1, 0)]); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); assert_eq!(items(), vec![(1, 0, 42)]); - assert_ok!(Uniques::force_create(Origin::root(), 1, 2, true)); + assert_ok!(Uniques::force_create(Origin::root(), 2, true)); assert_eq!(collections(), vec![(1, 0), (2, 1)]); assert_ok!(Uniques::mint(Origin::signed(2), 1, 69, 1)); assert_eq!(items(), vec![(1, 0, 42), (1, 1, 69)]); @@ -108,7 +108,7 @@ fn basic_minting_should_work() { fn lifecycle_should_work() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::create(Origin::signed(1), 0, 1)); + assert_ok!(Uniques::create(Origin::signed(1), 1)); assert_eq!(Balances::reserved_balance(&1), 2); assert_eq!(collections(), vec![(1, 0)]); assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0, 0], false)); @@ -151,7 +151,7 @@ fn lifecycle_should_work() { fn destroy_with_bad_witness_should_not_work() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::create(Origin::signed(1), 0, 1)); + assert_ok!(Uniques::create(Origin::signed(1), 1)); let w = Collection::::get(0).unwrap().destroy_witness(); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); @@ -162,7 +162,7 @@ fn destroy_with_bad_witness_should_not_work() { #[test] fn mint_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); assert_eq!(Uniques::owner(0, 42).unwrap(), 1); assert_eq!(collections(), vec![(1, 0)]); @@ -173,7 +173,7 @@ fn mint_should_work() { #[test] fn transfer_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 3)); @@ -188,7 +188,7 @@ fn transfer_should_work() { #[test] fn freezing_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); assert_ok!(Uniques::freeze(Origin::signed(1), 0, 42)); assert_noop!(Uniques::transfer(Origin::signed(1), 0, 42, 2), Error::::Frozen); @@ -205,7 +205,7 @@ fn freezing_should_work() { #[test] fn origin_guards_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); Balances::make_free_balance_be(&2, 100); @@ -230,7 +230,7 @@ fn transfer_owner_should_work() { Balances::make_free_balance_be(&1, 100); Balances::make_free_balance_be(&2, 100); Balances::make_free_balance_be(&3, 100); - assert_ok!(Uniques::create(Origin::signed(1), 0, 1)); + assert_ok!(Uniques::create(Origin::signed(1), 1)); assert_eq!(collections(), vec![(1, 0)]); assert_noop!( Uniques::transfer_ownership(Origin::signed(1), 0, 2), @@ -275,7 +275,7 @@ fn transfer_owner_should_work() { #[test] fn set_team_should_work() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); assert_ok!(Uniques::mint(Origin::signed(2), 0, 42, 2)); @@ -294,7 +294,7 @@ fn set_collection_metadata_should_work() { Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 20], false), Error::::UnknownCollection, ); - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); // Cannot add metadata to unowned item assert_noop!( Uniques::set_collection_metadata(Origin::signed(2), 0, bvec![0u8; 20], false), @@ -354,7 +354,7 @@ fn set_item_metadata_should_work() { Balances::make_free_balance_be(&1, 30); // Cannot add metadata to unknown item - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); // Cannot add metadata to unowned item assert_noop!( @@ -410,7 +410,7 @@ fn set_attribute_should_work() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); @@ -455,7 +455,7 @@ fn set_attribute_should_respect_freeze() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); @@ -487,7 +487,7 @@ fn force_item_status_should_work() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 69, 2)); assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0; 20], false)); @@ -521,7 +521,7 @@ fn force_item_status_should_work() { fn burn_works() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, false)); + assert_ok!(Uniques::force_create(Origin::root(), 1, false)); assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); assert_noop!( @@ -545,7 +545,7 @@ fn burn_works() { #[test] fn approval_lifecycle_works() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); assert_ok!(Uniques::transfer(Origin::signed(3), 0, 42, 4)); @@ -560,7 +560,7 @@ fn approval_lifecycle_works() { #[test] fn cancel_approval_works() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); @@ -592,7 +592,7 @@ fn cancel_approval_works() { #[test] fn cancel_approval_works_with_admin() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); @@ -620,7 +620,7 @@ fn cancel_approval_works_with_admin() { #[test] fn cancel_approval_works_with_force() { new_test_ext().execute_with(|| { - assert_ok!(Uniques::force_create(Origin::root(), 0, 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); @@ -653,7 +653,7 @@ fn max_supply_should_work() { let max_supply = 2; // validate set_collection_max_supply - assert_ok!(Uniques::force_create(Origin::root(), collection_id, user_id, true)); + assert_ok!(Uniques::force_create(Origin::root(), user_id, true)); assert!(!CollectionMaxSupply::::contains_key(collection_id)); assert_ok!(Uniques::set_collection_max_supply( From 995ee516378df71919f0ec4cde1f7d2767aba354 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 7 Jul 2022 08:46:31 +0200 Subject: [PATCH 02/32] fix --- frame/uniques/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/Cargo.toml b/frame/uniques/Cargo.toml index ef30c3f39e776..1f8be1566b43b 100644 --- a/frame/uniques/Cargo.toml +++ b/frame/uniques/Cargo.toml @@ -21,7 +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 = "0.2.15" +num-traits = { version = "0.2.15", default-features = false } [dev-dependencies] pallet-balances = { version = "4.0.0-dev", path = "../balances" } From d109bb1aceab9e8787e663b84acc8d5ef598402b Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 7 Jul 2022 09:17:01 +0200 Subject: [PATCH 03/32] benchmarking fix --- frame/uniques/src/benchmarking.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index 14a3e2c61809a..2af46a7b77139 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -44,7 +44,6 @@ fn create_collection, I: 'static>( T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); assert!(Uniques::::force_create( SystemOrigin::Root.into(), - collection, caller_lookup.clone(), false, ) @@ -143,7 +142,7 @@ benchmarks_instance_pallet! { whitelist_account!(caller); let admin = T::Lookup::unlookup(caller.clone()); T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); - let call = Call::::create { collection, admin }; + let call = Call::::create { admin }; }: { call.dispatch_bypass_filter(origin)? } verify { assert_last_event::(Event::Created { collection: T::Helper::collection(0), creator: caller.clone(), owner: caller }.into()); @@ -152,7 +151,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::(Event::ForceCreated { collection: T::Helper::collection(0), owner: caller }.into()); } From bc1544d67ea958b61ecaeed06efae9312e45ff0d Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 7 Jul 2022 09:21:19 +0200 Subject: [PATCH 04/32] fmt --- frame/uniques/src/benchmarking.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index 2af46a7b77139..46c347b3c42e9 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -42,12 +42,10 @@ fn create_collection, I: 'static>( let caller_lookup = T::Lookup::unlookup(caller.clone()); let collection = T::Helper::collection(0); T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); - assert!(Uniques::::force_create( - SystemOrigin::Root.into(), - caller_lookup.clone(), - false, - ) - .is_ok()); + assert!( + Uniques::::force_create(SystemOrigin::Root.into(), caller_lookup.clone(), false,) + .is_ok() + ); (collection, caller, caller_lookup) } From dd1be35f506ab7817a2c39604983badb2204a8f1 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 12:33:44 +0200 Subject: [PATCH 05/32] fix --- frame/uniques/src/functions.rs | 11 +- frame/uniques/src/lib.rs | 6 +- frame/uniques/src/tests.rs | 350 ++++++++++++++++----------------- 3 files changed, 189 insertions(+), 178 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index de38780ca3610..ef3549f40b44d 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -83,12 +83,21 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); - CollectionsCount::::set(collection + T::CollectionId::one()); Self::deposit_event(event); Ok(()) } + pub fn try_increment_id( + last_collection: T::CollectionId, + ) -> Result> { + let new_collection = last_collection + T::CollectionId::one(); + ensure!(!Collection::::contains_key(new_collection), Error::::InUse); + + CollectionsCount::::set(new_collection); + Ok(new_collection) + } + pub fn do_destroy_collection( collection: T::CollectionId, witness: DestroyWitness, diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 22564fe42b3a6..3bb49ff854bf9 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -424,7 +424,8 @@ pub mod pallet { origin: OriginFor, admin: ::Source, ) -> DispatchResult { - let collection = CollectionsCount::::get(); + let last_collection = CollectionsCount::::get(); + let collection = Self::try_increment_id(last_collection)?; let owner = T::CreateOrigin::ensure_origin(origin, &collection)?; let admin = T::Lookup::lookup(admin)?; @@ -464,7 +465,8 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; - let collection = CollectionsCount::::get(); + let last_collection = CollectionsCount::::get(); + let collection = Self::try_increment_id(last_collection)?; Self::do_create_collection( collection, diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 133925711dd1c..9ecc6e507df14 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -93,14 +93,14 @@ fn basic_setup_works() { fn basic_minting_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_eq!(collections(), vec![(1, 0)]); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_eq!(items(), vec![(1, 0, 42)]); + assert_eq!(collections(), vec![(1, 1)]); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_eq!(items(), vec![(1, 1, 42)]); assert_ok!(Uniques::force_create(Origin::root(), 2, true)); - assert_eq!(collections(), vec![(1, 0), (2, 1)]); - assert_ok!(Uniques::mint(Origin::signed(2), 1, 69, 1)); - assert_eq!(items(), vec![(1, 0, 42), (1, 1, 69)]); + assert_eq!(collections(), vec![(1, 1), (2, 2)]); + assert_ok!(Uniques::mint(Origin::signed(2), 2, 69, 1)); + assert_eq!(items(), vec![(1, 1, 42), (1, 2, 69)]); }); } @@ -110,30 +110,30 @@ fn lifecycle_should_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); assert_eq!(Balances::reserved_balance(&1), 2); - assert_eq!(collections(), vec![(1, 0)]); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0, 0], false)); + assert_eq!(collections(), vec![(1, 1)]); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0, 0], false)); assert_eq!(Balances::reserved_balance(&1), 5); - assert!(CollectionMetadataOf::::contains_key(0)); + assert!(CollectionMetadataOf::::contains_key(1)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 10)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 10)); assert_eq!(Balances::reserved_balance(&1), 6); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 69, 20)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 69, 20)); assert_eq!(Balances::reserved_balance(&1), 7); - assert_eq!(items(), vec![(10, 0, 42), (20, 0, 69)]); - assert_eq!(Collection::::get(0).unwrap().items, 2); - assert_eq!(Collection::::get(0).unwrap().item_metadatas, 0); + assert_eq!(items(), vec![(10, 1, 42), (20, 1, 69)]); + assert_eq!(Collection::::get(1).unwrap().items, 2); + assert_eq!(Collection::::get(1).unwrap().item_metadatas, 0); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![42, 42], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![42, 42], false)); assert_eq!(Balances::reserved_balance(&1), 10); - assert!(ItemMetadataOf::::contains_key(0, 42)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![69, 69], false)); + assert!(ItemMetadataOf::::contains_key(1, 42)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![69, 69], false)); assert_eq!(Balances::reserved_balance(&1), 13); - assert!(ItemMetadataOf::::contains_key(0, 69)); + assert!(ItemMetadataOf::::contains_key(1, 69)); - let w = Collection::::get(0).unwrap().destroy_witness(); + let w = Collection::::get(1).unwrap().destroy_witness(); assert_eq!(w.items, 2); assert_eq!(w.item_metadatas, 2); - assert_ok!(Uniques::destroy(Origin::signed(1), 0, w)); + assert_ok!(Uniques::destroy(Origin::signed(1), 1, w)); assert_eq!(Balances::reserved_balance(&1), 0); assert!(!Collection::::contains_key(0)); @@ -153,9 +153,9 @@ fn destroy_with_bad_witness_should_not_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); - let w = Collection::::get(0).unwrap().destroy_witness(); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_noop!(Uniques::destroy(Origin::signed(1), 0, w), Error::::BadWitness); + let w = Collection::::get(1).unwrap().destroy_witness(); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_noop!(Uniques::destroy(Origin::signed(1), 1, w), Error::::BadWitness); }); } @@ -163,10 +163,10 @@ fn destroy_with_bad_witness_should_not_work() { fn mint_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_eq!(Uniques::owner(0, 42).unwrap(), 1); - assert_eq!(collections(), vec![(1, 0)]); - assert_eq!(items(), vec![(1, 0, 42)]); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_eq!(Uniques::owner(1, 42).unwrap(), 1); + assert_eq!(collections(), vec![(1, 1)]); + assert_eq!(items(), vec![(1, 1, 42)]); }); } @@ -174,14 +174,14 @@ fn mint_should_work() { fn transfer_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 3)); - assert_eq!(items(), vec![(3, 0, 42)]); - assert_noop!(Uniques::transfer(Origin::signed(2), 0, 42, 4), Error::::NoPermission); + assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 3)); + assert_eq!(items(), vec![(3, 1, 42)]); + assert_noop!(Uniques::transfer(Origin::signed(2), 1, 42, 4), Error::::NoPermission); - assert_ok!(Uniques::approve_transfer(Origin::signed(3), 0, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 4)); + assert_ok!(Uniques::approve_transfer(Origin::signed(3), 1, 42, 2)); + assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 4)); }); } @@ -189,16 +189,16 @@ fn transfer_should_work() { fn freezing_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_ok!(Uniques::freeze(Origin::signed(1), 0, 42)); - assert_noop!(Uniques::transfer(Origin::signed(1), 0, 42, 2), Error::::Frozen); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_ok!(Uniques::freeze(Origin::signed(1), 1, 42)); + assert_noop!(Uniques::transfer(Origin::signed(1), 1, 42, 2), Error::::Frozen); - assert_ok!(Uniques::thaw(Origin::signed(1), 0, 42)); - assert_ok!(Uniques::freeze_collection(Origin::signed(1), 0)); - assert_noop!(Uniques::transfer(Origin::signed(1), 0, 42, 2), Error::::Frozen); + assert_ok!(Uniques::thaw(Origin::signed(1), 1, 42)); + assert_ok!(Uniques::freeze_collection(Origin::signed(1), 1)); + assert_noop!(Uniques::transfer(Origin::signed(1), 1, 42, 2), Error::::Frozen); - assert_ok!(Uniques::thaw_collection(Origin::signed(1), 0)); - assert_ok!(Uniques::transfer(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::thaw_collection(Origin::signed(1), 1)); + assert_ok!(Uniques::transfer(Origin::signed(1), 1, 42, 2)); }); } @@ -206,21 +206,21 @@ fn freezing_should_work() { fn origin_guards_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); Balances::make_free_balance_be(&2, 100); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(0))); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(1))); assert_noop!( - Uniques::transfer_ownership(Origin::signed(2), 0, 2), + Uniques::transfer_ownership(Origin::signed(2), 1, 2), Error::::NoPermission ); - assert_noop!(Uniques::set_team(Origin::signed(2), 0, 2, 2, 2), Error::::NoPermission); - assert_noop!(Uniques::freeze(Origin::signed(2), 0, 42), Error::::NoPermission); - assert_noop!(Uniques::thaw(Origin::signed(2), 0, 42), Error::::NoPermission); - assert_noop!(Uniques::mint(Origin::signed(2), 0, 69, 2), Error::::NoPermission); - assert_noop!(Uniques::burn(Origin::signed(2), 0, 42, None), Error::::NoPermission); - let w = Collection::::get(0).unwrap().destroy_witness(); - assert_noop!(Uniques::destroy(Origin::signed(2), 0, w), Error::::NoPermission); + assert_noop!(Uniques::set_team(Origin::signed(2), 1, 2, 2, 2), Error::::NoPermission); + assert_noop!(Uniques::freeze(Origin::signed(2), 1, 42), Error::::NoPermission); + assert_noop!(Uniques::thaw(Origin::signed(2), 1, 42), Error::::NoPermission); + assert_noop!(Uniques::mint(Origin::signed(2), 1, 69, 2), Error::::NoPermission); + assert_noop!(Uniques::burn(Origin::signed(2), 1, 42, None), Error::::NoPermission); + let w = Collection::::get(1).unwrap().destroy_witness(); + assert_noop!(Uniques::destroy(Origin::signed(2), 1, w), Error::::NoPermission); }); } @@ -231,33 +231,33 @@ fn transfer_owner_should_work() { Balances::make_free_balance_be(&2, 100); Balances::make_free_balance_be(&3, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); - assert_eq!(collections(), vec![(1, 0)]); + assert_eq!(collections(), vec![(1, 1)]); assert_noop!( - Uniques::transfer_ownership(Origin::signed(1), 0, 2), + Uniques::transfer_ownership(Origin::signed(1), 1, 2), Error::::Unaccepted ); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(0))); - assert_ok!(Uniques::transfer_ownership(Origin::signed(1), 0, 2)); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(1))); + assert_ok!(Uniques::transfer_ownership(Origin::signed(1), 1, 2)); - assert_eq!(collections(), vec![(2, 0)]); + assert_eq!(collections(), vec![(2, 1)]); assert_eq!(Balances::total_balance(&1), 98); assert_eq!(Balances::total_balance(&2), 102); assert_eq!(Balances::reserved_balance(&1), 0); assert_eq!(Balances::reserved_balance(&2), 2); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(1), Some(0))); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(1), Some(1))); assert_noop!( - Uniques::transfer_ownership(Origin::signed(1), 0, 1), + Uniques::transfer_ownership(Origin::signed(1), 1, 1), Error::::NoPermission ); // Mint and set metadata now and make sure that deposit gets transferred back. - assert_ok!(Uniques::set_collection_metadata(Origin::signed(2), 0, bvec![0u8; 20], false)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_ok!(Uniques::set_metadata(Origin::signed(2), 0, 42, bvec![0u8; 20], false)); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(3), Some(0))); - assert_ok!(Uniques::transfer_ownership(Origin::signed(2), 0, 3)); - assert_eq!(collections(), vec![(3, 0)]); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(2), 1, bvec![0u8; 20], false)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_ok!(Uniques::set_metadata(Origin::signed(2), 1, 42, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(3), Some(1))); + assert_ok!(Uniques::transfer_ownership(Origin::signed(2), 1, 3)); + assert_eq!(collections(), vec![(3, 1)]); assert_eq!(Balances::total_balance(&2), 57); assert_eq!(Balances::total_balance(&3), 145); assert_eq!(Balances::reserved_balance(&2), 0); @@ -276,13 +276,13 @@ fn transfer_owner_should_work() { fn set_team_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); + assert_ok!(Uniques::set_team(Origin::signed(1), 1, 2, 3, 4)); - assert_ok!(Uniques::mint(Origin::signed(2), 0, 42, 2)); - assert_ok!(Uniques::freeze(Origin::signed(4), 0, 42)); - assert_ok!(Uniques::thaw(Origin::signed(3), 0, 42)); - assert_ok!(Uniques::transfer(Origin::signed(3), 0, 42, 3)); - assert_ok!(Uniques::burn(Origin::signed(3), 0, 42, None)); + assert_ok!(Uniques::mint(Origin::signed(2), 1, 42, 2)); + assert_ok!(Uniques::freeze(Origin::signed(4), 1, 42)); + assert_ok!(Uniques::thaw(Origin::signed(3), 1, 42)); + assert_ok!(Uniques::transfer(Origin::signed(3), 1, 42, 3)); + assert_ok!(Uniques::burn(Origin::signed(3), 1, 42, None)); }); } @@ -291,60 +291,60 @@ fn set_collection_metadata_should_work() { new_test_ext().execute_with(|| { // Cannot add metadata to unknown item assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 20], false), + Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 20], false), Error::::UnknownCollection, ); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); // Cannot add metadata to unowned item assert_noop!( - Uniques::set_collection_metadata(Origin::signed(2), 0, bvec![0u8; 20], false), + Uniques::set_collection_metadata(Origin::signed(2), 1, bvec![0u8; 20], false), Error::::NoPermission, ); // Successfully add metadata and take deposit Balances::make_free_balance_be(&1, 30); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 20], false)); assert_eq!(Balances::free_balance(&1), 9); - assert!(CollectionMetadataOf::::contains_key(0)); + assert!(CollectionMetadataOf::::contains_key(1)); // Force origin works, too. - assert_ok!(Uniques::set_collection_metadata(Origin::root(), 0, bvec![0u8; 18], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::root(), 1, bvec![0u8; 18], false)); // Update deposit - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], false)); assert_eq!(Balances::free_balance(&1), 14); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 25], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 25], false)); assert_eq!(Balances::free_balance(&1), 4); // Cannot over-reserve assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 40], false), + Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 40], false), BalancesError::::InsufficientBalance, ); // Can't set or clear metadata once frozen - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], true)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], true)); assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], false), + Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], false), Error::::Frozen, ); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(1), 0), + Uniques::clear_collection_metadata(Origin::signed(1), 1), Error::::Frozen ); // Clear Metadata - assert_ok!(Uniques::set_collection_metadata(Origin::root(), 0, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::root(), 1, bvec![0u8; 15], false)); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(2), 0), + Uniques::clear_collection_metadata(Origin::signed(2), 1), Error::::NoPermission ); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(1), 1), + Uniques::clear_collection_metadata(Origin::signed(1), 2), Error::::UnknownCollection ); - assert_ok!(Uniques::clear_collection_metadata(Origin::signed(1), 0)); - assert!(!CollectionMetadataOf::::contains_key(0)); + assert_ok!(Uniques::clear_collection_metadata(Origin::signed(1), 1)); + assert!(!CollectionMetadataOf::::contains_key(1)); }); } @@ -355,52 +355,52 @@ fn set_item_metadata_should_work() { // Cannot add metadata to unknown item assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); // Cannot add metadata to unowned item assert_noop!( - Uniques::set_metadata(Origin::signed(2), 0, 42, bvec![0u8; 20], false), + Uniques::set_metadata(Origin::signed(2), 1, 42, bvec![0u8; 20], false), Error::::NoPermission, ); // Successfully add metadata and take deposit - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 20], false)); assert_eq!(Balances::free_balance(&1), 8); - assert!(ItemMetadataOf::::contains_key(0, 42)); + assert!(ItemMetadataOf::::contains_key(1, 42)); // Force origin works, too. - assert_ok!(Uniques::set_metadata(Origin::root(), 0, 42, bvec![0u8; 18], false)); + assert_ok!(Uniques::set_metadata(Origin::root(), 1, 42, bvec![0u8; 18], false)); // Update deposit - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], false)); assert_eq!(Balances::free_balance(&1), 13); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 25], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 25], false)); assert_eq!(Balances::free_balance(&1), 3); // Cannot over-reserve assert_noop!( - Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 40], false), + Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 40], false), BalancesError::::InsufficientBalance, ); // Can't set or clear metadata once frozen - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], true)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], true)); assert_noop!( - Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], false), + Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], false), Error::::Frozen, ); - assert_noop!(Uniques::clear_metadata(Origin::signed(1), 0, 42), Error::::Frozen); + assert_noop!(Uniques::clear_metadata(Origin::signed(1), 1, 42), Error::::Frozen); // Clear Metadata - assert_ok!(Uniques::set_metadata(Origin::root(), 0, 42, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_metadata(Origin::root(), 1, 42, bvec![0u8; 15], false)); assert_noop!( - Uniques::clear_metadata(Origin::signed(2), 0, 42), + Uniques::clear_metadata(Origin::signed(2), 1, 42), Error::::NoPermission ); assert_noop!( - Uniques::clear_metadata(Origin::signed(1), 1, 42), + Uniques::clear_metadata(Origin::signed(1), 2, 42), Error::::UnknownCollection ); - assert_ok!(Uniques::clear_metadata(Origin::signed(1), 0, 42)); + assert_ok!(Uniques::clear_metadata(Origin::signed(1), 1, 42)); assert!(!ItemMetadataOf::::contains_key(0, 42)); }); } @@ -412,11 +412,11 @@ fn set_attribute_should_work() { assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![1], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![1], bvec![0])); assert_eq!( - attributes(0), + attributes(1), vec![ (None, bvec![0], bvec![0]), (Some(0), bvec![0], bvec![0]), @@ -425,9 +425,9 @@ fn set_attribute_should_work() { ); assert_eq!(Balances::reserved_balance(1), 9); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0; 10])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0; 10])); assert_eq!( - attributes(0), + attributes(1), vec![ (None, bvec![0], bvec![0; 10]), (Some(0), bvec![0], bvec![0]), @@ -436,16 +436,16 @@ fn set_attribute_should_work() { ); assert_eq!(Balances::reserved_balance(1), 18); - assert_ok!(Uniques::clear_attribute(Origin::signed(1), 0, Some(0), bvec![1])); + assert_ok!(Uniques::clear_attribute(Origin::signed(1), 1, Some(0), bvec![1])); assert_eq!( - attributes(0), + attributes(1), vec![(None, bvec![0], bvec![0; 10]), (Some(0), bvec![0], bvec![0]),] ); assert_eq!(Balances::reserved_balance(1), 15); - let w = Collection::::get(0).unwrap().destroy_witness(); - assert_ok!(Uniques::destroy(Origin::signed(1), 0, w)); - assert_eq!(attributes(0), vec![]); + let w = Collection::::get(1).unwrap().destroy_witness(); + assert_ok!(Uniques::destroy(Origin::signed(1), 1, w)); + assert_eq!(attributes(1), vec![]); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -457,11 +457,11 @@ fn set_attribute_should_respect_freeze() { assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(1), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(1), bvec![0], bvec![0])); assert_eq!( - attributes(0), + attributes(1), vec![ (None, bvec![0], bvec![0]), (Some(0), bvec![0], bvec![0]), @@ -470,15 +470,15 @@ fn set_attribute_should_respect_freeze() { ); assert_eq!(Balances::reserved_balance(1), 9); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![], true)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![], true)); let e = Error::::Frozen; - assert_noop!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0]), e); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![1])); + assert_noop!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0]), e); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![1])); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 0, bvec![], true)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 0, bvec![], true)); let e = Error::::Frozen; - assert_noop!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![1]), e); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(1), bvec![0], bvec![1])); + assert_noop!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![1]), e); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(1), bvec![0], bvec![1])); }); } @@ -488,31 +488,31 @@ fn force_item_status_should_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 69, 2)); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![0; 20], false)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 69, 2)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 65); // force item status to be free holding - assert_ok!(Uniques::force_item_status(Origin::root(), 0, 1, 1, 1, 1, true, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 142, 1)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 169, 2)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 142, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 169, bvec![0; 20], false)); + assert_ok!(Uniques::force_item_status(Origin::root(), 1, 1, 1, 1, 1, true, false)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 142, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 169, 2)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 142, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 169, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 65); - assert_ok!(Uniques::redeposit(Origin::signed(1), 0, bvec![0, 42, 50, 69, 100])); + assert_ok!(Uniques::redeposit(Origin::signed(1), 1, bvec![0, 42, 50, 69, 100])); assert_eq!(Balances::reserved_balance(1), 63); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 42); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 21); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0; 20], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -522,22 +522,22 @@ fn burn_works() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); + assert_ok!(Uniques::set_team(Origin::signed(1), 1, 2, 3, 4)); assert_noop!( - Uniques::burn(Origin::signed(5), 0, 42, Some(5)), + Uniques::burn(Origin::signed(5), 1, 42, Some(5)), Error::::UnknownCollection ); - assert_ok!(Uniques::mint(Origin::signed(2), 0, 42, 5)); - assert_ok!(Uniques::mint(Origin::signed(2), 0, 69, 5)); + assert_ok!(Uniques::mint(Origin::signed(2), 1, 42, 5)); + assert_ok!(Uniques::mint(Origin::signed(2), 1, 69, 5)); assert_eq!(Balances::reserved_balance(1), 2); - assert_noop!(Uniques::burn(Origin::signed(0), 0, 42, None), Error::::NoPermission); - assert_noop!(Uniques::burn(Origin::signed(5), 0, 42, Some(6)), Error::::WrongOwner); + assert_noop!(Uniques::burn(Origin::signed(0), 1, 42, None), Error::::NoPermission); + assert_noop!(Uniques::burn(Origin::signed(5), 1, 42, Some(6)), Error::::WrongOwner); - assert_ok!(Uniques::burn(Origin::signed(5), 0, 42, Some(5))); - assert_ok!(Uniques::burn(Origin::signed(3), 0, 69, Some(5))); + assert_ok!(Uniques::burn(Origin::signed(5), 1, 42, Some(5))); + assert_ok!(Uniques::burn(Origin::signed(3), 1, 69, Some(5))); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -546,14 +546,14 @@ fn burn_works() { fn approval_lifecycle_works() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); - assert_ok!(Uniques::transfer(Origin::signed(3), 0, 42, 4)); - assert_noop!(Uniques::transfer(Origin::signed(3), 0, 42, 3), Error::::NoPermission); - assert!(Item::::get(0, 42).unwrap().approved.is_none()); - - assert_ok!(Uniques::approve_transfer(Origin::signed(4), 0, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); + assert_ok!(Uniques::transfer(Origin::signed(3), 1, 42, 4)); + assert_noop!(Uniques::transfer(Origin::signed(3), 1, 42, 3), Error::::NoPermission); + assert!(Item::::get(1, 42).unwrap().approved.is_none()); + + assert_ok!(Uniques::approve_transfer(Origin::signed(4), 1, 42, 2)); + assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 2)); }); } @@ -561,29 +561,29 @@ fn approval_lifecycle_works() { fn cancel_approval_works() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 1, 42, None), + Uniques::cancel_approval(Origin::signed(2), 2, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 0, 43, None), + Uniques::cancel_approval(Origin::signed(2), 2, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(3), 0, 42, None), + Uniques::cancel_approval(Origin::signed(3), 1, 42, None), Error::::NoPermission ); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 0, 42, Some(4)), + Uniques::cancel_approval(Origin::signed(2), 1, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::signed(2), 0, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::signed(2), 1, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 0, 42, None), + Uniques::cancel_approval(Origin::signed(2), 1, 42, None), Error::::NoDelegate ); }); @@ -593,25 +593,25 @@ fn cancel_approval_works() { fn cancel_approval_works_with_admin() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 1, 42, None), + Uniques::cancel_approval(Origin::signed(1), 2, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 0, 43, None), + Uniques::cancel_approval(Origin::signed(1), 1, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 0, 42, Some(4)), + Uniques::cancel_approval(Origin::signed(1), 1, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::signed(1), 0, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::signed(1), 1, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 0, 42, None), + Uniques::cancel_approval(Origin::signed(1), 1, 42, None), Error::::NoDelegate ); }); @@ -621,25 +621,25 @@ fn cancel_approval_works_with_admin() { fn cancel_approval_works_with_force() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::root(), 1, 42, None), + Uniques::cancel_approval(Origin::root(), 2, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::root(), 0, 43, None), + Uniques::cancel_approval(Origin::root(), 1, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::root(), 0, 42, Some(4)), + Uniques::cancel_approval(Origin::root(), 1, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::root(), 0, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::root(), 1, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::root(), 0, 42, None), + Uniques::cancel_approval(Origin::root(), 1, 42, None), Error::::NoDelegate ); }); @@ -648,7 +648,7 @@ fn cancel_approval_works_with_force() { #[test] fn max_supply_should_work() { new_test_ext().execute_with(|| { - let collection_id = 0; + let collection_id = 1; let user_id = 1; let max_supply = 2; From 6756619eddd3f829e83f2e0c80385954a19d7774 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 12:41:15 +0200 Subject: [PATCH 06/32] update before checking --- frame/uniques/src/functions.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index ef3549f40b44d..6f2b1939470f7 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -92,9 +92,10 @@ impl, I: 'static> Pallet { last_collection: T::CollectionId, ) -> Result> { let new_collection = last_collection + T::CollectionId::one(); + + CollectionsCount::::set(new_collection); ensure!(!Collection::::contains_key(new_collection), Error::::InUse); - CollectionsCount::::set(new_collection); Ok(new_collection) } From 72c50149c45bef1270a6da5ae5f206327153dbd1 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 12:44:51 +0200 Subject: [PATCH 07/32] fmt --- frame/uniques/src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 6f2b1939470f7..e08b445d6b501 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -92,7 +92,7 @@ impl, I: 'static> Pallet { last_collection: T::CollectionId, ) -> Result> { let new_collection = last_collection + T::CollectionId::one(); - + CollectionsCount::::set(new_collection); ensure!(!Collection::::contains_key(new_collection), Error::::InUse); From 3a26210d10f21f3e40956b5b5befeb2284629307 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 14:52:36 +0200 Subject: [PATCH 08/32] fix --- frame/uniques/src/functions.rs | 14 +- frame/uniques/src/lib.rs | 32 ++- frame/uniques/src/tests.rs | 361 +++++++++++++++++---------------- frame/uniques/src/weights.rs | 17 ++ 4 files changed, 233 insertions(+), 191 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index e08b445d6b501..1aa5c76773baf 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -64,6 +64,8 @@ impl, I: 'static> Pallet { free_holding: bool, event: Event, ) -> DispatchResult { + ensure!(!Collection::::contains_key(collection), Error::::InUse); + T::Currency::reserve(&owner, deposit)?; Collection::::insert( @@ -83,22 +85,12 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); + CollectionsCount::::set(collection.saturating_add(T::CollectionId::one())); Self::deposit_event(event); Ok(()) } - pub fn try_increment_id( - last_collection: T::CollectionId, - ) -> Result> { - let new_collection = last_collection + T::CollectionId::one(); - - CollectionsCount::::set(new_collection); - ensure!(!Collection::::contains_key(new_collection), Error::::InUse); - - Ok(new_collection) - } - pub fn do_destroy_collection( collection: T::CollectionId, witness: DestroyWitness, diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 3bb49ff854bf9..ecad6521b482f 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -96,7 +96,8 @@ pub mod pallet { + Copy + Default + Unsigned - + FromPrimitive; + + FromPrimitive + + Saturating; /// The type used to identify a unique item within a collection. type ItemId: Member + Parameter + MaxEncodedLen + Copy; @@ -354,6 +355,8 @@ pub mod pallet { OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option }, /// Max supply has been set for a collection. CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 }, + /// Event gets emmited when the `CollectionsCount` gets incremented in `try_increment_id` + CollectionsCountIncremented { collections_count: T::CollectionId }, } #[pallet::error] @@ -388,6 +391,8 @@ pub mod pallet { MaxSupplyAlreadySet, /// The provided max supply is less to the amount of items a collection already has. MaxSupplyTooSmall, + /// The next `CollectionId` is not being used. + NextIdNotUsed, } impl, I: 'static> Pallet { @@ -424,8 +429,7 @@ pub mod pallet { origin: OriginFor, admin: ::Source, ) -> DispatchResult { - let last_collection = CollectionsCount::::get(); - let collection = Self::try_increment_id(last_collection)?; + let collection = CollectionsCount::::get(); let owner = T::CreateOrigin::ensure_origin(origin, &collection)?; let admin = T::Lookup::lookup(admin)?; @@ -465,8 +469,7 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; - let last_collection = CollectionsCount::::get(); - let collection = Self::try_increment_id(last_collection)?; + let collection = CollectionsCount::::get(); Self::do_create_collection( collection, @@ -478,6 +481,25 @@ pub mod pallet { ) } + /// Increments the `CollectionId` stored in `CollectionsCount`. + /// This is only callable when the next `CollectionId` that would be generated is already + /// being used. + /// + /// The origin must be Signed and the sender must have sufficient funds free. + /// + /// Weight: `O(1)` + #[pallet::weight(T::WeightInfo::try_increment_id())] + pub fn try_increment_id(origin: OriginFor) -> DispatchResult { + ensure_signed(origin)?; + + let next_id = CollectionsCount::::get().saturating_add(T::CollectionId::one()); + ensure!(Collection::::contains_key(next_id), Error::::NextIdNotUsed); + + CollectionsCount::::set(next_id); + Self::deposit_event(Event::CollectionsCountIncremented { collections_count: next_id }); + Ok(()) + } + /// Destroy a collection of fungible items. /// /// The origin must conform to `ForceOrigin` or must be `Signed` and the sender must be the diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 9ecc6e507df14..0259f038478c4 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -93,14 +93,14 @@ fn basic_setup_works() { fn basic_minting_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_eq!(collections(), vec![(1, 1)]); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_eq!(items(), vec![(1, 1, 42)]); + assert_eq!(collections(), vec![(1, 0)]); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_eq!(items(), vec![(1, 0, 42)]); assert_ok!(Uniques::force_create(Origin::root(), 2, true)); - assert_eq!(collections(), vec![(1, 1), (2, 2)]); - assert_ok!(Uniques::mint(Origin::signed(2), 2, 69, 1)); - assert_eq!(items(), vec![(1, 1, 42), (1, 2, 69)]); + assert_eq!(collections(), vec![(1, 0), (2, 1)]); + assert_ok!(Uniques::mint(Origin::signed(2), 1, 69, 1)); + assert_eq!(items(), vec![(1, 0, 42), (1, 1, 69)]); }); } @@ -110,30 +110,30 @@ fn lifecycle_should_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); assert_eq!(Balances::reserved_balance(&1), 2); - assert_eq!(collections(), vec![(1, 1)]); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0, 0], false)); + assert_eq!(collections(), vec![(1, 0)]); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0, 0], false)); assert_eq!(Balances::reserved_balance(&1), 5); - assert!(CollectionMetadataOf::::contains_key(1)); + assert!(CollectionMetadataOf::::contains_key(0)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 10)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 10)); assert_eq!(Balances::reserved_balance(&1), 6); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 69, 20)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 69, 20)); assert_eq!(Balances::reserved_balance(&1), 7); - assert_eq!(items(), vec![(10, 1, 42), (20, 1, 69)]); - assert_eq!(Collection::::get(1).unwrap().items, 2); - assert_eq!(Collection::::get(1).unwrap().item_metadatas, 0); + assert_eq!(items(), vec![(10, 0, 42), (20, 0, 69)]); + assert_eq!(Collection::::get(0).unwrap().items, 2); + assert_eq!(Collection::::get(0).unwrap().item_metadatas, 0); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![42, 42], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![42, 42], false)); assert_eq!(Balances::reserved_balance(&1), 10); - assert!(ItemMetadataOf::::contains_key(1, 42)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![69, 69], false)); + assert!(ItemMetadataOf::::contains_key(0, 42)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![69, 69], false)); assert_eq!(Balances::reserved_balance(&1), 13); - assert!(ItemMetadataOf::::contains_key(1, 69)); + assert!(ItemMetadataOf::::contains_key(0, 69)); - let w = Collection::::get(1).unwrap().destroy_witness(); + let w = Collection::::get(0).unwrap().destroy_witness(); assert_eq!(w.items, 2); assert_eq!(w.item_metadatas, 2); - assert_ok!(Uniques::destroy(Origin::signed(1), 1, w)); + assert_ok!(Uniques::destroy(Origin::signed(1), 0, w)); assert_eq!(Balances::reserved_balance(&1), 0); assert!(!Collection::::contains_key(0)); @@ -153,9 +153,9 @@ fn destroy_with_bad_witness_should_not_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); - let w = Collection::::get(1).unwrap().destroy_witness(); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_noop!(Uniques::destroy(Origin::signed(1), 1, w), Error::::BadWitness); + let w = Collection::::get(0).unwrap().destroy_witness(); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_noop!(Uniques::destroy(Origin::signed(1), 0, w), Error::::BadWitness); }); } @@ -163,10 +163,10 @@ fn destroy_with_bad_witness_should_not_work() { fn mint_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_eq!(Uniques::owner(1, 42).unwrap(), 1); - assert_eq!(collections(), vec![(1, 1)]); - assert_eq!(items(), vec![(1, 1, 42)]); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_eq!(Uniques::owner(0, 42).unwrap(), 1); + assert_eq!(collections(), vec![(1, 0)]); + assert_eq!(items(), vec![(1, 0, 42)]); }); } @@ -174,14 +174,14 @@ fn mint_should_work() { fn transfer_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 3)); - assert_eq!(items(), vec![(3, 1, 42)]); - assert_noop!(Uniques::transfer(Origin::signed(2), 1, 42, 4), Error::::NoPermission); + assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 3)); + assert_eq!(items(), vec![(3, 0, 42)]); + assert_noop!(Uniques::transfer(Origin::signed(2), 0, 42, 4), Error::::NoPermission); - assert_ok!(Uniques::approve_transfer(Origin::signed(3), 1, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 4)); + assert_ok!(Uniques::approve_transfer(Origin::signed(3), 0, 42, 2)); + assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 4)); }); } @@ -189,16 +189,16 @@ fn transfer_should_work() { fn freezing_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_ok!(Uniques::freeze(Origin::signed(1), 1, 42)); - assert_noop!(Uniques::transfer(Origin::signed(1), 1, 42, 2), Error::::Frozen); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_ok!(Uniques::freeze(Origin::signed(1), 0, 42)); + assert_noop!(Uniques::transfer(Origin::signed(1), 0, 42, 2), Error::::Frozen); - assert_ok!(Uniques::thaw(Origin::signed(1), 1, 42)); - assert_ok!(Uniques::freeze_collection(Origin::signed(1), 1)); - assert_noop!(Uniques::transfer(Origin::signed(1), 1, 42, 2), Error::::Frozen); + assert_ok!(Uniques::thaw(Origin::signed(1), 0, 42)); + assert_ok!(Uniques::freeze_collection(Origin::signed(1), 0)); + assert_noop!(Uniques::transfer(Origin::signed(1), 0, 42, 2), Error::::Frozen); - assert_ok!(Uniques::thaw_collection(Origin::signed(1), 1)); - assert_ok!(Uniques::transfer(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::thaw_collection(Origin::signed(1), 0)); + assert_ok!(Uniques::transfer(Origin::signed(1), 0, 42, 2)); }); } @@ -206,21 +206,21 @@ fn freezing_should_work() { fn origin_guards_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); Balances::make_free_balance_be(&2, 100); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(1))); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(0))); assert_noop!( - Uniques::transfer_ownership(Origin::signed(2), 1, 2), + Uniques::transfer_ownership(Origin::signed(2), 0, 2), Error::::NoPermission ); - assert_noop!(Uniques::set_team(Origin::signed(2), 1, 2, 2, 2), Error::::NoPermission); - assert_noop!(Uniques::freeze(Origin::signed(2), 1, 42), Error::::NoPermission); - assert_noop!(Uniques::thaw(Origin::signed(2), 1, 42), Error::::NoPermission); - assert_noop!(Uniques::mint(Origin::signed(2), 1, 69, 2), Error::::NoPermission); - assert_noop!(Uniques::burn(Origin::signed(2), 1, 42, None), Error::::NoPermission); - let w = Collection::::get(1).unwrap().destroy_witness(); - assert_noop!(Uniques::destroy(Origin::signed(2), 1, w), Error::::NoPermission); + assert_noop!(Uniques::set_team(Origin::signed(2), 0, 2, 2, 2), Error::::NoPermission); + assert_noop!(Uniques::freeze(Origin::signed(2), 0, 42), Error::::NoPermission); + assert_noop!(Uniques::thaw(Origin::signed(2), 0, 42), Error::::NoPermission); + assert_noop!(Uniques::mint(Origin::signed(2), 0, 69, 2), Error::::NoPermission); + assert_noop!(Uniques::burn(Origin::signed(2), 0, 42, None), Error::::NoPermission); + let w = Collection::::get(0).unwrap().destroy_witness(); + assert_noop!(Uniques::destroy(Origin::signed(2), 0, w), Error::::NoPermission); }); } @@ -231,33 +231,33 @@ fn transfer_owner_should_work() { Balances::make_free_balance_be(&2, 100); Balances::make_free_balance_be(&3, 100); assert_ok!(Uniques::create(Origin::signed(1), 1)); - assert_eq!(collections(), vec![(1, 1)]); + assert_eq!(collections(), vec![(1, 0)]); assert_noop!( - Uniques::transfer_ownership(Origin::signed(1), 1, 2), + Uniques::transfer_ownership(Origin::signed(1), 0, 2), Error::::Unaccepted ); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(1))); - assert_ok!(Uniques::transfer_ownership(Origin::signed(1), 1, 2)); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(2), Some(0))); + assert_ok!(Uniques::transfer_ownership(Origin::signed(1), 0, 2)); - assert_eq!(collections(), vec![(2, 1)]); + assert_eq!(collections(), vec![(2, 0)]); assert_eq!(Balances::total_balance(&1), 98); assert_eq!(Balances::total_balance(&2), 102); assert_eq!(Balances::reserved_balance(&1), 0); assert_eq!(Balances::reserved_balance(&2), 2); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(1), Some(1))); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(1), Some(0))); assert_noop!( - Uniques::transfer_ownership(Origin::signed(1), 1, 1), + Uniques::transfer_ownership(Origin::signed(1), 0, 1), Error::::NoPermission ); // Mint and set metadata now and make sure that deposit gets transferred back. - assert_ok!(Uniques::set_collection_metadata(Origin::signed(2), 1, bvec![0u8; 20], false)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_ok!(Uniques::set_metadata(Origin::signed(2), 1, 42, bvec![0u8; 20], false)); - assert_ok!(Uniques::set_accept_ownership(Origin::signed(3), Some(1))); - assert_ok!(Uniques::transfer_ownership(Origin::signed(2), 1, 3)); - assert_eq!(collections(), vec![(3, 1)]); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(2), 0, bvec![0u8; 20], false)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_ok!(Uniques::set_metadata(Origin::signed(2), 0, 42, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_accept_ownership(Origin::signed(3), Some(0))); + assert_ok!(Uniques::transfer_ownership(Origin::signed(2), 0, 3)); + assert_eq!(collections(), vec![(3, 0)]); assert_eq!(Balances::total_balance(&2), 57); assert_eq!(Balances::total_balance(&3), 145); assert_eq!(Balances::reserved_balance(&2), 0); @@ -276,13 +276,13 @@ fn transfer_owner_should_work() { fn set_team_should_work() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::set_team(Origin::signed(1), 1, 2, 3, 4)); + assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); - assert_ok!(Uniques::mint(Origin::signed(2), 1, 42, 2)); - assert_ok!(Uniques::freeze(Origin::signed(4), 1, 42)); - assert_ok!(Uniques::thaw(Origin::signed(3), 1, 42)); - assert_ok!(Uniques::transfer(Origin::signed(3), 1, 42, 3)); - assert_ok!(Uniques::burn(Origin::signed(3), 1, 42, None)); + assert_ok!(Uniques::mint(Origin::signed(2), 0, 42, 2)); + assert_ok!(Uniques::freeze(Origin::signed(4), 0, 42)); + assert_ok!(Uniques::thaw(Origin::signed(3), 0, 42)); + assert_ok!(Uniques::transfer(Origin::signed(3), 0, 42, 3)); + assert_ok!(Uniques::burn(Origin::signed(3), 0, 42, None)); }); } @@ -291,60 +291,60 @@ fn set_collection_metadata_should_work() { new_test_ext().execute_with(|| { // Cannot add metadata to unknown item assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 20], false), + Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 20], false), Error::::UnknownCollection, ); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); // Cannot add metadata to unowned item assert_noop!( - Uniques::set_collection_metadata(Origin::signed(2), 1, bvec![0u8; 20], false), + Uniques::set_collection_metadata(Origin::signed(2), 0, bvec![0u8; 20], false), Error::::NoPermission, ); // Successfully add metadata and take deposit Balances::make_free_balance_be(&1, 30); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 20], false)); assert_eq!(Balances::free_balance(&1), 9); - assert!(CollectionMetadataOf::::contains_key(1)); + assert!(CollectionMetadataOf::::contains_key(0)); // Force origin works, too. - assert_ok!(Uniques::set_collection_metadata(Origin::root(), 1, bvec![0u8; 18], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::root(), 0, bvec![0u8; 18], false)); // Update deposit - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], false)); assert_eq!(Balances::free_balance(&1), 14); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 25], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 25], false)); assert_eq!(Balances::free_balance(&1), 4); // Cannot over-reserve assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 40], false), + Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 40], false), BalancesError::::InsufficientBalance, ); // Can't set or clear metadata once frozen - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], true)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], true)); assert_noop!( - Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0u8; 15], false), + Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0u8; 15], false), Error::::Frozen, ); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(1), 1), + Uniques::clear_collection_metadata(Origin::signed(1), 0), Error::::Frozen ); // Clear Metadata - assert_ok!(Uniques::set_collection_metadata(Origin::root(), 1, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::root(), 0, bvec![0u8; 15], false)); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(2), 1), + Uniques::clear_collection_metadata(Origin::signed(2), 0), Error::::NoPermission ); assert_noop!( - Uniques::clear_collection_metadata(Origin::signed(1), 2), + Uniques::clear_collection_metadata(Origin::signed(1), 1), Error::::UnknownCollection ); - assert_ok!(Uniques::clear_collection_metadata(Origin::signed(1), 1)); - assert!(!CollectionMetadataOf::::contains_key(1)); + assert_ok!(Uniques::clear_collection_metadata(Origin::signed(1), 0)); + assert!(!CollectionMetadataOf::::contains_key(0)); }); } @@ -355,52 +355,52 @@ fn set_item_metadata_should_work() { // Cannot add metadata to unknown item assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); // Cannot add metadata to unowned item assert_noop!( - Uniques::set_metadata(Origin::signed(2), 1, 42, bvec![0u8; 20], false), + Uniques::set_metadata(Origin::signed(2), 0, 42, bvec![0u8; 20], false), Error::::NoPermission, ); // Successfully add metadata and take deposit - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 20], false)); assert_eq!(Balances::free_balance(&1), 8); - assert!(ItemMetadataOf::::contains_key(1, 42)); + assert!(ItemMetadataOf::::contains_key(0, 42)); // Force origin works, too. - assert_ok!(Uniques::set_metadata(Origin::root(), 1, 42, bvec![0u8; 18], false)); + assert_ok!(Uniques::set_metadata(Origin::root(), 0, 42, bvec![0u8; 18], false)); // Update deposit - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], false)); assert_eq!(Balances::free_balance(&1), 13); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 25], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 25], false)); assert_eq!(Balances::free_balance(&1), 3); // Cannot over-reserve assert_noop!( - Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 40], false), + Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 40], false), BalancesError::::InsufficientBalance, ); // Can't set or clear metadata once frozen - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], true)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], true)); assert_noop!( - Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0u8; 15], false), + Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0u8; 15], false), Error::::Frozen, ); - assert_noop!(Uniques::clear_metadata(Origin::signed(1), 1, 42), Error::::Frozen); + assert_noop!(Uniques::clear_metadata(Origin::signed(1), 0, 42), Error::::Frozen); // Clear Metadata - assert_ok!(Uniques::set_metadata(Origin::root(), 1, 42, bvec![0u8; 15], false)); + assert_ok!(Uniques::set_metadata(Origin::root(), 0, 42, bvec![0u8; 15], false)); assert_noop!( - Uniques::clear_metadata(Origin::signed(2), 1, 42), + Uniques::clear_metadata(Origin::signed(2), 0, 42), Error::::NoPermission ); assert_noop!( - Uniques::clear_metadata(Origin::signed(1), 2, 42), + Uniques::clear_metadata(Origin::signed(1), 1, 42), Error::::UnknownCollection ); - assert_ok!(Uniques::clear_metadata(Origin::signed(1), 1, 42)); + assert_ok!(Uniques::clear_metadata(Origin::signed(1), 0, 42)); assert!(!ItemMetadataOf::::contains_key(0, 42)); }); } @@ -412,11 +412,11 @@ fn set_attribute_should_work() { assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![1], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![1], bvec![0])); assert_eq!( - attributes(1), + attributes(0), vec![ (None, bvec![0], bvec![0]), (Some(0), bvec![0], bvec![0]), @@ -425,9 +425,9 @@ fn set_attribute_should_work() { ); assert_eq!(Balances::reserved_balance(1), 9); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0; 10])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0; 10])); assert_eq!( - attributes(1), + attributes(0), vec![ (None, bvec![0], bvec![0; 10]), (Some(0), bvec![0], bvec![0]), @@ -436,16 +436,16 @@ fn set_attribute_should_work() { ); assert_eq!(Balances::reserved_balance(1), 18); - assert_ok!(Uniques::clear_attribute(Origin::signed(1), 1, Some(0), bvec![1])); + assert_ok!(Uniques::clear_attribute(Origin::signed(1), 0, Some(0), bvec![1])); assert_eq!( - attributes(1), + attributes(0), vec![(None, bvec![0], bvec![0; 10]), (Some(0), bvec![0], bvec![0]),] ); assert_eq!(Balances::reserved_balance(1), 15); - let w = Collection::::get(1).unwrap().destroy_witness(); - assert_ok!(Uniques::destroy(Origin::signed(1), 1, w)); - assert_eq!(attributes(1), vec![]); + let w = Collection::::get(0).unwrap().destroy_witness(); + assert_ok!(Uniques::destroy(Origin::signed(1), 0, w)); + assert_eq!(attributes(0), vec![]); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -457,11 +457,11 @@ fn set_attribute_should_respect_freeze() { assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![0])); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(1), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![0])); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(1), bvec![0], bvec![0])); assert_eq!( - attributes(1), + attributes(0), vec![ (None, bvec![0], bvec![0]), (Some(0), bvec![0], bvec![0]), @@ -470,15 +470,15 @@ fn set_attribute_should_respect_freeze() { ); assert_eq!(Balances::reserved_balance(1), 9); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![], true)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![], true)); let e = Error::::Frozen; - assert_noop!(Uniques::set_attribute(Origin::signed(1), 1, None, bvec![0], bvec![0]), e); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![1])); + assert_noop!(Uniques::set_attribute(Origin::signed(1), 0, None, bvec![0], bvec![0]), e); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![1])); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 0, bvec![], true)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 0, bvec![], true)); let e = Error::::Frozen; - assert_noop!(Uniques::set_attribute(Origin::signed(1), 1, Some(0), bvec![0], bvec![1]), e); - assert_ok!(Uniques::set_attribute(Origin::signed(1), 1, Some(1), bvec![0], bvec![1])); + assert_noop!(Uniques::set_attribute(Origin::signed(1), 0, Some(0), bvec![0], bvec![1]), e); + assert_ok!(Uniques::set_attribute(Origin::signed(1), 0, Some(1), bvec![0], bvec![1])); }); } @@ -488,31 +488,31 @@ fn force_item_status_should_work() { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 1)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 69, 2)); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![0; 20], false)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 69, 2)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 65); // force item status to be free holding - assert_ok!(Uniques::force_item_status(Origin::root(), 1, 1, 1, 1, 1, true, false)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 142, 1)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 169, 2)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 142, bvec![0; 20], false)); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 169, bvec![0; 20], false)); + assert_ok!(Uniques::force_item_status(Origin::root(), 0, 1, 1, 1, 1, true, false)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 142, 1)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 169, 2)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 142, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 169, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 65); - assert_ok!(Uniques::redeposit(Origin::signed(1), 1, bvec![0, 42, 50, 69, 100])); + assert_ok!(Uniques::redeposit(Origin::signed(1), 0, bvec![0, 42, 50, 69, 100])); assert_eq!(Balances::reserved_balance(1), 63); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 42, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 42, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 42); - assert_ok!(Uniques::set_metadata(Origin::signed(1), 1, 69, bvec![0; 20], false)); + assert_ok!(Uniques::set_metadata(Origin::signed(1), 0, 69, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 21); - assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 1, bvec![0; 20], false)); + assert_ok!(Uniques::set_collection_metadata(Origin::signed(1), 0, bvec![0; 20], false)); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -522,22 +522,22 @@ fn burn_works() { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&1, 100); assert_ok!(Uniques::force_create(Origin::root(), 1, false)); - assert_ok!(Uniques::set_team(Origin::signed(1), 1, 2, 3, 4)); + assert_ok!(Uniques::set_team(Origin::signed(1), 0, 2, 3, 4)); assert_noop!( - Uniques::burn(Origin::signed(5), 1, 42, Some(5)), + Uniques::burn(Origin::signed(5), 0, 42, Some(5)), Error::::UnknownCollection ); - assert_ok!(Uniques::mint(Origin::signed(2), 1, 42, 5)); - assert_ok!(Uniques::mint(Origin::signed(2), 1, 69, 5)); + assert_ok!(Uniques::mint(Origin::signed(2), 0, 42, 5)); + assert_ok!(Uniques::mint(Origin::signed(2), 0, 69, 5)); assert_eq!(Balances::reserved_balance(1), 2); - assert_noop!(Uniques::burn(Origin::signed(0), 1, 42, None), Error::::NoPermission); - assert_noop!(Uniques::burn(Origin::signed(5), 1, 42, Some(6)), Error::::WrongOwner); + assert_noop!(Uniques::burn(Origin::signed(0), 0, 42, None), Error::::NoPermission); + assert_noop!(Uniques::burn(Origin::signed(5), 0, 42, Some(6)), Error::::WrongOwner); - assert_ok!(Uniques::burn(Origin::signed(5), 1, 42, Some(5))); - assert_ok!(Uniques::burn(Origin::signed(3), 1, 69, Some(5))); + assert_ok!(Uniques::burn(Origin::signed(5), 0, 42, Some(5))); + assert_ok!(Uniques::burn(Origin::signed(3), 0, 69, Some(5))); assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -546,14 +546,14 @@ fn burn_works() { fn approval_lifecycle_works() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); - assert_ok!(Uniques::transfer(Origin::signed(3), 1, 42, 4)); - assert_noop!(Uniques::transfer(Origin::signed(3), 1, 42, 3), Error::::NoPermission); - assert!(Item::::get(1, 42).unwrap().approved.is_none()); - - assert_ok!(Uniques::approve_transfer(Origin::signed(4), 1, 42, 2)); - assert_ok!(Uniques::transfer(Origin::signed(2), 1, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); + assert_ok!(Uniques::transfer(Origin::signed(3), 0, 42, 4)); + assert_noop!(Uniques::transfer(Origin::signed(3), 0, 42, 3), Error::::NoPermission); + assert!(Item::::get(0, 42).unwrap().approved.is_none()); + + assert_ok!(Uniques::approve_transfer(Origin::signed(4), 0, 42, 2)); + assert_ok!(Uniques::transfer(Origin::signed(2), 0, 42, 2)); }); } @@ -561,29 +561,29 @@ fn approval_lifecycle_works() { fn cancel_approval_works() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 2, 42, None), + Uniques::cancel_approval(Origin::signed(2), 1, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 2, 43, None), + Uniques::cancel_approval(Origin::signed(2), 0, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(3), 1, 42, None), + Uniques::cancel_approval(Origin::signed(3), 0, 42, None), Error::::NoPermission ); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 1, 42, Some(4)), + Uniques::cancel_approval(Origin::signed(2), 0, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::signed(2), 1, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::signed(2), 0, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::signed(2), 1, 42, None), + Uniques::cancel_approval(Origin::signed(2), 0, 42, None), Error::::NoDelegate ); }); @@ -593,25 +593,25 @@ fn cancel_approval_works() { fn cancel_approval_works_with_admin() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 2, 42, None), + Uniques::cancel_approval(Origin::signed(1), 1, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 1, 43, None), + Uniques::cancel_approval(Origin::signed(1), 0, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 1, 42, Some(4)), + Uniques::cancel_approval(Origin::signed(1), 0, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::signed(1), 1, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::signed(1), 0, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::signed(1), 1, 42, None), + Uniques::cancel_approval(Origin::signed(1), 0, 42, None), Error::::NoDelegate ); }); @@ -621,25 +621,25 @@ fn cancel_approval_works_with_admin() { fn cancel_approval_works_with_force() { new_test_ext().execute_with(|| { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); - assert_ok!(Uniques::mint(Origin::signed(1), 1, 42, 2)); + assert_ok!(Uniques::mint(Origin::signed(1), 0, 42, 2)); - assert_ok!(Uniques::approve_transfer(Origin::signed(2), 1, 42, 3)); + assert_ok!(Uniques::approve_transfer(Origin::signed(2), 0, 42, 3)); assert_noop!( - Uniques::cancel_approval(Origin::root(), 2, 42, None), + Uniques::cancel_approval(Origin::root(), 1, 42, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::root(), 1, 43, None), + Uniques::cancel_approval(Origin::root(), 0, 43, None), Error::::UnknownCollection ); assert_noop!( - Uniques::cancel_approval(Origin::root(), 1, 42, Some(4)), + Uniques::cancel_approval(Origin::root(), 0, 42, Some(4)), Error::::WrongDelegate ); - assert_ok!(Uniques::cancel_approval(Origin::root(), 1, 42, Some(3))); + assert_ok!(Uniques::cancel_approval(Origin::root(), 0, 42, Some(3))); assert_noop!( - Uniques::cancel_approval(Origin::root(), 1, 42, None), + Uniques::cancel_approval(Origin::root(), 0, 42, None), Error::::NoDelegate ); }); @@ -648,7 +648,7 @@ fn cancel_approval_works_with_force() { #[test] fn max_supply_should_work() { new_test_ext().execute_with(|| { - let collection_id = 1; + let collection_id = 0; let user_id = 1; let max_supply = 2; @@ -694,3 +694,14 @@ fn max_supply_should_work() { assert!(!CollectionMaxSupply::::contains_key(collection_id)); }); } + +#[test] +fn try_increment_id_works() { + new_test_ext().execute_with(|| { + // should fail because the next `CollectionId` is not being used. + assert_noop!( + Uniques::try_increment_id(Origin::signed(2)), + Error::::NextIdNotUsed + ); + }); +} diff --git a/frame/uniques/src/weights.rs b/frame/uniques/src/weights.rs index d885077a8dee9..eb7cbdaa40cf2 100644 --- a/frame/uniques/src/weights.rs +++ b/frame/uniques/src/weights.rs @@ -70,6 +70,7 @@ pub trait WeightInfo { fn cancel_approval() -> Weight; fn set_accept_ownership() -> Weight; fn set_collection_max_supply() -> Weight; + fn try_increment_id() -> Weight; } /// Weights for pallet_uniques using the Substrate node and recommended hardware. @@ -270,6 +271,14 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + + // Storage: Uniques CollectionsCount (r:1 w:1) + // Storage: Uniques Collection (r:1 w:0) + fn try_increment_id() -> Weight { + (10_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } } // For backwards compatibility and tests @@ -469,4 +478,12 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + + // Storage: Uniques CollectionsCount (r:1 w:1) + // Storage: Uniques Collection (r:1 w:0) + fn try_increment_id() -> Weight { + (10_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } } From b974c529a2ddfcc147e2e223b5c241a8d18b9ca3 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 14:54:04 +0200 Subject: [PATCH 09/32] fmt --- frame/uniques/src/tests.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 0259f038478c4..095c7f3c75e98 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -699,9 +699,6 @@ fn max_supply_should_work() { fn try_increment_id_works() { new_test_ext().execute_with(|| { // should fail because the next `CollectionId` is not being used. - assert_noop!( - Uniques::try_increment_id(Origin::signed(2)), - Error::::NextIdNotUsed - ); + assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); } From 95c2aafb51af605b9b6bd4dc979fa87637aeedfe Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 11 Jul 2022 14:59:34 +0200 Subject: [PATCH 10/32] commit --- frame/uniques/src/functions.rs | 2 +- frame/uniques/src/lib.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 1aa5c76773baf..2f951ebf58162 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -85,7 +85,7 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); - CollectionsCount::::set(collection.saturating_add(T::CollectionId::one())); + CollectionsCount::::set(collection + T::CollectionId::one()); Self::deposit_event(event); Ok(()) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index ecad6521b482f..41c3c053d6511 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -96,8 +96,7 @@ pub mod pallet { + Copy + Default + Unsigned - + FromPrimitive - + Saturating; + + FromPrimitive; /// The type used to identify a unique item within a collection. type ItemId: Member + Parameter + MaxEncodedLen + Copy; @@ -492,7 +491,7 @@ pub mod pallet { pub fn try_increment_id(origin: OriginFor) -> DispatchResult { ensure_signed(origin)?; - let next_id = CollectionsCount::::get().saturating_add(T::CollectionId::one()); + let next_id = CollectionsCount::::get() + T::CollectionId::one(); ensure!(Collection::::contains_key(next_id), Error::::NextIdNotUsed); CollectionsCount::::set(next_id); From 3acff156da8c1b61a814926f62eb515d0d9b909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergej=20Saka=C4=8D?= Date: Tue, 12 Jul 2022 10:27:42 +0300 Subject: [PATCH 11/32] tests & fix --- frame/uniques/src/functions.rs | 9 +++++++++ frame/uniques/src/lib.rs | 6 ++++-- frame/uniques/src/tests.rs | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 2f951ebf58162..65f31fe42e8b5 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -202,4 +202,13 @@ impl, I: 'static> Pallet { Self::deposit_event(Event::Burned { collection, item, owner }); Ok(()) } + + #[cfg(test)] + pub fn set_collections_count(count: u32) { + CollectionsCount::::set(T::CollectionId::from_u32(count).unwrap()); + } + + pub fn get_collections_count() -> T::CollectionId { + CollectionsCount::::get() + } } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 41c3c053d6511..b42182caa987c 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -490,10 +490,12 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::try_increment_id())] pub fn try_increment_id(origin: OriginFor) -> DispatchResult { ensure_signed(origin)?; + ensure!( + Collection::::contains_key(CollectionsCount::::get()), + Error::::NextIdNotUsed + ); let next_id = CollectionsCount::::get() + T::CollectionId::one(); - ensure!(Collection::::contains_key(next_id), Error::::NextIdNotUsed); - CollectionsCount::::set(next_id); Self::deposit_event(Event::CollectionsCountIncremented { collections_count: next_id }); Ok(()) diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 095c7f3c75e98..e2b5d86ae5e45 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -700,5 +700,23 @@ fn try_increment_id_works() { new_test_ext().execute_with(|| { // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); + + // create two collections. + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); + assert_ok!(Uniques::force_create(Origin::root(), 1, true)); + + // there are now two collections. + assert_eq!(Uniques::get_collections_count(), 2); + + // reset the collections counter to test if the `try_increment_id` + // works. + Uniques::set_collections_count(0); + assert_ok!(Uniques::try_increment_id(Origin::signed(2))); + + // because reset, the collections count should be now 1 + assert_eq!(Uniques::get_collections_count(), 1); + + // should fail because the next `CollectionId` is not being used. + assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); } From 364f01b35ec8d2d36c744bc994f21f07278aadf1 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 10:33:05 +0300 Subject: [PATCH 12/32] fix --- frame/uniques/src/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index e2b5d86ae5e45..802fd0605fa6a 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -716,6 +716,7 @@ fn try_increment_id_works() { // because reset, the collections count should be now 1 assert_eq!(Uniques::get_collections_count(), 1); + assert_ok!(Uniques::try_increment_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); From d75b5657e5881428ed331c24a4d58f286944d13c Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 10:36:21 +0300 Subject: [PATCH 13/32] commit --- frame/uniques/src/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 802fd0605fa6a..f54ae7d60e179 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -716,7 +716,8 @@ fn try_increment_id_works() { // because reset, the collections count should be now 1 assert_eq!(Uniques::get_collections_count(), 1); - assert_ok!(Uniques::try_increment_id(Origin::signed(2))); + // increment the collections count again. + assert_ok!(Uniques::try_incremegnt_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); From 0ac9b356fbf73cbc97d4bb12d4ebf712697896d6 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 13:46:27 +0300 Subject: [PATCH 14/32] docs --- frame/uniques/src/lib.rs | 3 ++- frame/uniques/src/tests.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index b42182caa987c..f605f4eef6f0f 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -259,7 +259,8 @@ pub mod pallet { StorageMap<_, Blake2_128Concat, T::CollectionId, u32, OptionQuery>; #[pallet::storage] - /// Stores the last CollectionId of the collections. + /// Stores the `CollectionId` that is going to be used for the next collection. + /// This gets incremented by 1 when creating a collection. pub(super) type CollectionsCount, I: 'static = ()> = StorageValue<_, T::CollectionId, ValueQuery>; diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index f54ae7d60e179..f79df7bccd43c 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -717,7 +717,7 @@ fn try_increment_id_works() { assert_eq!(Uniques::get_collections_count(), 1); // increment the collections count again. - assert_ok!(Uniques::try_incremegnt_id(Origin::signed(2))); + assert_ok!(Uniques::try_incremegnt_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); From 9001ca786a304f375179c5cfc980820b8a303100 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 13:54:57 +0300 Subject: [PATCH 15/32] safe math --- frame/uniques/src/functions.rs | 2 +- frame/uniques/src/lib.rs | 5 +++-- frame/uniques/src/tests.rs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 65f31fe42e8b5..6909f4acdb360 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -85,7 +85,7 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); - CollectionsCount::::set(collection + T::CollectionId::one()); + CollectionsCount::::set(collection.saturating_add(T::CollectionId::one())); Self::deposit_event(event); Ok(()) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index f605f4eef6f0f..56185f79e8850 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -96,7 +96,8 @@ pub mod pallet { + Copy + Default + Unsigned - + FromPrimitive; + + FromPrimitive + + Saturating; /// The type used to identify a unique item within a collection. type ItemId: Member + Parameter + MaxEncodedLen + Copy; @@ -496,7 +497,7 @@ pub mod pallet { Error::::NextIdNotUsed ); - let next_id = CollectionsCount::::get() + T::CollectionId::one(); + let next_id = CollectionsCount::::get().saturating_add(T::CollectionId::one()); CollectionsCount::::set(next_id); Self::deposit_event(Event::CollectionsCountIncremented { collections_count: next_id }); Ok(()) diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index f79df7bccd43c..ec81dae5cbe24 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -717,7 +717,7 @@ fn try_increment_id_works() { assert_eq!(Uniques::get_collections_count(), 1); // increment the collections count again. - assert_ok!(Uniques::try_incremegnt_id(Origin::signed(2))); + assert_ok!(Uniques::try_increment_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); }); From 83d4233fbe599c89fca41fa3da49902e305b1b79 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 14:00:47 +0300 Subject: [PATCH 16/32] unexpose function --- frame/uniques/src/functions.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 6909f4acdb360..638f94750d918 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -208,6 +208,7 @@ impl, I: 'static> Pallet { CollectionsCount::::set(T::CollectionId::from_u32(count).unwrap()); } + #[cfg(test)] pub fn get_collections_count() -> T::CollectionId { CollectionsCount::::get() } From 8d76e7d037d8441282ef5059b879c1b44614e672 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 20:57:17 +0300 Subject: [PATCH 17/32] benchmark --- frame/uniques/src/benchmarking.rs | 10 ++++++++++ frame/uniques/src/functions.rs | 2 +- frame/uniques/src/lib.rs | 4 +++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index 46c347b3c42e9..f1b564cba3c78 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -405,5 +405,15 @@ benchmarks_instance_pallet! { }.into()); } + try_increment_id { + let (_, caller, _) = create_collection::(); + Uniques::::set_collections_count(0); + }: _(SystemOrigin::Signed(caller.clone())) + verify { + assert_last_event::(Event::CollectionsCountIncremented { + collections_count: T::CollectionId::from_u8(1).unwrap() + }.into()); + } + impl_benchmark_test_suite!(Uniques, crate::mock::new_test_ext(), crate::mock::Test); } diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 638f94750d918..23846c9742f2a 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -203,7 +203,7 @@ impl, I: 'static> Pallet { Ok(()) } - #[cfg(test)] + #[cfg(any(test, feature = "runtime-benchmarks"))] pub fn set_collections_count(count: u32) { CollectionsCount::::set(T::CollectionId::from_u32(count).unwrap()); } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 56185f79e8850..b26f6bb689861 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -488,8 +488,10 @@ pub mod pallet { /// /// The origin must be Signed and the sender must have sufficient funds free. /// + /// Emits `CollectionsCountIncremented` event when successful. + /// /// Weight: `O(1)` - #[pallet::weight(T::WeightInfo::try_increment_id())] + #[pallet::weight(1000)] pub fn try_increment_id(origin: OriginFor) -> DispatchResult { ensure_signed(origin)?; ensure!( From 1a05a4e6688e590bc2b1824f00739ec542cf1a11 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Tue, 12 Jul 2022 20:58:15 +0300 Subject: [PATCH 18/32] fmt --- frame/uniques/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index f1b564cba3c78..3eaa4c742c408 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -412,7 +412,7 @@ benchmarks_instance_pallet! { verify { assert_last_event::(Event::CollectionsCountIncremented { collections_count: T::CollectionId::from_u8(1).unwrap() - }.into()); + }.into()); } impl_benchmark_test_suite!(Uniques, crate::mock::new_test_ext(), crate::mock::Test); From 3e4d3c38eff616ba6044f2a97d8d6d7db5d90344 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Wed, 13 Jul 2022 10:27:49 +0300 Subject: [PATCH 19/32] better naming --- frame/uniques/src/benchmarking.rs | 6 +++--- frame/uniques/src/functions.rs | 10 +++++----- frame/uniques/src/lib.rs | 30 ++++++++++++++++-------------- frame/uniques/src/tests.rs | 9 ++++++--- frame/uniques/src/weights.rs | 17 ----------------- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index 3eaa4c742c408..28e162eb97ee3 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -407,11 +407,11 @@ benchmarks_instance_pallet! { try_increment_id { let (_, caller, _) = create_collection::(); - Uniques::::set_collections_count(0); + Uniques::::set_next_id(0); }: _(SystemOrigin::Signed(caller.clone())) verify { - assert_last_event::(Event::CollectionsCountIncremented { - collections_count: T::CollectionId::from_u8(1).unwrap() + assert_last_event::(Event::NextCollectionIdIncremented { + next_id: T::CollectionId::from_u8(1).unwrap() }.into()); } diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 23846c9742f2a..3c3a0fd1b52ae 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -85,7 +85,7 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); - CollectionsCount::::set(collection.saturating_add(T::CollectionId::one())); + NextCollectionId::::set(collection.saturating_add(T::CollectionId::one())); Self::deposit_event(event); Ok(()) @@ -204,12 +204,12 @@ impl, I: 'static> Pallet { } #[cfg(any(test, feature = "runtime-benchmarks"))] - pub fn set_collections_count(count: u32) { - CollectionsCount::::set(T::CollectionId::from_u32(count).unwrap()); + pub fn set_next_id(count: u32) { + NextCollectionId::::set(T::CollectionId::from_u32(count).unwrap()); } #[cfg(test)] - pub fn get_collections_count() -> T::CollectionId { - CollectionsCount::::get() + pub fn get_next_id() -> T::CollectionId { + NextCollectionId::::get() } } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index b26f6bb689861..191874259caee 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -262,7 +262,7 @@ pub mod pallet { #[pallet::storage] /// Stores the `CollectionId` that is going to be used for the next collection. /// This gets incremented by 1 when creating a collection. - pub(super) type CollectionsCount, I: 'static = ()> = + pub(super) type NextCollectionId, I: 'static = ()> = StorageValue<_, T::CollectionId, ValueQuery>; #[pallet::event] @@ -356,8 +356,8 @@ pub mod pallet { OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option }, /// Max supply has been set for a collection. CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 }, - /// Event gets emmited when the `CollectionsCount` gets incremented in `try_increment_id` - CollectionsCountIncremented { collections_count: T::CollectionId }, + /// Event gets emmited when the `NextCollectionId` gets incremented in `try_increment_id` + NextCollectionIdIncremented { next_id: T::CollectionId }, } #[pallet::error] @@ -430,7 +430,7 @@ pub mod pallet { origin: OriginFor, admin: ::Source, ) -> DispatchResult { - let collection = CollectionsCount::::get(); + let collection = NextCollectionId::::get(); let owner = T::CreateOrigin::ensure_origin(origin, &collection)?; let admin = T::Lookup::lookup(admin)?; @@ -470,7 +470,7 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; - let collection = CollectionsCount::::get(); + let collection = NextCollectionId::::get(); Self::do_create_collection( collection, @@ -482,26 +482,28 @@ pub mod pallet { ) } - /// Increments the `CollectionId` stored in `CollectionsCount`. - /// This is only callable when the next `CollectionId` that would be generated is already - /// being used. + /// Increments the `CollectionId` stored in `NextCollectionId`. /// - /// The origin must be Signed and the sender must have sufficient funds free. + ///This is only callable when the next `CollectionId` is already being + /// used for some other collection. + /// + /// The origin must be Signed and the sender must have sufficient funds + /// free. /// - /// Emits `CollectionsCountIncremented` event when successful. + /// Emits `NextCollectionIdIncremented` event when successful. /// /// Weight: `O(1)` #[pallet::weight(1000)] pub fn try_increment_id(origin: OriginFor) -> DispatchResult { ensure_signed(origin)?; ensure!( - Collection::::contains_key(CollectionsCount::::get()), + Collection::::contains_key(NextCollectionId::::get()), Error::::NextIdNotUsed ); - let next_id = CollectionsCount::::get().saturating_add(T::CollectionId::one()); - CollectionsCount::::set(next_id); - Self::deposit_event(Event::CollectionsCountIncremented { collections_count: next_id }); + let next_id = NextCollectionId::::get().saturating_add(T::CollectionId::one()); + NextCollectionId::::set(next_id); + Self::deposit_event(Event::NextCollectionIdIncremented { next_id }); Ok(()) } diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index ec81dae5cbe24..9f3a38428ea23 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -706,15 +706,18 @@ fn try_increment_id_works() { assert_ok!(Uniques::force_create(Origin::root(), 1, true)); // there are now two collections. - assert_eq!(Uniques::get_collections_count(), 2); + assert_eq!(Uniques::get_next_id(), 2); // reset the collections counter to test if the `try_increment_id` // works. - Uniques::set_collections_count(0); + Uniques::set_next_id(0); assert_ok!(Uniques::try_increment_id(Origin::signed(2))); + // `try_increment_id` should emit an event when successful. + assert!(events().contains(&Event::::NextCollectionIdIncremented { next_id: 1 })); + // because reset, the collections count should be now 1 - assert_eq!(Uniques::get_collections_count(), 1); + assert_eq!(Uniques::get_next_id(), 1); // increment the collections count again. assert_ok!(Uniques::try_increment_id(Origin::signed(2))); diff --git a/frame/uniques/src/weights.rs b/frame/uniques/src/weights.rs index eb7cbdaa40cf2..d885077a8dee9 100644 --- a/frame/uniques/src/weights.rs +++ b/frame/uniques/src/weights.rs @@ -70,7 +70,6 @@ pub trait WeightInfo { fn cancel_approval() -> Weight; fn set_accept_ownership() -> Weight; fn set_collection_max_supply() -> Weight; - fn try_increment_id() -> Weight; } /// Weights for pallet_uniques using the Substrate node and recommended hardware. @@ -271,14 +270,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } - - // Storage: Uniques CollectionsCount (r:1 w:1) - // Storage: Uniques Collection (r:1 w:0) - fn try_increment_id() -> Weight { - (10_000_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(2 as Weight)) - .saturating_add(RocksDbWeight::get().writes(1 as Weight)) - } } // For backwards compatibility and tests @@ -478,12 +469,4 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } - - // Storage: Uniques CollectionsCount (r:1 w:1) - // Storage: Uniques Collection (r:1 w:0) - fn try_increment_id() -> Weight { - (10_000_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(2 as Weight)) - .saturating_add(RocksDbWeight::get().writes(1 as Weight)) - } } From c946847986eeef4cf9522faa20a7a75c0a66e7db Mon Sep 17 00:00:00 2001 From: Szegoo Date: Wed, 13 Jul 2022 12:23:47 +0300 Subject: [PATCH 20/32] fix? --- frame/uniques/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 191874259caee..f94c1c5998b12 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -26,6 +26,7 @@ // Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] +#![recursion_limit = "256"] #[cfg(feature = "runtime-benchmarks")] mod benchmarking; @@ -392,7 +393,7 @@ pub mod pallet { MaxSupplyAlreadySet, /// The provided max supply is less to the amount of items a collection already has. MaxSupplyTooSmall, - /// The next `CollectionId` is not being used. + /// The `CollectionId` in `NextCollectionId` is not being used. NextIdNotUsed, } From 119194664c4e20cb23e612dade5a467a936382a5 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 14 Jul 2022 22:02:25 +0300 Subject: [PATCH 21/32] merge fixes --- frame/uniques/src/benchmarking.rs | 2 +- frame/uniques/src/functions.rs | 4 ++-- frame/uniques/src/lib.rs | 2 +- frame/uniques/src/tests.rs | 13 +++++++------ 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index e95d0452ccc1a..bf680d166b9a4 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -448,7 +448,7 @@ benchmarks_instance_pallet! { price, seller, buyer, - }.into()); + }.into()); } impl_benchmark_test_suite!(Uniques, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index c7dd6d1b7c3f6..a0590e4495b28 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -218,7 +218,7 @@ impl, I: 'static> Pallet { #[cfg(test)] pub fn get_next_id() -> T::CollectionId { NextCollectionId::::get() - } + } pub fn do_set_price( collection: T::CollectionId, @@ -288,5 +288,5 @@ impl, I: 'static> Pallet { }); Ok(()) - } + } } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 00e4958401cf9..ff2f32651e331 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -372,7 +372,7 @@ pub mod pallet { OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option }, /// Max supply has been set for a collection. CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 }, - /// Event gets emmited when the `NextCollectionId` gets incremented in `try_increment_id` + /// Event gets emmited when the `NextCollectionId` gets incremented in `try_increment_id`. NextCollectionIdIncremented { next_id: T::CollectionId }, /// The price was set for the instance. ItemPriceSet { diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 21389573231ec..384deabcad5ff 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -723,17 +723,18 @@ fn try_increment_id_works() { assert_ok!(Uniques::try_increment_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); - }); + }); } +#[test] fn set_price_should_work() { new_test_ext().execute_with(|| { let user_id = 1; - let collection_id = 0; + let collection_id: u32 = 0; let item_1 = 1; let item_2 = 2; - assert_ok!(Uniques::force_create(Origin::root(), collection_id, user_id, true)); + assert_ok!(Uniques::force_create(Origin::root(), user_id, true)); assert_ok!(Uniques::mint(Origin::signed(user_id), collection_id, item_1, user_id)); assert_ok!(Uniques::mint(Origin::signed(user_id), collection_id, item_2, user_id)); @@ -785,7 +786,7 @@ fn buy_item_should_work() { let user_1 = 1; let user_2 = 2; let user_3 = 3; - let collection_id = 0; + let collection_id: u32 = 0; let item_1 = 1; let item_2 = 2; let item_3 = 3; @@ -797,7 +798,7 @@ fn buy_item_should_work() { Balances::make_free_balance_be(&user_2, initial_balance); Balances::make_free_balance_be(&user_3, initial_balance); - assert_ok!(Uniques::force_create(Origin::root(), collection_id, user_1, true)); + assert_ok!(Uniques::force_create(Origin::root(), user_1, true)); assert_ok!(Uniques::mint(Origin::signed(user_1), collection_id, item_1, user_1)); assert_ok!(Uniques::mint(Origin::signed(user_1), collection_id, item_2, user_1)); @@ -898,5 +899,5 @@ fn buy_item_should_work() { }); assert_noop!(buy_item_call.dispatch(Origin::signed(user_2)), Error::::Frozen); } - } + }); } From 8fb3fbc8574838c9a910893be67f841b2196f86e Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 14 Jul 2022 22:05:15 +0300 Subject: [PATCH 22/32] fmt --- frame/uniques/src/benchmarking.rs | 6 +++--- frame/uniques/src/functions.rs | 4 ++-- frame/uniques/src/tests.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index bf680d166b9a4..e5034e0b27c5a 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -412,9 +412,9 @@ benchmarks_instance_pallet! { verify { assert_last_event::(Event::NextCollectionIdIncremented { next_id: T::CollectionId::from_u8(1).unwrap() - }.into()); + }.into()); } - + set_price { let (collection, caller, _) = create_collection::(); let (item, ..) = mint_item::(0); @@ -448,7 +448,7 @@ benchmarks_instance_pallet! { price, seller, buyer, - }.into()); + }.into()); } impl_benchmark_test_suite!(Uniques, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index a0590e4495b28..0136a674156a4 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -218,7 +218,7 @@ impl, I: 'static> Pallet { #[cfg(test)] pub fn get_next_id() -> T::CollectionId { NextCollectionId::::get() - } + } pub fn do_set_price( collection: T::CollectionId, @@ -288,5 +288,5 @@ impl, I: 'static> Pallet { }); Ok(()) - } + } } diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index 384deabcad5ff..bdecec6e3aa02 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -723,7 +723,7 @@ fn try_increment_id_works() { assert_ok!(Uniques::try_increment_id(Origin::signed(2))); // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); - }); + }); } #[test] @@ -899,5 +899,5 @@ fn buy_item_should_work() { }); assert_noop!(buy_item_call.dispatch(Origin::signed(user_2)), Error::::Frozen); } - }); + }); } From f51f9f6efd558979f07cd061168754e0634533ca Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Fri, 15 Jul 2022 09:08:08 +0000 Subject: [PATCH 23/32] ".git/.scripts/bench-bot.sh" pallet dev pallet_uniques --- frame/uniques/src/weights.rs | 169 +++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 75 deletions(-) diff --git a/frame/uniques/src/weights.rs b/frame/uniques/src/weights.rs index 7c8cb170b1b1d..75bb89f26b428 100644 --- a/frame/uniques/src/weights.rs +++ b/frame/uniques/src/weights.rs @@ -18,12 +18,12 @@ //! Autogenerated weights for pallet_uniques //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-07-13, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `test-bench-bot`, CPU: `Intel(R) Xeon(R) CPU @ 3.10GHz` +//! DATE: 2022-07-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `bm3`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// target/production/substrate +// /home/benchbot/cargo_target_dir/production/substrate // benchmark // pallet // --steps=50 @@ -70,6 +70,7 @@ pub trait WeightInfo { fn cancel_approval() -> Weight; fn set_accept_ownership() -> Weight; fn set_collection_max_supply() -> Weight; + fn try_increment_id() -> Weight; fn set_price() -> Weight; fn buy_item() -> Weight; } @@ -77,19 +78,21 @@ pub trait WeightInfo { /// Weights for pallet_uniques using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { + // Storage: Uniques NextCollectionId (r:1 w:1) // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn create() -> Weight { - (33_075_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) + (30_481_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } + // Storage: Uniques NextCollectionId (r:1 w:1) // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn force_create() -> Weight { - (19_528_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(2 as Weight)) + (19_811_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques Asset (r:1 w:0) @@ -104,12 +107,12 @@ impl WeightInfo for SubstrateWeight { /// The range of component `a` is `[0, 1000]`. fn destroy(n: u32, m: u32, a: u32, ) -> Weight { (0 as Weight) - // Standard Error: 25_000 - .saturating_add((13_639_000 as Weight).saturating_mul(n as Weight)) - // Standard Error: 25_000 - .saturating_add((2_393_000 as Weight).saturating_mul(m as Weight)) - // Standard Error: 25_000 - .saturating_add((2_217_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 17_000 + .saturating_add((10_950_000 as Weight).saturating_mul(n as Weight)) + // Standard Error: 17_000 + .saturating_add((1_657_000 as Weight).saturating_mul(m as Weight)) + // Standard Error: 17_000 + .saturating_add((1_512_000 as Weight).saturating_mul(a as Weight)) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(n as Weight))) .saturating_add(T::DbWeight::get().writes(4 as Weight)) @@ -122,7 +125,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques CollectionMaxSupply (r:1 w:0) // Storage: Uniques Account (r:0 w:1) fn mint() -> Weight { - (42_146_000 as Weight) + (36_980_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } @@ -131,7 +134,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques Account (r:0 w:1) // Storage: Uniques ItemPriceOf (r:0 w:1) fn burn() -> Weight { - (42_960_000 as Weight) + (38_771_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -140,7 +143,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques Account (r:0 w:2) // Storage: Uniques ItemPriceOf (r:0 w:1) fn transfer() -> Weight { - (33_025_000 as Weight) + (29_914_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -149,8 +152,8 @@ impl WeightInfo for SubstrateWeight { /// The range of component `i` is `[0, 5000]`. fn redeposit(i: u32, ) -> Weight { (0 as Weight) - // Standard Error: 24_000 - .saturating_add((15_540_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 16_000 + .saturating_add((12_759_000 as Weight).saturating_mul(i as Weight)) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -159,26 +162,26 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques Asset (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn freeze() -> Weight { - (25_194_000 as Weight) + (22_425_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Asset (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn thaw() -> Weight { - (25_397_000 as Weight) + (23_011_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn freeze_collection() -> Weight { - (19_278_000 as Weight) + (17_718_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn thaw_collection() -> Weight { - (19_304_000 as Weight) + (17_619_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -186,20 +189,20 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:2) fn transfer_ownership() -> Weight { - (28_615_000 as Weight) + (25_869_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn set_team() -> Weight { - (19_943_000 as Weight) + (18_058_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn force_item_status() -> Weight { - (22_583_000 as Weight) + (20_720_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } @@ -207,7 +210,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques InstanceMetadataOf (r:1 w:0) // Storage: Uniques Attribute (r:1 w:1) fn set_attribute() -> Weight { - (47_520_000 as Weight) + (41_808_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } @@ -215,69 +218,76 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques InstanceMetadataOf (r:1 w:0) // Storage: Uniques Attribute (r:1 w:1) fn clear_attribute() -> Weight { - (45_316_000 as Weight) + (39_866_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques InstanceMetadataOf (r:1 w:1) fn set_metadata() -> Weight { - (38_391_000 as Weight) + (34_767_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques InstanceMetadataOf (r:1 w:1) fn clear_metadata() -> Weight { - (38_023_000 as Weight) + (33_910_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassMetadataOf (r:1 w:1) fn set_collection_metadata() -> Weight { - (37_398_000 as Weight) + (33_827_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques ClassMetadataOf (r:1 w:1) fn clear_collection_metadata() -> Weight { - (35_621_000 as Weight) + (31_998_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Asset (r:1 w:1) fn approve_transfer() -> Weight { - (25_856_000 as Weight) + (23_607_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Asset (r:1 w:1) fn cancel_approval() -> Weight { - (26_098_000 as Weight) + (23_341_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques OwnershipAcceptance (r:1 w:1) fn set_accept_ownership() -> Weight { - (24_076_000 as Weight) + (21_969_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques CollectionMaxSupply (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn set_collection_max_supply() -> Weight { - (22_035_000 as Weight) + (20_355_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + // Storage: Uniques NextCollectionId (r:1 w:1) + // Storage: Uniques Class (r:1 w:0) + fn try_increment_id() -> Weight { + (19_233_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } // Storage: Uniques Asset (r:1 w:0) // Storage: Uniques ItemPriceOf (r:0 w:1) fn set_price() -> Weight { - (22_534_000 as Weight) + (20_733_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -286,7 +296,7 @@ impl WeightInfo for SubstrateWeight { // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Account (r:0 w:2) fn buy_item() -> Weight { - (45_272_000 as Weight) + (40_798_000 as Weight) .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } @@ -294,19 +304,21 @@ impl WeightInfo for SubstrateWeight { // For backwards compatibility and tests impl WeightInfo for () { + // Storage: Uniques NextCollectionId (r:1 w:1) // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn create() -> Weight { - (33_075_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + (30_481_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } + // Storage: Uniques NextCollectionId (r:1 w:1) // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn force_create() -> Weight { - (19_528_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + (19_811_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques Asset (r:1 w:0) @@ -321,12 +333,12 @@ impl WeightInfo for () { /// The range of component `a` is `[0, 1000]`. fn destroy(n: u32, m: u32, a: u32, ) -> Weight { (0 as Weight) - // Standard Error: 25_000 - .saturating_add((13_639_000 as Weight).saturating_mul(n as Weight)) - // Standard Error: 25_000 - .saturating_add((2_393_000 as Weight).saturating_mul(m as Weight)) - // Standard Error: 25_000 - .saturating_add((2_217_000 as Weight).saturating_mul(a as Weight)) + // Standard Error: 17_000 + .saturating_add((10_950_000 as Weight).saturating_mul(n as Weight)) + // Standard Error: 17_000 + .saturating_add((1_657_000 as Weight).saturating_mul(m as Weight)) + // Standard Error: 17_000 + .saturating_add((1_512_000 as Weight).saturating_mul(a as Weight)) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(n as Weight))) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) @@ -339,7 +351,7 @@ impl WeightInfo for () { // Storage: Uniques CollectionMaxSupply (r:1 w:0) // Storage: Uniques Account (r:0 w:1) fn mint() -> Weight { - (42_146_000 as Weight) + (36_980_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(3 as Weight)) } @@ -348,7 +360,7 @@ impl WeightInfo for () { // Storage: Uniques Account (r:0 w:1) // Storage: Uniques ItemPriceOf (r:0 w:1) fn burn() -> Weight { - (42_960_000 as Weight) + (38_771_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } @@ -357,7 +369,7 @@ impl WeightInfo for () { // Storage: Uniques Account (r:0 w:2) // Storage: Uniques ItemPriceOf (r:0 w:1) fn transfer() -> Weight { - (33_025_000 as Weight) + (29_914_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } @@ -366,8 +378,8 @@ impl WeightInfo for () { /// The range of component `i` is `[0, 5000]`. fn redeposit(i: u32, ) -> Weight { (0 as Weight) - // Standard Error: 24_000 - .saturating_add((15_540_000 as Weight).saturating_mul(i as Weight)) + // Standard Error: 16_000 + .saturating_add((12_759_000 as Weight).saturating_mul(i as Weight)) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(i as Weight))) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) @@ -376,26 +388,26 @@ impl WeightInfo for () { // Storage: Uniques Asset (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn freeze() -> Weight { - (25_194_000 as Weight) + (22_425_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Asset (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn thaw() -> Weight { - (25_397_000 as Weight) + (23_011_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn freeze_collection() -> Weight { - (19_278_000 as Weight) + (17_718_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn thaw_collection() -> Weight { - (19_304_000 as Weight) + (17_619_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } @@ -403,20 +415,20 @@ impl WeightInfo for () { // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:2) fn transfer_ownership() -> Weight { - (28_615_000 as Weight) + (25_869_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } // Storage: Uniques Class (r:1 w:1) fn set_team() -> Weight { - (19_943_000 as Weight) + (18_058_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassAccount (r:0 w:1) fn force_item_status() -> Weight { - (22_583_000 as Weight) + (20_720_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } @@ -424,7 +436,7 @@ impl WeightInfo for () { // Storage: Uniques InstanceMetadataOf (r:1 w:0) // Storage: Uniques Attribute (r:1 w:1) fn set_attribute() -> Weight { - (47_520_000 as Weight) + (41_808_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } @@ -432,69 +444,76 @@ impl WeightInfo for () { // Storage: Uniques InstanceMetadataOf (r:1 w:0) // Storage: Uniques Attribute (r:1 w:1) fn clear_attribute() -> Weight { - (45_316_000 as Weight) + (39_866_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques InstanceMetadataOf (r:1 w:1) fn set_metadata() -> Weight { - (38_391_000 as Weight) + (34_767_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques InstanceMetadataOf (r:1 w:1) fn clear_metadata() -> Weight { - (38_023_000 as Weight) + (33_910_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:1) // Storage: Uniques ClassMetadataOf (r:1 w:1) fn set_collection_metadata() -> Weight { - (37_398_000 as Weight) + (33_827_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(2 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques ClassMetadataOf (r:1 w:1) fn clear_collection_metadata() -> Weight { - (35_621_000 as Weight) + (31_998_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Asset (r:1 w:1) fn approve_transfer() -> Weight { - (25_856_000 as Weight) + (23_607_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Asset (r:1 w:1) fn cancel_approval() -> Weight { - (26_098_000 as Weight) + (23_341_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques OwnershipAcceptance (r:1 w:1) fn set_accept_ownership() -> Weight { - (24_076_000 as Weight) + (21_969_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques CollectionMaxSupply (r:1 w:1) // Storage: Uniques Class (r:1 w:0) fn set_collection_max_supply() -> Weight { - (22_035_000 as Weight) + (20_355_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + // Storage: Uniques NextCollectionId (r:1 w:1) + // Storage: Uniques Class (r:1 w:0) + fn try_increment_id() -> Weight { + (19_233_000 as Weight) .saturating_add(RocksDbWeight::get().reads(2 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } // Storage: Uniques Asset (r:1 w:0) // Storage: Uniques ItemPriceOf (r:0 w:1) fn set_price() -> Weight { - (22_534_000 as Weight) + (20_733_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } @@ -503,7 +522,7 @@ impl WeightInfo for () { // Storage: Uniques Class (r:1 w:0) // Storage: Uniques Account (r:0 w:2) fn buy_item() -> Weight { - (45_272_000 as Weight) + (40_798_000 as Weight) .saturating_add(RocksDbWeight::get().reads(3 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } From 0b4dce4427f50d7b76fd1ea4882485ebd1884369 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Thu, 21 Jul 2022 08:51:14 +0300 Subject: [PATCH 24/32] wrong weight --- frame/uniques/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index ff2f32651e331..4fc0ca86fe3e0 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -532,7 +532,7 @@ pub mod pallet { /// Emits `NextCollectionIdIncremented` event when successful. /// /// Weight: `O(1)` - #[pallet::weight(1000)] + #[pallet::weight(T::WeightInfo::try_increment_id())] pub fn try_increment_id(origin: OriginFor) -> DispatchResult { ensure_signed(origin)?; ensure!( From 45a21e04b0ea7f34d314d790b7b391e556ad6584 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:29:49 +0200 Subject: [PATCH 25/32] Update frame/uniques/src/lib.rs Co-authored-by: Oliver Tale-Yazdi --- frame/uniques/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 4fc0ca86fe3e0..d628ea6e97303 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -426,6 +426,8 @@ pub mod pallet { /// The provided max supply is less to the amount of items a collection already has. MaxSupplyTooSmall, /// The `CollectionId` in `NextCollectionId` is not being used. + /// + /// This means that you can directly proceed to call `create`. NextIdNotUsed, /// The given item ID is unknown. UnknownItem, From 706600077eed4e8b6d0e134725231c5439cfe8c1 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:30:05 +0200 Subject: [PATCH 26/32] Update frame/uniques/src/lib.rs Co-authored-by: Oliver Tale-Yazdi --- frame/uniques/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index d628ea6e97303..26dff3c3af7fc 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -525,7 +525,7 @@ pub mod pallet { /// Increments the `CollectionId` stored in `NextCollectionId`. /// - ///This is only callable when the next `CollectionId` is already being + /// This is only callable when the next `CollectionId` is already being /// used for some other collection. /// /// The origin must be Signed and the sender must have sufficient funds From e37673d18fcfe1a1d1d445f55113b55c3194de5c Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 25 Jul 2022 12:00:50 +0200 Subject: [PATCH 27/32] using substrate trait instead of num-traits --- Cargo.lock | 1 - frame/uniques/Cargo.toml | 1 - frame/uniques/src/functions.rs | 4 ++-- frame/uniques/src/lib.rs | 7 +++---- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b4c232f63f79..c2c75f30e5ea1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6489,7 +6489,6 @@ dependencies = [ "frame-support", "frame-system", "log", - "num-traits", "pallet-balances", "parity-scale-codec", "scale-info", diff --git a/frame/uniques/Cargo.toml b/frame/uniques/Cargo.toml index 1f8be1566b43b..19b0790947f84 100644 --- a/frame/uniques/Cargo.toml +++ b/frame/uniques/Cargo.toml @@ -21,7 +21,6 @@ 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" } diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 0136a674156a4..8a26e4fe0b099 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -89,7 +89,7 @@ impl, I: 'static> Pallet { ); CollectionAccount::::insert(&owner, &collection, ()); - NextCollectionId::::set(collection.saturating_add(T::CollectionId::one())); + NextCollectionId::::set(collection.saturating_add(1u32.into())); Self::deposit_event(event); Ok(()) @@ -212,7 +212,7 @@ impl, I: 'static> Pallet { #[cfg(any(test, feature = "runtime-benchmarks"))] pub fn set_next_id(count: u32) { - NextCollectionId::::set(T::CollectionId::from_u32(count).unwrap()); + NextCollectionId::::set(count.into()); } #[cfg(test)] diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 26dff3c3af7fc..714445faf92c1 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -56,7 +56,7 @@ use sp_runtime::{ }; use sp_std::prelude::*; -use num_traits::{FromPrimitive, One, Unsigned}; +use sp_runtime::traits::AtLeast32BitUnsigned; pub use pallet::*; pub use types::*; @@ -99,8 +99,7 @@ pub mod pallet { + MaxEncodedLen + Copy + Default - + Unsigned - + FromPrimitive + + AtLeast32BitUnsigned + Saturating; /// The type used to identify a unique item within a collection. @@ -542,7 +541,7 @@ pub mod pallet { Error::::NextIdNotUsed ); - let next_id = NextCollectionId::::get().saturating_add(T::CollectionId::one()); + let next_id = NextCollectionId::::get().saturating_add(1u32.into()); NextCollectionId::::set(next_id); Self::deposit_event(Event::NextCollectionIdIncremented { next_id }); Ok(()) From 2342b079dae079cf4a19a90b327b9e7380edc53e Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 25 Jul 2022 12:02:20 +0200 Subject: [PATCH 28/32] remove unnecessary trait --- frame/uniques/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 714445faf92c1..0c43c701aa809 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -99,8 +99,7 @@ pub mod pallet { + MaxEncodedLen + Copy + Default - + AtLeast32BitUnsigned - + Saturating; + + AtLeast32BitUnsigned; /// The type used to identify a unique item within a collection. type ItemId: Member + Parameter + MaxEncodedLen + Copy; From 576b21c1e386d76cf9b8b41605c622333c3c83c6 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 25 Jul 2022 12:06:28 +0200 Subject: [PATCH 29/32] emit NextCollectionIdIncremented in do_create_collection --- frame/uniques/src/functions.rs | 5 ++++- frame/uniques/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/frame/uniques/src/functions.rs b/frame/uniques/src/functions.rs index 8a26e4fe0b099..dcbb1f27973d7 100644 --- a/frame/uniques/src/functions.rs +++ b/frame/uniques/src/functions.rs @@ -88,9 +88,12 @@ impl, I: 'static> Pallet { }, ); + let next_id = collection.saturating_add(1u32.into()); + CollectionAccount::::insert(&owner, &collection, ()); - NextCollectionId::::set(collection.saturating_add(1u32.into())); + NextCollectionId::::set(next_id); + Self::deposit_event(Event::NextCollectionIdIncremented { next_id }); Self::deposit_event(event); Ok(()) } diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 0c43c701aa809..ebadd903d76be 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -370,7 +370,7 @@ pub mod pallet { OwnershipAcceptanceChanged { who: T::AccountId, maybe_collection: Option }, /// Max supply has been set for a collection. CollectionMaxSupplySet { collection: T::CollectionId, max_supply: u32 }, - /// Event gets emmited when the `NextCollectionId` gets incremented in `try_increment_id`. + /// Event gets emmited when the `NextCollectionId` gets incremented. NextCollectionIdIncremented { next_id: T::CollectionId }, /// The price was set for the instance. ItemPriceSet { From 167c84c76b53f62a7014b094a4a03329e6dc8b2a Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 25 Jul 2022 12:22:31 +0200 Subject: [PATCH 30/32] fix in benchmarks --- frame/uniques/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/benchmarking.rs b/frame/uniques/src/benchmarking.rs index e5034e0b27c5a..86247fb964ff5 100644 --- a/frame/uniques/src/benchmarking.rs +++ b/frame/uniques/src/benchmarking.rs @@ -411,7 +411,7 @@ benchmarks_instance_pallet! { }: _(SystemOrigin::Signed(caller.clone())) verify { assert_last_event::(Event::NextCollectionIdIncremented { - next_id: T::CollectionId::from_u8(1).unwrap() + next_id: 1u32.into() }.into()); } From 01f0f2080e3bdfa092d8c1d6f4b2fd01f7aa1762 Mon Sep 17 00:00:00 2001 From: Szegoo Date: Mon, 25 Jul 2022 16:00:42 +0200 Subject: [PATCH 31/32] check for event & group import --- frame/uniques/src/lib.rs | 4 +--- frame/uniques/src/tests.rs | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index ebadd903d76be..258a02a276ab5 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -51,13 +51,11 @@ use frame_support::{ }; use frame_system::Config as SystemConfig; use sp_runtime::{ - traits::{Saturating, StaticLookup, Zero}, + traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero}, ArithmeticError, RuntimeDebug, }; use sp_std::prelude::*; -use sp_runtime::traits::AtLeast32BitUnsigned; - pub use pallet::*; pub use types::*; pub use weights::WeightInfo; diff --git a/frame/uniques/src/tests.rs b/frame/uniques/src/tests.rs index bdecec6e3aa02..bd3a2b032945e 100644 --- a/frame/uniques/src/tests.rs +++ b/frame/uniques/src/tests.rs @@ -701,9 +701,11 @@ fn try_increment_id_works() { // should fail because the next `CollectionId` is not being used. assert_noop!(Uniques::try_increment_id(Origin::signed(2)), Error::::NextIdNotUsed); - // create two collections. + // create two collections & check for events. assert_ok!(Uniques::force_create(Origin::root(), 1, true)); + assert!(events().contains(&Event::::NextCollectionIdIncremented { next_id: 1 })); assert_ok!(Uniques::force_create(Origin::root(), 1, true)); + assert!(events().contains(&Event::::NextCollectionIdIncremented { next_id: 2 })); // there are now two collections. assert_eq!(Uniques::get_next_id(), 2); From bf61196c0f56c002ae721eb8dbb4ca24cc3be70a Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Sat, 30 Jul 2022 13:34:35 +0200 Subject: [PATCH 32/32] docs --- frame/uniques/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/uniques/src/lib.rs b/frame/uniques/src/lib.rs index 258a02a276ab5..b25fb2f87ea97 100644 --- a/frame/uniques/src/lib.rs +++ b/frame/uniques/src/lib.rs @@ -273,7 +273,7 @@ pub mod pallet { #[pallet::storage] /// Stores the `CollectionId` that is going to be used for the next collection. - /// This gets incremented by 1 when creating a collection. + /// This gets incremented by 1 whenever a new collection is created. pub(super) type NextCollectionId, I: 'static = ()> = StorageValue<_, T::CollectionId, ValueQuery>;