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

difftreelog

source

pallets/nonfungible/src/common.rs5.3 KiBsourcehistory
1use core::marker::PhantomData;23use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};4use nft_data_structs::TokenId;5use pallet_common::{6	CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,7};8use sp_runtime::DispatchError;9use sp_std::vec::Vec;1011use crate::{12	AccountBalance, Allowance, Config, CreateItemData, DataKind, Error, NonfungibleHandle, Owned,13	Owner, Pallet, SelfWeightOf, TokenData, weights::WeightInfo,14};1516pub struct CommonWeights<T: Config>(PhantomData<T>);17impl<T: Config> CommonWeightInfo for CommonWeights<T> {18	fn create_item() -> Weight {19		<SelfWeightOf<T>>::create_item()20	}2122	fn create_multiple_items(amount: u32) -> Weight {23		<SelfWeightOf<T>>::create_multiple_items(amount)24	}2526	fn burn_item() -> Weight {27		<SelfWeightOf<T>>::burn_item()28	}2930	fn transfer() -> Weight {31		<SelfWeightOf<T>>::transfer()32	}3334	fn approve() -> Weight {35		<SelfWeightOf<T>>::approve()36	}3738	fn transfer_from() -> Weight {39		<SelfWeightOf<T>>::transfer_from()40	}4142	fn set_variable_metadata(_bytes: u32) -> Weight {43		<SelfWeightOf<T>>::set_variable_metadata()44	}45}4647fn map_create_data<T: Config>(48	data: nft_data_structs::CreateItemData,49	to: &T::CrossAccountId,50) -> Result<CreateItemData<T>, DispatchError> {51	match data {52		nft_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData {53			const_data: data.const_data,54			variable_data: data.variable_data,55			owner: to.clone(),56		}),57		_ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),58	}59}6061impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {62	fn create_item(63		&self,64		sender: T::CrossAccountId,65		to: T::CrossAccountId,66		data: nft_data_structs::CreateItemData,67	) -> DispatchResultWithPostInfo {68		with_weight(69			<Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),70			<SelfWeightOf<T>>::create_item(),71		)72	}7374	fn create_multiple_items(75		&self,76		sender: T::CrossAccountId,77		to: T::CrossAccountId,78		data: Vec<nft_data_structs::CreateItemData>,79	) -> DispatchResultWithPostInfo {80		let data = data81			.into_iter()82			.map(|d| map_create_data::<T>(d, &to))83			.collect::<Result<Vec<_>, DispatchError>>()?;8485		let amount = data.len();86		with_weight(87			<Pallet<T>>::create_multiple_items(self, &sender, data),88			<SelfWeightOf<T>>::create_multiple_items(amount as u32),89		)90	}9192	fn burn_item(93		&self,94		sender: T::CrossAccountId,95		token: TokenId,96		amount: u128,97	) -> DispatchResultWithPostInfo {98		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);99		if amount == 1 {100			with_weight(101				<Pallet<T>>::burn(&self, &sender, token),102				<SelfWeightOf<T>>::burn_item(),103			)104		} else {105			Ok(().into())106		}107	}108109	fn transfer(110		&self,111		from: T::CrossAccountId,112		to: T::CrossAccountId,113		token: TokenId,114		amount: u128,115	) -> DispatchResultWithPostInfo {116		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);117		if amount == 1 {118			with_weight(119				<Pallet<T>>::transfer(&self, &from, &to, token),120				<SelfWeightOf<T>>::transfer(),121			)122		} else {123			Ok(().into())124		}125	}126127	fn approve(128		&self,129		sender: T::CrossAccountId,130		spender: T::CrossAccountId,131		token: TokenId,132		amount: u128,133	) -> DispatchResultWithPostInfo {134		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);135136		with_weight(137			if amount == 1 {138				<Pallet<T>>::set_allowance(&self, &sender, token, Some(&spender))139			} else {140				<Pallet<T>>::set_allowance(&self, &sender, token, None)141			},142			<SelfWeightOf<T>>::approve(),143		)144	}145146	fn transfer_from(147		&self,148		sender: T::CrossAccountId,149		from: T::CrossAccountId,150		to: T::CrossAccountId,151		token: TokenId,152		amount: u128,153	) -> DispatchResultWithPostInfo {154		ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);155156		if amount == 1 {157			with_weight(158				<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token),159				<SelfWeightOf<T>>::transfer_from(),160			)161		} else {162			Ok(().into())163		}164	}165166	fn set_variable_metadata(167		&self,168		sender: T::CrossAccountId,169		token: TokenId,170		data: Vec<u8>,171	) -> DispatchResultWithPostInfo {172		with_weight(173			<Pallet<T>>::set_variable_metadata(&self, &sender, token, data),174			<SelfWeightOf<T>>::set_variable_metadata(),175		)176	}177178	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {179		<Owned<T>>::iter_prefix((self.id, account.as_sub()))180			.map(|(id, _)| id)181			.collect()182	}183184	fn token_exists(&self, token: TokenId) -> bool {185		<Pallet<T>>::token_exists(self, token)186	}187188	fn token_owner(&self, token: TokenId) -> T::CrossAccountId {189		<Owner<T>>::get((self.id, token))190	}191	fn const_metadata(&self, token: TokenId) -> Vec<u8> {192		<TokenData<T>>::get((self.id, token, DataKind::Constant))193	}194	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {195		<TokenData<T>>::get((self.id, token, DataKind::Variable))196	}197198	fn collection_tokens(&self) -> u32 {199		<Pallet<T>>::total_supply(self)200	}201202	fn account_balance(&self, account: T::CrossAccountId) -> u32 {203		<AccountBalance<T>>::get((self.id, account.as_sub()))204	}205206	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {207		if <Owner<T>>::get((self.id, token)) == account {208			1209		} else {210			0211		}212	}213214	fn allowance(215		&self,216		sender: T::CrossAccountId,217		spender: T::CrossAccountId,218		token: TokenId,219	) -> u128 {220		if <Owner<T>>::get((self.id, token)) != sender {221			0222		} else if <Allowance<T>>::get((self.id, token)) == Some(spender) {223			1224		} else {225			0226		}227	}228}