git.delta.rocks / unique-network / refs/commits / 900be63a359f

difftreelog

source

pallets/nonfungible/src/common.rs14.9 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};20use pallet_common::{21	init_token_properties_delta, weights::WeightInfo as _, with_weight, CommonCollectionOperations,22	CommonWeightInfo, RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,23};24use pallet_structure::Pallet as PalletStructure;25use sp_runtime::DispatchError;26use sp_std::{vec, vec::Vec};27use up_data_structs::{28	budget::Budget, CollectionId, CreateItemExData, Property, PropertyKey, PropertyKeyPermission,29	PropertyValue, TokenId, TokenOwnerError,30};3132use crate::{33	weights::WeightInfo, AccountBalance, Allowance, Config, CreateItemData, Error,34	NonfungibleHandle, Owned, Pallet, SelfWeightOf, TokenData, TokenProperties, TokensMinted,35};3637pub struct CommonWeights<T: Config>(PhantomData<T>);38impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {39	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				.saturating_add(init_token_properties_delta::<T, _>(43					t.iter().map(|t| t.properties.len() as u32),44					<SelfWeightOf<T>>::init_token_properties,45				)),46			_ => Weight::zero(),47		}48	}4950	fn create_multiple_items(data: &[up_data_structs::CreateItemData]) -> Weight {51		<SelfWeightOf<T>>::create_multiple_items(data.len() as u32).saturating_add(52			init_token_properties_delta::<T, _>(53				data.iter().map(|t| match t {54					up_data_structs::CreateItemData::NFT(n) => n.properties.len() as u32,55					_ => 0,56				}),57				<SelfWeightOf<T>>::init_token_properties,58			),59		)60	}6162	fn burn_item() -> Weight {63		<SelfWeightOf<T>>::burn_item()64	}6566	fn set_collection_properties(amount: u32) -> Weight {67		<pallet_common::SelfWeightOf<T>>::set_collection_properties(amount)68	}6970	fn delete_collection_properties(amount: u32) -> Weight {71		<pallet_common::SelfWeightOf<T>>::delete_collection_properties(amount)72	}7374	fn set_token_properties(amount: u32) -> Weight {75		<SelfWeightOf<T>>::set_token_properties(amount)76	}7778	fn delete_token_properties(amount: u32) -> Weight {79		<SelfWeightOf<T>>::delete_token_properties(amount)80	}8182	fn set_token_property_permissions(amount: u32) -> Weight {83		<SelfWeightOf<T>>::set_token_property_permissions(amount)84	}8586	fn transfer() -> Weight {87		<SelfWeightOf<T>>::transfer_raw() + <PalletCommonWeightOf<T>>::check_accesslist() * 288	}8990	fn approve() -> Weight {91		<SelfWeightOf<T>>::approve()92	}9394	fn approve_from() -> Weight {95		<SelfWeightOf<T>>::approve_from()96	}9798	fn transfer_from() -> Weight {99		Self::transfer() + <SelfWeightOf<T>>::check_allowed_raw()100	}101102	fn burn_from() -> Weight {103		<SelfWeightOf<T>>::burn_from()104	}105106	fn burn_recursively_self_raw() -> Weight {107		<SelfWeightOf<T>>::burn_recursively_self_raw()108	}109110	fn burn_recursively_breadth_raw(amount: u32) -> Weight {111		<SelfWeightOf<T>>::burn_recursively_breadth_plus_self_plus_self_per_each_raw(amount)112			.saturating_sub(Self::burn_recursively_self_raw().saturating_mul(amount as u64 + 1))113	}114115	fn token_owner() -> Weight {116		<SelfWeightOf<T>>::token_owner()117	}118119	fn set_allowance_for_all() -> Weight {120		<SelfWeightOf<T>>::set_allowance_for_all()121	}122123	fn force_repair_item() -> Weight {124		<SelfWeightOf<T>>::repair_item()125	}126}127128fn map_create_data<T: Config>(129	data: up_data_structs::CreateItemData,130	to: &T::CrossAccountId,131) -> Result<CreateItemData<T>, DispatchError> {132	match data {133		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {134			properties: data.properties,135			owner: to.clone(),136		}),137		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),138	}139}140141/// Implementation of `CommonCollectionOperations` for `NonfungibleHandle`. It wraps Nonfungible Pallete142/// methods and adds weight info.143impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {144	fn create_item(145		&self,146		sender: T::CrossAccountId,147		to: T::CrossAccountId,148		data: up_data_structs::CreateItemData,149		nesting_budget: &dyn Budget,150	) -> DispatchResultWithPostInfo {151		let weight = <CommonWeights<T>>::create_item(&data);152		with_weight(153			<Pallet<T>>::create_item(154				self,155				&sender,156				map_create_data::<T>(data, &to)?,157				nesting_budget,158			),159			weight,160		)161	}162163	fn create_multiple_items(164		&self,165		sender: T::CrossAccountId,166		to: T::CrossAccountId,167		data: Vec<up_data_structs::CreateItemData>,168		nesting_budget: &dyn Budget,169	) -> DispatchResultWithPostInfo {170		let weight = <CommonWeights<T>>::create_multiple_items(&data);171		let data = data172			.into_iter()173			.map(|d| map_create_data::<T>(d, &to))174			.collect::<Result<Vec<_>, DispatchError>>()?;175176		with_weight(177			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),178			weight,179		)180	}181182	fn create_multiple_items_ex(183		&self,184		sender: <T>::CrossAccountId,185		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,186		nesting_budget: &dyn Budget,187	) -> DispatchResultWithPostInfo {188		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);189		let data = match data {190			up_data_structs::CreateItemExData::NFT(nft) => nft,191			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),192		};193194		with_weight(195			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),196			weight,197		)198	}199200	fn set_collection_properties(201		&self,202		sender: T::CrossAccountId,203		properties: Vec<Property>,204	) -> DispatchResultWithPostInfo {205		let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);206207		with_weight(208			<Pallet<T>>::set_collection_properties(self, &sender, properties),209			weight,210		)211	}212213	fn delete_collection_properties(214		&self,215		sender: &T::CrossAccountId,216		property_keys: Vec<PropertyKey>,217	) -> DispatchResultWithPostInfo {218		let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);219220		with_weight(221			<Pallet<T>>::delete_collection_properties(self, sender, property_keys),222			weight,223		)224	}225226	fn set_token_properties(227		&self,228		sender: T::CrossAccountId,229		token_id: TokenId,230		properties: Vec<Property>,231		nesting_budget: &dyn Budget,232	) -> DispatchResultWithPostInfo {233		let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);234235		with_weight(236			<Pallet<T>>::set_token_properties(237				self,238				&sender,239				token_id,240				properties.into_iter(),241				nesting_budget,242			),243			weight,244		)245	}246247	fn delete_token_properties(248		&self,249		sender: T::CrossAccountId,250		token_id: TokenId,251		property_keys: Vec<PropertyKey>,252		nesting_budget: &dyn Budget,253	) -> DispatchResultWithPostInfo {254		let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);255256		with_weight(257			<Pallet<T>>::delete_token_properties(258				self,259				&sender,260				token_id,261				property_keys.into_iter(),262				nesting_budget,263			),264			weight,265		)266	}267268	fn get_token_properties_raw(269		&self,270		token_id: TokenId,271	) -> Option<up_data_structs::TokenProperties> {272		<TokenProperties<T>>::get((self.id, token_id))273	}274275	fn set_token_properties_raw(&self, token_id: TokenId, map: up_data_structs::TokenProperties) {276		<TokenProperties<T>>::insert((self.id, token_id), map)277	}278279	fn set_token_property_permissions(280		&self,281		sender: &T::CrossAccountId,282		property_permissions: Vec<PropertyKeyPermission>,283	) -> DispatchResultWithPostInfo {284		let weight =285			<CommonWeights<T>>::set_token_property_permissions(property_permissions.len() as u32);286287		with_weight(288			<Pallet<T>>::set_token_property_permissions(self, sender, property_permissions),289			weight,290		)291	}292293	fn burn_item(294		&self,295		sender: T::CrossAccountId,296		token: TokenId,297		amount: u128,298	) -> DispatchResultWithPostInfo {299		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);300		if amount == 1 {301			with_weight(302				<Pallet<T>>::burn(self, &sender, token),303				<CommonWeights<T>>::burn_item(),304			)305		} else {306			<Pallet<T>>::check_token_immediate_ownership(self, token, &sender)?;307			Ok(().into())308		}309	}310311	fn burn_item_recursively(312		&self,313		sender: T::CrossAccountId,314		token: TokenId,315		self_budget: &dyn Budget,316		breadth_budget: &dyn Budget,317	) -> DispatchResultWithPostInfo {318		<Pallet<T>>::burn_recursively(self, &sender, token, self_budget, breadth_budget)319	}320321	fn transfer(322		&self,323		from: T::CrossAccountId,324		to: T::CrossAccountId,325		token: TokenId,326		amount: u128,327		nesting_budget: &dyn Budget,328	) -> DispatchResultWithPostInfo {329		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);330		if amount == 1 {331			<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget)332		} else {333			<Pallet<T>>::check_token_immediate_ownership(self, token, &from)?;334			Ok(().into())335		}336	}337338	fn approve(339		&self,340		sender: T::CrossAccountId,341		spender: T::CrossAccountId,342		token: TokenId,343		amount: u128,344	) -> DispatchResultWithPostInfo {345		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);346347		with_weight(348			if amount == 1 {349				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))350			} else {351				<Pallet<T>>::set_allowance(self, &sender, token, None)352			},353			<CommonWeights<T>>::approve(),354		)355	}356357	fn approve_from(358		&self,359		sender: T::CrossAccountId,360		from: T::CrossAccountId,361		to: T::CrossAccountId,362		token: TokenId,363		amount: u128,364	) -> DispatchResultWithPostInfo {365		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);366367		with_weight(368			if amount == 1 {369				<Pallet<T>>::set_allowance_from(self, &sender, &from, token, Some(&to))370			} else {371				<Pallet<T>>::set_allowance_from(self, &sender, &from, token, None)372			},373			<CommonWeights<T>>::approve_from(),374		)375	}376377	fn transfer_from(378		&self,379		sender: T::CrossAccountId,380		from: T::CrossAccountId,381		to: T::CrossAccountId,382		token: TokenId,383		amount: u128,384		nesting_budget: &dyn Budget,385	) -> DispatchResultWithPostInfo {386		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);387388		if amount == 1 {389			<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget)390		} else {391			<Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;392393			Ok(().into())394		}395	}396397	fn burn_from(398		&self,399		sender: T::CrossAccountId,400		from: T::CrossAccountId,401		token: TokenId,402		amount: u128,403		nesting_budget: &dyn Budget,404	) -> DispatchResultWithPostInfo {405		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);406407		if amount == 1 {408			with_weight(409				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),410				<CommonWeights<T>>::burn_from(),411			)412		} else {413			<Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;414415			Ok(().into())416		}417	}418419	fn check_nesting(420		&self,421		sender: T::CrossAccountId,422		from: (CollectionId, TokenId),423		under: TokenId,424		nesting_budget: &dyn Budget,425	) -> sp_runtime::DispatchResult {426		<Pallet<T>>::check_nesting(self, sender, from, under, nesting_budget)427	}428429	fn nest(&self, under: TokenId, to_nest: (CollectionId, TokenId)) {430		<Pallet<T>>::nest((self.id, under), to_nest);431	}432433	fn unnest(&self, under: TokenId, to_unnest: (CollectionId, TokenId)) {434		<Pallet<T>>::unnest((self.id, under), to_unnest);435	}436437	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {438		<Owned<T>>::iter_prefix((self.id, account))439			.map(|(id, _)| id)440			.collect()441	}442443	fn collection_tokens(&self) -> Vec<TokenId> {444		<TokenData<T>>::iter_prefix((self.id,))445			.map(|(id, _)| id)446			.collect()447	}448449	fn token_exists(&self, token: TokenId) -> bool {450		<Pallet<T>>::token_exists(self, token)451	}452453	fn last_token_id(&self) -> TokenId {454		TokenId(<TokensMinted<T>>::get(self.id))455	}456457	fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError> {458		<TokenData<T>>::get((self.id, token))459			.map(|t| t.owner)460			.ok_or(TokenOwnerError::NotFound)461	}462463	fn check_token_indirect_owner(464		&self,465		token: TokenId,466		maybe_owner: &T::CrossAccountId,467		nesting_budget: &dyn Budget,468	) -> Result<bool, DispatchError> {469		<PalletStructure<T>>::check_indirectly_owned(470			maybe_owner.clone(),471			self.id,472			token,473			None,474			nesting_budget,475		)476	}477478	/// Returns token owners.479	fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {480		self.token_owner(token).map_or_else(|_| vec![], |t| vec![t])481	}482483	fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {484		<Pallet<T>>::token_properties((self.id, token_id))?485			.get(key)486			.cloned()487	}488489	fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {490		let Some(properties) = <Pallet<T>>::token_properties((self.id, token_id)) else {491			return vec![];492		};493494		keys.map(|keys| {495			keys.into_iter()496				.filter_map(|key| {497					properties.get(&key).map(|value| Property {498						key,499						value: value.clone(),500					})501				})502				.collect()503		})504		.unwrap_or_else(|| {505			properties506				.into_iter()507				.map(|(key, value)| Property { key, value })508				.collect()509		})510	}511512	fn total_supply(&self) -> u32 {513		<Pallet<T>>::total_supply(self)514	}515516	fn account_balance(&self, account: T::CrossAccountId) -> u32 {517		<AccountBalance<T>>::get((self.id, account))518	}519520	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {521		if <TokenData<T>>::get((self.id, token))522			.map(|a| a.owner == account)523			.unwrap_or(false)524		{525			1526		} else {527			0528		}529	}530531	fn allowance(532		&self,533		sender: T::CrossAccountId,534		spender: T::CrossAccountId,535		token: TokenId,536	) -> u128 {537		if <TokenData<T>>::get((self.id, token))538			.map(|a| a.owner != sender)539			.unwrap_or(true)540		{541			0542		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {543			1544		} else {545			0546		}547	}548549	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {550		None551	}552553	fn total_pieces(&self, token: TokenId) -> Option<u128> {554		if <TokenData<T>>::contains_key((self.id, token)) {555			Some(1)556		} else {557			None558		}559	}560561	fn set_allowance_for_all(562		&self,563		owner: T::CrossAccountId,564		operator: T::CrossAccountId,565		approve: bool,566	) -> DispatchResultWithPostInfo {567		with_weight(568			<Pallet<T>>::set_allowance_for_all(self, &owner, &operator, approve),569			<CommonWeights<T>>::set_allowance_for_all(),570		)571	}572573	fn allowance_for_all(&self, owner: T::CrossAccountId, operator: T::CrossAccountId) -> bool {574		<Pallet<T>>::allowance_for_all(self, &owner, &operator)575	}576577	fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {578		with_weight(579			<Pallet<T>>::repair_item(self, token),580			<CommonWeights<T>>::force_repair_item(),581		)582	}583}