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
47 // Storage: Common CollectionPropertyPermissions (r:0 w:1)47 // Storage: Common CollectionPropertyPermissions (r:0 w:1)
48 // Storage: Common CollectionProperties (r:0 w:1)48 // Storage: Common CollectionProperties (r:0 w:1)
49 // Storage: Common CollectionById (r:0 w:1)49 // Storage: Common CollectionById (r:0 w:1)
50 // Storage: RmrkEquip InernalPartId (r:1 w:1)
50 // Storage: Nonfungible TokensMinted (r:1 w:1)51 // Storage: Nonfungible TokensMinted (r:1 w:1)
51 // Storage: Nonfungible AccountBalance (r:1 w:1)52 // Storage: Nonfungible AccountBalance (r:1 w:1)
52 // Storage: Nonfungible TokenProperties (r:1 w:1)53 // Storage: Nonfungible TokenProperties (r:1 w:1)
53 // Storage: Nonfungible TokenData (r:0 w:1)54 // Storage: Nonfungible TokenData (r:0 w:1)
54 // Storage: Nonfungible Owned (r:0 w:1)55 // Storage: Nonfungible Owned (r:0 w:1)
55 // Storage: RmrkEquip InernalPartId (r:0 w:1)
56 fn create_base(b: u32, ) -> Weight {56 fn create_base(b: u32, ) -> Weight {
57 (44_632_000 as Weight)57 (44_927_000 as Weight)
58 // Standard Error: 10_00058 // Standard Error: 9_000
59 .saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))59 .saturating_add((18_896_000 as Weight).saturating_mul(b as Weight))
60 .saturating_add(T::DbWeight::get().reads(6 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)))61 .saturating_add(T::DbWeight::get().reads((2 as Weight).saturating_mul(b as Weight)))
62 .saturating_add(T::DbWeight::get().writes(8 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)))63 .saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
64 }64 }
71 // Storage: Nonfungible TokenData (r:0 w:1)71 // Storage: Nonfungible TokenData (r:0 w:1)
72 // Storage: Nonfungible Owned (r:0 w:1)72 // Storage: Nonfungible Owned (r:0 w:1)
73 fn theme_add(b: u32, ) -> Weight {73 fn theme_add(b: u32, ) -> Weight {
74 (39_525_000 as Weight)74 (39_120_000 as Weight)
75 // Standard Error: 12_00075 // Standard Error: 11_000
76 .saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))76 .saturating_add((2_367_000 as Weight).saturating_mul(b as Weight))
77 .saturating_add(T::DbWeight::get().reads(6 as Weight))77 .saturating_add(T::DbWeight::get().reads(6 as Weight))
78 .saturating_add(T::DbWeight::get().writes(5 as Weight))78 .saturating_add(T::DbWeight::get().writes(5 as Weight))
79 }79 }
82 // Storage: RmrkEquip InernalPartId (r:1 w:0)82 // Storage: RmrkEquip InernalPartId (r:1 w:0)
83 // Storage: Nonfungible TokenProperties (r:1 w:1)83 // Storage: Nonfungible TokenProperties (r:1 w:1)
84 fn equippable() -> Weight {84 fn equippable() -> Weight {
85 (27_371_000 as Weight)85 (27_172_000 as Weight)
86 .saturating_add(T::DbWeight::get().reads(4 as Weight))86 .saturating_add(T::DbWeight::get().reads(4 as Weight))
87 .saturating_add(T::DbWeight::get().writes(1 as Weight))87 .saturating_add(T::DbWeight::get().writes(1 as Weight))
88 }88 }
96 // Storage: Common CollectionPropertyPermissions (r:0 w:1)96 // Storage: Common CollectionPropertyPermissions (r:0 w:1)
97 // Storage: Common CollectionProperties (r:0 w:1)97 // Storage: Common CollectionProperties (r:0 w:1)
98 // Storage: Common CollectionById (r:0 w:1)98 // Storage: Common CollectionById (r:0 w:1)
99 // Storage: RmrkEquip InernalPartId (r:1 w:1)
99 // Storage: Nonfungible TokensMinted (r:1 w:1)100 // Storage: Nonfungible TokensMinted (r:1 w:1)
100 // Storage: Nonfungible AccountBalance (r:1 w:1)101 // Storage: Nonfungible AccountBalance (r:1 w:1)
101 // Storage: Nonfungible TokenProperties (r:1 w:1)102 // Storage: Nonfungible TokenProperties (r:1 w:1)
102 // Storage: Nonfungible TokenData (r:0 w:1)103 // Storage: Nonfungible TokenData (r:0 w:1)
103 // Storage: Nonfungible Owned (r:0 w:1)104 // Storage: Nonfungible Owned (r:0 w:1)
104 // Storage: RmrkEquip InernalPartId (r:0 w:1)
105 fn create_base(b: u32, ) -> Weight {105 fn create_base(b: u32, ) -> Weight {
106 (44_632_000 as Weight)106 (44_927_000 as Weight)
107 // Standard Error: 10_000107 // Standard Error: 9_000
108 .saturating_add((16_912_000 as Weight).saturating_mul(b as Weight))108 .saturating_add((18_896_000 as Weight).saturating_mul(b as Weight))
109 .saturating_add(RocksDbWeight::get().reads(6 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)))110 .saturating_add(RocksDbWeight::get().reads((2 as Weight).saturating_mul(b as Weight)))
111 .saturating_add(RocksDbWeight::get().writes(8 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)))112 .saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
113 }113 }
120 // Storage: Nonfungible TokenData (r:0 w:1)120 // Storage: Nonfungible TokenData (r:0 w:1)
121 // Storage: Nonfungible Owned (r:0 w:1)121 // Storage: Nonfungible Owned (r:0 w:1)
122 fn theme_add(b: u32, ) -> Weight {122 fn theme_add(b: u32, ) -> Weight {
123 (39_525_000 as Weight)123 (39_120_000 as Weight)
124 // Standard Error: 12_000124 // Standard Error: 11_000
125 .saturating_add((2_494_000 as Weight).saturating_mul(b as Weight))125 .saturating_add((2_367_000 as Weight).saturating_mul(b as Weight))
126 .saturating_add(RocksDbWeight::get().reads(6 as Weight))126 .saturating_add(RocksDbWeight::get().reads(6 as Weight))
127 .saturating_add(RocksDbWeight::get().writes(5 as Weight))127 .saturating_add(RocksDbWeight::get().writes(5 as Weight))
128 }128 }
131 // Storage: RmrkEquip InernalPartId (r:1 w:0)131 // Storage: RmrkEquip InernalPartId (r:1 w:0)
132 // Storage: Nonfungible TokenProperties (r:1 w:1)132 // Storage: Nonfungible TokenProperties (r:1 w:1)
133 fn equippable() -> Weight {133 fn equippable() -> Weight {
134 (27_371_000 as Weight)134 (27_172_000 as Weight)
135 .saturating_add(RocksDbWeight::get().reads(4 as Weight))135 .saturating_add(RocksDbWeight::get().reads(4 as Weight))
136 .saturating_add(RocksDbWeight::get().writes(1 as Weight))136 .saturating_add(RocksDbWeight::get().writes(1 as Weight))
137 }137 }