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_resource_types_array<S: Get<u32>>(num: usize) -> BoundedVec<RmrkResourceTypes, S> {57 vec![RmrkResourceTypes::Composable(create_composable_resource()); num]58 .try_into()59 .expect("num <= S")60}6162fn create_max_collection<T: Config>(owner: &T::AccountId) -> DispatchResult {63 <T as pallet_common::Config>::Currency::deposit_creating(64 owner,65 T::CollectionCreationPrice::get(),66 );6768 let metadata = create_data();69 let max = None;70 let symbol = create_data();7172 <Pallet<T>>::create_collection(73 RawOrigin::Signed(owner.clone()).into(),74 metadata,75 max,76 symbol,77 )78}7980fn create_nft<T: Config>(owner: &T::AccountId, collection_id: RmrkCollectionId) -> DispatchResult {81 let royalty_recipient = Some(owner.clone());82 let royalty_amount = Some(Permill::from_percent(25));83 let metadata = create_data();84 let transferable = true;8586 <Pallet<T>>::mint_nft(87 RawOrigin::Signed(owner.clone()).into(),88 owner.clone(),89 collection_id,90 royalty_recipient,91 royalty_amount,92 metadata,93 transferable,94 None,95 )96}9798struct NftBuilder {99 collection_id: RmrkCollectionId,100 current_nft_id: RmrkNftId,101}102103impl NftBuilder {104 fn new(collection_id: RmrkCollectionId) -> Self {105 Self {106 collection_id,107 current_nft_id: 0,108 }109 }110111 fn current_nft_id(&self) -> RmrkNftId {112 self.current_nft_id113 }114115 fn build<T: Config>(&mut self, owner: &T::AccountId) -> Result<RmrkNftId, DispatchError> {116 create_nft::<T>(owner, self.collection_id)?;117 self.current_nft_id += 1;118119 Ok(self.current_nft_id)120 }121122 fn build_tower<T: Config>(123 &mut self,124 owner: &T::AccountId,125 height: u32,126 ) -> Result<(RmrkNftId, RmrkNftId), DispatchError> {127 self.build::<T>(owner)?;128129 let root_nft_id = self.current_nft_id;130 let mut prev_nft_id = root_nft_id;131132 for _ in 0..height {133 self.build::<T>(owner)?;134135 let new_owner =136 <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(137 self.collection_id,138 prev_nft_id,139 );140141 <Pallet<T>>::send(142 RawOrigin::Signed(owner.clone()).into(),143 self.collection_id,144 self.current_nft_id,145 new_owner,146 )?;147148 prev_nft_id = self.current_nft_id;149 }150151 let deepest_nft_id = self.current_nft_id;152153 Ok((root_nft_id, deepest_nft_id))154 }155156 fn build_wide_tree<T: Config>(157 &mut self,158 owner: &T::AccountId,159 width: u32,160 ) -> Result<RmrkNftId, DispatchError> {161 self.build::<T>(owner)?;162163 let root_nft_id = self.current_nft_id;164165 let root_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(166 self.collection_id,167 root_nft_id,168 );169170 for _ in 0..width {171 self.build::<T>(owner)?;172173 <Pallet<T>>::send(174 RawOrigin::Signed(owner.clone()).into(),175 self.collection_id,176 self.current_nft_id,177 root_owner.clone(),178 )?;179 }180181 Ok(root_nft_id)182 }183}184185benchmarks! {186 create_collection {187 let caller = account("caller", 0, SEED);188 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());189 let metadata = create_data();190 let max = None;191 let symbol = create_data();192 }: _(RawOrigin::Signed(caller), metadata, max, symbol)193194 destroy_collection {195 let caller = account("caller", 0, SEED);196197 create_max_collection::<T>(&caller)?;198 let collection_id = 0;199 }: _(RawOrigin::Signed(caller), collection_id)200201 change_collection_issuer {202 let caller: T::AccountId = account("caller", 0, SEED);203204 create_max_collection::<T>(&caller)?;205 let collection_id = 0;206207 let new_owner: T::AccountId = account("new_owner", 0, SEED);208209 let new_owner_source = T::Lookup::unlookup(new_owner);210 }: _(RawOrigin::Signed(caller), collection_id, new_owner_source)211212 lock_collection {213 let caller: T::AccountId = account("caller", 0, SEED);214215 create_max_collection::<T>(&caller)?;216 let collection_id = 0;217 }: _(RawOrigin::Signed(caller), collection_id)218219 mint_nft {220 let b in 0..100;221222 let caller: T::AccountId = account("caller", 0, SEED);223224 create_max_collection::<T>(&caller)?;225 let collection_id = 0;226 let owner = caller.clone();227228 let royalty_recipient = Some(caller.clone());229 let royalty_amount = Some(Permill::from_percent(25));230 let metadata = create_data();231 let transferable = true;232 }: _(233 RawOrigin::Signed(caller),234 owner,235 collection_id,236 royalty_recipient,237 royalty_amount,238 metadata,239 transferable,240 Some(create_max_resource_types_array(b as usize))241 )242243 burn_nft {244 let b in 0..200;245246 let caller: T::AccountId = account("caller", 0, SEED);247 create_max_collection::<T>(&caller)?;248 let collection_id = 0;249250 let mut nft_builder = NftBuilder::new(collection_id);251 let root_nft_id = nft_builder.build_wide_tree::<T>(&caller, b)?;252 let max_burns = b + 1;253 }: _(254 RawOrigin::Signed(caller),255 collection_id,256 root_nft_id,257 max_burns258 )259260 send {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 src_nft_id = nft_builder.build::<T>(&caller)?;267 let (_, target_nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 2)?;268 }: _(269 RawOrigin::Signed(caller),270 collection_id,271 src_nft_id,272 <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(collection_id, target_nft_id)273 )274275 accept_nft {276 let caller: T::AccountId = account("caller", 0, SEED);277 let sender: T::AccountId = account("sender", 0, SEED);278279 create_max_collection::<T>(&sender)?;280 let src_collection_id = 0;281282 create_max_collection::<T>(&caller)?;283 let target_collection_id = 1;284285 let mut src_nft_builder = NftBuilder::new(src_collection_id);286 let src_nft_id = src_nft_builder.build::<T>(&sender)?;287288 let mut target_nft_builder = NftBuilder::new(target_collection_id);289 let fake_target_nft_id = target_nft_builder.build::<T>(&caller)?;290 let (_, target_nft_id) = target_nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;291292 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(293 target_collection_id,294 fake_target_nft_id295 );296297 let actual_new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(298 target_collection_id,299 target_nft_id300 );301302 <Pallet<T>>::send(303 RawOrigin::Signed(sender.clone()).into(),304 src_collection_id,305 src_nft_id,306 new_owner,307 )?;308 }: _(309 RawOrigin::Signed(caller),310 src_collection_id,311 src_nft_id,312 actual_new_owner313 )314315 reject_nft {316 let caller: T::AccountId = account("caller", 0, SEED);317 let sender: T::AccountId = account("sender", 0, SEED);318319 create_max_collection::<T>(&sender)?;320 let src_collection_id = 0;321322 create_max_collection::<T>(&caller)?;323 let target_collection_id = 1;324325 let mut src_nft_builder = NftBuilder::new(src_collection_id);326 let (src_root_nft_id, _) = src_nft_builder.build_tower::<T>(&sender, NESTING_BUDGET - 1)?;327328 let mut target_nft_builder = NftBuilder::new(target_collection_id);329 let target_nft_id = target_nft_builder.build::<T>(&caller)?;330331 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(332 target_collection_id,333 target_nft_id334 );335336 <Pallet<T>>::send(337 RawOrigin::Signed(sender.clone()).into(),338 src_collection_id,339 src_root_nft_id,340 new_owner,341 )?;342 }: _(343 RawOrigin::Signed(caller),344 src_collection_id,345 src_root_nft_id346 )347348 set_property {349 let caller: T::AccountId = account("caller", 0, SEED);350 create_max_collection::<T>(&caller)?;351 let collection_id = 0;352353 let mut nft_builder = NftBuilder::new(collection_id);354 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;355356 let key = create_data();357 let value = create_data();358 }: _(359 RawOrigin::Signed(caller),360 collection_id,361 Some(nft_id),362 key,363 value364 )365366 set_priority {367 let caller: T::AccountId = account("caller", 0, SEED);368 create_max_collection::<T>(&caller)?;369 let collection_id = 0;370371 let mut nft_builder = NftBuilder::new(collection_id);372 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;373 let priorities = create_u32_array();374 }: _(375 RawOrigin::Signed(caller),376 collection_id,377 nft_id,378 priorities379 )380381 add_basic_resource {382 let caller: T::AccountId = account("caller", 0, SEED);383384 create_max_collection::<T>(&caller)?;385 let collection_id = 0;386387 let mut nft_builder = NftBuilder::new(collection_id);388 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;389 let resource = create_basic_resource();390 }: _(391 RawOrigin::Signed(caller),392 collection_id,393 nft_id,394 resource395 )396397 add_composable_resource {398 let caller: T::AccountId = account("caller", 0, SEED);399400 create_max_collection::<T>(&caller)?;401 let collection_id = 0;402403 let mut nft_builder = NftBuilder::new(collection_id);404 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;405 let resource = create_composable_resource();406 }: _(407 RawOrigin::Signed(caller),408 collection_id,409 nft_id,410 resource411 )412413 add_slot_resource {414 let caller: T::AccountId = account("caller", 0, SEED);415416 create_max_collection::<T>(&caller)?;417 let collection_id = 0;418419 let mut nft_builder = NftBuilder::new(collection_id);420 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;421 let resource = create_slot_resource();422 }: _(423 RawOrigin::Signed(caller),424 collection_id,425 nft_id,426 resource427 )428429 remove_resource {430 let caller: T::AccountId = account("caller", 0, SEED);431432 create_max_collection::<T>(&caller)?;433 let collection_id = 0;434435 let mut nft_builder = NftBuilder::new(collection_id);436 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;437 let resource = create_basic_resource();438439 <Pallet<T>>::add_basic_resource(440 RawOrigin::Signed(caller.clone()).into(),441 collection_id,442 nft_id,443 resource444 )?;445446 let resource_id = 0;447 }: _(448 RawOrigin::Signed(caller),449 collection_id,450 nft_id,451 resource_id452 )453454 accept_resource {455 let caller: T::AccountId = account("caller", 0, SEED);456 let admin: T::AccountId = account("admin", 0, SEED);457458 create_max_collection::<T>(&admin)?;459 let collection_id = 0;460461 let mut nft_builder = NftBuilder::new(collection_id);462 let root_nft_id = 1;463 let (_, nested_nft_id) = nft_builder.build_tower::<T>(&admin, NESTING_BUDGET - 1)?;464 let resource = create_basic_resource();465466 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::AccountId(caller.clone());467468 <Pallet<T>>::send(469 RawOrigin::Signed(admin.clone()).into(),470 collection_id,471 root_nft_id,472 new_owner,473 )?;474475 <Pallet<T>>::add_basic_resource(476 RawOrigin::Signed(admin.clone()).into(),477 collection_id,478 nested_nft_id,479 resource480 )?;481482 let resource_id = 0;483 }: _(484 RawOrigin::Signed(caller),485 collection_id,486 nested_nft_id,487 resource_id488 )489490 accept_resource_removal {491 let caller: T::AccountId = account("caller", 0, SEED);492 let admin: T::AccountId = account("admin", 0, SEED);493494 create_max_collection::<T>(&admin)?;495 let collection_id = 0;496497 let mut nft_builder = NftBuilder::new(collection_id);498 let root_nft_id = 1;499 let (_, nested_nft_id) = nft_builder.build_tower::<T>(&admin, NESTING_BUDGET - 1)?;500 let resource = create_basic_resource();501502 <Pallet<T>>::add_basic_resource(503 RawOrigin::Signed(admin.clone()).into(),504 collection_id,505 nested_nft_id,506 resource507 )?;508509 let resource_id = 0;510511 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::AccountId(caller.clone());512513 <Pallet<T>>::send(514 RawOrigin::Signed(admin.clone()).into(),515 collection_id,516 root_nft_id,517 new_owner,518 )?;519520 <Pallet<T>>::remove_resource(521 RawOrigin::Signed(admin).into(),522 collection_id,523 nested_nft_id,524 resource_id525 )?;526 }: _(527 RawOrigin::Signed(caller),528 collection_id,529 nested_nft_id,530 resource_id531 )532}