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
21 vec![0; S::get() as usize].try_into().expect("size == S")21 vec![0; S::get() as usize].try_into().expect("size == S")
22}22}
2323
24fn create_max_part() -> RmrkPartType {24fn create_max_part(id: RmrkSlotId) -> RmrkPartType {
25 RmrkPartType::SlotPart(RmrkSlotPart {25 RmrkPartType::SlotPart(RmrkSlotPart {
26 id: 42,26 id,
27 equippable: RmrkEquippableList::Custom(create_u32_array()),27 equippable: RmrkEquippableList::Custom(create_u32_array()),
28 src: create_data(),28 src: create_data(),
29 z: 1,29 z: 1,
30 })30 })
31}31}
3232
33fn create_parts_array<S: Get<u32>>(num: u32) -> BoundedVec<RmrkPartType, S> {33fn create_parts_array<S: Get<u32>>(num: u32) -> BoundedVec<RmrkPartType, S> {
34 vec![create_max_part(); num as usize]34 let mut parts: BoundedVec<RmrkPartType, S> = vec![].try_into().expect("0 <= S");
35 .try_into()35
36 .expect("num <= S")36 for i in 0..num {
37 parts.try_push(create_max_part(i)).expect("num <= S");
38 }
39
40 parts
37}41}
3842
39fn create_max_theme_property() -> RmrkThemeProperty {43fn create_max_theme_property() -> RmrkThemeProperty {
91 let theme = create_max_theme(create_data(), b);95 let theme = create_max_theme(create_data(), b);
92 }: _(RawOrigin::Signed(caller), base_id, theme)96 }: _(RawOrigin::Signed(caller), base_id, theme)
97
98 equippable {
99 let caller = account("caller", 0, SEED);
100 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());
101
102 let base_id = 1;
103 let slot_id = 42;
104
105 let base_type = create_data();
106 let symbol = create_data();
107 let parts = vec! {
108 RmrkPartType::SlotPart(RmrkSlotPart {
109 id: slot_id,
110 equippable: RmrkEquippableList::All,
111 src: create_data(),
112 z: 1,
113 })
114 }.try_into().expect("1 <= RmrkPartsLimit");
115
116 <Pallet<T>>::create_base(RawOrigin::Signed(caller.clone()).into(), base_type, symbol, parts)?;
117
118 let equippables = RmrkEquippableList::Custom(create_u32_array());
119 }: _(RawOrigin::Signed(caller), base_id, slot_id, equippables)
93}120}
94121
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
--- a/pallets/proxy-rmrk-equip/src/weights.rs
+++ b/pallets/proxy-rmrk-equip/src/weights.rs
@@ -47,18 +47,18 @@
 	// Storage: Common CollectionPropertyPermissions (r:0 w:1)
 	// Storage: Common CollectionProperties (r:0 w:1)
 	// Storage: Common CollectionById (r:0 w:1)
+	// Storage: RmrkEquip InernalPartId (r:1 w:1)
 	// Storage: Nonfungible TokensMinted (r:1 w:1)
 	// Storage: Nonfungible AccountBalance (r:1 w:1)
 	// Storage: Nonfungible TokenProperties (r:1 w:1)
 	// Storage: Nonfungible TokenData (r:0 w:1)
 	// Storage: Nonfungible Owned (r:0 w:1)
-	// Storage: RmrkEquip InernalPartId (r:0 w:1)
 	fn create_base(b: u32, ) -> Weight {
-		(44_632_000 as Weight)
-			// Standard Error: 10_000
-			.saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))
+		(44_927_000 as Weight)
+			// Standard Error: 9_000
+			.saturating_add((18_896_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(6 as Weight))
-			.saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+			.saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(T::DbWeight::get().writes(8 as Weight))
 			.saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
 	}
@@ -71,9 +71,9 @@
 	// Storage: Nonfungible TokenData (r:0 w:1)
 	// Storage: Nonfungible Owned (r:0 w:1)
 	fn theme_add(b: u32, ) -> Weight {
-		(39_525_000 as Weight)
-			// Standard Error: 12_000
-			.saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))
+		(39_120_000 as Weight)
+			// Standard Error: 11_000
+			.saturating_add((2_367_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(6 as Weight))
 			.saturating_add(T::DbWeight::get().writes(5 as Weight))
 	}
@@ -82,7 +82,7 @@
 	// Storage: RmrkEquip InernalPartId (r:1 w:0)
 	// Storage: Nonfungible TokenProperties (r:1 w:1)
 	fn equippable() -> Weight {
-		(27_371_000 as Weight)
+		(27_172_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
@@ -96,18 +96,18 @@
 	// Storage: Common CollectionPropertyPermissions (r:0 w:1)
 	// Storage: Common CollectionProperties (r:0 w:1)
 	// Storage: Common CollectionById (r:0 w:1)
+	// Storage: RmrkEquip InernalPartId (r:1 w:1)
 	// Storage: Nonfungible TokensMinted (r:1 w:1)
 	// Storage: Nonfungible AccountBalance (r:1 w:1)
 	// Storage: Nonfungible TokenProperties (r:1 w:1)
 	// Storage: Nonfungible TokenData (r:0 w:1)
 	// Storage: Nonfungible Owned (r:0 w:1)
-	// Storage: RmrkEquip InernalPartId (r:0 w:1)
 	fn create_base(b: u32, ) -> Weight {
-		(44_632_000 as Weight)
-			// Standard Error: 10_000
-			.saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))
+		(44_927_000 as Weight)
+			// Standard Error: 9_000
+			.saturating_add((18_896_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(6 as Weight))
-			.saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+			.saturating_add(RocksDbWeight::get().reads((2 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(RocksDbWeight::get().writes(8 as Weight))
 			.saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
 	}
@@ -120,9 +120,9 @@
 	// Storage: Nonfungible TokenData (r:0 w:1)
 	// Storage: Nonfungible Owned (r:0 w:1)
 	fn theme_add(b: u32, ) -> Weight {
-		(39_525_000 as Weight)
-			// Standard Error: 12_000
-			.saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))
+		(39_120_000 as Weight)
+			// Standard Error: 11_000
+			.saturating_add((2_367_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(6 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
 	}
@@ -131,7 +131,7 @@
 	// Storage: RmrkEquip InernalPartId (r:1 w:0)
 	// Storage: Nonfungible TokenProperties (r:1 w:1)
 	fn equippable() -> Weight {
-		(27_371_000 as Weight)
+		(27_172_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}