difftreelog
misk: change add -> saturating_add
in: master
1 file changed
pallets/refungible/src/common.rsdiffbeforeafterboth1// 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 sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, traits::Get};21use up_data_structs::{22 CollectionId, TokenId, CreateItemExData, CreateRefungibleExData, budget::Budget, Property,23 PropertyKey, PropertyValue, PropertyKeyPermission, CreateItemData, CollectionPropertiesVec,24};25use pallet_common::{26 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,27 weights::WeightInfo as _,28};29use pallet_structure::Error as StructureError;30use sp_runtime::{DispatchError};31use sp_std::{vec::Vec, vec};3233use crate::{34 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,35 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,36};3738macro_rules! max_weight_of {39 ($($method:ident ($($args:tt)*)),*) => {40 041 $(42 .max(<SelfWeightOf<T>>::$method($($args)*))43 )*44 };45}4647fn properties_weight<T: Config>(properties: &CollectionPropertiesVec) -> u64 {48 if properties.len() > 0 {49 <CommonWeights<T>>::set_token_properties(properties.len() as u32)50 } else {51 052 }53}5455pub struct CommonWeights<T: Config>(PhantomData<T>);56impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {57 fn create_item() -> Weight {58 <SelfWeightOf<T>>::create_item()59 }6061 fn create_multiple_items(data: &[CreateItemData]) -> Weight {62 <SelfWeightOf<T>>::create_multiple_items(data.len() as u32)63 + data64 .iter()65 .map(|data| match data {66 CreateItemData::ReFungible(rft_data) => {67 properties_weight::<T>(&rft_data.properties)68 }69 _ => 0,70 })71 .sum::<u64>()72 }7374 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {75 match call {76 CreateItemExData::RefungibleMultipleOwners(i) => {77 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)78 + properties_weight::<T>(&i.properties)79 }80 CreateItemExData::RefungibleMultipleItems(i) => {81 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)82 + i.iter()83 .map(|d| properties_weight::<T>(&d.properties))84 .sum::<u64>()85 }86 _ => 0,87 }88 }8990 fn burn_item() -> Weight {91 max_weight_of!(burn_item_partial(), burn_item_fully())92 }9394 fn set_collection_properties(amount: u32) -> Weight {95 <pallet_common::SelfWeightOf<T>>::set_collection_properties(amount)96 }9798 fn delete_collection_properties(amount: u32) -> Weight {99 <pallet_common::SelfWeightOf<T>>::delete_collection_properties(amount)100 }101102 fn set_token_properties(amount: u32) -> Weight {103 <SelfWeightOf<T>>::set_token_properties(amount)104 }105106 fn delete_token_properties(amount: u32) -> Weight {107 <SelfWeightOf<T>>::delete_token_properties(amount)108 }109110 fn set_token_property_permissions(amount: u32) -> Weight {111 <SelfWeightOf<T>>::set_token_property_permissions(amount)112 }113114 fn transfer() -> Weight {115 max_weight_of!(116 transfer_normal(),117 transfer_creating(),118 transfer_removing(),119 transfer_creating_removing()120 )121 }122123 fn approve() -> Weight {124 <SelfWeightOf<T>>::approve()125 }126127 fn transfer_from() -> Weight {128 max_weight_of!(129 transfer_from_normal(),130 transfer_from_creating(),131 transfer_from_removing(),132 transfer_from_creating_removing()133 )134 }135136 fn burn_from() -> Weight {137 <SelfWeightOf<T>>::burn_from()138 }139140 fn burn_recursively_self_raw() -> Weight {141 // Read to get total balance142 Self::burn_item() + T::DbWeight::get().reads(1)143 }144 fn burn_recursively_breadth_raw(_amount: u32) -> Weight {145 // Refungible token can't have children146 0147 }148}149150fn map_create_data<T: Config>(151 data: up_data_structs::CreateItemData,152 to: &T::CrossAccountId,153) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {154 match data {155 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {156 const_data: data.const_data,157 users: {158 let mut out = BTreeMap::new();159 out.insert(to.clone(), data.pieces);160 out.try_into().expect("limit > 0")161 },162 properties: data.properties,163 }),164 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),165 }166}167168/// Implementation of `CommonCollectionOperations` for `RefungibleHandle`. It wraps Refungible Pallete169/// methods and adds weight info.170impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {171 fn create_item(172 &self,173 sender: T::CrossAccountId,174 to: T::CrossAccountId,175 data: up_data_structs::CreateItemData,176 nesting_budget: &dyn Budget,177 ) -> DispatchResultWithPostInfo {178 with_weight(179 <Pallet<T>>::create_item(180 self,181 &sender,182 map_create_data::<T>(data, &to)?,183 nesting_budget,184 ),185 <CommonWeights<T>>::create_item(),186 )187 }188189 fn create_multiple_items(190 &self,191 sender: T::CrossAccountId,192 to: T::CrossAccountId,193 data: Vec<up_data_structs::CreateItemData>,194 nesting_budget: &dyn Budget,195 ) -> DispatchResultWithPostInfo {196 let weight = <CommonWeights<T>>::create_multiple_items(&data);197 let data = data198 .into_iter()199 .map(|d| map_create_data::<T>(d, &to))200 .collect::<Result<Vec<_>, DispatchError>>()?;201202 with_weight(203 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),204 weight,205 )206 }207208 fn create_multiple_items_ex(209 &self,210 sender: <T>::CrossAccountId,211 data: CreateItemExData<T::CrossAccountId>,212 nesting_budget: &dyn Budget,213 ) -> DispatchResultWithPostInfo {214 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);215 let data = match data {216 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],217 CreateItemExData::RefungibleMultipleItems(r)218 if r.iter().all(|i| i.users.len() == 1) =>219 {220 r.into_inner()221 }222 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),223 };224225 with_weight(226 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),227 weight,228 )229 }230231 fn burn_item(232 &self,233 sender: T::CrossAccountId,234 token: TokenId,235 amount: u128,236 ) -> DispatchResultWithPostInfo {237 with_weight(238 <Pallet<T>>::burn(self, &sender, token, amount),239 <CommonWeights<T>>::burn_item(),240 )241 }242243 fn burn_item_recursively(244 &self,245 sender: T::CrossAccountId,246 token: TokenId,247 self_budget: &dyn Budget,248 _breadth_budget: &dyn Budget,249 ) -> DispatchResultWithPostInfo {250 ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);251 with_weight(252 <Pallet<T>>::burn(253 self,254 &sender,255 token,256 <Balance<T>>::get((self.id, token, &sender)),257 ),258 <CommonWeights<T>>::burn_recursively_self_raw(),259 )260 }261262 fn transfer(263 &self,264 from: T::CrossAccountId,265 to: T::CrossAccountId,266 token: TokenId,267 amount: u128,268 nesting_budget: &dyn Budget,269 ) -> DispatchResultWithPostInfo {270 with_weight(271 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),272 <CommonWeights<T>>::transfer(),273 )274 }275276 fn approve(277 &self,278 sender: T::CrossAccountId,279 spender: T::CrossAccountId,280 token: TokenId,281 amount: u128,282 ) -> DispatchResultWithPostInfo {283 with_weight(284 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),285 <CommonWeights<T>>::approve(),286 )287 }288289 fn transfer_from(290 &self,291 sender: T::CrossAccountId,292 from: T::CrossAccountId,293 to: T::CrossAccountId,294 token: TokenId,295 amount: u128,296 nesting_budget: &dyn Budget,297 ) -> DispatchResultWithPostInfo {298 with_weight(299 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),300 <CommonWeights<T>>::transfer_from(),301 )302 }303304 fn burn_from(305 &self,306 sender: T::CrossAccountId,307 from: T::CrossAccountId,308 token: TokenId,309 amount: u128,310 nesting_budget: &dyn Budget,311 ) -> DispatchResultWithPostInfo {312 with_weight(313 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),314 <CommonWeights<T>>::burn_from(),315 )316 }317318 fn set_collection_properties(319 &self,320 sender: T::CrossAccountId,321 properties: Vec<Property>,322 ) -> DispatchResultWithPostInfo {323 let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);324325 with_weight(326 <Pallet<T>>::set_collection_properties(self, &sender, properties),327 weight,328 )329 }330331 fn delete_collection_properties(332 &self,333 sender: &T::CrossAccountId,334 property_keys: Vec<PropertyKey>,335 ) -> DispatchResultWithPostInfo {336 let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);337338 with_weight(339 <Pallet<T>>::delete_collection_properties(self, sender, property_keys),340 weight,341 )342 }343344 fn set_token_properties(345 &self,346 sender: T::CrossAccountId,347 token_id: TokenId,348 properties: Vec<Property>,349 nesting_budget: &dyn Budget,350 ) -> DispatchResultWithPostInfo {351 let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);352353 with_weight(354 <Pallet<T>>::set_token_properties(355 self,356 &sender,357 token_id,358 properties.into_iter(),359 false,360 nesting_budget,361 ),362 weight,363 )364 }365366 fn set_token_property_permissions(367 &self,368 sender: &T::CrossAccountId,369 property_permissions: Vec<PropertyKeyPermission>,370 ) -> DispatchResultWithPostInfo {371 let weight =372 <CommonWeights<T>>::set_token_property_permissions(property_permissions.len() as u32);373374 with_weight(375 <Pallet<T>>::set_token_property_permissions(self, sender, property_permissions),376 weight,377 )378 }379380 fn delete_token_properties(381 &self,382 sender: T::CrossAccountId,383 token_id: TokenId,384 property_keys: Vec<PropertyKey>,385 nesting_budget: &dyn Budget,386 ) -> DispatchResultWithPostInfo {387 let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);388389 with_weight(390 <Pallet<T>>::delete_token_properties(391 self,392 &sender,393 token_id,394 property_keys.into_iter(),395 nesting_budget,396 ),397 weight,398 )399 }400401 fn check_nesting(402 &self,403 _sender: <T>::CrossAccountId,404 _from: (CollectionId, TokenId),405 _under: TokenId,406 _nesting_budget: &dyn Budget,407 ) -> sp_runtime::DispatchResult {408 fail!(<Error<T>>::RefungibleDisallowsNesting)409 }410411 fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}412413 fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}414415 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {416 <Owned<T>>::iter_prefix((self.id, account))417 .map(|(id, _)| id)418 .collect()419 }420421 fn collection_tokens(&self) -> Vec<TokenId> {422 <TokenData<T>>::iter_prefix((self.id,))423 .map(|(id, _)| id)424 .collect()425 }426427 fn token_exists(&self, token: TokenId) -> bool {428 <Pallet<T>>::token_exists(self, token)429 }430431 fn last_token_id(&self) -> TokenId {432 TokenId(<TokensMinted<T>>::get(self.id))433 }434435 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {436 <Pallet<T>>::token_owner(self.id, token)437 }438439 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {440 None441 }442443 fn token_properties(444 &self,445 _token_id: TokenId,446 _keys: Option<Vec<PropertyKey>>,447 ) -> Vec<Property> {448 Vec::new()449 }450451 fn total_supply(&self) -> u32 {452 <Pallet<T>>::total_supply(self)453 }454455 fn account_balance(&self, account: T::CrossAccountId) -> u32 {456 <AccountBalance<T>>::get((self.id, account))457 }458459 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {460 <Balance<T>>::get((self.id, token, account))461 }462463 fn allowance(464 &self,465 sender: T::CrossAccountId,466 spender: T::CrossAccountId,467 token: TokenId,468 ) -> u128 {469 <Allowance<T>>::get((self.id, token, sender, spender))470 }471472 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {473 Some(self)474 }475476 fn total_pieces(&self, token: TokenId) -> Option<u128> {477 <Pallet<T>>::total_pieces(self.id, token)478 }479}480481impl<T: Config> RefungibleExtensions<T> for RefungibleHandle<T> {482 fn repartition(483 &self,484 owner: &T::CrossAccountId,485 token: TokenId,486 amount: u128,487 ) -> DispatchResultWithPostInfo {488 with_weight(489 <Pallet<T>>::repartition(self, owner, token, amount),490 <SelfWeightOf<T>>::repartition_item(),491 )492 }493}