git.delta.rocks / unique-network / refs/commits / 55c4d052d75c

difftreelog

source

pallets/fungible/src/common.rs8.6 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 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::{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	}88}8990impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {91	fn create_item(92		&self,93		sender: T::CrossAccountId,94		to: T::CrossAccountId,95		data: up_data_structs::CreateItemData,96		nesting_budget: &dyn Budget,97	) -> DispatchResultWithPostInfo {98		match data {99			up_data_structs::CreateItemData::Fungible(data) => with_weight(100				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),101				<CommonWeights<T>>::create_item(),102			),103			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),104		}105	}106107	fn create_multiple_items(108		&self,109		sender: T::CrossAccountId,110		to: T::CrossAccountId,111		data: Vec<up_data_structs::CreateItemData>,112		nesting_budget: &dyn Budget,113	) -> DispatchResultWithPostInfo {114		let mut sum: u128 = 0;115		for data in data {116			match data {117				up_data_structs::CreateItemData::Fungible(data) => {118					sum = sum119						.checked_add(data.value)120						.ok_or(ArithmeticError::Overflow)?;121				}122				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),123			}124		}125126		with_weight(127			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),128			<CommonWeights<T>>::create_item(),129		)130	}131132	fn create_multiple_items_ex(133		&self,134		sender: <T>::CrossAccountId,135		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,136		nesting_budget: &dyn Budget,137	) -> DispatchResultWithPostInfo {138		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);139		let data = match data {140			up_data_structs::CreateItemExData::Fungible(f) => f,141			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),142		};143144		with_weight(145			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),146			weight,147		)148	}149150	fn burn_item(151		&self,152		sender: T::CrossAccountId,153		token: TokenId,154		amount: u128,155	) -> DispatchResultWithPostInfo {156		ensure!(157			token == TokenId::default(),158			<Error<T>>::FungibleItemsHaveNoId159		);160161		with_weight(162			<Pallet<T>>::burn(self, &sender, amount),163			<CommonWeights<T>>::burn_item(),164		)165	}166167	fn transfer(168		&self,169		from: T::CrossAccountId,170		to: T::CrossAccountId,171		token: TokenId,172		amount: u128,173		nesting_budget: &dyn Budget,174	) -> DispatchResultWithPostInfo {175		ensure!(176			token == TokenId::default(),177			<Error<T>>::FungibleItemsHaveNoId178		);179180		with_weight(181			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),182			<CommonWeights<T>>::transfer(),183		)184	}185186	fn approve(187		&self,188		sender: T::CrossAccountId,189		spender: T::CrossAccountId,190		token: TokenId,191		amount: u128,192	) -> DispatchResultWithPostInfo {193		ensure!(194			token == TokenId::default(),195			<Error<T>>::FungibleItemsHaveNoId196		);197198		with_weight(199			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),200			<CommonWeights<T>>::approve(),201		)202	}203204	fn transfer_from(205		&self,206		sender: T::CrossAccountId,207		from: T::CrossAccountId,208		to: T::CrossAccountId,209		token: TokenId,210		amount: u128,211		nesting_budget: &dyn Budget,212	) -> DispatchResultWithPostInfo {213		ensure!(214			token == TokenId::default(),215			<Error<T>>::FungibleItemsHaveNoId216		);217218		with_weight(219			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),220			<CommonWeights<T>>::transfer_from(),221		)222	}223224	fn burn_from(225		&self,226		sender: T::CrossAccountId,227		from: T::CrossAccountId,228		token: TokenId,229		amount: u128,230		nesting_budget: &dyn Budget,231	) -> DispatchResultWithPostInfo {232		ensure!(233			token == TokenId::default(),234			<Error<T>>::FungibleItemsHaveNoId235		);236237		with_weight(238			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),239			<CommonWeights<T>>::burn_from(),240		)241	}242243	fn set_collection_properties(244		&self,245		_sender: T::CrossAccountId,246		_property: Vec<Property>,247	) -> DispatchResultWithPostInfo {248		fail!(<Error<T>>::SettingPropertiesNotAllowed)249	}250251	fn delete_collection_properties(252		&self,253		_sender: &T::CrossAccountId,254		_property_keys: Vec<PropertyKey>,255	) -> DispatchResultWithPostInfo {256		fail!(<Error<T>>::SettingPropertiesNotAllowed)257	}258259	fn set_token_properties(260		&self,261		_sender: T::CrossAccountId,262		_token_id: TokenId,263		_property: Vec<Property>,264	) -> DispatchResultWithPostInfo {265		fail!(<Error<T>>::SettingPropertiesNotAllowed)266	}267268	fn set_property_permissions(269		&self,270		_sender: &T::CrossAccountId,271		_property_permissions: Vec<PropertyKeyPermission>,272	) -> DispatchResultWithPostInfo {273		fail!(<Error<T>>::SettingPropertiesNotAllowed)274	}275276	fn delete_token_properties(277		&self,278		_sender: T::CrossAccountId,279		_token_id: TokenId,280		_property_keys: Vec<PropertyKey>,281	) -> DispatchResultWithPostInfo {282		fail!(<Error<T>>::SettingPropertiesNotAllowed)283	}284285	fn check_nesting(286		&self,287		_sender: <T>::CrossAccountId,288		_from: (CollectionId, TokenId),289		_under: TokenId,290		_budget: &dyn Budget,291	) -> sp_runtime::DispatchResult {292		fail!(<Error<T>>::FungibleDisallowsNesting)293	}294295	fn collection_tokens(&self) -> Vec<TokenId> {296		vec![TokenId::default()]297	}298299	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {300		if <Balance<T>>::get((self.id, account)) != 0 {301			vec![TokenId::default()]302		} else {303			vec![]304		}305	}306307	fn token_exists(&self, token: TokenId) -> bool {308		token == TokenId::default()309	}310311	fn last_token_id(&self) -> TokenId {312		TokenId::default()313	}314315	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {316		None317	}318	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {319		Vec::new()320	}321322	fn token_properties(323		&self,324		_token_id: TokenId,325		_keys: Option<Vec<PropertyKey>>,326	) -> Vec<Property> {327		Vec::new()328	}329330	fn total_supply(&self) -> u32 {331		1332	}333334	fn account_balance(&self, account: T::CrossAccountId) -> u32 {335		if <Balance<T>>::get((self.id, account)) != 0 {336			1337		} else {338			0339		}340	}341342	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {343		if token != TokenId::default() {344			return 0;345		}346		<Balance<T>>::get((self.id, account))347	}348349	fn allowance(350		&self,351		sender: T::CrossAccountId,352		spender: T::CrossAccountId,353		token: TokenId,354	) -> u128 {355		if token != TokenId::default() {356			return 0;357		}358		<Allowance<T>>::get((self.id, sender, spender))359	}360}