1234567891011121314151617#![cfg(feature = "runtime-benchmarks")]1819use frame_benchmarking::v2::*;20use frame_support::traits::{21 fungible::{Inspect, Mutate, Unbalanced},22 OnInitialize,23};24use frame_system::{pallet_prelude::*, RawOrigin};25use pallet_evm::account::CrossAccountId;26use pallet_evm_migration::Pallet as EvmMigrationPallet;27use pallet_unique::benchmarking::create_nft_collection;28use sp_core::{Get, H160};29use sp_runtime::{30 traits::{BlockNumberProvider, Bounded},31 Perbill,32};33use sp_std::{iter::Sum, vec, vec::Vec};3435use super::{BalanceOf, Call, Config, Pallet, Staked, PENDING_LIMIT_PER_BLOCK};36use crate::{pallet, Pallet as PromototionPallet};3738const SEED: u32 = 0;3940fn set_admin<T>() -> Result<T::AccountId, sp_runtime::DispatchError>41where42 T: Config + pallet_unique::Config + pallet_evm_migration::Config,43 BlockNumberFor<T>: From<u32> + Into<u32>,44 BalanceOf<T>: Sum + From<u128>,45{46 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);4748 <T as Config>::Currency::set_balance(49 &pallet_admin,50 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),51 );5253 PromototionPallet::<T>::set_admin_address(54 RawOrigin::Root.into(),55 T::CrossAccountId::from_sub(pallet_admin.clone()),56 )?;5758 Ok(pallet_admin)59}6061#[benchmarks(62 where T: Config + pallet_unique::Config + pallet_evm_migration::Config ,63 BlockNumberFor<T>: From<u32> + Into<u32>,64 BalanceOf<T>: Sum + From<u128>65)]66mod benchmarks {67 use super::*;6869 #[benchmark]70 fn on_initialize(b: Linear<0, PENDING_LIMIT_PER_BLOCK>) -> Result<(), BenchmarkError> {71 set_admin::<T>()?;7273 (0..b).try_for_each(|index| {74 let staker = account::<T::AccountId>("staker", index, SEED);75 <T as Config>::Currency::write_balance(76 &staker,77 Into::<BalanceOf<T>>::into(10_000u128) * T::Nominal::get(),78 )?;79 PromototionPallet::<T>::stake(80 RawOrigin::Signed(staker.clone()).into(),81 Into::<BalanceOf<T>>::into(100u128) * T::Nominal::get(),82 )?;83 PromototionPallet::<T>::unstake_all(RawOrigin::Signed(staker).into())?;84 Result::<(), sp_runtime::DispatchError>::Ok(())85 })?;86 let block_number =87 <frame_system::Pallet<T>>::current_block_number() + T::PendingInterval::get();8889 #[block]90 {91 PromototionPallet::<T>::on_initialize(block_number);92 }9394 Ok(())95 }9697 #[benchmark]98 fn set_admin_address() -> Result<(), BenchmarkError> {99 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);100 let _ = <T as Config>::Currency::set_balance(101 &pallet_admin,102 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),103 );104105 #[extrinsic_call]106 _(RawOrigin::Root, T::CrossAccountId::from_sub(pallet_admin));107108 Ok(())109 }110111 #[benchmark]112 fn payout_stakers(b: Linear<0, 100>) -> Result<(), BenchmarkError> {113 let pallet_admin = account::<T::AccountId>("admin", 1, SEED);114 PromototionPallet::<T>::set_admin_address(115 RawOrigin::Root.into(),116 T::CrossAccountId::from_sub(pallet_admin.clone()),117 )?;118 <T as Config>::Currency::write_balance(119 &pallet_admin,120 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),121 )?;122 <T as Config>::Currency::write_balance(123 &<T as pallet::Config>::TreasuryAccountId::get(),124 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),125 )?;126127 let stakers: Vec<T::AccountId> =128 (0..b).map(|index| account("staker", index, SEED)).collect();129 stakers.iter().try_for_each(|staker| {130 <T as Config>::Currency::write_balance(131 staker,132 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),133 )?;134 Result::<(), sp_runtime::DispatchError>::Ok(())135 })?;136 (1..11).try_for_each(|i| {137 <frame_system::Pallet<T>>::set_block_number(i.into());138 T::RelayBlockNumberProvider::set_block_number((2 * i).into());139 assert_eq!(<frame_system::Pallet<T>>::block_number(), i.into());140 assert_eq!(141 T::RelayBlockNumberProvider::current_block_number(),142 (2 * i).into()143 );144 stakers145 .iter()146 .map(|staker| {147 PromototionPallet::<T>::stake(148 RawOrigin::Signed(staker.clone()).into(),149 Into::<BalanceOf<T>>::into(100u128) * T::Nominal::get(),150 )151 })152 .collect::<Result<Vec<_>, _>>()?;153154 Result::<(), sp_runtime::DispatchError>::Ok(())155 })?;156157 let stakes = Staked::<T>::iter_prefix((&stakers[0],)).collect::<Vec<_>>();158 assert_eq!(stakes.len(), 10);159160 <frame_system::Pallet<T>>::set_block_number(15_000.into());161 T::RelayBlockNumberProvider::set_block_number(30_000.into());162163 #[extrinsic_call]164 _(RawOrigin::Signed(pallet_admin.clone()), Some(b as u8));165166 Ok(())167 }168169 #[benchmark]170 fn stake() -> Result<(), BenchmarkError> {171 let caller = account::<T::AccountId>("caller", 0, SEED);172 let share = Perbill::from_rational(1u32, 10);173174 let _ = <T as Config>::Currency::write_balance(175 &caller,176 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),177 );178179 #[extrinsic_call]180 _(181 RawOrigin::Signed(caller.clone()),182 share * <T as Config>::Currency::total_balance(&caller),183 );184185 Ok(())186 }187188 #[benchmark]189 fn unstake_all() -> Result<(), BenchmarkError> {190 let caller = account::<T::AccountId>("caller", 0, SEED);191 let share = Perbill::from_rational(1u32, 20);192 let _ = <T as Config>::Currency::write_balance(193 &caller,194 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),195 );196 (1..11)197 .map(|i| {198 199 <frame_system::Pallet<T>>::set_block_number(i.into());200 T::RelayBlockNumberProvider::set_block_number((2 * i).into());201 assert_eq!(<frame_system::Pallet<T>>::block_number(), i.into());202 assert_eq!(203 T::RelayBlockNumberProvider::current_block_number(),204 (2 * i).into()205 );206 PromototionPallet::<T>::stake(207 RawOrigin::Signed(caller.clone()).into(),208 share * <T as Config>::Currency::total_balance(&caller),209 )210 })211 .collect::<Result<Vec<_>, _>>()?;212213 #[extrinsic_call]214 _(RawOrigin::Signed(caller.clone()));215216 Ok(())217 }218219 #[benchmark]220 fn unstake_partial() -> Result<(), BenchmarkError> {221 let caller = account::<T::AccountId>("caller", 0, SEED);222 let _ = <T as Config>::Currency::write_balance(223 &caller,224 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),225 );226 (1..11)227 .map(|i| {228 229 <frame_system::Pallet<T>>::set_block_number(i.into());230 T::RelayBlockNumberProvider::set_block_number((2 * i).into());231 assert_eq!(<frame_system::Pallet<T>>::block_number(), i.into());232 assert_eq!(233 T::RelayBlockNumberProvider::current_block_number(),234 (2 * i).into()235 );236 PromototionPallet::<T>::stake(237 RawOrigin::Signed(caller.clone()).into(),238 Into::<BalanceOf<T>>::into(100u128) * T::Nominal::get(),239 )240 })241 .collect::<Result<Vec<_>, _>>()?;242243 #[extrinsic_call]244 _(245 RawOrigin::Signed(caller.clone()),246 Into::<BalanceOf<T>>::into(1000u128) * T::Nominal::get(),247 );248249 Ok(())250 }251252 #[benchmark]253 fn sponsor_collection() -> Result<(), BenchmarkError> {254 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);255 PromototionPallet::<T>::set_admin_address(256 RawOrigin::Root.into(),257 T::CrossAccountId::from_sub(pallet_admin.clone()),258 )?;259 let _ = <T as Config>::Currency::write_balance(260 &pallet_admin,261 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),262 );263 let caller: T::AccountId = account("caller", 0, SEED);264 let _ = <T as Config>::Currency::write_balance(265 &caller,266 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),267 );268 let collection = create_nft_collection::<T>(caller)?;269270 #[extrinsic_call]271 _(RawOrigin::Signed(pallet_admin.clone()), collection);272273 Ok(())274 }275276 #[benchmark]277 fn stop_sponsoring_collection() -> Result<(), BenchmarkError> {278 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);279 PromototionPallet::<T>::set_admin_address(280 RawOrigin::Root.into(),281 T::CrossAccountId::from_sub(pallet_admin.clone()),282 )?;283 let _ = <T as Config>::Currency::write_balance(284 &pallet_admin,285 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),286 );287 let caller: T::AccountId = account("caller", 0, SEED);288 let _ = <T as Config>::Currency::write_balance(289 &caller,290 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),291 );292 let collection = create_nft_collection::<T>(caller)?;293 PromototionPallet::<T>::sponsor_collection(294 RawOrigin::Signed(pallet_admin.clone()).into(),295 collection,296 )?;297298 #[extrinsic_call]299 _(RawOrigin::Signed(pallet_admin.clone()), collection);300301 Ok(())302 }303304 #[benchmark]305 fn sponsor_contract() -> Result<(), BenchmarkError> {306 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);307 PromototionPallet::<T>::set_admin_address(308 RawOrigin::Root.into(),309 T::CrossAccountId::from_sub(pallet_admin.clone()),310 )?;311312 let _ = <T as Config>::Currency::write_balance(313 &pallet_admin,314 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),315 );316 let address = H160::from_low_u64_be(SEED as u64);317 let data: Vec<u8> = (0..20).collect();318 <EvmMigrationPallet<T>>::begin(RawOrigin::Root.into(), address)?;319 <EvmMigrationPallet<T>>::finish(RawOrigin::Root.into(), address, data)?;320321 #[extrinsic_call]322 _(RawOrigin::Signed(pallet_admin.clone()), address);323324 Ok(())325 }326327 #[benchmark]328 fn stop_sponsoring_contract() -> Result<(), BenchmarkError> {329 let pallet_admin = account::<T::AccountId>("admin", 0, SEED);330 PromototionPallet::<T>::set_admin_address(331 RawOrigin::Root.into(),332 T::CrossAccountId::from_sub(pallet_admin.clone()),333 )?;334335 let _ = <T as Config>::Currency::write_balance(336 &pallet_admin,337 Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value(),338 );339 let address = H160::from_low_u64_be(SEED as u64);340 let data: Vec<u8> = (0..20).collect();341 <EvmMigrationPallet<T>>::begin(RawOrigin::Root.into(), address)?;342 <EvmMigrationPallet<T>>::finish(RawOrigin::Root.into(), address, data)?;343 PromototionPallet::<T>::sponsor_contract(344 RawOrigin::Signed(pallet_admin.clone()).into(),345 address,346 )?;347348 #[extrinsic_call]349 _(RawOrigin::Signed(pallet_admin.clone()), address);350351 Ok(())352 }353}