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

difftreelog

source

pallets/fungible/src/common.rs10.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, traits::Get};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight};22use pallet_structure::Error as StructureError;23use sp_runtime::ArithmeticError;24use sp_std::{vec::Vec, vec};25use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};2627use crate::{28	Allowance, TotalSupply, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf,29	weights::WeightInfo,30};3132pub struct CommonWeights<T: Config>(PhantomData<T>);33impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {34	fn create_item() -> Weight {35		<SelfWeightOf<T>>::create_item()36	}3738	fn create_multiple_items(_data: &[CreateItemData]) -> Weight {39		// All items minted for the same user, so it works same as create_item40		Self::create_item()41	}4243	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {44		match data {45			CreateItemExData::Fungible(f) => {46				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)47			}48			_ => Weight::zero(),49		}50	}5152	fn burn_item() -> Weight {53		<SelfWeightOf<T>>::burn_item()54	}5556	fn set_collection_properties(_amount: u32) -> Weight {57		// Error58		Weight::zero()59	}6061	fn delete_collection_properties(_amount: u32) -> Weight {62		// Error63		Weight::zero()64	}6566	fn set_token_properties(_amount: u32) -> Weight {67		// Error68		Weight::zero()69	}7071	fn delete_token_properties(_amount: u32) -> Weight {72		// Error73		Weight::zero()74	}7576	fn set_token_property_permissions(_amount: u32) -> Weight {77		// Error78		Weight::zero()79	}8081	fn transfer() -> Weight {82		<SelfWeightOf<T>>::transfer()83	}8485	fn approve() -> Weight {86		<SelfWeightOf<T>>::approve()87	}8889	fn transfer_from() -> Weight {90		<SelfWeightOf<T>>::transfer_from()91	}9293	fn burn_from() -> Weight {94		<SelfWeightOf<T>>::burn_from()95	}9697	fn burn_recursively_self_raw() -> Weight {98		// Read to get total balance99		Self::burn_item() + T::DbWeight::get().reads(1)100	}101102	fn burn_recursively_breadth_raw(_amount: u32) -> Weight {103		// Fungible tokens can't have children104		Weight::zero()105	}106107	fn token_owner() -> Weight {108		Weight::zero()109	}110111	fn set_allowance_for_all() -> Weight {112		Weight::zero()113	}114115	fn repair_item() -> Weight {116		Weight::zero()117	}118}119120/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete121/// methods and adds weight info.122impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {123	fn create_item(124		&self,125		sender: T::CrossAccountId,126		to: T::CrossAccountId,127		data: up_data_structs::CreateItemData,128		nesting_budget: &dyn Budget,129	) -> DispatchResultWithPostInfo {130		match data {131			up_data_structs::CreateItemData::Fungible(data) => with_weight(132				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),133				<CommonWeights<T>>::create_item(),134			),135			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),136		}137	}138139	fn create_multiple_items(140		&self,141		sender: T::CrossAccountId,142		to: T::CrossAccountId,143		data: Vec<up_data_structs::CreateItemData>,144		nesting_budget: &dyn Budget,145	) -> DispatchResultWithPostInfo {146		let mut sum: u128 = 0;147		for data in data {148			match data {149				up_data_structs::CreateItemData::Fungible(data) => {150					sum = sum151						.checked_add(data.value)152						.ok_or(ArithmeticError::Overflow)?;153				}154				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),155			}156		}157158		with_weight(159			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),160			<CommonWeights<T>>::create_item(),161		)162	}163164	fn create_multiple_items_ex(165		&self,166		sender: <T>::CrossAccountId,167		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,168		nesting_budget: &dyn Budget,169	) -> DispatchResultWithPostInfo {170		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);171		let data = match data {172			up_data_structs::CreateItemExData::Fungible(f) => f,173			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),174		};175176		with_weight(177			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),178			weight,179		)180	}181182	fn burn_item(183		&self,184		sender: T::CrossAccountId,185		token: TokenId,186		amount: u128,187	) -> DispatchResultWithPostInfo {188		ensure!(189			token == TokenId::default(),190			<Error<T>>::FungibleItemsHaveNoId191		);192193		with_weight(194			<Pallet<T>>::burn(self, &sender, amount),195			<CommonWeights<T>>::burn_item(),196		)197	}198199	fn burn_item_recursively(200		&self,201		sender: T::CrossAccountId,202		token: TokenId,203		self_budget: &dyn Budget,204		_breadth_budget: &dyn Budget,205	) -> DispatchResultWithPostInfo {206		// Should not happen?207		ensure!(208			token == TokenId::default(),209			<Error<T>>::FungibleItemsHaveNoId210		);211		ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);212213		with_weight(214			<Pallet<T>>::burn(self, &sender, <Balance<T>>::get((self.id, &sender))),215			<CommonWeights<T>>::burn_recursively_self_raw(),216		)217	}218219	fn transfer(220		&self,221		from: T::CrossAccountId,222		to: T::CrossAccountId,223		token: TokenId,224		amount: u128,225		nesting_budget: &dyn Budget,226	) -> DispatchResultWithPostInfo {227		ensure!(228			token == TokenId::default(),229			<Error<T>>::FungibleItemsHaveNoId230		);231232		with_weight(233			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),234			<CommonWeights<T>>::transfer(),235		)236	}237238	fn approve(239		&self,240		sender: T::CrossAccountId,241		spender: T::CrossAccountId,242		token: TokenId,243		amount: u128,244	) -> DispatchResultWithPostInfo {245		ensure!(246			token == TokenId::default(),247			<Error<T>>::FungibleItemsHaveNoId248		);249250		with_weight(251			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),252			<CommonWeights<T>>::approve(),253		)254	}255256	fn transfer_from(257		&self,258		sender: T::CrossAccountId,259		from: T::CrossAccountId,260		to: T::CrossAccountId,261		token: TokenId,262		amount: u128,263		nesting_budget: &dyn Budget,264	) -> DispatchResultWithPostInfo {265		ensure!(266			token == TokenId::default(),267			<Error<T>>::FungibleItemsHaveNoId268		);269270		with_weight(271			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),272			<CommonWeights<T>>::transfer_from(),273		)274	}275276	fn burn_from(277		&self,278		sender: T::CrossAccountId,279		from: T::CrossAccountId,280		token: TokenId,281		amount: u128,282		nesting_budget: &dyn Budget,283	) -> DispatchResultWithPostInfo {284		ensure!(285			token == TokenId::default(),286			<Error<T>>::FungibleItemsHaveNoId287		);288289		with_weight(290			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),291			<CommonWeights<T>>::burn_from(),292		)293	}294295	fn set_collection_properties(296		&self,297		_sender: T::CrossAccountId,298		_property: Vec<Property>,299	) -> DispatchResultWithPostInfo {300		fail!(<Error<T>>::SettingPropertiesNotAllowed)301	}302303	fn delete_collection_properties(304		&self,305		_sender: &T::CrossAccountId,306		_property_keys: Vec<PropertyKey>,307	) -> DispatchResultWithPostInfo {308		fail!(<Error<T>>::SettingPropertiesNotAllowed)309	}310311	fn set_token_properties(312		&self,313		_sender: T::CrossAccountId,314		_token_id: TokenId,315		_property: Vec<Property>,316		_nesting_budget: &dyn Budget,317	) -> DispatchResultWithPostInfo {318		fail!(<Error<T>>::SettingPropertiesNotAllowed)319	}320321	fn set_token_property_permissions(322		&self,323		_sender: &T::CrossAccountId,324		_property_permissions: Vec<PropertyKeyPermission>,325	) -> DispatchResultWithPostInfo {326		fail!(<Error<T>>::SettingPropertiesNotAllowed)327	}328329	fn delete_token_properties(330		&self,331		_sender: T::CrossAccountId,332		_token_id: TokenId,333		_property_keys: Vec<PropertyKey>,334		_nesting_budget: &dyn Budget,335	) -> DispatchResultWithPostInfo {336		fail!(<Error<T>>::SettingPropertiesNotAllowed)337	}338339	fn check_nesting(340		&self,341		_sender: <T>::CrossAccountId,342		_from: (CollectionId, TokenId),343		_under: TokenId,344		_nesting_budget: &dyn Budget,345	) -> sp_runtime::DispatchResult {346		fail!(<Error<T>>::FungibleDisallowsNesting)347	}348349	fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}350351	fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}352353	fn collection_tokens(&self) -> Vec<TokenId> {354		vec![TokenId::default()]355	}356357	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {358		if <Balance<T>>::get((self.id, account)) != 0 {359			vec![TokenId::default()]360		} else {361			vec![]362		}363	}364365	fn token_exists(&self, token: TokenId) -> bool {366		token == TokenId::default()367	}368369	fn last_token_id(&self) -> TokenId {370		TokenId::default()371	}372373	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {374		None375	}376377	/// Returns 10 tokens owners in no particular order.378	fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {379		<Pallet<T>>::token_owners(self.id, token).unwrap_or_default()380	}381382	fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {383		None384	}385386	fn token_properties(387		&self,388		_token_id: TokenId,389		_keys: Option<Vec<PropertyKey>>,390	) -> Vec<Property> {391		Vec::new()392	}393394	fn total_supply(&self) -> u32 {395		1396	}397398	fn account_balance(&self, account: T::CrossAccountId) -> u32 {399		if <Balance<T>>::get((self.id, account)) != 0 {400			1401		} else {402			0403		}404	}405406	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {407		if token != TokenId::default() {408			return 0;409		}410		<Balance<T>>::get((self.id, account))411	}412413	fn allowance(414		&self,415		sender: T::CrossAccountId,416		spender: T::CrossAccountId,417		token: TokenId,418	) -> u128 {419		if token != TokenId::default() {420			return 0;421		}422		<Allowance<T>>::get((self.id, sender, spender))423	}424425	fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {426		None427	}428429	fn total_pieces(&self, token: TokenId) -> Option<u128> {430		if token != TokenId::default() {431			return None;432		}433		<TotalSupply<T>>::try_get(self.id).ok()434	}435436	fn set_allowance_for_all(437		&self,438		_owner: T::CrossAccountId,439		_operator: T::CrossAccountId,440		_approve: bool,441	) -> DispatchResultWithPostInfo {442		fail!(<Error<T>>::SettingAllowanceForAllNotAllowed)443	}444445	fn allowance_for_all(&self, _owner: T::CrossAccountId, _operator: T::CrossAccountId) -> bool {446		false447	}448449	/// Repairs a possibly broken item.450	fn repair_item(&self, _token: TokenId) -> DispatchResultWithPostInfo {451		fail!(<Error<T>>::FungibleTokensAreAlwaysValid)452	}453}