git.delta.rocks / unique-network / refs/commits / 4458a1dde4ff

difftreelog

source

pallets/unique/src/benchmarking.rs6.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg(feature = "runtime-benchmarks")]1819use super::*;20use crate::Pallet;21use frame_system::RawOrigin;22use frame_support::traits::{tokens::currency::Currency, Get};23use frame_benchmarking::{benchmarks, account};24use sp_runtime::DispatchError;25use pallet_common::{26	Config as CommonConfig,27	benchmarking::{create_data, create_u16_data},28};2930const SEED: u32 = 1;3132fn create_collection_helper<T: Config>(33	owner: T::AccountId,34	mode: CollectionMode,35) -> Result<CollectionId, DispatchError> {36	<T as CommonConfig>::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());37	let col_name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();38	let col_desc = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();39	let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();40	<Pallet<T>>::create_collection(41		RawOrigin::Signed(owner).into(),42		col_name,43		col_desc,44		token_prefix,45		mode,46	)?;47	Ok(<pallet_common::CreatedCollectionCount<T>>::get())48}49fn create_nft_collection<T: Config>(owner: T::AccountId) -> Result<CollectionId, DispatchError> {50	create_collection_helper::<T>(owner, CollectionMode::NFT)51}5253benchmarks! {54	create_collection {55		let col_name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();56		let col_desc = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();57		let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();58		let mode: CollectionMode = CollectionMode::NFT;59		let caller: T::AccountId = account("caller", 0, SEED);60		<T as CommonConfig>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());61	}: _(RawOrigin::Signed(caller.clone()), col_name.clone(), col_desc.clone(), token_prefix.clone(), mode)62	verify {63		assert_eq!(<pallet_common::CollectionById<T>>::get(CollectionId(1)).unwrap().owner, caller);64	}6566	destroy_collection {67		let caller: T::AccountId = account("caller", 0, SEED);68		let collection = create_nft_collection::<T>(caller.clone())?;69	}: _(RawOrigin::Signed(caller.clone()), collection)7071	add_to_allow_list {72		let caller: T::AccountId = account("caller", 0, SEED);73		let allowlist_account: T::AccountId = account("admin", 0, SEED);74		let collection = create_nft_collection::<T>(caller.clone())?;75	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))7677	remove_from_allow_list {78		let caller: T::AccountId = account("caller", 0, SEED);79		let allowlist_account: T::AccountId = account("admin", 0, SEED);80		let collection = create_nft_collection::<T>(caller.clone())?;81		<Pallet<T>>::add_to_allow_list(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(allowlist_account.clone()))?;82	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))8384	change_collection_owner {85		let caller: T::AccountId = account("caller", 0, SEED);86		let collection = create_nft_collection::<T>(caller.clone())?;87		let new_owner: T::AccountId = account("admin", 0, SEED);88	}: _(RawOrigin::Signed(caller.clone()), collection, new_owner)8990	add_collection_admin {91		let caller: T::AccountId = account("caller", 0, SEED);92		let collection = create_nft_collection::<T>(caller.clone())?;93		let new_admin: T::AccountId = account("admin", 0, SEED);94	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))9596	remove_collection_admin {97		let caller: T::AccountId = account("caller", 0, SEED);98		let collection = create_nft_collection::<T>(caller.clone())?;99		let new_admin: T::AccountId = account("admin", 0, SEED);100		<Pallet<T>>::add_collection_admin(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(new_admin.clone()))?;101	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))102103	set_collection_sponsor {104		let caller: T::AccountId = account("caller", 0, SEED);105		let collection = create_nft_collection::<T>(caller.clone())?;106	}: _(RawOrigin::Signed(caller.clone()), collection, caller.clone())107108	confirm_sponsorship {109		let caller: T::AccountId = account("caller", 0, SEED);110		let collection = create_nft_collection::<T>(caller.clone())?;111		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;112	}: _(RawOrigin::Signed(caller.clone()), collection)113114	remove_collection_sponsor {115		let caller: T::AccountId = account("caller", 0, SEED);116		let collection = create_nft_collection::<T>(caller.clone())?;117		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;118		<Pallet<T>>::confirm_sponsorship(RawOrigin::Signed(caller.clone()).into(), collection)?;119	}: _(RawOrigin::Signed(caller.clone()), collection)120121	set_transfers_enabled_flag {122		let caller: T::AccountId = account("caller", 0, SEED);123		let collection = create_nft_collection::<T>(caller.clone())?;124	}: _(RawOrigin::Signed(caller.clone()), collection, false)125126127	set_collection_limits{128		let caller: T::AccountId = account("caller", 0, SEED);129		let collection = create_nft_collection::<T>(caller.clone())?;130131		let cl = CollectionLimits {132			account_token_ownership_limit: Some(0),133			sponsored_data_size: Some(0),134			token_limit: Some(1),135			sponsor_transfer_timeout: Some(0),136			sponsor_approve_timeout: None,137			owner_can_destroy: Some(true),138			owner_can_transfer: Some(true),139			sponsored_data_rate_limit: None,140			transfers_enabled: Some(true),141		};142	}: set_collection_limits(RawOrigin::Signed(caller.clone()), collection, cl)143}