git.delta.rocks / unique-network / refs/commits / 6ac8e66dbab7

difftreelog

source

pallets/fungible/src/common.rs8.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, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{CustomDataLimit, Property, PropertyKey, PropertyKeyPermission};2526use crate::{27	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,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(_amount: u32) -> Weight {37		Self::create_item()38	}3940	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41		match data {42			CreateItemExData::Fungible(f) => {43				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44			}45			_ => 0,46		}47	}4849	fn burn_item() -> Weight {50		<SelfWeightOf<T>>::burn_item()51	}5253	fn set_collection_properties(amount: u32) -> Weight {54		<SelfWeightOf<T>>::set_collection_properties(amount)55	}5657	fn delete_collection_properties(amount: u32) -> Weight {58		<SelfWeightOf<T>>::delete_collection_properties(amount)59	}6061	fn set_token_properties(amount: u32) -> Weight {62		<SelfWeightOf<T>>::set_token_properties(amount)63	}6465	fn delete_token_properties(amount: u32) -> Weight {66		<SelfWeightOf<T>>::delete_token_properties(amount)67	}6869	fn set_property_permissions(amount: u32) -> Weight {70		<SelfWeightOf<T>>::set_property_permissions(amount)71	}7273	fn transfer() -> Weight {74		<SelfWeightOf<T>>::transfer()75	}7677	fn approve() -> Weight {78		<SelfWeightOf<T>>::approve()79	}8081	fn transfer_from() -> Weight {82		<SelfWeightOf<T>>::transfer_from()83	}8485	fn burn_from() -> Weight {86		<SelfWeightOf<T>>::burn_from()87	}8889	fn set_variable_metadata(_bytes: u32) -> Weight {90		// Error91		092	}93}9495impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {96	fn create_item(97		&self,98		sender: T::CrossAccountId,99		to: T::CrossAccountId,100		data: up_data_structs::CreateItemData,101		nesting_budget: &dyn Budget,102	) -> DispatchResultWithPostInfo {103		match data {104			up_data_structs::CreateItemData::Fungible(data) => with_weight(105				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),106				<CommonWeights<T>>::create_item(),107			),108			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),109		}110	}111112	fn create_multiple_items(113		&self,114		sender: T::CrossAccountId,115		to: T::CrossAccountId,116		data: Vec<up_data_structs::CreateItemData>,117		nesting_budget: &dyn Budget,118	) -> DispatchResultWithPostInfo {119		let mut sum: u128 = 0;120		for data in data {121			match data {122				up_data_structs::CreateItemData::Fungible(data) => {123					sum = sum124						.checked_add(data.value)125						.ok_or(ArithmeticError::Overflow)?;126				}127				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),128			}129		}130131		with_weight(132			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),133			<CommonWeights<T>>::create_item(),134		)135	}136137	fn create_multiple_items_ex(138		&self,139		sender: <T>::CrossAccountId,140		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,141		nesting_budget: &dyn Budget,142	) -> DispatchResultWithPostInfo {143		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);144		let data = match data {145			up_data_structs::CreateItemExData::Fungible(f) => f,146			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),147		};148149		with_weight(150			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),151			weight,152		)153	}154155	fn burn_item(156		&self,157		sender: T::CrossAccountId,158		token: TokenId,159		amount: u128,160	) -> DispatchResultWithPostInfo {161		ensure!(162			token == TokenId::default(),163			<Error<T>>::FungibleItemsHaveNoId164		);165166		with_weight(167			<Pallet<T>>::burn(self, &sender, amount),168			<CommonWeights<T>>::burn_item(),169		)170	}171172	fn transfer(173		&self,174		from: T::CrossAccountId,175		to: T::CrossAccountId,176		token: TokenId,177		amount: u128,178		nesting_budget: &dyn Budget,179	) -> DispatchResultWithPostInfo {180		ensure!(181			token == TokenId::default(),182			<Error<T>>::FungibleItemsHaveNoId183		);184185		with_weight(186			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),187			<CommonWeights<T>>::transfer(),188		)189	}190191	fn approve(192		&self,193		sender: T::CrossAccountId,194		spender: T::CrossAccountId,195		token: TokenId,196		amount: u128,197	) -> DispatchResultWithPostInfo {198		ensure!(199			token == TokenId::default(),200			<Error<T>>::FungibleItemsHaveNoId201		);202203		with_weight(204			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),205			<CommonWeights<T>>::approve(),206		)207	}208209	fn transfer_from(210		&self,211		sender: T::CrossAccountId,212		from: T::CrossAccountId,213		to: T::CrossAccountId,214		token: TokenId,215		amount: u128,216		nesting_budget: &dyn Budget,217	) -> DispatchResultWithPostInfo {218		ensure!(219			token == TokenId::default(),220			<Error<T>>::FungibleItemsHaveNoId221		);222223		with_weight(224			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),225			<CommonWeights<T>>::transfer_from(),226		)227	}228229	fn burn_from(230		&self,231		sender: T::CrossAccountId,232		from: T::CrossAccountId,233		token: TokenId,234		amount: u128,235		nesting_budget: &dyn Budget,236	) -> DispatchResultWithPostInfo {237		ensure!(238			token == TokenId::default(),239			<Error<T>>::FungibleItemsHaveNoId240		);241242		with_weight(243			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),244			<CommonWeights<T>>::burn_from(),245		)246	}247248	fn set_collection_properties(249		&self,250		_sender: T::CrossAccountId,251		_property: Vec<Property>,252	) -> DispatchResultWithPostInfo {253		fail!(<Error<T>>::SettingPropertiesNotAllowed)254	}255256	fn delete_collection_properties(257		&self,258		_sender: &T::CrossAccountId,259		_property_keys: Vec<PropertyKey>,260	) -> DispatchResultWithPostInfo {261		fail!(<Error<T>>::SettingPropertiesNotAllowed)262	}263264	fn set_token_properties(265		&self,266		_sender: T::CrossAccountId,267		_token_id: TokenId,268		_property: Vec<Property>,269	) -> DispatchResultWithPostInfo {270		fail!(<Error<T>>::SettingPropertiesNotAllowed)271	}272273	fn set_property_permissions(274		&self,275		_sender: &T::CrossAccountId,276		_property_permissions: Vec<PropertyKeyPermission>,277	) -> DispatchResultWithPostInfo {278		fail!(<Error<T>>::SettingPropertiesNotAllowed)279	}280281	fn delete_token_properties(282		&self,283		_sender: T::CrossAccountId,284		_token_id: TokenId,285		_property_keys: Vec<PropertyKey>,286	) -> DispatchResultWithPostInfo {287		fail!(<Error<T>>::SettingPropertiesNotAllowed)288	}289290	fn set_variable_metadata(291		&self,292		_sender: T::CrossAccountId,293		_token: TokenId,294		_data: BoundedVec<u8, CustomDataLimit>,295	) -> DispatchResultWithPostInfo {296		fail!(<Error<T>>::FungibleItemsDontHaveData)297	}298299	fn check_nesting(300		&self,301		_sender: <T>::CrossAccountId,302		_from: (CollectionId, TokenId),303		_under: TokenId,304		_budget: &dyn Budget,305	) -> sp_runtime::DispatchResult {306		fail!(<Error<T>>::FungibleDisallowsNesting)307	}308309	fn collection_tokens(&self) -> Vec<TokenId> {310		vec![TokenId::default()]311	}312313	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {314		if <Balance<T>>::get((self.id, account)) != 0 {315			vec![TokenId::default()]316		} else {317			vec![]318		}319	}320321	fn token_exists(&self, token: TokenId) -> bool {322		token == TokenId::default()323	}324325	fn last_token_id(&self) -> TokenId {326		TokenId::default()327	}328329	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {330		None331	}332	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {333		Vec::new()334	}335	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {336		Vec::new()337	}338339	fn token_properties(&self, _token_id: TokenId, _keys: Vec<PropertyKey>) -> Vec<Property> {340		Vec::new()341	}342343	fn total_supply(&self) -> u32 {344		1345	}346347	fn account_balance(&self, account: T::CrossAccountId) -> u32 {348		if <Balance<T>>::get((self.id, account)) != 0 {349			1350		} else {351			0352		}353	}354355	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {356		if token != TokenId::default() {357			return 0;358		}359		<Balance<T>>::get((self.id, account))360	}361362	fn allowance(363		&self,364		sender: T::CrossAccountId,365		spender: T::CrossAccountId,366		token: TokenId,367	) -> u128 {368		if token != TokenId::default() {369			return 0;370		}371		<Allowance<T>>::get((self.id, sender, spender))372	}373}