pallet_nft_fractionalization/
benchmarking.rs1#![cfg(feature = "runtime-benchmarks")]
21
22use super::*;
23use frame::benchmarking::prelude::*;
24
25use frame::deps::frame_support::assert_ok;
26use fungible::{Inspect as InspectFungible, Mutate as MutateFungible};
27use nonfungibles_v2::{Create, Mutate};
28
29use frame_system::RawOrigin as SystemOrigin;
30use pallet_nfts::{CollectionConfig, CollectionSettings, ItemConfig, MintSettings};
31
32use crate::Pallet as NftFractionalization;
33
34type BalanceOf<T> =
35 <<T as Config>::Currency as InspectFungible<<T as SystemConfig>::AccountId>>::Balance;
36
37type CollectionConfigOf<T> =
38 CollectionConfig<BalanceOf<T>, BlockNumberFor<T>, <T as Config>::NftCollectionId>;
39
40fn default_collection_config<T: Config>() -> CollectionConfigOf<T>
41where
42 T::Currency: InspectFungible<T::AccountId>,
43{
44 CollectionConfig {
45 settings: CollectionSettings::all_enabled(),
46 max_supply: None,
47 mint_settings: MintSettings::default(),
48 }
49}
50
51fn mint_nft<T: Config>(nft_id: T::NftId) -> (T::AccountId, AccountIdLookupOf<T>)
52where
53 T::Nfts: Create<T::AccountId, CollectionConfig<BalanceOf<T>, BlockNumberFor<T>, T::NftCollectionId>>
54 + Mutate<T::AccountId, ItemConfig>,
55{
56 let caller: T::AccountId = whitelisted_caller();
57 let caller_lookup = T::Lookup::unlookup(caller.clone());
58 let ed = T::Currency::minimum_balance();
59 let multiplier = BalanceOf::<T>::from(100u8);
60 T::Currency::set_balance(&caller, ed * multiplier + T::Deposit::get() * multiplier);
61
62 assert_ok!(T::Nfts::create_collection(&caller, &caller, &default_collection_config::<T>()));
63 let collection = T::BenchmarkHelper::collection(0);
64 assert_ok!(T::Nfts::mint_into(&collection, &nft_id, &caller, &ItemConfig::default(), true));
65 (caller, caller_lookup)
66}
67
68fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
69 let events = frame_system::Pallet::<T>::events();
70 let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
71 let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
73 assert_eq!(event, &system_event);
74}
75
76#[benchmarks(
77 where
78 T::Nfts:
79 Create<
80 T::AccountId,
81 CollectionConfig<BalanceOf<T>,
82 frame_system::pallet_prelude::BlockNumberFor::<T>,
83 T::NftCollectionId>
84 >
85 + Mutate<T::AccountId, ItemConfig>,
86)]
87mod benchmarks {
88 use super::*;
89
90 #[benchmark]
91 fn fractionalize() {
92 let asset = T::BenchmarkHelper::asset(0);
93 let collection = T::BenchmarkHelper::collection(0);
94 let nft = T::BenchmarkHelper::nft(0);
95 let (caller, caller_lookup) = mint_nft::<T>(nft);
96
97 #[extrinsic_call]
98 _(
99 SystemOrigin::Signed(caller.clone()),
100 collection,
101 nft,
102 asset.clone(),
103 caller_lookup,
104 1000u32.into(),
105 );
106
107 assert_last_event::<T>(
108 Event::NftFractionalized {
109 nft_collection: collection,
110 nft,
111 fractions: 1000u32.into(),
112 asset,
113 beneficiary: caller,
114 }
115 .into(),
116 );
117 }
118
119 #[benchmark]
120 fn unify() {
121 let asset = T::BenchmarkHelper::asset(0);
122 let collection = T::BenchmarkHelper::collection(0);
123 let nft = T::BenchmarkHelper::nft(0);
124 let (caller, caller_lookup) = mint_nft::<T>(nft);
125
126 assert_ok!(NftFractionalization::<T>::fractionalize(
127 SystemOrigin::Signed(caller.clone()).into(),
128 collection,
129 nft,
130 asset.clone(),
131 caller_lookup.clone(),
132 1000u32.into(),
133 ));
134
135 #[extrinsic_call]
136 _(SystemOrigin::Signed(caller.clone()), collection, nft, asset.clone(), caller_lookup);
137
138 assert_last_event::<T>(
139 Event::NftUnified { nft_collection: collection, nft, asset, beneficiary: caller }
140 .into(),
141 );
142 }
143
144 impl_benchmark_test_suite!(
145 NftFractionalization,
146 crate::mock::new_test_ext(),
147 crate::mock::Test
148 );
149}