git.delta.rocks / unique-network / refs/commits / cc9cedd6dbfe

difftreelog

Add extrinsic: delete collection property

Daniel Shiposha2022-05-04parent: #aa3d2ae.patch.diff
in: master

10 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -292,6 +292,8 @@
 
 		CollectionPropertySet(CollectionId, Property),
 
+		CollectionPropertyDeleted(CollectionId, PropertyKey),
+
 		TokenPropertySet(CollectionId, TokenId, Property),
 
 		TokenPropertyDeleted(CollectionId, TokenId, PropertyKey),
@@ -761,6 +763,34 @@
 		Ok(())
 	}
 
+	pub fn delete_collection_property(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		property_key: PropertyKey,
+	) -> DispatchResult {
+		collection.check_is_owner_or_admin(sender)?;
+
+		CollectionProperties::<T>::mutate(collection.id, |properties| {
+			properties.remove_property(&property_key);
+		});
+
+		Self::deposit_event(Event::CollectionPropertyDeleted(collection.id, property_key));
+
+		Ok(())
+	}
+
+	pub fn delete_collection_properties(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResult {
+		for key in property_keys {
+			Self::delete_collection_property(collection, sender, key)?;
+		}
+
+		Ok(())
+	}
+
 	pub fn set_property_permission(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
@@ -956,6 +986,7 @@
 	fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
+	fn delete_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
 	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
@@ -998,6 +1029,11 @@
 		sender: T::CrossAccountId,
 		properties: Vec<Property>,
 	) -> DispatchResultWithPostInfo;
+	fn delete_collection_properties(
+		&self,
+		sender: &T::CrossAccountId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo;
 	fn set_token_properties(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
before · pallets/fungible/src/common.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{CustomDataLimit, Property, PropertyKey, PropertyKeyPermission};2526use crate::{27	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,28};2930pub struct CommonWeights<T: Config>(PhantomData<T>);31impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {32	fn create_item() -> Weight {33		<SelfWeightOf<T>>::create_item()34	}3536	fn create_multiple_items(_amount: u32) -> Weight {37		Self::create_item()38	}3940	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41		match data {42			CreateItemExData::Fungible(f) => {43				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44			}45			_ => 0,46		}47	}4849	fn burn_item() -> Weight {50		<SelfWeightOf<T>>::burn_item()51	}5253	fn set_collection_properties(amount: u32) -> Weight {54		<SelfWeightOf<T>>::set_collection_properties(amount)55	}5657	fn set_token_properties(amount: u32) -> Weight {58		<SelfWeightOf<T>>::set_token_properties(amount)59	}6061	fn delete_token_properties(amount: u32) -> Weight {62		<SelfWeightOf<T>>::delete_token_properties(amount)63	}6465	fn set_property_permissions(amount: u32) -> Weight {66		<SelfWeightOf<T>>::set_property_permissions(amount)67	}6869	fn transfer() -> Weight {70		<SelfWeightOf<T>>::transfer()71	}7273	fn approve() -> Weight {74		<SelfWeightOf<T>>::approve()75	}7677	fn transfer_from() -> Weight {78		<SelfWeightOf<T>>::transfer_from()79	}8081	fn burn_from() -> Weight {82		<SelfWeightOf<T>>::burn_from()83	}8485	fn set_variable_metadata(_bytes: u32) -> Weight {86		// Error87		088	}89}9091impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {92	fn create_item(93		&self,94		sender: T::CrossAccountId,95		to: T::CrossAccountId,96		data: up_data_structs::CreateItemData,97		nesting_budget: &dyn Budget,98	) -> DispatchResultWithPostInfo {99		match data {100			up_data_structs::CreateItemData::Fungible(data) => with_weight(101				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),102				<CommonWeights<T>>::create_item(),103			),104			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),105		}106	}107108	fn create_multiple_items(109		&self,110		sender: T::CrossAccountId,111		to: T::CrossAccountId,112		data: Vec<up_data_structs::CreateItemData>,113		nesting_budget: &dyn Budget,114	) -> DispatchResultWithPostInfo {115		let mut sum: u128 = 0;116		for data in data {117			match data {118				up_data_structs::CreateItemData::Fungible(data) => {119					sum = sum120						.checked_add(data.value)121						.ok_or(ArithmeticError::Overflow)?;122				}123				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),124			}125		}126127		with_weight(128			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),129			<CommonWeights<T>>::create_item(),130		)131	}132133	fn create_multiple_items_ex(134		&self,135		sender: <T>::CrossAccountId,136		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,137		nesting_budget: &dyn Budget,138	) -> DispatchResultWithPostInfo {139		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);140		let data = match data {141			up_data_structs::CreateItemExData::Fungible(f) => f,142			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),143		};144145		with_weight(146			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),147			weight,148		)149	}150151	fn burn_item(152		&self,153		sender: T::CrossAccountId,154		token: TokenId,155		amount: u128,156	) -> DispatchResultWithPostInfo {157		ensure!(158			token == TokenId::default(),159			<Error<T>>::FungibleItemsHaveNoId160		);161162		with_weight(163			<Pallet<T>>::burn(self, &sender, amount),164			<CommonWeights<T>>::burn_item(),165		)166	}167168	fn transfer(169		&self,170		from: T::CrossAccountId,171		to: T::CrossAccountId,172		token: TokenId,173		amount: u128,174		nesting_budget: &dyn Budget,175	) -> DispatchResultWithPostInfo {176		ensure!(177			token == TokenId::default(),178			<Error<T>>::FungibleItemsHaveNoId179		);180181		with_weight(182			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),183			<CommonWeights<T>>::transfer(),184		)185	}186187	fn approve(188		&self,189		sender: T::CrossAccountId,190		spender: T::CrossAccountId,191		token: TokenId,192		amount: u128,193	) -> DispatchResultWithPostInfo {194		ensure!(195			token == TokenId::default(),196			<Error<T>>::FungibleItemsHaveNoId197		);198199		with_weight(200			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),201			<CommonWeights<T>>::approve(),202		)203	}204205	fn transfer_from(206		&self,207		sender: T::CrossAccountId,208		from: T::CrossAccountId,209		to: T::CrossAccountId,210		token: TokenId,211		amount: u128,212		nesting_budget: &dyn Budget,213	) -> DispatchResultWithPostInfo {214		ensure!(215			token == TokenId::default(),216			<Error<T>>::FungibleItemsHaveNoId217		);218219		with_weight(220			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),221			<CommonWeights<T>>::transfer_from(),222		)223	}224225	fn burn_from(226		&self,227		sender: T::CrossAccountId,228		from: T::CrossAccountId,229		token: TokenId,230		amount: u128,231		nesting_budget: &dyn Budget,232	) -> DispatchResultWithPostInfo {233		ensure!(234			token == TokenId::default(),235			<Error<T>>::FungibleItemsHaveNoId236		);237238		with_weight(239			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),240			<CommonWeights<T>>::burn_from(),241		)242	}243244	fn set_collection_properties(245		&self,246		_sender: T::CrossAccountId,247		_property: Vec<Property>,248	) -> DispatchResultWithPostInfo {249		fail!(<Error<T>>::PropertiesNotAllowed)250	}251252	fn set_token_properties(253		&self,254		_sender: T::CrossAccountId,255		_token_id: TokenId,256		_property: Vec<Property>,257	) -> DispatchResultWithPostInfo {258		fail!(<Error<T>>::PropertiesNotAllowed)259	}260261	fn set_property_permissions(262		&self,263		_sender: &T::CrossAccountId,264		_property_permissions: Vec<PropertyKeyPermission>,265	) -> DispatchResultWithPostInfo {266		fail!(<Error<T>>::PropertiesNotAllowed)267	}268269	fn delete_token_properties(270		&self,271		_sender: T::CrossAccountId,272		_token_id: TokenId,273		_property_keys: Vec<PropertyKey>,274	) -> DispatchResultWithPostInfo {275		fail!(<Error<T>>::PropertiesNotAllowed)276	}277278	fn set_variable_metadata(279		&self,280		_sender: T::CrossAccountId,281		_token: TokenId,282		_data: BoundedVec<u8, CustomDataLimit>,283	) -> DispatchResultWithPostInfo {284		fail!(<Error<T>>::FungibleItemsDontHaveData)285	}286287	fn check_nesting(288		&self,289		_sender: <T>::CrossAccountId,290		_from: (CollectionId, TokenId),291		_under: TokenId,292		_budget: &dyn Budget,293	) -> sp_runtime::DispatchResult {294		fail!(<Error<T>>::FungibleDisallowsNesting)295	}296297	fn collection_tokens(&self) -> Vec<TokenId> {298		vec![TokenId::default()]299	}300301	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {302		if <Balance<T>>::get((self.id, account)) != 0 {303			vec![TokenId::default()]304		} else {305			vec![]306		}307	}308309	fn token_exists(&self, token: TokenId) -> bool {310		token == TokenId::default()311	}312313	fn last_token_id(&self) -> TokenId {314		TokenId::default()315	}316317	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {318		None319	}320	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {321		Vec::new()322	}323	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {324		Vec::new()325	}326327	fn total_supply(&self) -> u32 {328		1329	}330331	fn account_balance(&self, account: T::CrossAccountId) -> u32 {332		if <Balance<T>>::get((self.id, account)) != 0 {333			1334		} else {335			0336		}337	}338339	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {340		if token != TokenId::default() {341			return 0;342		}343		<Balance<T>>::get((self.id, account))344	}345346	fn allowance(347		&self,348		sender: T::CrossAccountId,349		spender: T::CrossAccountId,350		token: TokenId,351	) -> u128 {352		if token != TokenId::default() {353			return 0;354		}355		<Allowance<T>>::get((self.id, sender, spender))356	}357}
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -36,6 +36,7 @@
 	fn create_multiple_items_ex(b: u32, ) -> Weight;
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
+	fn delete_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
 	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
@@ -79,6 +80,11 @@
 		0
 	}
 
+	fn delete_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
@@ -157,6 +163,11 @@
 		0
 	}
 
+	fn delete_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -55,6 +55,10 @@
 		<SelfWeightOf<T>>::set_collection_properties(amount)
 	}
 
+	fn delete_collection_properties(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::delete_collection_properties(amount)
+	}
+
 	fn set_token_properties(amount: u32) -> Weight {
 		<SelfWeightOf<T>>::set_token_properties(amount)
 	}
@@ -171,6 +175,19 @@
 		)
 	}
 
+	fn delete_collection_properties(
+		&self,
+		sender: &T::CrossAccountId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo {
+		let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);
+
+		with_weight(
+			<Pallet<T>>::delete_collection_properties(self, &sender, property_keys),
+			weight
+		)
+	}
+
 	fn set_token_properties(
 		&self,
 		sender: T::CrossAccountId,
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -366,6 +366,14 @@
 		<PalletCommon<T>>::set_collection_properties(collection, sender, properties)
 	}
 
+	pub fn delete_collection_properties(
+		collection: &CollectionHandle<T>,
+		sender: &T::CrossAccountId,
+		property_keys: Vec<PropertyKey>,
+	) -> DispatchResult {
+		<PalletCommon<T>>::delete_collection_properties(collection, sender, property_keys)
+	}
+
 	pub fn set_property_permissions(
 		collection: &CollectionHandle<T>,
 		sender: &T::CrossAccountId,
modifiedpallets/nonfungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -37,6 +37,7 @@
 	fn create_multiple_items_ex(b: u32, ) -> Weight;
 	fn burn_item() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
+	fn delete_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
 	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
@@ -100,6 +101,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn delete_collection_properties(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	fn set_token_properties(amount: u32) -> Weight {
 		// TODO calculate appropriate weight
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
@@ -210,6 +216,11 @@
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
 	}
 
+	fn delete_collection_properties(amount: u32) -> Weight {
+		// TODO calculate appropriate weight
+		(50_000_000 as Weight).saturating_mul(amount as Weight)
+	}
+
 	fn set_token_properties(amount: u32) -> Weight {
 		// TODO calculate appropriate weight
 		(50_000_000 as Weight).saturating_mul(amount as Weight)
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -70,6 +70,10 @@
 		<SelfWeightOf<T>>::set_collection_properties(amount)
 	}
 
+	fn delete_collection_properties(amount: u32) -> Weight {
+		<SelfWeightOf<T>>::delete_collection_properties(amount)
+	}
+
 	fn set_token_properties(amount: u32) -> Weight {
 		<SelfWeightOf<T>>::set_token_properties(amount)
 	}
@@ -268,6 +272,14 @@
 		fail!(<Error<T>>::PropertiesNotAllowed)
 	}
 
+	fn delete_collection_properties(
+		&self,
+		_sender: &T::CrossAccountId,
+		_property_keys: Vec<PropertyKey>,
+	) -> DispatchResultWithPostInfo {
+		fail!(<Error<T>>::PropertiesNotAllowed)
+	}
+
 	fn set_token_properties(
 		&self,
 		_sender: T::CrossAccountId,
modifiedpallets/refungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -39,6 +39,7 @@
 	fn burn_item_partial() -> Weight;
 	fn burn_item_fully() -> Weight;
 	fn set_collection_properties(amount: u32) -> Weight;
+	fn delete_collection_properties(amount: u32) -> Weight;
 	fn set_token_properties(amount: u32) -> Weight;
 	fn delete_token_properties(amount: u32) -> Weight;
 	fn set_property_permissions(amount: u32) -> Weight;
@@ -139,6 +140,11 @@
 		0
 	}
 
+	fn delete_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
@@ -328,6 +334,11 @@
 		0
 	}
 
+	fn delete_collection_properties(_amount: u32) -> Weight {
+		// Error
+		0
+	}
+
 	fn set_token_properties(_amount: u32) -> Weight {
 		// Error
 		0
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -708,6 +708,20 @@
 			dispatch_call::<T, _>(collection_id, |d| d.set_collection_properties(sender, properties))
 		}
 
+		#[weight = T::CommonWeightInfo::delete_collection_properties(property_keys.len() as u32)]
+		#[transactional]
+		pub fn delete_collection_properties(
+			origin,
+			collection_id: CollectionId,
+			property_keys: Vec<PropertyKey>,
+		) -> DispatchResultWithPostInfo {
+			ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
+
+			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+			dispatch_call::<T, _>(collection_id, |d| d.delete_collection_properties(&sender, property_keys))
+		}
+
 		#[weight = T::CommonWeightInfo::set_token_properties(properties.len() as u32)]
 		#[transactional]
 		pub fn set_token_properties(
@@ -723,19 +737,19 @@
 			dispatch_call::<T, _>(collection_id, |d| d.set_token_properties(sender, token_id, properties))
 		}
 
-		#[weight = T::CommonWeightInfo::delete_token_properties(properties.len() as u32)]
+		#[weight = T::CommonWeightInfo::delete_token_properties(property_keys.len() as u32)]
 		#[transactional]
 		pub fn delete_token_properties(
 			origin,
 			collection_id: CollectionId,
 			token_id: TokenId,
-			properties: Vec<PropertyKey>
+			property_keys: Vec<PropertyKey>
 		) -> DispatchResultWithPostInfo {
-			ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
+			ensure!(!property_keys.is_empty(), Error::<T>::EmptyArgument);
 
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 
-			dispatch_call::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, properties))
+			dispatch_call::<T, _>(collection_id, |d| d.delete_token_properties(sender, token_id, property_keys))
 		}
 
 		#[weight = T::CommonWeightInfo::set_property_permissions(property_permissions.len() as u32)]
modifiedruntime/common/src/weights.rsdiffbeforeafterboth
--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -58,6 +58,10 @@
 		dispatch_weight::<T>() + max_weight_of!(set_collection_properties(amount))
 	}
 
+	fn delete_collection_properties(amount: u32) -> Weight {
+		dispatch_weight::<T>() + max_weight_of!(delete_collection_properties(amount))
+	}
+
 	fn set_token_properties(amount: u32) -> Weight {
 		dispatch_weight::<T>() + max_weight_of!(set_token_properties(amount))
 	}