1234567891011121314151617use 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, budget::Budget, Property, PropertyKey, PropertyValue,23 PropertyKeyPermission, CollectionPropertiesVec, CreateRefungibleExMultipleOwners,24 CreateRefungibleExSingleOwner,25};26use pallet_common::{27 CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,28 weights::WeightInfo as _,29};30use pallet_structure::Error as StructureError;31use sp_runtime::{DispatchError};32use sp_std::{vec::Vec, vec};3334use crate::{35 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,36 SelfWeightOf, weights::WeightInfo, TokensMinted, TotalSupply, CreateItemData,37};3839macro_rules! max_weight_of {40 ($($method:ident ($($args:tt)*)),*) => {41 Weight::zero()42 $(43 .max(<SelfWeightOf<T>>::$method($($args)*))44 )*45 };46}4748fn properties_weight<T: Config>(properties: &CollectionPropertiesVec) -> Weight {49 if properties.len() > 0 {50 <CommonWeights<T>>::set_token_properties(properties.len() as u32)51 } else {52 Weight::zero()53 }54}5556pub struct CommonWeights<T: Config>(PhantomData<T>);57impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {58 fn create_item() -> Weight {59 <SelfWeightOf<T>>::create_item()60 }6162 fn create_multiple_items(data: &[up_data_structs::CreateItemData]) -> Weight {63 <SelfWeightOf<T>>::create_multiple_items(data.len() as u32).saturating_add(64 data.iter()65 .map(|data| match data {66 up_data_structs::CreateItemData::ReFungible(rft_data) => {67 properties_weight::<T>(&rft_data.properties)68 }69 _ => Weight::zero(),70 })71 .fold(Weight::zero(), |a, b| a.saturating_add(b)),72 )73 }7475 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {76 match call {77 CreateItemExData::RefungibleMultipleOwners(i) => {78 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)79 .saturating_add(properties_weight::<T>(&i.properties))80 }81 CreateItemExData::RefungibleMultipleItems(i) => {82 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)83 .saturating_add(84 i.iter()85 .map(|d| properties_weight::<T>(&d.properties))86 .fold(Weight::zero(), |a, b| a.saturating_add(b)),87 )88 }89 _ => Weight::zero(),90 }91 }9293 fn burn_item() -> Weight {94 max_weight_of!(burn_item_partial(), burn_item_fully())95 }9697 fn set_collection_properties(amount: u32) -> Weight {98 <pallet_common::SelfWeightOf<T>>::set_collection_properties(amount)99 }100101 fn delete_collection_properties(amount: u32) -> Weight {102 <pallet_common::SelfWeightOf<T>>::delete_collection_properties(amount)103 }104105 fn set_token_properties(amount: u32) -> Weight {106 <SelfWeightOf<T>>::set_token_properties(amount)107 }108109 fn delete_token_properties(amount: u32) -> Weight {110 <SelfWeightOf<T>>::delete_token_properties(amount)111 }112113 fn set_token_property_permissions(amount: u32) -> Weight {114 <SelfWeightOf<T>>::set_token_property_permissions(amount)115 }116117 fn transfer() -> Weight {118 max_weight_of!(119 transfer_normal(),120 transfer_creating(),121 transfer_removing(),122 transfer_creating_removing()123 )124 }125126 fn approve() -> Weight {127 <SelfWeightOf<T>>::approve()128 }129130 fn approve_from() -> Weight {131 <SelfWeightOf<T>>::approve_from()132 }133134 fn transfer_from() -> Weight {135 max_weight_of!(136 transfer_from_normal(),137 transfer_from_creating(),138 transfer_from_removing(),139 transfer_from_creating_removing()140 )141 }142143 fn burn_from() -> Weight {144 <SelfWeightOf<T>>::burn_from()145 }146147 fn burn_recursively_self_raw() -> Weight {148 149 Self::burn_item() + T::DbWeight::get().reads(1)150 }151 fn burn_recursively_breadth_raw(_amount: u32) -> Weight {152 153 Weight::zero()154 }155156 fn token_owner() -> Weight {157 <SelfWeightOf<T>>::token_owner()158 }159160 fn set_allowance_for_all() -> Weight {161 <SelfWeightOf<T>>::set_allowance_for_all()162 }163164 fn force_repair_item() -> Weight {165 <SelfWeightOf<T>>::repair_item()166 }167}168169fn map_create_data<T: Config>(170 data: up_data_structs::CreateItemData,171 to: &T::CrossAccountId,172) -> Result<CreateItemData<T>, DispatchError> {173 match data {174 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData::<T> {175 users: {176 let mut out = BTreeMap::new();177 out.insert(to.clone(), data.pieces);178 out.try_into().expect("limit > 0")179 },180 properties: data.properties,181 }),182 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),183 }184}185186187188impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {189 fn mode(&self) -> up_data_structs::CollectionMode {190 self.0.mode()191 }192193 fn create_item(194 &self,195 sender: T::CrossAccountId,196 to: T::CrossAccountId,197 data: up_data_structs::CreateItemData,198 nesting_budget: &dyn Budget,199 ) -> DispatchResultWithPostInfo {200 with_weight(201 <Pallet<T>>::create_item(202 self,203 &sender,204 map_create_data::<T>(data, &to)?,205 nesting_budget,206 ),207 <CommonWeights<T>>::create_item(),208 )209 }210211 fn create_multiple_items(212 &self,213 sender: T::CrossAccountId,214 to: T::CrossAccountId,215 data: Vec<up_data_structs::CreateItemData>,216 nesting_budget: &dyn Budget,217 ) -> DispatchResultWithPostInfo {218 let weight = <CommonWeights<T>>::create_multiple_items(&data);219 let data = data220 .into_iter()221 .map(|d| map_create_data::<T>(d, &to))222 .collect::<Result<Vec<_>, DispatchError>>()?;223224 with_weight(225 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),226 weight,227 )228 }229230 fn create_multiple_items_ex(231 &self,232 sender: <T>::CrossAccountId,233 data: CreateItemExData<T::CrossAccountId>,234 nesting_budget: &dyn Budget,235 ) -> DispatchResultWithPostInfo {236 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);237 let data = match data {238 CreateItemExData::RefungibleMultipleOwners(CreateRefungibleExMultipleOwners {239 users,240 properties,241 }) => vec![CreateItemData::<T> { users, properties }],242 CreateItemExData::RefungibleMultipleItems(r) => r243 .into_inner()244 .into_iter()245 .map(246 |CreateRefungibleExSingleOwner {247 user,248 pieces,249 properties,250 }| CreateItemData::<T> {251 users: BTreeMap::from([(user, pieces)])252 .try_into()253 .expect("limit >= 1"),254 properties,255 },256 )257 .collect(),258 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),259 };260261 with_weight(262 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),263 weight,264 )265 }266267 fn burn_item(268 &self,269 sender: T::CrossAccountId,270 token: TokenId,271 amount: u128,272 ) -> DispatchResultWithPostInfo {273 with_weight(274 <Pallet<T>>::burn(self, &sender, token, amount),275 <CommonWeights<T>>::burn_item(),276 )277 }278279 fn burn_item_recursively(280 &self,281 sender: T::CrossAccountId,282 token: TokenId,283 self_budget: &dyn Budget,284 _breadth_budget: &dyn Budget,285 ) -> DispatchResultWithPostInfo {286 ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);287 with_weight(288 <Pallet<T>>::burn(289 self,290 &sender,291 token,292 <Balance<T>>::get((self.id, token, &sender)),293 ),294 <CommonWeights<T>>::burn_recursively_self_raw(),295 )296 }297298 fn transfer(299 &self,300 from: T::CrossAccountId,301 to: T::CrossAccountId,302 token: TokenId,303 amount: u128,304 nesting_budget: &dyn Budget,305 ) -> DispatchResultWithPostInfo {306 with_weight(307 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),308 <CommonWeights<T>>::transfer(),309 )310 }311312 fn approve(313 &self,314 sender: T::CrossAccountId,315 spender: T::CrossAccountId,316 token: TokenId,317 amount: u128,318 ) -> DispatchResultWithPostInfo {319 with_weight(320 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),321 <CommonWeights<T>>::approve(),322 )323 }324325 fn approve_from(326 &self,327 sender: T::CrossAccountId,328 from: T::CrossAccountId,329 to: T::CrossAccountId,330 token_id: TokenId,331 amount: u128,332 ) -> DispatchResultWithPostInfo {333 with_weight(334 <Pallet<T>>::set_allowance_from(self, &sender, &from, &to, token_id, amount),335 <CommonWeights<T>>::approve_from(),336 )337 }338339 fn transfer_from(340 &self,341 sender: T::CrossAccountId,342 from: T::CrossAccountId,343 to: T::CrossAccountId,344 token: TokenId,345 amount: u128,346 nesting_budget: &dyn Budget,347 ) -> DispatchResultWithPostInfo {348 with_weight(349 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),350 <CommonWeights<T>>::transfer_from(),351 )352 }353354 fn burn_from(355 &self,356 sender: T::CrossAccountId,357 from: T::CrossAccountId,358 token: TokenId,359 amount: u128,360 nesting_budget: &dyn Budget,361 ) -> DispatchResultWithPostInfo {362 with_weight(363 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),364 <CommonWeights<T>>::burn_from(),365 )366 }367368 fn set_collection_properties(369 &self,370 sender: T::CrossAccountId,371 properties: Vec<Property>,372 ) -> DispatchResultWithPostInfo {373 let weight = <CommonWeights<T>>::set_collection_properties(properties.len() as u32);374375 with_weight(376 <Pallet<T>>::set_collection_properties(self, &sender, properties),377 weight,378 )379 }380381 fn delete_collection_properties(382 &self,383 sender: &T::CrossAccountId,384 property_keys: Vec<PropertyKey>,385 ) -> DispatchResultWithPostInfo {386 let weight = <CommonWeights<T>>::delete_collection_properties(property_keys.len() as u32);387388 with_weight(389 <Pallet<T>>::delete_collection_properties(self, sender, property_keys),390 weight,391 )392 }393394 fn set_token_properties(395 &self,396 sender: T::CrossAccountId,397 token_id: TokenId,398 properties: Vec<Property>,399 nesting_budget: &dyn Budget,400 ) -> DispatchResultWithPostInfo {401 let weight = <CommonWeights<T>>::set_token_properties(properties.len() as u32);402403 with_weight(404 <Pallet<T>>::set_token_properties(405 self,406 &sender,407 token_id,408 properties.into_iter(),409 false,410 nesting_budget,411 ),412 weight,413 )414 }415416 fn set_token_property_permissions(417 &self,418 sender: &T::CrossAccountId,419 property_permissions: Vec<PropertyKeyPermission>,420 ) -> DispatchResultWithPostInfo {421 let weight =422 <CommonWeights<T>>::set_token_property_permissions(property_permissions.len() as u32);423424 with_weight(425 <Pallet<T>>::set_token_property_permissions(self, sender, property_permissions),426 weight,427 )428 }429430 fn delete_token_properties(431 &self,432 sender: T::CrossAccountId,433 token_id: TokenId,434 property_keys: Vec<PropertyKey>,435 nesting_budget: &dyn Budget,436 ) -> DispatchResultWithPostInfo {437 let weight = <CommonWeights<T>>::delete_token_properties(property_keys.len() as u32);438439 with_weight(440 <Pallet<T>>::delete_token_properties(441 self,442 &sender,443 token_id,444 property_keys.into_iter(),445 nesting_budget,446 ),447 weight,448 )449 }450451 fn check_nesting(452 &self,453 _sender: <T>::CrossAccountId,454 _from: (CollectionId, TokenId),455 _under: TokenId,456 _nesting_budget: &dyn Budget,457 ) -> sp_runtime::DispatchResult {458 fail!(<Error<T>>::RefungibleDisallowsNesting)459 }460461 fn nest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}462463 fn unnest(&self, _under: TokenId, _to_nest: (CollectionId, TokenId)) {}464465 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {466 <Owned<T>>::iter_prefix((self.id, account))467 .map(|(id, _)| id)468 .collect()469 }470471 fn collection_tokens(&self) -> Vec<TokenId> {472 <TotalSupply<T>>::iter_prefix((self.id,))473 .map(|(id, _)| id)474 .collect()475 }476477 fn token_exists(&self, token: TokenId) -> bool {478 <Pallet<T>>::token_exists(self, token)479 }480481 fn last_token_id(&self) -> TokenId {482 TokenId(<TokensMinted<T>>::get(self.id))483 }484485 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {486 <Pallet<T>>::token_owner(self.id, token)487 }488489 490 fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {491 <Pallet<T>>::token_owners(self.id, token).unwrap_or_default()492 }493494 fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {495 <Pallet<T>>::token_properties((self.id, token_id))496 .get(key)497 .cloned()498 }499500 fn token_properties(&self, token_id: TokenId, keys: Option<Vec<PropertyKey>>) -> Vec<Property> {501 let properties = <Pallet<T>>::token_properties((self.id, token_id));502503 keys.map(|keys| {504 keys.into_iter()505 .filter_map(|key| {506 properties.get(&key).map(|value| Property {507 key,508 value: value.clone(),509 })510 })511 .collect()512 })513 .unwrap_or_else(|| {514 properties515 .into_iter()516 .map(|(key, value)| Property { key, value })517 .collect()518 })519 }520521 fn total_supply(&self) -> u32 {522 <Pallet<T>>::total_supply(self)523 }524525 fn account_balance(&self, account: T::CrossAccountId) -> u32 {526 <AccountBalance<T>>::get((self.id, account))527 }528529 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {530 <Balance<T>>::get((self.id, token, account))531 }532533 fn allowance(534 &self,535 sender: T::CrossAccountId,536 spender: T::CrossAccountId,537 token: TokenId,538 ) -> u128 {539 <Allowance<T>>::get((self.id, token, sender, spender))540 }541542 fn refungible_extensions(&self) -> Option<&dyn RefungibleExtensions<T>> {543 Some(self)544 }545546 fn total_pieces(&self, token: TokenId) -> Option<u128> {547 <Pallet<T>>::total_pieces(self.id, token)548 }549550 fn set_allowance_for_all(551 &self,552 owner: T::CrossAccountId,553 operator: T::CrossAccountId,554 approve: bool,555 ) -> DispatchResultWithPostInfo {556 with_weight(557 <Pallet<T>>::set_allowance_for_all(self, &owner, &operator, approve),558 <CommonWeights<T>>::set_allowance_for_all(),559 )560 }561562 fn allowance_for_all(&self, owner: T::CrossAccountId, operator: T::CrossAccountId) -> bool {563 <Pallet<T>>::allowance_for_all(self, &owner, &operator)564 }565566 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {567 with_weight(568 <Pallet<T>>::repair_item(self, token),569 <CommonWeights<T>>::force_repair_item(),570 )571 }572}573574impl<T: Config> RefungibleExtensions<T> for RefungibleHandle<T> {575 fn repartition(576 &self,577 owner: &T::CrossAccountId,578 token: TokenId,579 amount: u128,580 ) -> DispatchResultWithPostInfo {581 with_weight(582 <Pallet<T>>::repartition(self, owner, token, amount),583 <SelfWeightOf<T>>::repartition_item(),584 )585 }586}