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

difftreelog

source

pallets/nonfungible/src/common.rs8.3 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::{TokenId, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::DispatchError;23use sp_std::vec::Vec;2425use crate::{26	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,27	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,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_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {37		match data {38			CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),39			_ => 0,40		}41	}4243	fn create_multiple_items(amount: u32) -> Weight {44		<SelfWeightOf<T>>::create_multiple_items(amount)45	}4647	fn burn_item() -> Weight {48		<SelfWeightOf<T>>::burn_item()49	}5051	fn transfer() -> Weight {52		<SelfWeightOf<T>>::transfer()53	}5455	fn approve() -> Weight {56		<SelfWeightOf<T>>::approve()57	}5859	fn transfer_from() -> Weight {60		<SelfWeightOf<T>>::transfer_from()61	}6263	fn burn_from() -> Weight {64		<SelfWeightOf<T>>::burn_from()65	}6667	fn set_variable_metadata(bytes: u32) -> Weight {68		<SelfWeightOf<T>>::set_variable_metadata(bytes)69	}70}7172fn map_create_data<T: Config>(73	data: up_data_structs::CreateItemData,74	to: &T::CrossAccountId,75) -> Result<CreateItemData<T>, DispatchError> {76	match data {77		up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {78			const_data: data.const_data,79			variable_data: data.variable_data,80			owner: to.clone(),81		}),82		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),83	}84}8586impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {87	fn create_item(88		&self,89		sender: T::CrossAccountId,90		to: T::CrossAccountId,91		data: up_data_structs::CreateItemData,92		nesting_budget: &dyn Budget,93	) -> DispatchResultWithPostInfo {94		with_weight(95			<Pallet<T>>::create_item(96				self,97				&sender,98				map_create_data::<T>(data, &to)?,99				nesting_budget,100			),101			<CommonWeights<T>>::create_item(),102		)103	}104105	fn create_multiple_items(106		&self,107		sender: T::CrossAccountId,108		to: T::CrossAccountId,109		data: Vec<up_data_structs::CreateItemData>,110		nesting_budget: &dyn Budget,111	) -> DispatchResultWithPostInfo {112		let data = data113			.into_iter()114			.map(|d| map_create_data::<T>(d, &to))115			.collect::<Result<Vec<_>, DispatchError>>()?;116117		let amount = data.len();118		with_weight(119			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),120			<CommonWeights<T>>::create_multiple_items(amount as u32),121		)122	}123124	fn create_multiple_items_ex(125		&self,126		sender: <T>::CrossAccountId,127		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,128		nesting_budget: &dyn Budget,129	) -> DispatchResultWithPostInfo {130		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);131		let data = match data {132			up_data_structs::CreateItemExData::NFT(nft) => nft,133			_ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),134		};135136		with_weight(137			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),138			weight,139		)140	}141142	fn burn_item(143		&self,144		sender: T::CrossAccountId,145		token: TokenId,146		amount: u128,147	) -> DispatchResultWithPostInfo {148		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);149		if amount == 1 {150			with_weight(151				<Pallet<T>>::burn(self, &sender, token),152				<CommonWeights<T>>::burn_item(),153			)154		} else {155			Ok(().into())156		}157	}158159	fn transfer(160		&self,161		from: T::CrossAccountId,162		to: T::CrossAccountId,163		token: TokenId,164		amount: u128,165		nesting_budget: &dyn Budget,166	) -> DispatchResultWithPostInfo {167		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);168		if amount == 1 {169			with_weight(170				<Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),171				<CommonWeights<T>>::transfer(),172			)173		} else {174			Ok(().into())175		}176	}177178	fn approve(179		&self,180		sender: T::CrossAccountId,181		spender: T::CrossAccountId,182		token: TokenId,183		amount: u128,184	) -> DispatchResultWithPostInfo {185		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);186187		with_weight(188			if amount == 1 {189				<Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))190			} else {191				<Pallet<T>>::set_allowance(self, &sender, token, None)192			},193			<CommonWeights<T>>::approve(),194		)195	}196197	fn transfer_from(198		&self,199		sender: T::CrossAccountId,200		from: T::CrossAccountId,201		to: T::CrossAccountId,202		token: TokenId,203		amount: u128,204		nesting_budget: &dyn Budget,205	) -> DispatchResultWithPostInfo {206		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);207208		if amount == 1 {209			with_weight(210				<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),211				<CommonWeights<T>>::transfer_from(),212			)213		} else {214			Ok(().into())215		}216	}217218	fn burn_from(219		&self,220		sender: T::CrossAccountId,221		from: T::CrossAccountId,222		token: TokenId,223		amount: u128,224		nesting_budget: &dyn Budget,225	) -> DispatchResultWithPostInfo {226		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);227228		if amount == 1 {229			with_weight(230				<Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),231				<CommonWeights<T>>::burn_from(),232			)233		} else {234			Ok(().into())235		}236	}237238	fn set_variable_metadata(239		&self,240		sender: T::CrossAccountId,241		token: TokenId,242		data: BoundedVec<u8, CustomDataLimit>,243	) -> DispatchResultWithPostInfo {244		let len = data.len();245		with_weight(246			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),247			<CommonWeights<T>>::set_variable_metadata(len as u32),248		)249	}250251	fn check_nesting(252		&self,253		sender: T::CrossAccountId,254		from: (CollectionId, TokenId),255		under: TokenId,256		budget: &dyn Budget,257	) -> sp_runtime::DispatchResult {258		<Pallet<T>>::check_nesting(self, sender, from, under, budget)259	}260261	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {262		<Owned<T>>::iter_prefix((self.id, account))263			.map(|(id, _)| id)264			.collect()265	}266267	fn collection_tokens(&self) -> Vec<TokenId> {268		<TokenData<T>>::iter_prefix((self.id,))269			.map(|(id, _)| id)270			.collect()271	}272273	fn token_exists(&self, token: TokenId) -> bool {274		<Pallet<T>>::token_exists(self, token)275	}276277	fn last_token_id(&self) -> TokenId {278		TokenId(<TokensMinted<T>>::get(self.id))279	}280281	fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {282		<TokenData<T>>::get((self.id, token)).map(|t| t.owner)283	}284	fn const_metadata(&self, token: TokenId) -> Vec<u8> {285		<TokenData<T>>::get((self.id, token))286			.map(|t| t.const_data)287			.unwrap_or_default()288			.into_inner()289	}290	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {291		<TokenData<T>>::get((self.id, token))292			.map(|t| t.variable_data)293			.unwrap_or_default()294			.into_inner()295	}296297	fn total_supply(&self) -> u32 {298		<Pallet<T>>::total_supply(self)299	}300301	fn account_balance(&self, account: T::CrossAccountId) -> u32 {302		<AccountBalance<T>>::get((self.id, account))303	}304305	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {306		if <TokenData<T>>::get((self.id, token))307			.map(|a| a.owner == account)308			.unwrap_or(false)309		{310			1311		} else {312			0313		}314	}315316	fn allowance(317		&self,318		sender: T::CrossAccountId,319		spender: T::CrossAccountId,320		token: TokenId,321	) -> u128 {322		if <TokenData<T>>::get((self.id, token))323			.map(|a| a.owner != sender)324			.unwrap_or(true)325		{326			0327		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {328			1329		} else {330			0331		}332	}333}