1234567891011121314151617use 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 40 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 58 Weight::zero()59 }6061 fn delete_collection_properties(_amount: u32) -> Weight {62 63 Weight::zero()64 }6566 fn set_token_properties(_amount: u32) -> Weight {67 68 Weight::zero()69 }7071 fn delete_token_properties(_amount: u32) -> Weight {72 73 Weight::zero()74 }7576 fn set_token_property_permissions(_amount: u32) -> Weight {77 78 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 99 Self::burn_item() + T::DbWeight::get().reads(1)100 }101102 fn burn_recursively_breadth_raw(_amount: u32) -> Weight {103 104 Weight::zero()105 }106107 fn token_owner() -> Weight {108 Weight::zero()109 }110111 fn set_approval_for_all() -> Weight {112 Weight::zero()113 }114}115116117118impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {119 fn create_item(120 &self,121 sender: T::CrossAccountId,122 to: T::CrossAccountId,123 data: up_data_structs::CreateItemData,124 nesting_budget: &dyn Budget,125 ) -> DispatchResultWithPostInfo {126 match data {127 up_data_structs::CreateItemData::Fungible(data) => with_weight(128 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),129 <CommonWeights<T>>::create_item(),130 ),131 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),132 }133 }134135 fn create_multiple_items(136 &self,137 sender: T::CrossAccountId,138 to: T::CrossAccountId,139 data: Vec<up_data_structs::CreateItemData>,140 nesting_budget: &dyn Budget,141 ) -> DispatchResultWithPostInfo {142 let mut sum: u128 = 0;143 for data in data {144 match data {145 up_data_structs::CreateItemData::Fungible(data) => {146 sum = sum147 .checked_add(data.value)148 .ok_or(ArithmeticError::Overflow)?;149 }150 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),151 }152 }153154 with_weight(155 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),156 <CommonWeights<T>>::create_item(),157 )158 }159160 fn create_multiple_items_ex(161 &self,162 sender: <T>::CrossAccountId,163 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,164 nesting_budget: &dyn Budget,165 ) -> DispatchResultWithPostInfo {166 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);167 let data = match data {168 up_data_structs::CreateItemExData::Fungible(f) => f,169 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),170 };171172 with_weight(173 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),174 weight,175 )176 }177178 fn burn_item(179 &self,180 sender: T::CrossAccountId,181 token: TokenId,182 amount: u128,183 ) -> DispatchResultWithPostInfo {184 ensure!(185 token == TokenId::default(),186 <Error<T>>::FungibleItemsHaveNoId187 );188189 with_weight(190 <Pallet<T>>::burn(self, &sender, amount),191 <CommonWeights<T>>::burn_item(),192 )193 }194195 fn burn_item_recursively(196 &self,197 sender: T::CrossAccountId,198 token: TokenId,199 self_budget: &dyn Budget,200 _breadth_budget: &dyn Budget,201 ) -> DispatchResultWithPostInfo {202 203 ensure!(204 token == TokenId::default(),205 <Error<T>>::FungibleItemsHaveNoId206 );207 ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);208209 with_weight(210 <Pallet<T>>::burn(self, &sender, <Balance<T>>::get((self.id, &sender))),211 <CommonWeights<T>>::burn_recursively_self_raw(),212 )213 }214215 fn transfer(216 &self,217 from: T::CrossAccountId,218 to: T::CrossAccountId,219 token: TokenId,220 amount: u128,221 nesting_budget: &dyn Budget,222 ) -> DispatchResultWithPostInfo {223 ensure!(224 token == TokenId::default(),225 <Error<T>>::FungibleItemsHaveNoId226 );227228 with_weight(229 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),230 <CommonWeights<T>>::transfer(),231 )232 }233234 fn approve(235 &self,236 sender: T::CrossAccountId,237 spender: T::CrossAccountId,238 token: TokenId,239 amount: u128,240 ) -> DispatchResultWithPostInfo {241 ensure!(242 token == TokenId::default(),243 <Error<T>>::FungibleItemsHaveNoId244 );245246 with_weight(247 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),248 <CommonWeights<T>>::approve(),249 )250 }251252 fn transfer_from(253 &self,254 sender: T::CrossAccountId,255 from: T::CrossAccountId,256 to: T::CrossAccountId,257 token: TokenId,258 amount: u128,259 nesting_budget: &dyn Budget,260 ) -> DispatchResultWithPostInfo {261 ensure!(262 token == TokenId::default(),263 <Error<T>>::FungibleItemsHaveNoId264 );265266 with_weight(267 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),268 <CommonWeights<T>>::transfer_from(),269 )270 }271272 fn burn_from(273 &self,274 sender: T::CrossAccountId,275 from: T::CrossAccountId,276 token: TokenId,277 amount: u128,278 nesting_budget: &dyn Budget,279 ) -> DispatchResultWithPostInfo {280 ensure!(281 token == TokenId::default(),282 <Error<T>>::FungibleItemsHaveNoId283 );284285 with_weight(286 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),287 <CommonWeights<T>>::burn_from(),288 )289 }290291 fn set_collection_properties(292 &self,293 _sender: T::CrossAccountId,294 _property: Vec<Property>,295 ) -> DispatchResultWithPostInfo {296 fail!(<Error<T>>::SettingPropertiesNotAllowed)297 }298299 fn delete_collection_properties(300 &self,301 _sender: &T::CrossAccountId,302 _property_keys: Vec<PropertyKey>,303 ) -> DispatchResultWithPostInfo {304 fail!(<Error<T>>::SettingPropertiesNotAllowed)305 }306307 fn set_token_properties(308 &self,309 _sender: T::CrossAccountId,310 _token_id: TokenId,311 _property: Vec<Property>,312 _nesting_budget: &dyn Budget,313 ) -> DispatchResultWithPostInfo {314 fail!(<Error<T>>::SettingPropertiesNotAllowed)315 }316317 fn set_token_property_permissions(318 &self,319 _sender: &T::CrossAccountId,320 _property_permissions: Vec<PropertyKeyPermission>,321 ) -> DispatchResultWithPostInfo {322 fail!(<Error<T>>::SettingPropertiesNotAllowed)323 }324325 fn delete_token_properties(326 &self,327 _sender: T::CrossAccountId,328 _token_id: TokenId,329 _property_keys: Vec<PropertyKey>,330 _nesting_budget: &dyn Budget,331 ) -> DispatchResultWithPostInfo {332 fail!(<Error<T>>::SettingPropertiesNotAllowed)333 }334335 fn check_nesting(336 &self,337 _sender: <T>::CrossAccountId,338 _from: (CollectionId, TokenId),339 _under: TokenId,340 _nesting_budget: &dyn Budget,341 ) -> sp_runtime::DispatchResult {342 fail!(<Error<T>>::FungibleDisallowsNesting)343 }344345 fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}346347 fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}348349 fn collection_tokens(&self) -> Vec<TokenId> {350 vec![TokenId::default()]351 }352353 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {354 if <Balance<T>>::get((self.id, account)) != 0 {355 vec![TokenId::default()]356 } else {357 vec![]358 }359 }360361 fn token_exists(&self, token: TokenId) -> bool {362 token == TokenId::default()363 }364365 fn last_token_id(&self) -> TokenId {366 TokenId::default()367 }368369 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {370 None371 }372373 374 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {375 <Pallet<T>>::token_owners(self.id, token).unwrap_or_default()376 }377378 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {379 None380 }381382 fn token_properties(383 &self,384 _token_id: TokenId,385 _keys: Option<Vec<PropertyKey>>,386 ) -> Vec<Property> {387 Vec::new()388 }389390 fn total_supply(&self) -> u32 {391 1392 }393394 fn account_balance(&self, account: T::CrossAccountId) -> u32 {395 if <Balance<T>>::get((self.id, account)) != 0 {396 1397 } else {398 0399 }400 }401402 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {403 if token != TokenId::default() {404 return 0;405 }406 <Balance<T>>::get((self.id, account))407 }408409 fn allowance(410 &self,411 sender: T::CrossAccountId,412 spender: T::CrossAccountId,413 token: TokenId,414 ) -> u128 {415 if token != TokenId::default() {416 return 0;417 }418 <Allowance<T>>::get((self.id, sender, spender))419 }420421 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {422 None423 }424425 fn total_pieces(&self, token: TokenId) -> Option<u128> {426 if token != TokenId::default() {427 return None;428 }429 <TotalSupply<T>>::try_get(self.id).ok()430 }431432 fn set_approval_for_all(433 &self,434 _owner: T::CrossAccountId,435 _operator: T::CrossAccountId,436 _approve: bool,437 ) -> DispatchResultWithPostInfo {438 fail!(<Error<T>>::SettingApprovalForAllNotAllowed)439 }440441 fn is_approved_for_all(&self, _owner: T::CrossAccountId, _operator: T::CrossAccountId) -> bool {442 false443 }444}