1use sp_std::vec;23use frame_benchmarking::{benchmarks, account};4use frame_system::RawOrigin;5use frame_support::{6 traits::{Currency, Get},7 BoundedVec,8};9use sp_runtime::Permill;1011use up_data_structs::*;1213use super::*;1415const SEED: u32 = 1;1617fn create_data<S: Get<u32>>() -> BoundedVec<u8, S> {18 vec![b'A'; S::get() as usize].try_into().expect("size == S")19}2021fn create_u32_array<S: Get<u32>>() -> BoundedVec<u32, S> {22 vec![0; S::get() as usize].try_into().expect("size == S")23}2425fn create_basic_resource() -> RmrkBasicResource {26 RmrkBasicResource {27 src: Some(create_data()),28 metadata: Some(create_data()),29 license: Some(create_data()),30 thumb: Some(create_data()),31 }32}3334fn create_composable_resource() -> RmrkComposableResource {35 RmrkComposableResource {36 parts: create_u32_array(),37 base: 100,38 src: Some(create_data()),39 metadata: Some(create_data()),40 license: Some(create_data()),41 thumb: Some(create_data()),42 }43}4445fn create_slot_resource() -> RmrkSlotResource {46 RmrkSlotResource {47 base: 100,48 slot: 200,49 src: Some(create_data()),50 metadata: Some(create_data()),51 license: Some(create_data()),52 thumb: Some(create_data()),53 }54}5556fn create_max_collection<T: Config>(owner: &T::AccountId) -> DispatchResult {57 <T as pallet_common::Config>::Currency::deposit_creating(owner, T::CollectionCreationPrice::get());5859 let metadata = create_data();60 let max = None;61 let symbol = create_data();6263 <Pallet<T>>::create_collection(RawOrigin::Signed(owner.clone()).into(), metadata, max, symbol)64}6566fn create_max_nft<T: Config>(owner: &T::AccountId, collection_id: RmrkCollectionId) -> DispatchResult {67 let royalty_recipient = Some(owner.clone());68 let royalty_amount = Some(Permill::from_percent(25));69 let metadata = create_data();70 let transferable = true;7172 <Pallet<T>>::mint_nft(73 RawOrigin::Signed(owner.clone()).into(),74 owner.clone(),75 collection_id,76 royalty_recipient,77 royalty_amount,78 metadata,79 transferable80 )81}8283struct NftBuilder {84 collection_id: RmrkCollectionId,85 current_nft_id: RmrkNftId86}8788impl NftBuilder {89 fn new(collection_id: RmrkCollectionId) -> Self {90 Self {91 collection_id,92 current_nft_id: 093 }94 }9596 fn build<T: Config>(&mut self, owner: &T::AccountId) -> Result<RmrkNftId, DispatchError> {97 create_max_nft::<T>(owner, self.collection_id)?;98 self.current_nft_id += 1;99100 Ok(self.current_nft_id)101 }102103 fn build_tower<T: Config>(&mut self, owner: &T::AccountId, height: u32) -> Result<RmrkNftId, DispatchError> {104 self.build::<T>(owner)?;105106 let mut prev_nft_id = self.current_nft_id;107108 for _ in 0..height {109 self.build::<T>(owner)?;110 111 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(112 self.collection_id,113 prev_nft_id114 );115116 <Pallet<T>>::send(117 RawOrigin::Signed(owner.clone()).into(),118 self.collection_id,119 self.current_nft_id,120 new_owner,121 )?;122123 prev_nft_id = self.current_nft_id;124 }125126 let deepest_nft_id = self.current_nft_id;127128 Ok(deepest_nft_id)129 }130}131132benchmarks! {133 create_collection {134 let caller = account("caller", 0, SEED);135 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());136 let metadata = create_data();137 let max = None;138 let symbol = create_data();139 }: _(RawOrigin::Signed(caller), metadata, max, symbol)140141 destroy_collection {142 let caller = account("caller", 0, SEED);143 144 create_max_collection::<T>(&caller)?;145 let collection_id = 0;146 }: _(RawOrigin::Signed(caller), collection_id)147148 change_collection_issuer {149 let caller: T::AccountId = account("caller", 0, SEED);150151 create_max_collection::<T>(&caller)?;152 let collection_id = 0;153154 let new_owner: T::AccountId = account("new_owner", 0, SEED);155156 let new_owner_source = T::Lookup::unlookup(new_owner);157 }: _(RawOrigin::Signed(caller), collection_id, new_owner_source)158159 lock_collection {160 let caller: T::AccountId = account("caller", 0, SEED);161162 create_max_collection::<T>(&caller)?;163 let collection_id = 0;164 }: _(RawOrigin::Signed(caller), collection_id)165166 mint_nft {167 let caller: T::AccountId = account("caller", 0, SEED);168169 create_max_collection::<T>(&caller)?;170 let collection_id = 0;171 let owner = caller.clone();172 173 let royalty_recipient = Some(caller.clone());174 let royalty_amount = Some(Permill::from_percent(25));175 let metadata = create_data();176 let transferable = true;177 }: _(178 RawOrigin::Signed(caller),179 owner,180 collection_id,181 royalty_recipient,182 royalty_amount,183 metadata,184 transferable185 )186187 send {188 let caller: T::AccountId = account("caller", 0, SEED);189 create_max_collection::<T>(&caller)?;190 let collection_id = 0;191192 let mut nft_builder = NftBuilder::new(collection_id);193 let src_nft_id = nft_builder.build::<T>(&caller)?;194 let target_nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 2)?;195 }: _(196 RawOrigin::Signed(caller),197 collection_id,198 src_nft_id,199 <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(collection_id, target_nft_id)200 )201202 accept_nft {203 let caller: T::AccountId = account("caller", 0, SEED);204 let sender: T::AccountId = account("sender", 0, SEED);205206 create_max_collection::<T>(&sender)?;207 let src_collection_id = 0;208209 create_max_collection::<T>(&caller)?;210 let target_collection_id = 1;211212 let mut src_nft_builder = NftBuilder::new(src_collection_id);213 let src_nft_id = src_nft_builder.build::<T>(&sender)?;214215 let mut target_nft_builder = NftBuilder::new(target_collection_id);216 let fake_target_nft_id = target_nft_builder.build::<T>(&caller)?;217 let target_nft_id = target_nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;218219 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(220 target_collection_id,221 fake_target_nft_id222 );223224 let actual_new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(225 target_collection_id,226 target_nft_id227 );228229 <Pallet<T>>::send(230 RawOrigin::Signed(sender.clone()).into(),231 src_collection_id,232 src_nft_id,233 new_owner,234 )?;235 }: _(236 RawOrigin::Signed(caller),237 src_collection_id,238 src_nft_id,239 actual_new_owner240 )241242 set_property {243 let caller: T::AccountId = account("caller", 0, SEED);244 create_max_collection::<T>(&caller)?;245 let collection_id = 0;246247 let mut nft_builder = NftBuilder::new(collection_id);248 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;249250 let key = create_data();251 let value = create_data();252 }: _(253 RawOrigin::Signed(caller),254 collection_id,255 Some(nft_id),256 key,257 value258 )259260 set_priority {261 let caller: T::AccountId = account("caller", 0, SEED);262 create_max_collection::<T>(&caller)?;263 let collection_id = 0;264265 let mut nft_builder = NftBuilder::new(collection_id);266 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;267 let priorities = create_u32_array();268 }: _(269 RawOrigin::Signed(caller),270 collection_id,271 nft_id,272 priorities273 )274275 add_basic_resource {276 let caller: T::AccountId = account("caller", 0, SEED);277 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());278279 create_max_collection::<T>(&caller)?;280 let collection_id = 0;281282 let mut nft_builder = NftBuilder::new(collection_id);283 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;284 let resource = create_basic_resource();285 }: _(286 RawOrigin::Signed(caller),287 collection_id,288 nft_id,289 resource290 )291292 add_composable_resource {293 let caller: T::AccountId = account("caller", 0, SEED);294 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());295296 create_max_collection::<T>(&caller)?;297 let collection_id = 0;298299 let mut nft_builder = NftBuilder::new(collection_id);300 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;301 let resource = create_composable_resource();302 }: _(303 RawOrigin::Signed(caller),304 collection_id,305 nft_id,306 resource307 )308309 add_slot_resource {310 let caller: T::AccountId = account("caller", 0, SEED);311 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());312313 create_max_collection::<T>(&caller)?;314 let collection_id = 0;315316 let mut nft_builder = NftBuilder::new(collection_id);317 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;318 let resource = create_slot_resource();319 }: _(320 RawOrigin::Signed(caller),321 collection_id,322 nft_id,323 resource324 )325326 remove_resource {327 let caller: T::AccountId = account("caller", 0, SEED);328 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());329330 create_max_collection::<T>(&caller)?;331 let collection_id = 0;332333 let mut nft_builder = NftBuilder::new(collection_id);334 let nft_id = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;335 let resource = create_basic_resource();336337 <Pallet<T>>::add_basic_resource(338 RawOrigin::Signed(caller.clone()).into(),339 collection_id,340 nft_id,341 resource342 )?;343344 let resource_id = 1;345 }: _(346 RawOrigin::Signed(caller),347 collection_id,348 nft_id,349 resource_id350 )351}