git.delta.rocks / unique-network / refs/commits / bce2cc1e9ab8

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 super::*;20use crate::Pallet;21use frame_system::RawOrigin;22use frame_support::traits::{fungible::Balanced, Get, tokens::Precision};23use frame_benchmarking::{benchmarks, account};24use sp_runtime::DispatchError;25use pallet_common::{26	Config as CommonConfig,27	benchmarking::{create_data, create_u16_data},28};29use up_data_structs::{30	CollectionId, CollectionMode, MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH,31	MAX_COLLECTION_DESCRIPTION_LENGTH, CollectionLimits,32};33use pallet_common::erc::CrossAccountId;3435const SEED: u32 = 1;3637fn create_collection_helper<T: Config>(38	owner: T::AccountId,39	mode: CollectionMode,40) -> Result<CollectionId, DispatchError> {41	let _ = <T as CommonConfig>::Currency::deposit(42		&owner,43		T::CollectionCreationPrice::get(),44		Precision::Exact,45	)46	.unwrap();47	let col_name = create_u16_data::<{ MAX_COLLECTION_NAME_LENGTH }>();48	let col_desc = create_u16_data::<{ MAX_COLLECTION_DESCRIPTION_LENGTH }>();49	let token_prefix = create_data::<{ MAX_TOKEN_PREFIX_LENGTH }>();50	<Pallet<T>>::create_collection(51		RawOrigin::Signed(owner).into(),52		col_name,53		col_desc,54		token_prefix,55		mode,56	)?;57	Ok(<pallet_common::CreatedCollectionCount<T>>::get())58}59pub fn create_nft_collection<T: Config>(60	owner: T::AccountId,61) -> Result<CollectionId, DispatchError> {62	create_collection_helper::<T>(owner, CollectionMode::NFT)63}6465benchmarks! {66	create_collection {67		let col_name = create_u16_data::<{MAX_COLLECTION_NAME_LENGTH}>();68		let col_desc = create_u16_data::<{MAX_COLLECTION_DESCRIPTION_LENGTH}>();69		let token_prefix = create_data::<{MAX_TOKEN_PREFIX_LENGTH}>();70		let mode: CollectionMode = CollectionMode::NFT;71		let caller: T::AccountId = account("caller", 0, SEED);72		let _ = <T as CommonConfig>::Currency::deposit(&caller, T::CollectionCreationPrice::get(), Precision::Exact).unwrap();73	}: _(RawOrigin::Signed(caller.clone()), col_name, col_desc, token_prefix, mode)74	verify {75		assert_eq!(<pallet_common::CollectionById<T>>::get(CollectionId(1)).unwrap().owner, caller);76	}7778	destroy_collection {79		let caller: T::AccountId = account("caller", 0, SEED);80		let collection = create_nft_collection::<T>(caller.clone())?;81	}: _(RawOrigin::Signed(caller.clone()), collection)8283	add_to_allow_list {84		let caller: T::AccountId = account("caller", 0, SEED);85		let allowlist_account: T::AccountId = account("admin", 0, SEED);86		let collection = create_nft_collection::<T>(caller.clone())?;87	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))8889	remove_from_allow_list {90		let caller: T::AccountId = account("caller", 0, SEED);91		let allowlist_account: T::AccountId = account("admin", 0, SEED);92		let collection = create_nft_collection::<T>(caller.clone())?;93		<Pallet<T>>::add_to_allow_list(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(allowlist_account.clone()))?;94	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(allowlist_account))9596	change_collection_owner {97		let caller: T::AccountId = account("caller", 0, SEED);98		let collection = create_nft_collection::<T>(caller.clone())?;99		let new_owner: T::AccountId = account("admin", 0, SEED);100	}: _(RawOrigin::Signed(caller.clone()), collection, new_owner)101102	add_collection_admin {103		let caller: T::AccountId = account("caller", 0, SEED);104		let collection = create_nft_collection::<T>(caller.clone())?;105		let new_admin: T::AccountId = account("admin", 0, SEED);106	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))107108	remove_collection_admin {109		let caller: T::AccountId = account("caller", 0, SEED);110		let collection = create_nft_collection::<T>(caller.clone())?;111		let new_admin: T::AccountId = account("admin", 0, SEED);112		<Pallet<T>>::add_collection_admin(RawOrigin::Signed(caller.clone()).into(), collection, T::CrossAccountId::from_sub(new_admin.clone()))?;113	}: _(RawOrigin::Signed(caller.clone()), collection, T::CrossAccountId::from_sub(new_admin))114115	set_collection_sponsor {116		let caller: T::AccountId = account("caller", 0, SEED);117		let collection = create_nft_collection::<T>(caller.clone())?;118	}: _(RawOrigin::Signed(caller.clone()), collection, caller.clone())119120	confirm_sponsorship {121		let caller: T::AccountId = account("caller", 0, SEED);122		let collection = create_nft_collection::<T>(caller.clone())?;123		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;124	}: _(RawOrigin::Signed(caller.clone()), collection)125126	remove_collection_sponsor {127		let caller: T::AccountId = account("caller", 0, SEED);128		let collection = create_nft_collection::<T>(caller.clone())?;129		<Pallet<T>>::set_collection_sponsor(RawOrigin::Signed(caller.clone()).into(), collection, caller.clone())?;130		<Pallet<T>>::confirm_sponsorship(RawOrigin::Signed(caller.clone()).into(), collection)?;131	}: _(RawOrigin::Signed(caller.clone()), collection)132133	set_transfers_enabled_flag {134		let caller: T::AccountId = account("caller", 0, SEED);135		let collection = create_nft_collection::<T>(caller.clone())?;136	}: _(RawOrigin::Signed(caller.clone()), collection, false)137138139	set_collection_limits {140		let caller: T::AccountId = account("caller", 0, SEED);141		let collection = create_nft_collection::<T>(caller.clone())?;142143		let cl = CollectionLimits {144			account_token_ownership_limit: Some(0),145			sponsored_data_size: Some(0),146			token_limit: Some(1),147			sponsor_transfer_timeout: Some(0),148			sponsor_approve_timeout: None,149			owner_can_destroy: Some(true),150			owner_can_transfer: Some(true),151			sponsored_data_rate_limit: None,152			transfers_enabled: Some(true),153		};154	}: set_collection_limits(RawOrigin::Signed(caller.clone()), collection, cl)155156	force_repair_collection {157		let caller: T::AccountId = account("caller", 0, SEED);158		let collection = create_nft_collection::<T>(caller)?;159	}: _(RawOrigin::Root, collection)160}