1234567891011121314151617use core::marker::PhantomData;1819use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, traits::Get};20use up_data_structs::{21 TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData, TokenOwnerError,22};23use pallet_common::{24 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,25 weights::WeightInfo as _,26};27use pallet_structure::Error as StructureError;28use sp_runtime::ArithmeticError;29use sp_std::{vec::Vec, vec};30use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};3132use crate::{33 Allowance, TotalSupply, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf,34 weights::WeightInfo,35};3637pub struct CommonWeights<T: Config>(PhantomData<T>);38impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {39 fn create_item() -> Weight {40 <SelfWeightOf<T>>::create_item()41 }4243 fn create_multiple_items(_data: &[CreateItemData]) -> Weight {44 45 Self::create_item()46 }4748 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {49 match data {50 CreateItemExData::Fungible(f) => {51 <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)52 }53 _ => Weight::zero(),54 }55 }5657 fn burn_item() -> Weight {58 <SelfWeightOf<T>>::burn_item()59 }6061 fn set_collection_properties(amount: u32) -> Weight {62 <pallet_common::SelfWeightOf<T>>::set_collection_properties(amount)63 }6465 fn delete_collection_properties(amount: u32) -> Weight {66 <pallet_common::SelfWeightOf<T>>::delete_collection_properties(amount)67 }6869 fn set_token_properties(_amount: u32) -> Weight {70 71 Weight::zero()72 }7374 fn delete_token_properties(_amount: u32) -> Weight {75 76 Weight::zero()77 }7879 fn set_token_property_permissions(_amount: u32) -> Weight {80 81 Weight::zero()82 }8384 fn transfer() -> Weight {85 <SelfWeightOf<T>>::transfer()86 }8788 fn approve() -> Weight {89 <SelfWeightOf<T>>::approve()90 }9192 fn approve_from() -> Weight {93 <SelfWeightOf<T>>::approve_from()94 }9596 fn transfer_from() -> Weight {97 <SelfWeightOf<T>>::transfer_from()98 }99100 fn burn_from() -> Weight {101 <SelfWeightOf<T>>::burn_from()102 }103104 fn burn_recursively_self_raw() -> Weight {105 106 Self::burn_item() + T::DbWeight::get().reads(1)107 }108109 fn burn_recursively_breadth_raw(_amount: u32) -> Weight {110 111 Weight::zero()112 }113114 fn token_owner() -> Weight {115 Weight::zero()116 }117118 fn set_allowance_for_all() -> Weight {119 Weight::zero()120 }121122 fn force_repair_item() -> Weight {123 Weight::zero()124 }125}126127128129impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {130 fn create_item(131 &self,132 sender: T::CrossAccountId,133 to: T::CrossAccountId,134 data: up_data_structs::CreateItemData,135 nesting_budget: &dyn Budget,136 ) -> DispatchResultWithPostInfo {137 match data {138 up_data_structs::CreateItemData::Fungible(data) => with_weight(139 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),140 <CommonWeights<T>>::create_item(),141 ),142 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),143 }144 }145146 fn create_multiple_items(147 &self,148 sender: T::CrossAccountId,149 to: T::CrossAccountId,150 data: Vec<up_data_structs::CreateItemData>,151 nesting_budget: &dyn Budget,152 ) -> DispatchResultWithPostInfo {153 let mut sum: u128 = 0;154 for data in data {155 match data {156 up_data_structs::CreateItemData::Fungible(data) => {157 sum = sum158 .checked_add(data.value)159 .ok_or(ArithmeticError::Overflow)?;160 }161 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),162 }163 }164165 with_weight(166 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),167 <CommonWeights<T>>::create_item(),168 )169 }170171 fn create_multiple_items_ex(172 &self,173 sender: <T>::CrossAccountId,174 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,175 nesting_budget: &dyn Budget,176 ) -> DispatchResultWithPostInfo {177 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);178 let data = match data {179 up_data_structs::CreateItemExData::Fungible(f) => f,180 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),181 };182183 with_weight(184 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),185 weight,186 )187 }188189 fn burn_item(190 &self,191 sender: T::CrossAccountId,192 token: TokenId,193 amount: u128,194 ) -> DispatchResultWithPostInfo {195 ensure!(196 token == TokenId::default(),197 <Error<T>>::FungibleItemsHaveNoId198 );199200 with_weight(201 <Pallet<T>>::burn(self, &sender, amount),202 <CommonWeights<T>>::burn_item(),203 )204 }205206 fn burn_item_recursively(207 &self,208 sender: T::CrossAccountId,209 token: TokenId,210 self_budget: &dyn Budget,211 _breadth_budget: &dyn Budget,212 ) -> DispatchResultWithPostInfo {213 214 ensure!(215 token == TokenId::default(),216 <Error<T>>::FungibleItemsHaveNoId217 );218 ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);219220 with_weight(221 <Pallet<T>>::burn(self, &sender, <Balance<T>>::get((self.id, &sender))),222 <CommonWeights<T>>::burn_recursively_self_raw(),223 )224 }225226 fn transfer(227 &self,228 from: T::CrossAccountId,229 to: T::CrossAccountId,230 token: TokenId,231 amount: u128,232 nesting_budget: &dyn Budget,233 ) -> DispatchResultWithPostInfo {234 ensure!(235 token == TokenId::default(),236 <Error<T>>::FungibleItemsHaveNoId237 );238239 with_weight(240 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),241 <CommonWeights<T>>::transfer(),242 )243 }244245 fn approve(246 &self,247 sender: T::CrossAccountId,248 spender: T::CrossAccountId,249 token: TokenId,250 amount: u128,251 ) -> DispatchResultWithPostInfo {252 ensure!(253 token == TokenId::default(),254 <Error<T>>::FungibleItemsHaveNoId255 );256257 with_weight(258 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),259 <CommonWeights<T>>::approve(),260 )261 }262263 fn approve_from(264 &self,265 sender: T::CrossAccountId,266 from: T::CrossAccountId,267 to: T::CrossAccountId,268 token: TokenId,269 amount: u128,270 ) -> DispatchResultWithPostInfo {271 ensure!(272 token == TokenId::default(),273 <Error<T>>::FungibleItemsHaveNoId274 );275276 with_weight(277 <Pallet<T>>::set_allowance_from(self, &sender, &from, &to, amount),278 <CommonWeights<T>>::approve_from(),279 )280 }281282 fn transfer_from(283 &self,284 sender: T::CrossAccountId,285 from: T::CrossAccountId,286 to: T::CrossAccountId,287 token: TokenId,288 amount: u128,289 nesting_budget: &dyn Budget,290 ) -> DispatchResultWithPostInfo {291 ensure!(292 token == TokenId::default(),293 <Error<T>>::FungibleItemsHaveNoId294 );295296 with_weight(297 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),298 <CommonWeights<T>>::transfer_from(),299 )300 }301302 fn burn_from(303 &self,304 sender: T::CrossAccountId,305 from: T::CrossAccountId,306 token: TokenId,307 amount: u128,308 nesting_budget: &dyn Budget,309 ) -> DispatchResultWithPostInfo {310 ensure!(311 token == TokenId::default(),312 <Error<T>>::FungibleItemsHaveNoId313 );314315 with_weight(316 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),317 <CommonWeights<T>>::burn_from(),318 )319 }320321 fn set_collection_properties(322 &self,323 sender: T::CrossAccountId,324 properties: Vec<Property>,325 ) -> DispatchResultWithPostInfo {326 let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);327328 with_weight(329 <Pallet<T>>::set_collection_properties(self, &sender, properties),330 weight,331 )332 }333334 fn delete_collection_properties(335 &self,336 sender: &T::CrossAccountId,337 property_keys: Vec<PropertyKey>,338 ) -> DispatchResultWithPostInfo {339 let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);340341 with_weight(342 <Pallet<T>>::delete_collection_properties(self, sender, property_keys),343 weight,344 )345 }346347 fn set_token_properties(348 &self,349 _sender: T::CrossAccountId,350 _token_id: TokenId,351 _property: Vec<Property>,352 _nesting_budget: &dyn Budget,353 ) -> DispatchResultWithPostInfo {354 fail!(<Error<T>>::SettingPropertiesNotAllowed)355 }356357 fn set_token_property_permissions(358 &self,359 _sender: &T::CrossAccountId,360 _property_permissions: Vec<PropertyKeyPermission>,361 ) -> DispatchResultWithPostInfo {362 fail!(<Error<T>>::SettingPropertiesNotAllowed)363 }364365 fn delete_token_properties(366 &self,367 _sender: T::CrossAccountId,368 _token_id: TokenId,369 _property_keys: Vec<PropertyKey>,370 _nesting_budget: &dyn Budget,371 ) -> DispatchResultWithPostInfo {372 fail!(<Error<T>>::SettingPropertiesNotAllowed)373 }374375 fn check_nesting(376 &self,377 _sender: <T>::CrossAccountId,378 _from: (CollectionId, TokenId),379 _under: TokenId,380 _nesting_budget: &dyn Budget,381 ) -> sp_runtime::DispatchResult {382 fail!(<Error<T>>::FungibleDisallowsNesting)383 }384385 fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}386387 fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}388389 fn collection_tokens(&self) -> Vec<TokenId> {390 vec![TokenId::default()]391 }392393 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {394 if <Balance<T>>::get((self.id, account)) != 0 {395 vec![TokenId::default()]396 } else {397 vec![]398 }399 }400401 fn token_exists(&self, token: TokenId) -> bool {402 token == TokenId::default()403 }404405 fn last_token_id(&self) -> TokenId {406 TokenId::default()407 }408409 fn token_owner(&self, _token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError> {410 Err(TokenOwnerError::MultipleOwners)411 }412413 414 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {415 <Pallet<T>>::token_owners(self.id, token).unwrap_or_default()416 }417418 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {419 None420 }421422 fn token_properties(423 &self,424 _token_id: TokenId,425 _keys: Option<Vec<PropertyKey>>,426 ) -> Vec<Property> {427 Vec::new()428 }429430 fn total_supply(&self) -> u32 {431 1432 }433434 fn account_balance(&self, account: T::CrossAccountId) -> u32 {435 if <Balance<T>>::get((self.id, account)) != 0 {436 1437 } else {438 0439 }440 }441442 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {443 if token != TokenId::default() {444 return 0;445 }446 <Balance<T>>::get((self.id, account))447 }448449 fn allowance(450 &self,451 sender: T::CrossAccountId,452 spender: T::CrossAccountId,453 token: TokenId,454 ) -> u128 {455 if token != TokenId::default() {456 return 0;457 }458 <Allowance<T>>::get((self.id, sender, spender))459 }460461 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {462 None463 }464465 fn total_pieces(&self, token: TokenId) -> Option<u128> {466 if token != TokenId::default() {467 return None;468 }469 <TotalSupply<T>>::try_get(self.id).ok()470 }471472 fn set_allowance_for_all(473 &self,474 _owner: T::CrossAccountId,475 _operator: T::CrossAccountId,476 _approve: bool,477 ) -> DispatchResultWithPostInfo {478 fail!(<Error<T>>::SettingAllowanceForAllNotAllowed)479 }480481 fn allowance_for_all(&self, _owner: T::CrossAccountId, _operator: T::CrossAccountId) -> bool {482 false483 }484485 486 fn repair_item(&self, _token: TokenId) -> DispatchResultWithPostInfo {487 fail!(<Error<T>>::FungibleTokensAreAlwaysValid)488 }489}