git.delta.rocks / unique-network / refs/commits / 3e410d6948a5

difftreelog

fix(rmrk) rewrite existing parts if dups exist

Daniel Shiposha2022-06-20parent: #da5e7f4.patch.diff
in: master

3 files changed

modifiedpallets/proxy-rmrk-equip/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-equip/src/benchmarking.rs
+++ b/pallets/proxy-rmrk-equip/src/benchmarking.rs
@@ -21,9 +21,9 @@
 	vec![0; S::get() as usize].try_into().expect("size == S")
 }
 
-fn create_max_part() -> RmrkPartType {
+fn create_max_part(id: RmrkSlotId) -> RmrkPartType {
 	RmrkPartType::SlotPart(RmrkSlotPart {
-		id: 42,
+		id,
 		equippable: RmrkEquippableList::Custom(create_u32_array()),
 		src: create_data(),
 		z: 1,
@@ -31,9 +31,13 @@
 }
 
 fn create_parts_array<S: Get<u32>>(num: u32) -> BoundedVec<RmrkPartType, S> {
-	vec![create_max_part(); num as usize]
-		.try_into()
-		.expect("num <= S")
+	let mut parts: BoundedVec<RmrkPartType, S> = vec![].try_into().expect("0 <= S");
+
+	for i in 0..num {
+		parts.try_push(create_max_part(i)).expect("num <= S");
+	}
+
+	parts
 }
 
 fn create_max_theme_property() -> RmrkThemeProperty {
@@ -90,4 +94,27 @@
 
 		let theme = create_max_theme(create_data(), b);
 	}: _(RawOrigin::Signed(caller), base_id, theme)
+
+	equippable {
+		let caller = account("caller", 0, SEED);
+		<T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());
+
+		let base_id = 1;
+		let slot_id = 42;
+
+		let base_type = create_data();
+		let symbol = create_data();
+		let parts = vec! {
+			RmrkPartType::SlotPart(RmrkSlotPart {
+				id: slot_id,
+				equippable: RmrkEquippableList::All,
+				src: create_data(),
+				z: 1,
+			})
+		}.try_into().expect("1 <= RmrkPartsLimit");
+
+		<Pallet<T>>::create_base(RawOrigin::Signed(caller.clone()).into(), base_type, symbol, parts)?;
+
+		let equippables = RmrkEquippableList::Custom(create_u32_array());
+	}: _(RawOrigin::Signed(caller), base_id, slot_id, equippables)
 }
modifiedpallets/proxy-rmrk-equip/src/lib.rsdiffbeforeafterboth
--- a/pallets/proxy-rmrk-equip/src/lib.rs
+++ b/pallets/proxy-rmrk-equip/src/lib.rs
@@ -141,17 +141,7 @@
 			let collection = <PalletCore<T>>::get_nft_collection(collection_id)?;
 
 			for part in parts {
-				let part_id = part.id();
-				let part_token_id = Self::create_part(&cross_sender, &collection, part)?;
-
-				<InernalPartId<T>>::insert(collection_id, part_id, part_token_id);
-
-				<PalletNft<T>>::set_scoped_token_property(
-					collection_id,
-					part_token_id,
-					PropertyScope::Rmrk,
-					<PalletCore<T>>::rmrk_property(ExternalPartId, &part_id)?,
-				)?;
+				Self::create_part(&cross_sender, &collection, part)?;
 			}
 
 			Self::deposit_event(Event::BaseCreated {
@@ -279,9 +269,10 @@
 		sender: &T::CrossAccountId,
 		collection: &NonfungibleHandle<T>,
 		part: RmrkPartType,
-	) -> Result<TokenId, DispatchError> {
+	) -> DispatchResult {
 		let owner = sender;
 
+		let part_id = part.id();
 		let src = part.src();
 		let z_index = part.z_index();
 
@@ -290,32 +281,55 @@
 			RmrkPartType::SlotPart(_) => NftType::SlotPart,
 		};
 
-		let token_id = <PalletCore<T>>::create_nft(
-			sender,
-			owner,
-			collection,
+		let token_id = match Self::internal_part_id(collection.id, part_id) {
+			Some(token_id) => token_id,
+			None => {
+				let token_id = <PalletCore<T>>::create_nft(
+					sender,
+					owner,
+					collection,
+					[].into_iter(),
+				)
+				.map_err(|err| match err {
+					DispatchError::Arithmetic(_) => <Error<T>>::NoAvailablePartId.into(),
+					err => err,
+				})?;
+
+				<InernalPartId<T>>::insert(collection.id, part_id, token_id);
+
+				<PalletNft<T>>::set_scoped_token_property(
+					collection.id,
+					token_id,
+					PropertyScope::Rmrk,
+					<PalletCore<T>>::rmrk_property(ExternalPartId, &part_id)?,
+				)?;
+
+				token_id
+			}
+		};
+
+		<PalletNft<T>>::set_scoped_token_properties(
+			collection.id,
+			token_id,
+			PropertyScope::Rmrk,
 			[
 				<PalletCore<T>>::rmrk_property(TokenType, &nft_type)?,
 				<PalletCore<T>>::rmrk_property(Src, &src)?,
 				<PalletCore<T>>::rmrk_property(ZIndex, &z_index)?,
 			]
-			.into_iter(),
-		)
-		.map_err(|err| match err {
-			DispatchError::Arithmetic(_) => <Error<T>>::NoAvailablePartId.into(),
-			err => err,
-		})?;
+			.into_iter()
+		)?;
 
 		if let RmrkPartType::SlotPart(part) = part {
 			<PalletNft<T>>::set_scoped_token_property(
 				collection.id,
 				token_id,
 				PropertyScope::Rmrk,
-				<PalletCore<T>>::rmrk_property(EquippableList, &part.equippable)?,
+				<PalletCore<T>>::rmrk_property(EquippableList, &part.equippable)?
 			)?;
 		}
 
-		Ok(token_id)
+		Ok(())
 	}
 
 	fn get_base(base_id: CollectionId) -> Result<NonfungibleHandle<T>, DispatchError> {
modifiedpallets/proxy-rmrk-equip/src/weights.rsdiffbeforeafterboth
before · pallets/proxy-rmrk-equip/src/weights.rs
1// Template adopted from https://github.com/paritytech/substrate/blob/master/.maintain/frame-weight-template.hbs23//! Autogenerated weights for pallet_proxy_rmrk_equip4//!5//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev6//! DATE: 2022-06-20, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`7//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 102489// Executed Command:10// target/release/unique-collator11// benchmark12// pallet13// --pallet14// pallet-proxy-rmrk-equip15// --wasm-execution16// compiled17// --extrinsic18// *19// --template20// .maintain/frame-weight-template.hbs21// --steps=5022// --repeat=8023// --heap-pages=409624// --output=./pallets/proxy-rmrk-equip/src/weights.rs2526#![cfg_attr(rustfmt, rustfmt_skip)]27#![allow(unused_parens)]28#![allow(unused_imports)]29#![allow(clippy::unnecessary_cast)]3031use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};32use sp_std::marker::PhantomData;3334/// Weight functions needed for pallet_proxy_rmrk_equip.35pub trait WeightInfo {36	fn create_base(b: u32, ) -> Weight;37	fn theme_add(b: u32, ) -> Weight;38	fn equippable() -> Weight;39}4041/// Weights for pallet_proxy_rmrk_equip using the Substrate node and recommended hardware.42pub struct SubstrateWeight<T>(PhantomData<T>);43impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {44	// Storage: Common CreatedCollectionCount (r:1 w:1)45	// Storage: Common DestroyedCollectionCount (r:1 w:0)46	// Storage: System Account (r:2 w:2)47	// Storage: Common CollectionPropertyPermissions (r:0 w:1)48	// Storage: Common CollectionProperties (r:0 w:1)49	// Storage: Common CollectionById (r:0 w:1)50	// Storage: Nonfungible TokensMinted (r:1 w:1)51	// Storage: Nonfungible AccountBalance (r:1 w:1)52	// Storage: Nonfungible TokenProperties (r:1 w:1)53	// Storage: Nonfungible TokenData (r:0 w:1)54	// Storage: Nonfungible Owned (r:0 w:1)55	// Storage: RmrkEquip InernalPartId (r:0 w:1)56	fn create_base(b: u32, ) -> Weight {57		(44_632_000 as Weight)58			// Standard Error: 10_00059			.saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))60			.saturating_add(T::DbWeight::get().reads(6 as Weight))61			.saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))62			.saturating_add(T::DbWeight::get().writes(8 as Weight))63			.saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))64	}65	// Storage: Common CollectionProperties (r:1 w:0)66	// Storage: Common CollectionById (r:1 w:0)67	// Storage: RmrkEquip BaseHasDefaultTheme (r:1 w:0)68	// Storage: Nonfungible TokensMinted (r:1 w:1)69	// Storage: Nonfungible AccountBalance (r:1 w:1)70	// Storage: Nonfungible TokenProperties (r:1 w:1)71	// Storage: Nonfungible TokenData (r:0 w:1)72	// Storage: Nonfungible Owned (r:0 w:1)73	fn theme_add(b: u32, ) -> Weight {74		(39_525_000 as Weight)75			// Standard Error: 12_00076			.saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))77			.saturating_add(T::DbWeight::get().reads(6 as Weight))78			.saturating_add(T::DbWeight::get().writes(5 as Weight))79	}80	// Storage: Common CollectionProperties (r:1 w:0)81	// Storage: Common CollectionById (r:1 w:0)82	// Storage: RmrkEquip InernalPartId (r:1 w:0)83	// Storage: Nonfungible TokenProperties (r:1 w:1)84	fn equippable() -> Weight {85		(27_371_000 as Weight)86			.saturating_add(T::DbWeight::get().reads(4 as Weight))87			.saturating_add(T::DbWeight::get().writes(1 as Weight))88	}89}9091// For backwards compatibility and tests92impl WeightInfo for () {93	// Storage: Common CreatedCollectionCount (r:1 w:1)94	// Storage: Common DestroyedCollectionCount (r:1 w:0)95	// Storage: System Account (r:2 w:2)96	// Storage: Common CollectionPropertyPermissions (r:0 w:1)97	// Storage: Common CollectionProperties (r:0 w:1)98	// Storage: Common CollectionById (r:0 w:1)99	// Storage: Nonfungible TokensMinted (r:1 w:1)100	// Storage: Nonfungible AccountBalance (r:1 w:1)101	// Storage: Nonfungible TokenProperties (r:1 w:1)102	// Storage: Nonfungible TokenData (r:0 w:1)103	// Storage: Nonfungible Owned (r:0 w:1)104	// Storage: RmrkEquip InernalPartId (r:0 w:1)105	fn create_base(b: u32, ) -> Weight {106		(44_632_000 as Weight)107			// Standard Error: 10_000108			.saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))109			.saturating_add(RocksDbWeight::get().reads(6 as Weight))110			.saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))111			.saturating_add(RocksDbWeight::get().writes(8 as Weight))112			.saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))113	}114	// Storage: Common CollectionProperties (r:1 w:0)115	// Storage: Common CollectionById (r:1 w:0)116	// Storage: RmrkEquip BaseHasDefaultTheme (r:1 w:0)117	// Storage: Nonfungible TokensMinted (r:1 w:1)118	// Storage: Nonfungible AccountBalance (r:1 w:1)119	// Storage: Nonfungible TokenProperties (r:1 w:1)120	// Storage: Nonfungible TokenData (r:0 w:1)121	// Storage: Nonfungible Owned (r:0 w:1)122	fn theme_add(b: u32, ) -> Weight {123		(39_525_000 as Weight)124			// Standard Error: 12_000125			.saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))126			.saturating_add(RocksDbWeight::get().reads(6 as Weight))127			.saturating_add(RocksDbWeight::get().writes(5 as Weight))128	}129	// Storage: Common CollectionProperties (r:1 w:0)130	// Storage: Common CollectionById (r:1 w:0)131	// Storage: RmrkEquip InernalPartId (r:1 w:0)132	// Storage: Nonfungible TokenProperties (r:1 w:1)133	fn equippable() -> Weight {134		(27_371_000 as Weight)135			.saturating_add(RocksDbWeight::get().reads(4 as Weight))136			.saturating_add(RocksDbWeight::get().writes(1 as Weight))137	}138}