1234567891011121314151617use core::marker::PhantomData;1819use sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};21use up_data_structs::{22 CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,23 budget::Budget, Property, PropertyKey, PropertyKeyPermission,24};25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};26use sp_runtime::DispatchError;27use sp_std::{vec::Vec, vec};2829use crate::{30 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,31 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,32};3334macro_rules! max_weight_of {35 ($($method:ident ($($args:tt)*)),*) => {36 037 $(38 .max(<SelfWeightOf<T>>::$method($($args)*))39 )*40 };41}4243pub struct CommonWeights<T: Config>(PhantomData<T>);44impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {45 fn create_item() -> Weight {46 <SelfWeightOf<T>>::create_item()47 }4849 fn create_multiple_items(amount: u32) -> Weight {50 <SelfWeightOf<T>>::create_multiple_items(amount)51 }5253 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {54 match call {55 CreateItemExData::RefungibleMultipleOwners(i) => {56 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)57 }58 CreateItemExData::RefungibleMultipleItems(i) => {59 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)60 }61 _ => 0,62 }63 }6465 fn burn_item() -> Weight {66 max_weight_of!(burn_item_partial(), burn_item_fully())67 }6869 fn set_collection_properties(amount: u32) -> Weight {70 <SelfWeightOf<T>>::set_collection_properties(amount)71 }7273 fn delete_collection_properties(amount: u32) -> Weight {74 <SelfWeightOf<T>>::delete_collection_properties(amount)75 }7677 fn set_token_properties(amount: u32) -> Weight {78 <SelfWeightOf<T>>::set_token_properties(amount)79 }8081 fn delete_token_properties(amount: u32) -> Weight {82 <SelfWeightOf<T>>::delete_token_properties(amount)83 }8485 fn set_property_permissions(amount: u32) -> Weight {86 <SelfWeightOf<T>>::set_property_permissions(amount)87 }8889 fn transfer() -> Weight {90 max_weight_of!(91 transfer_normal(),92 transfer_creating(),93 transfer_removing(),94 transfer_creating_removing()95 )96 }9798 fn approve() -> Weight {99 <SelfWeightOf<T>>::approve()100 }101102 fn transfer_from() -> Weight {103 max_weight_of!(104 transfer_from_normal(),105 transfer_from_creating(),106 transfer_from_removing(),107 transfer_from_creating_removing()108 )109 }110111 fn burn_from() -> Weight {112 <SelfWeightOf<T>>::burn_from()113 }114115 fn set_variable_metadata(bytes: u32) -> Weight {116 <SelfWeightOf<T>>::set_variable_metadata(bytes)117 }118}119120fn map_create_data<T: Config>(121 data: up_data_structs::CreateItemData,122 to: &T::CrossAccountId,123) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {124 match data {125 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {126 const_data: data.const_data,127 variable_data: data.variable_data,128 users: {129 let mut out = BTreeMap::new();130 out.insert(to.clone(), data.pieces);131 out.try_into().expect("limit > 0")132 },133 }),134 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),135 }136}137138impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {139 fn create_item(140 &self,141 sender: T::CrossAccountId,142 to: T::CrossAccountId,143 data: up_data_structs::CreateItemData,144 nesting_budget: &dyn Budget,145 ) -> DispatchResultWithPostInfo {146 with_weight(147 <Pallet<T>>::create_item(148 self,149 &sender,150 map_create_data::<T>(data, &to)?,151 nesting_budget,152 ),153 <CommonWeights<T>>::create_item(),154 )155 }156157 fn create_multiple_items(158 &self,159 sender: T::CrossAccountId,160 to: T::CrossAccountId,161 data: Vec<up_data_structs::CreateItemData>,162 nesting_budget: &dyn Budget,163 ) -> DispatchResultWithPostInfo {164 let data = data165 .into_iter()166 .map(|d| map_create_data::<T>(d, &to))167 .collect::<Result<Vec<_>, DispatchError>>()?;168169 let amount = data.len();170 with_weight(171 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),172 <CommonWeights<T>>::create_multiple_items(amount as u32),173 )174 }175176 fn create_multiple_items_ex(177 &self,178 sender: <T>::CrossAccountId,179 data: CreateItemExData<T::CrossAccountId>,180 nesting_budget: &dyn Budget,181 ) -> DispatchResultWithPostInfo {182 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);183 let data = match data {184 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],185 CreateItemExData::RefungibleMultipleItems(r)186 if r.iter().all(|i| i.users.len() == 1) =>187 {188 r.into_inner()189 }190 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),191 };192193 with_weight(194 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),195 weight,196 )197 }198199 fn burn_item(200 &self,201 sender: T::CrossAccountId,202 token: TokenId,203 amount: u128,204 ) -> DispatchResultWithPostInfo {205 with_weight(206 <Pallet<T>>::burn(self, &sender, token, amount),207 <CommonWeights<T>>::burn_item(),208 )209 }210211 fn transfer(212 &self,213 from: T::CrossAccountId,214 to: T::CrossAccountId,215 token: TokenId,216 amount: u128,217 nesting_budget: &dyn Budget,218 ) -> DispatchResultWithPostInfo {219 with_weight(220 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),221 <CommonWeights<T>>::transfer(),222 )223 }224225 fn approve(226 &self,227 sender: T::CrossAccountId,228 spender: T::CrossAccountId,229 token: TokenId,230 amount: u128,231 ) -> DispatchResultWithPostInfo {232 with_weight(233 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),234 <CommonWeights<T>>::approve(),235 )236 }237238 fn transfer_from(239 &self,240 sender: T::CrossAccountId,241 from: T::CrossAccountId,242 to: T::CrossAccountId,243 token: TokenId,244 amount: u128,245 nesting_budget: &dyn Budget,246 ) -> DispatchResultWithPostInfo {247 with_weight(248 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),249 <CommonWeights<T>>::transfer_from(),250 )251 }252253 fn burn_from(254 &self,255 sender: T::CrossAccountId,256 from: T::CrossAccountId,257 token: TokenId,258 amount: u128,259 nesting_budget: &dyn Budget,260 ) -> DispatchResultWithPostInfo {261 with_weight(262 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),263 <CommonWeights<T>>::burn_from(),264 )265 }266267 fn set_collection_properties(268 &self,269 _sender: T::CrossAccountId,270 _property: Vec<Property>,271 ) -> DispatchResultWithPostInfo {272 fail!(<Error<T>>::SettingPropertiesNotAllowed)273 }274275 fn delete_collection_properties(276 &self,277 _sender: &T::CrossAccountId,278 _property_keys: Vec<PropertyKey>,279 ) -> DispatchResultWithPostInfo {280 fail!(<Error<T>>::SettingPropertiesNotAllowed)281 }282283 fn set_token_properties(284 &self,285 _sender: T::CrossAccountId,286 _token_id: TokenId,287 _property: Vec<Property>,288 ) -> DispatchResultWithPostInfo {289 fail!(<Error<T>>::SettingPropertiesNotAllowed)290 }291292 fn set_property_permissions(293 &self,294 _sender: &T::CrossAccountId,295 _property_permissions: Vec<PropertyKeyPermission>,296 ) -> DispatchResultWithPostInfo {297 fail!(<Error<T>>::SettingPropertiesNotAllowed)298 }299300 fn delete_token_properties(301 &self,302 _sender: T::CrossAccountId,303 _token_id: TokenId,304 _property_keys: Vec<PropertyKey>,305 ) -> DispatchResultWithPostInfo {306 fail!(<Error<T>>::SettingPropertiesNotAllowed)307 }308309 fn set_variable_metadata(310 &self,311 sender: T::CrossAccountId,312 token: TokenId,313 data: BoundedVec<u8, CustomDataLimit>,314 ) -> DispatchResultWithPostInfo {315 let len = data.len();316 with_weight(317 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),318 <CommonWeights<T>>::set_variable_metadata(len as u32),319 )320 }321322 fn check_nesting(323 &self,324 _sender: <T>::CrossAccountId,325 _from: (CollectionId, TokenId),326 _under: TokenId,327 _budget: &dyn Budget,328 ) -> sp_runtime::DispatchResult {329 fail!(<Error<T>>::RefungibleDisallowsNesting)330 }331332 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {333 <Owned<T>>::iter_prefix((self.id, account))334 .map(|(id, _)| id)335 .collect()336 }337338 fn collection_tokens(&self) -> Vec<TokenId> {339 <TokenData<T>>::iter_prefix((self.id,))340 .map(|(id, _)| id)341 .collect()342 }343344 fn token_exists(&self, token: TokenId) -> bool {345 <Pallet<T>>::token_exists(self, token)346 }347348 fn last_token_id(&self) -> TokenId {349 TokenId(<TokensMinted<T>>::get(self.id))350 }351352 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {353 None354 }355 fn const_metadata(&self, token: TokenId) -> Vec<u8> {356 <TokenData<T>>::get((self.id, token))357 .const_data358 .into_inner()359 }360 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {361 <TokenData<T>>::get((self.id, token))362 .variable_data363 .into_inner()364 }365366 fn token_properties(&self, _token_id: TokenId, _keys: Vec<PropertyKey>) -> Vec<Property> {367 Vec::new()368 }369370 fn total_supply(&self) -> u32 {371 <Pallet<T>>::total_supply(self)372 }373374 fn account_balance(&self, account: T::CrossAccountId) -> u32 {375 <AccountBalance<T>>::get((self.id, account))376 }377378 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {379 <Balance<T>>::get((self.id, token, account))380 }381382 fn allowance(383 &self,384 sender: T::CrossAccountId,385 spender: T::CrossAccountId,386 token: TokenId,387 ) -> u128 {388 <Allowance<T>>::get((self.id, token, sender, spender))389 }390}