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

difftreelog

source

pallets/nonfungible/src/common.rs10.1 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 set_token_properties(amount: u32) -> Weight {59		<SelfWeightOf<T>>::set_token_properties(amount)60	}6162	fn delete_token_properties(amount: u32) -> Weight {63		<SelfWeightOf<T>>::delete_token_properties(amount)64	}6566	fn set_property_permissions(amount: u32) -> Weight {67		<SelfWeightOf<T>>::set_property_permissions(amount)68	}6970	fn transfer() -> Weight {71		<SelfWeightOf<T>>::transfer()72	}7374	fn approve() -> Weight {75		<SelfWeightOf<T>>::approve()76	}7778	fn transfer_from() -> Weight {79		<SelfWeightOf<T>>::transfer_from()80	}8182	fn burn_from() -> Weight {83		<SelfWeightOf<T>>::burn_from()84	}8586	fn set_variable_metadata(bytes: u32) -> Weight {87		<SelfWeightOf<T>>::set_variable_metadata(bytes)88	}89}9091fn map_create_data<T: Config>(92	data: up_data_structs::CreateItemData,93	to: &T::CrossAccountId,94) -> Result<CreateItemData<T>, DispatchError> {95	match data {96		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {97			const_data: data.const_data,98			variable_data: data.variable_data,99			owner: to.clone(),100		}),101		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),102	}103}104105impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {106	fn create_item(107		&self,108		sender: T::CrossAccountId,109		to: T::CrossAccountId,110		data: up_data_structs::CreateItemData,111		nesting_budget: &dyn Budget,112	) -> DispatchResultWithPostInfo {113		with_weight(114			<Pallet<T>>::create_item(115				self,116				&sender,117				map_create_data::<T>(data, &to)?,118				nesting_budget,119			),120			<CommonWeights<T>>::create_item(),121		)122	}123124	fn create_multiple_items(125		&self,126		sender: T::CrossAccountId,127		to: T::CrossAccountId,128		data: Vec<up_data_structs::CreateItemData>,129		nesting_budget: &dyn Budget,130	) -> DispatchResultWithPostInfo {131		let data = data132			.into_iter()133			.map(|d| map_create_data::<T>(d, &to))134			.collect::<Result<Vec<_>, DispatchError>>()?;135136		let amount = data.len();137		with_weight(138			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),139			<CommonWeights<T>>::create_multiple_items(amount as u32),140		)141	}142143	fn create_multiple_items_ex(144		&self,145		sender: <T>::CrossAccountId,146		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,147		nesting_budget: &dyn Budget,148	) -> DispatchResultWithPostInfo {149		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);150		let data = match data {151			up_data_structs::CreateItemExData::NFT(nft) => nft,152			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),153		};154155		with_weight(156			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),157			weight,158		)159	}160161	fn set_collection_properties(162		&self,163		sender: T::CrossAccountId,164		properties: Vec<Property>,165	) -> DispatchResultWithPostInfo {166		let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);167168		with_weight(169			<Pallet<T>>::set_collection_properties(self, &sender, properties),170			weight,171		)172	}173174	fn set_token_properties(175		&self,176		sender: T::CrossAccountId,177		token_id: TokenId,178		properties: Vec<Property>,179	) -> DispatchResultWithPostInfo {180		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);181182		with_weight(183			<Pallet<T>>::set_token_properties(self, &sender, token_id, properties),184			weight,185		)186	}187188	fn delete_token_properties(189		&self,190		sender: T::CrossAccountId,191		token_id: TokenId,192		property_keys: Vec<PropertyKey>,193	) -> DispatchResultWithPostInfo {194		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);195196		with_weight(197			<Pallet<T>>::delete_token_properties(self, &sender, token_id, property_keys),198			weight,199		)200	}201202	fn set_property_permissions(203		&self,204		sender: &T::CrossAccountId,205		property_permissions: Vec<PropertyKeyPermission>,206	) -> DispatchResultWithPostInfo {207		let weight =208			<CommonWeights<T>>::set_property_permissions(property_permissions.len() as u32);209210		with_weight(211			<Pallet<T>>::set_property_permissions(self, sender, property_permissions),212			weight,213		)214	}215216	fn burn_item(217		&self,218		sender: T::CrossAccountId,219		token: TokenId,220		amount: u128,221	) -> DispatchResultWithPostInfo {222		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);223		if amount == 1 {224			with_weight(225				<Pallet<T>>::burn(self, &sender, token),226				<CommonWeights<T>>::burn_item(),227			)228		} else {229			Ok(().into())230		}231	}232233	fn transfer(234		&self,235		from: T::CrossAccountId,236		to: T::CrossAccountId,237		token: TokenId,238		amount: u128,239		nesting_budget: &dyn Budget,240	) -> DispatchResultWithPostInfo {241		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);242		if amount == 1 {243			with_weight(244				<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),245				<CommonWeights<T>>::transfer(),246			)247		} else {248			Ok(().into())249		}250	}251252	fn approve(253		&self,254		sender: T::CrossAccountId,255		spender: T::CrossAccountId,256		token: TokenId,257		amount: u128,258	) -> DispatchResultWithPostInfo {259		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);260261		with_weight(262			if amount == 1 {263				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))264			} else {265				<Pallet<T>>::set_allowance(self, &sender, token, None)266			},267			<CommonWeights<T>>::approve(),268		)269	}270271	fn transfer_from(272		&self,273		sender: T::CrossAccountId,274		from: T::CrossAccountId,275		to: T::CrossAccountId,276		token: TokenId,277		amount: u128,278		nesting_budget: &dyn Budget,279	) -> DispatchResultWithPostInfo {280		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);281282		if amount == 1 {283			with_weight(284				<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),285				<CommonWeights<T>>::transfer_from(),286			)287		} else {288			Ok(().into())289		}290	}291292	fn burn_from(293		&self,294		sender: T::CrossAccountId,295		from: T::CrossAccountId,296		token: TokenId,297		amount: u128,298		nesting_budget: &dyn Budget,299	) -> DispatchResultWithPostInfo {300		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);301302		if amount == 1 {303			with_weight(304				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),305				<CommonWeights<T>>::burn_from(),306			)307		} else {308			Ok(().into())309		}310	}311312	fn set_variable_metadata(313		&self,314		sender: T::CrossAccountId,315		token: TokenId,316		data: BoundedVec<u8, CustomDataLimit>,317	) -> DispatchResultWithPostInfo {318		let len = data.len();319		with_weight(320			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),321			<CommonWeights<T>>::set_variable_metadata(len as u32),322		)323	}324325	fn check_nesting(326		&self,327		sender: T::CrossAccountId,328		from: (CollectionId, TokenId),329		under: TokenId,330		budget: &dyn Budget,331	) -> sp_runtime::DispatchResult {332		<Pallet<T>>::check_nesting(self, sender, from, under, budget)333	}334335	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {336		<Owned<T>>::iter_prefix((self.id, account))337			.map(|(id, _)| id)338			.collect()339	}340341	fn collection_tokens(&self) -> Vec<TokenId> {342		<TokenData<T>>::iter_prefix((self.id,))343			.map(|(id, _)| id)344			.collect()345	}346347	fn token_exists(&self, token: TokenId) -> bool {348		<Pallet<T>>::token_exists(self, token)349	}350351	fn last_token_id(&self) -> TokenId {352		TokenId(<TokensMinted<T>>::get(self.id))353	}354355	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {356		<TokenData<T>>::get((self.id, token)).map(|t| t.owner)357	}358	fn const_metadata(&self, token: TokenId) -> Vec<u8> {359		<TokenData<T>>::get((self.id, token))360			.map(|t| t.const_data)361			.unwrap_or_default()362			.into_inner()363	}364	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {365		<TokenData<T>>::get((self.id, token))366			.map(|t| t.variable_data)367			.unwrap_or_default()368			.into_inner()369	}370371	fn total_supply(&self) -> u32 {372		<Pallet<T>>::total_supply(self)373	}374375	fn account_balance(&self, account: T::CrossAccountId) -> u32 {376		<AccountBalance<T>>::get((self.id, account))377	}378379	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {380		if <TokenData<T>>::get((self.id, token))381			.map(|a| a.owner == account)382			.unwrap_or(false)383		{384			1385		} else {386			0387		}388	}389390	fn allowance(391		&self,392		sender: T::CrossAccountId,393		spender: T::CrossAccountId,394		token: TokenId,395	) -> u128 {396		if <TokenData<T>>::get((self.id, token))397			.map(|a| a.owner != sender)398			.unwrap_or(true)399		{400			0401		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {402			1403		} else {404			0405		}406	}407}