git.delta.rocks / unique-network / refs/commits / 900be63a359f

difftreelog

source

pallets/unique/src/benchmarking.rs6.5 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 frame_benchmarking::{account, benchmarks};20use frame_support::traits::{fungible::Balanced, tokens::Precision, Get};21use frame_system::RawOrigin;22use pallet_common::{23	benchmarking::{create_data, create_u16_data},24	erc::CrossAccountId,25	Config as CommonConfig,26};27use sp_runtime::DispatchError;28use up_data_structs::{29	CollectionId, CollectionLimits, CollectionMode, MAX_COLLECTION_DESCRIPTION_LENGTH,30	MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH,31};3233use super::*;34use crate::Pallet;3536const SEED: u32 = 1;3738fn create_collection_helper<T: Config>(39	owner: T::AccountId,40	mode: CollectionMode,41) -> Result<CollectionId, DispatchError> {42	let _ = <T as CommonConfig>::Currency::deposit(43		&owner,44		T::CollectionCreationPrice::get(),45		Precision::Exact,46	)47	.unwrap();48	let col_name = create_u16_data::<{ MAX_COLLECTION_NAME_LENGTH }>();49	let col_desc = create_u16_data::<{ MAX_COLLECTION_DESCRIPTION_LENGTH }>();50	let token_prefix = create_data::<{ MAX_TOKEN_PREFIX_LENGTH }>();51	<Pallet<T>>::create_collection(52		RawOrigin::Signed(owner).into(),53		col_name,54		col_desc,55		token_prefix,56		mode,57	)?;58	Ok(<pallet_common::CreatedCollectionCount<T>>::get())59}60pub fn create_nft_collection<T: Config>(61	owner: T::AccountId,62) -> Result<CollectionId, DispatchError> {63	create_collection_helper::<T>(owner, CollectionMode::NFT)64}6566benchmarks! {67	create_collection {68		let col_name = create_u16_data::<{MAX_COLLECTION_NAME_LENGTH}>();69		let col_desc = create_u16_data::<{MAX_COLLECTION_DESCRIPTION_LENGTH}>();70		let token_prefix = create_data::<{MAX_TOKEN_PREFIX_LENGTH}>();71		let mode: CollectionMode = CollectionMode::NFT;72		let caller: T::AccountId = account("caller", 0, SEED);73		let _ = <T as CommonConfig>::Currency::deposit(&caller, T::CollectionCreationPrice::get(), Precision::Exact).unwrap();74	}: _(RawOrigin::Signed(caller.clone()), col_name, col_desc, token_prefix, mode)75	verify {76		assert_eq!(<pallet_common::CollectionById<T>>::get(CollectionId(1)).unwrap().owner, caller);77	}7879	destroy_collection {80		let caller: T::AccountId = account("caller", 0, SEED);81		let collection = create_nft_collection::<T>(caller.clone())?;82	}: _(RawOrigin::Signed(caller.clone()), collection)8384	add_to_allow_list {85		let caller: T::AccountId = account("caller", 0, SEED);86		let allowlist_account: T::AccountId = account("admin", 0, SEED);87		let collection = create_nft_collection::<T>(caller.clone())?;88	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))8990	remove_from_allow_list {91		let caller: T::AccountId = account("caller", 0, SEED);92		let allowlist_account: T::AccountId = account("admin", 0, SEED);93		let collection = create_nft_collection::<T>(caller.clone())?;94		<Pallet<T>>::add_to_allow_list(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(allowlist_account.clone()))?;95	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))9697	change_collection_owner {98		let caller: T::AccountId = account("caller", 0, SEED);99		let collection = create_nft_collection::<T>(caller.clone())?;100		let new_owner: T::AccountId = account("admin", 0, SEED);101	}: _(RawOrigin::Signed(caller.clone()), collection, new_owner)102103	add_collection_admin {104		let caller: T::AccountId = account("caller", 0, SEED);105		let collection = create_nft_collection::<T>(caller.clone())?;106		let new_admin: T::AccountId = account("admin", 0, SEED);107	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))108109	remove_collection_admin {110		let caller: T::AccountId = account("caller", 0, SEED);111		let collection = create_nft_collection::<T>(caller.clone())?;112		let new_admin: T::AccountId = account("admin", 0, SEED);113		<Pallet<T>>::add_collection_admin(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(new_admin.clone()))?;114	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))115116	set_collection_sponsor {117		let caller: T::AccountId = account("caller", 0, SEED);118		let collection = create_nft_collection::<T>(caller.clone())?;119	}: _(RawOrigin::Signed(caller.clone()), collection, caller.clone())120121	confirm_sponsorship {122		let caller: T::AccountId = account("caller", 0, SEED);123		let collection = create_nft_collection::<T>(caller.clone())?;124		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;125	}: _(RawOrigin::Signed(caller.clone()), collection)126127	remove_collection_sponsor {128		let caller: T::AccountId = account("caller", 0, SEED);129		let collection = create_nft_collection::<T>(caller.clone())?;130		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;131		<Pallet<T>>::confirm_sponsorship(RawOrigin::Signed(caller.clone()).into(), collection)?;132	}: _(RawOrigin::Signed(caller.clone()), collection)133134	set_transfers_enabled_flag {135		let caller: T::AccountId = account("caller", 0, SEED);136		let collection = create_nft_collection::<T>(caller.clone())?;137	}: _(RawOrigin::Signed(caller.clone()), collection, false)138139140	set_collection_limits {141		let caller: T::AccountId = account("caller", 0, SEED);142		let collection = create_nft_collection::<T>(caller.clone())?;143144		let cl = CollectionLimits {145			account_token_ownership_limit: Some(0),146			sponsored_data_size: Some(0),147			token_limit: Some(1),148			sponsor_transfer_timeout: Some(0),149			sponsor_approve_timeout: None,150			owner_can_destroy: Some(true),151			owner_can_transfer: Some(true),152			sponsored_data_rate_limit: None,153			transfers_enabled: Some(true),154		};155	}: set_collection_limits(RawOrigin::Signed(caller.clone()), collection, cl)156157	force_repair_collection {158		let caller: T::AccountId = account("caller", 0, SEED);159		let collection = create_nft_collection::<T>(caller)?;160	}: _(RawOrigin::Root, collection)161}