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,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 transfer() -> Weight {70 max_weight_of!(71 transfer_normal(),72 transfer_creating(),73 transfer_removing(),74 transfer_creating_removing()75 )76 }7778 fn approve() -> Weight {79 <SelfWeightOf<T>>::approve()80 }8182 fn transfer_from() -> Weight {83 max_weight_of!(84 transfer_from_normal(),85 transfer_from_creating(),86 transfer_from_removing(),87 transfer_from_creating_removing()88 )89 }9091 fn burn_from() -> Weight {92 <SelfWeightOf<T>>::burn_from()93 }9495 fn set_variable_metadata(bytes: u32) -> Weight {96 <SelfWeightOf<T>>::set_variable_metadata(bytes)97 }98}99100fn map_create_data<T: Config>(101 data: up_data_structs::CreateItemData,102 to: &T::CrossAccountId,103) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {104 match data {105 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {106 const_data: data.const_data,107 variable_data: data.variable_data,108 users: {109 let mut out = BTreeMap::new();110 out.insert(to.clone(), data.pieces);111 out.try_into().expect("limit > 0")112 },113 }),114 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),115 }116}117118impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<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 with_weight(127 <Pallet<T>>::create_item(128 self,129 &sender,130 map_create_data::<T>(data, &to)?,131 nesting_budget,132 ),133 <CommonWeights<T>>::create_item(),134 )135 }136137 fn create_multiple_items(138 &self,139 sender: T::CrossAccountId,140 to: T::CrossAccountId,141 data: Vec<up_data_structs::CreateItemData>,142 nesting_budget: &dyn Budget,143 ) -> DispatchResultWithPostInfo {144 let data = data145 .into_iter()146 .map(|d| map_create_data::<T>(d, &to))147 .collect::<Result<Vec<_>, DispatchError>>()?;148149 let amount = data.len();150 with_weight(151 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),152 <CommonWeights<T>>::create_multiple_items(amount as u32),153 )154 }155156 fn create_multiple_items_ex(157 &self,158 sender: <T>::CrossAccountId,159 data: CreateItemExData<T::CrossAccountId>,160 nesting_budget: &dyn Budget,161 ) -> DispatchResultWithPostInfo {162 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);163 let data = match data {164 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],165 CreateItemExData::RefungibleMultipleItems(r)166 if r.iter().all(|i| i.users.len() == 1) =>167 {168 r.into_inner()169 }170 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),171 };172173 with_weight(174 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),175 weight,176 )177 }178179 fn burn_item(180 &self,181 sender: T::CrossAccountId,182 token: TokenId,183 amount: u128,184 ) -> DispatchResultWithPostInfo {185 with_weight(186 <Pallet<T>>::burn(self, &sender, token, amount),187 <CommonWeights<T>>::burn_item(),188 )189 }190191 fn transfer(192 &self,193 from: T::CrossAccountId,194 to: T::CrossAccountId,195 token: TokenId,196 amount: u128,197 nesting_budget: &dyn Budget,198 ) -> DispatchResultWithPostInfo {199 with_weight(200 <Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),201 <CommonWeights<T>>::transfer(),202 )203 }204205 fn approve(206 &self,207 sender: T::CrossAccountId,208 spender: T::CrossAccountId,209 token: TokenId,210 amount: u128,211 ) -> DispatchResultWithPostInfo {212 with_weight(213 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),214 <CommonWeights<T>>::approve(),215 )216 }217218 fn transfer_from(219 &self,220 sender: T::CrossAccountId,221 from: T::CrossAccountId,222 to: T::CrossAccountId,223 token: TokenId,224 amount: u128,225 nesting_budget: &dyn Budget,226 ) -> DispatchResultWithPostInfo {227 with_weight(228 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),229 <CommonWeights<T>>::transfer_from(),230 )231 }232233 fn burn_from(234 &self,235 sender: T::CrossAccountId,236 from: T::CrossAccountId,237 token: TokenId,238 amount: u128,239 nesting_budget: &dyn Budget,240 ) -> DispatchResultWithPostInfo {241 with_weight(242 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),243 <CommonWeights<T>>::burn_from(),244 )245 }246247 fn set_variable_metadata(248 &self,249 sender: T::CrossAccountId,250 token: TokenId,251 data: BoundedVec<u8, CustomDataLimit>,252 ) -> DispatchResultWithPostInfo {253 let len = data.len();254 with_weight(255 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),256 <CommonWeights<T>>::set_variable_metadata(len as u32),257 )258 }259260 fn check_nesting(261 &self,262 _sender: <T>::CrossAccountId,263 _from: (CollectionId, TokenId),264 _under: TokenId,265 _budget: &dyn Budget,266 ) -> sp_runtime::DispatchResult {267 fail!(<Error<T>>::RefungibleDisallowsNesting)268 }269270 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {271 <Owned<T>>::iter_prefix((self.id, account))272 .map(|(id, _)| id)273 .collect()274 }275276 fn collection_tokens(&self) -> Vec<TokenId> {277 <TokenData<T>>::iter_prefix((self.id,))278 .map(|(id, _)| id)279 .collect()280 }281282 fn token_exists(&self, token: TokenId) -> bool {283 <Pallet<T>>::token_exists(self, token)284 }285286 fn last_token_id(&self) -> TokenId {287 TokenId(<TokensMinted<T>>::get(self.id))288 }289290 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {291 None292 }293 fn const_metadata(&self, token: TokenId) -> Vec<u8> {294 <TokenData<T>>::get((self.id, token))295 .const_data296 .into_inner()297 }298 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {299 <TokenData<T>>::get((self.id, token))300 .variable_data301 .into_inner()302 }303304 fn total_supply(&self) -> u32 {305 <Pallet<T>>::total_supply(self)306 }307308 fn account_balance(&self, account: T::CrossAccountId) -> u32 {309 <AccountBalance<T>>::get((self.id, account))310 }311312 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {313 <Balance<T>>::get((self.id, token, account))314 }315316 fn allowance(317 &self,318 sender: T::CrossAccountId,319 spender: T::CrossAccountId,320 token: TokenId,321 ) -> u128 {322 <Allowance<T>>::get((self.id, token, sender, spender))323 }324}