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

difftreelog

source

pallets/fungible/src/common.rs8.5 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 set_token_properties(amount: u32) -> Weight {58		<SelfWeightOf<T>>::set_token_properties(amount)59	}6061	fn delete_token_properties(amount: u32) -> Weight {62		<SelfWeightOf<T>>::delete_token_properties(amount)63	}6465	fn set_property_permissions(amount: u32) -> Weight {66		<SelfWeightOf<T>>::set_property_permissions(amount)67	}6869	fn transfer() -> Weight {70		<SelfWeightOf<T>>::transfer()71	}7273	fn approve() -> Weight {74		<SelfWeightOf<T>>::approve()75	}7677	fn transfer_from() -> Weight {78		<SelfWeightOf<T>>::transfer_from()79	}8081	fn burn_from() -> Weight {82		<SelfWeightOf<T>>::burn_from()83	}8485	fn set_variable_metadata(_bytes: u32) -> Weight {86		// Error87		088	}89}9091impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {92	fn create_item(93		&self,94		sender: T::CrossAccountId,95		to: T::CrossAccountId,96		data: up_data_structs::CreateItemData,97		nesting_budget: &dyn Budget,98	) -> DispatchResultWithPostInfo {99		match data {100			up_data_structs::CreateItemData::Fungible(data) => with_weight(101				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),102				<CommonWeights<T>>::create_item(),103			),104			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),105		}106	}107108	fn create_multiple_items(109		&self,110		sender: T::CrossAccountId,111		to: T::CrossAccountId,112		data: Vec<up_data_structs::CreateItemData>,113		nesting_budget: &dyn Budget,114	) -> DispatchResultWithPostInfo {115		let mut sum: u128 = 0;116		for data in data {117			match data {118				up_data_structs::CreateItemData::Fungible(data) => {119					sum = sum120						.checked_add(data.value)121						.ok_or(ArithmeticError::Overflow)?;122				}123				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),124			}125		}126127		with_weight(128			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),129			<CommonWeights<T>>::create_item(),130		)131	}132133	fn create_multiple_items_ex(134		&self,135		sender: <T>::CrossAccountId,136		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,137		nesting_budget: &dyn Budget,138	) -> DispatchResultWithPostInfo {139		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);140		let data = match data {141			up_data_structs::CreateItemExData::Fungible(f) => f,142			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),143		};144145		with_weight(146			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),147			weight,148		)149	}150151	fn burn_item(152		&self,153		sender: T::CrossAccountId,154		token: TokenId,155		amount: u128,156	) -> DispatchResultWithPostInfo {157		ensure!(158			token == TokenId::default(),159			<Error<T>>::FungibleItemsHaveNoId160		);161162		with_weight(163			<Pallet<T>>::burn(self, &sender, amount),164			<CommonWeights<T>>::burn_item(),165		)166	}167168	fn transfer(169		&self,170		from: T::CrossAccountId,171		to: T::CrossAccountId,172		token: TokenId,173		amount: u128,174		nesting_budget: &dyn Budget,175	) -> DispatchResultWithPostInfo {176		ensure!(177			token == TokenId::default(),178			<Error<T>>::FungibleItemsHaveNoId179		);180181		with_weight(182			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),183			<CommonWeights<T>>::transfer(),184		)185	}186187	fn approve(188		&self,189		sender: T::CrossAccountId,190		spender: T::CrossAccountId,191		token: TokenId,192		amount: u128,193	) -> DispatchResultWithPostInfo {194		ensure!(195			token == TokenId::default(),196			<Error<T>>::FungibleItemsHaveNoId197		);198199		with_weight(200			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),201			<CommonWeights<T>>::approve(),202		)203	}204205	fn transfer_from(206		&self,207		sender: T::CrossAccountId,208		from: T::CrossAccountId,209		to: T::CrossAccountId,210		token: TokenId,211		amount: u128,212		nesting_budget: &dyn Budget,213	) -> DispatchResultWithPostInfo {214		ensure!(215			token == TokenId::default(),216			<Error<T>>::FungibleItemsHaveNoId217		);218219		with_weight(220			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),221			<CommonWeights<T>>::transfer_from(),222		)223	}224225	fn burn_from(226		&self,227		sender: T::CrossAccountId,228		from: T::CrossAccountId,229		token: TokenId,230		amount: u128,231		nesting_budget: &dyn Budget,232	) -> DispatchResultWithPostInfo {233		ensure!(234			token == TokenId::default(),235			<Error<T>>::FungibleItemsHaveNoId236		);237238		with_weight(239			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),240			<CommonWeights<T>>::burn_from(),241		)242	}243244	fn set_collection_properties(245		&self,246		_sender: T::CrossAccountId,247		_property: Vec<Property>,248	) -> DispatchResultWithPostInfo {249		fail!(<Error<T>>::PropertiesNotAllowed)250	}251252	fn set_token_properties(253		&self,254		_sender: T::CrossAccountId,255		_token_id: TokenId,256		_property: Vec<Property>,257	) -> DispatchResultWithPostInfo {258		fail!(<Error<T>>::PropertiesNotAllowed)259	}260261	fn set_property_permissions(262		&self,263		_sender: &T::CrossAccountId,264		_property_permissions: Vec<PropertyKeyPermission>,265	) -> DispatchResultWithPostInfo {266		fail!(<Error<T>>::PropertiesNotAllowed)267	}268269	fn delete_token_properties(270		&self,271		_sender: T::CrossAccountId,272		_token_id: TokenId,273		_property_keys: Vec<PropertyKey>,274	) -> DispatchResultWithPostInfo {275		fail!(<Error<T>>::PropertiesNotAllowed)276	}277278	fn set_variable_metadata(279		&self,280		_sender: T::CrossAccountId,281		_token: TokenId,282		_data: BoundedVec<u8, CustomDataLimit>,283	) -> DispatchResultWithPostInfo {284		fail!(<Error<T>>::FungibleItemsDontHaveData)285	}286287	fn check_nesting(288		&self,289		_sender: <T>::CrossAccountId,290		_from: (CollectionId, TokenId),291		_under: TokenId,292		_budget: &dyn Budget,293	) -> sp_runtime::DispatchResult {294		fail!(<Error<T>>::FungibleDisallowsNesting)295	}296297	fn collection_tokens(&self) -> Vec<TokenId> {298		vec![TokenId::default()]299	}300301	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {302		if <Balance<T>>::get((self.id, account)) != 0 {303			vec![TokenId::default()]304		} else {305			vec![]306		}307	}308309	fn token_exists(&self, token: TokenId) -> bool {310		token == TokenId::default()311	}312313	fn last_token_id(&self) -> TokenId {314		TokenId::default()315	}316317	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {318		None319	}320	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {321		Vec::new()322	}323	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {324		Vec::new()325	}326327	fn total_supply(&self) -> u32 {328		1329	}330331	fn account_balance(&self, account: T::CrossAccountId) -> u32 {332		if <Balance<T>>::get((self.id, account)) != 0 {333			1334		} else {335			0336		}337	}338339	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {340		if token != TokenId::default() {341			return 0;342		}343		<Balance<T>>::get((self.id, account))344	}345346	fn allowance(347		&self,348		sender: T::CrossAccountId,349		spender: T::CrossAccountId,350		token: TokenId,351	) -> u128 {352		if token != TokenId::default() {353			return 0;354		}355		<Allowance<T>>::get((self.id, sender, spender))356	}357}