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

difftreelog

source

pallets/nonfungible/src/common.rs10.6 KiBsourcehistory
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::{21	TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget, Property,22	PropertyKey, PropertyKeyPermission,23};24use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};25use sp_runtime::DispatchError;26use sp_std::vec::Vec;2728use crate::{29	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,30	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,31};3233pub struct CommonWeights<T: Config>(PhantomData<T>);34impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {35	fn create_item() -> Weight {36		<SelfWeightOf<T>>::create_item()37	}3839	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {40		match data {41			CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),42			_ => 0,43		}44	}4546	fn create_multiple_items(amount: u32) -> Weight {47		<SelfWeightOf<T>>::create_multiple_items(amount)48	}4950	fn burn_item() -> Weight {51		<SelfWeightOf<T>>::burn_item()52	}5354	fn set_collection_properties(amount: u32) -> Weight {55		<SelfWeightOf<T>>::set_collection_properties(amount)56	}5758	fn delete_collection_properties(amount: u32) -> Weight {59		<SelfWeightOf<T>>::delete_collection_properties(amount)60	}6162	fn set_token_properties(amount: u32) -> Weight {63		<SelfWeightOf<T>>::set_token_properties(amount)64	}6566	fn delete_token_properties(amount: u32) -> Weight {67		<SelfWeightOf<T>>::delete_token_properties(amount)68	}6970	fn set_property_permissions(amount: u32) -> Weight {71		<SelfWeightOf<T>>::set_property_permissions(amount)72	}7374	fn transfer() -> Weight {75		<SelfWeightOf<T>>::transfer()76	}7778	fn approve() -> Weight {79		<SelfWeightOf<T>>::approve()80	}8182	fn transfer_from() -> Weight {83		<SelfWeightOf<T>>::transfer_from()84	}8586	fn burn_from() -> Weight {87		<SelfWeightOf<T>>::burn_from()88	}8990	fn set_variable_metadata(bytes: u32) -> Weight {91		<SelfWeightOf<T>>::set_variable_metadata(bytes)92	}93}9495fn map_create_data<T: Config>(96	data: up_data_structs::CreateItemData,97	to: &T::CrossAccountId,98) -> Result<CreateItemData<T>, DispatchError> {99	match data {100		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {101			const_data: data.const_data,102			variable_data: data.variable_data,103			owner: to.clone(),104		}),105		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),106	}107}108109impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {110	fn create_item(111		&self,112		sender: T::CrossAccountId,113		to: T::CrossAccountId,114		data: up_data_structs::CreateItemData,115		nesting_budget: &dyn Budget,116	) -> DispatchResultWithPostInfo {117		with_weight(118			<Pallet<T>>::create_item(119				self,120				&sender,121				map_create_data::<T>(data, &to)?,122				nesting_budget,123			),124			<CommonWeights<T>>::create_item(),125		)126	}127128	fn create_multiple_items(129		&self,130		sender: T::CrossAccountId,131		to: T::CrossAccountId,132		data: Vec<up_data_structs::CreateItemData>,133		nesting_budget: &dyn Budget,134	) -> DispatchResultWithPostInfo {135		let data = data136			.into_iter()137			.map(|d| map_create_data::<T>(d, &to))138			.collect::<Result<Vec<_>, DispatchError>>()?;139140		let amount = data.len();141		with_weight(142			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),143			<CommonWeights<T>>::create_multiple_items(amount as u32),144		)145	}146147	fn create_multiple_items_ex(148		&self,149		sender: <T>::CrossAccountId,150		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,151		nesting_budget: &dyn Budget,152	) -> DispatchResultWithPostInfo {153		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);154		let data = match data {155			up_data_structs::CreateItemExData::NFT(nft) => nft,156			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),157		};158159		with_weight(160			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),161			weight,162		)163	}164165	fn set_collection_properties(166		&self,167		sender: T::CrossAccountId,168		properties: Vec<Property>,169	) -> DispatchResultWithPostInfo {170		let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);171172		with_weight(173			<Pallet<T>>::set_collection_properties(self, &sender, properties),174			weight,175		)176	}177178	fn delete_collection_properties(179		&self,180		sender: &T::CrossAccountId,181		property_keys: Vec<PropertyKey>,182	) -> DispatchResultWithPostInfo {183		let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);184185		with_weight(186			<Pallet<T>>::delete_collection_properties(self, &sender, property_keys),187			weight,188		)189	}190191	fn set_token_properties(192		&self,193		sender: T::CrossAccountId,194		token_id: TokenId,195		properties: Vec<Property>,196	) -> DispatchResultWithPostInfo {197		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);198199		with_weight(200			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties),201			weight,202		)203	}204205	fn delete_token_properties(206		&self,207		sender: T::CrossAccountId,208		token_id: TokenId,209		property_keys: Vec<PropertyKey>,210	) -> DispatchResultWithPostInfo {211		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);212213		with_weight(214			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),215			weight,216		)217	}218219	fn set_property_permissions(220		&self,221		sender: &T::CrossAccountId,222		property_permissions: Vec<PropertyKeyPermission>,223	) -> DispatchResultWithPostInfo {224		let weight =225			<CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);226227		with_weight(228			<Pallet<T>>::set_property_permissions(self, sender, property_permissions),229			weight,230		)231	}232233	fn burn_item(234		&self,235		sender: T::CrossAccountId,236		token: TokenId,237		amount: u128,238	) -> DispatchResultWithPostInfo {239		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);240		if amount == 1 {241			with_weight(242				<Pallet<T>>::burn(self, &sender, token),243				<CommonWeights<T>>::burn_item(),244			)245		} else {246			Ok(().into())247		}248	}249250	fn transfer(251		&self,252		from: T::CrossAccountId,253		to: T::CrossAccountId,254		token: TokenId,255		amount: u128,256		nesting_budget: &dyn Budget,257	) -> DispatchResultWithPostInfo {258		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);259		if amount == 1 {260			with_weight(261				<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),262				<CommonWeights<T>>::transfer(),263			)264		} else {265			Ok(().into())266		}267	}268269	fn approve(270		&self,271		sender: T::CrossAccountId,272		spender: T::CrossAccountId,273		token: TokenId,274		amount: u128,275	) -> DispatchResultWithPostInfo {276		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);277278		with_weight(279			if amount == 1 {280				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))281			} else {282				<Pallet<T>>::set_allowance(self, &sender, token, None)283			},284			<CommonWeights<T>>::approve(),285		)286	}287288	fn transfer_from(289		&self,290		sender: T::CrossAccountId,291		from: T::CrossAccountId,292		to: T::CrossAccountId,293		token: TokenId,294		amount: u128,295		nesting_budget: &dyn Budget,296	) -> DispatchResultWithPostInfo {297		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);298299		if amount == 1 {300			with_weight(301				<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),302				<CommonWeights<T>>::transfer_from(),303			)304		} else {305			Ok(().into())306		}307	}308309	fn burn_from(310		&self,311		sender: T::CrossAccountId,312		from: T::CrossAccountId,313		token: TokenId,314		amount: u128,315		nesting_budget: &dyn Budget,316	) -> DispatchResultWithPostInfo {317		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);318319		if amount == 1 {320			with_weight(321				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),322				<CommonWeights<T>>::burn_from(),323			)324		} else {325			Ok(().into())326		}327	}328329	fn set_variable_metadata(330		&self,331		sender: T::CrossAccountId,332		token: TokenId,333		data: BoundedVec<u8, CustomDataLimit>,334	) -> DispatchResultWithPostInfo {335		let len = data.len();336		with_weight(337			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),338			<CommonWeights<T>>::set_variable_metadata(len as u32),339		)340	}341342	fn check_nesting(343		&self,344		sender: T::CrossAccountId,345		from: (CollectionId, TokenId),346		under: TokenId,347		budget: &dyn Budget,348	) -> sp_runtime::DispatchResult {349		<Pallet<T>>::check_nesting(self, sender, from, under, budget)350	}351352	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {353		<Owned<T>>::iter_prefix((self.id, account))354			.map(|(id, _)| id)355			.collect()356	}357358	fn collection_tokens(&self) -> Vec<TokenId> {359		<TokenData<T>>::iter_prefix((self.id,))360			.map(|(id, _)| id)361			.collect()362	}363364	fn token_exists(&self, token: TokenId) -> bool {365		<Pallet<T>>::token_exists(self, token)366	}367368	fn last_token_id(&self) -> TokenId {369		TokenId(<TokensMinted<T>>::get(self.id))370	}371372	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {373		<TokenData<T>>::get((self.id, token)).map(|t| t.owner)374	}375	fn const_metadata(&self, token: TokenId) -> Vec<u8> {376		<TokenData<T>>::get((self.id, token))377			.map(|t| t.const_data)378			.unwrap_or_default()379			.into_inner()380	}381	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {382		<TokenData<T>>::get((self.id, token))383			.map(|t| t.variable_data)384			.unwrap_or_default()385			.into_inner()386	}387388	fn total_supply(&self) -> u32 {389		<Pallet<T>>::total_supply(self)390	}391392	fn account_balance(&self, account: T::CrossAccountId) -> u32 {393		<AccountBalance<T>>::get((self.id, account))394	}395396	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {397		if <TokenData<T>>::get((self.id, token))398			.map(|a| a.owner == account)399			.unwrap_or(false)400		{401			1402		} else {403			0404		}405	}406407	fn allowance(408		&self,409		sender: T::CrossAccountId,410		spender: T::CrossAccountId,411		token: TokenId,412	) -> u128 {413		if <TokenData<T>>::get((self.id, token))414			.map(|a| a.owner != sender)415			.unwrap_or(true)416		{417			0418		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {419			1420		} else {421			0422		}423	}424}