git.delta.rocks / unique-network / refs/commits / 4e1bd65c37e5

difftreelog

feat(nonfungible) benchmarking

Yaroslav Bolyukin2021-10-22parent: #54a17a4.patch.diff
in: master

5 files changed

modifiedpallets/nonfungible/Cargo.tomldiffbeforeafterboth
--- a/pallets/nonfungible/Cargo.toml
+++ b/pallets/nonfungible/Cargo.toml
@@ -20,6 +20,7 @@
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
 ethereum = { default-features = false, version = "0.9.0" }
+frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 
 [features]
 default = ["std"]
@@ -33,5 +34,10 @@
     "evm-coder/std",
     "ethereum/std",
     "pallet-evm-coder-substrate/std",
+    'frame-benchmarking/std',
+]
+runtime-benchmarks = [
+    'frame-benchmarking',
+    'frame-support/runtime-benchmarks',
+    'frame-system/runtime-benchmarks',
 ]
-runtime-benchmarks = []
modifiedpallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/benchmarking.rs
+++ b/pallets/nonfungible/src/benchmarking.rs
@@ -1 +1,101 @@
-#![cfg(feature = "runtime-benchmarking")]
+use super::*;
+use crate::{Pallet, Config, NonfungibleHandle};
+
+use sp_std::prelude::*;
+use pallet_common::benchmarking::{create_collection_raw, create_data};
+use frame_benchmarking::{benchmarks, account};
+use nft_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH};
+use pallet_common::bench_init;
+use core::convert::TryInto;
+
+const SEED: u32 = 1;
+
+fn create_max_item_data<T: Config>(owner: T::CrossAccountId) -> CreateItemData<T> {
+	let const_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();
+	let variable_data = create_data(CUSTOM_DATA_LIMIT as usize).try_into().unwrap();
+	CreateItemData {
+		const_data,
+		variable_data,
+		owner,
+	}
+}
+fn create_max_item<T: Config>(
+	collection: &NonfungibleHandle<T>,
+	sender: &T::CrossAccountId,
+	owner: T::CrossAccountId,
+) -> Result<TokenId, DispatchError> {
+	<Pallet<T>>::create_item(&collection, sender, create_max_item_data(owner))?;
+	Ok(TokenId(<TokensMinted<T>>::get(&collection.id)))
+}
+
+fn create_collection<T: Config>(
+	owner: T::AccountId,
+) -> Result<NonfungibleHandle<T>, DispatchError> {
+	create_collection_raw(
+		owner,
+		CollectionMode::NFT,
+		<Pallet<T>>::init_collection,
+		NonfungibleHandle::cast,
+	)
+}
+
+benchmarks! {
+	create_item {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			sender: cross_from_sub(owner); to: cross_sub;
+		};
+	}: {create_max_item(&collection, &sender, to.clone())?}
+
+	create_multiple_items {
+		let b in 0..MAX_ITEMS_PER_BATCH;
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			sender: cross_from_sub(owner); to: cross_sub;
+		};
+		let data = (0..b).map(|_| create_max_item_data(to.clone())).collect();
+	}: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
+
+	burn_item {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			sender: cross_from_sub(owner); burner: cross_sub;
+		};
+		let item = create_max_item(&collection, &sender, burner.clone())?;
+	}: {<Pallet<T>>::burn(&collection, &burner, item)?}
+
+	transfer {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; receiver: cross_sub;
+		};
+		let item = create_max_item(&collection, &owner, sender.clone())?;
+	}: {<Pallet<T>>::transfer(&collection, &sender, &receiver, item)?}
+
+	approve {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; spender: cross_sub;
+		};
+		let item = create_max_item(&collection, &owner, sender.clone())?;
+	}: {<Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?}
+
+	transfer_from {
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
+		};
+		let item = create_max_item(&collection, &owner, sender.clone())?;
+		<Pallet<T>>::set_allowance(&collection, &sender, item, Some(&spender))?;
+	}: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, item)?}
+
+	set_variable_metadata {
+		let b in 0..CUSTOM_DATA_LIMIT;
+		bench_init!{
+			owner: sub; collection: collection(owner);
+			owner: cross_from_sub; sender: cross_sub;
+		};
+		let item = create_max_item(&collection, &owner, sender.clone())?;
+		let data = create_data(b as usize);
+	}: {<Pallet<T>>::set_variable_metadata(&collection, &sender, item, data)?}
+}
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -9,8 +9,8 @@
 use sp_std::vec::Vec;
 
 use crate::{
-	AccountBalance, Allowance, Config, CreateItemData, DataKind, Error, NonfungibleHandle, Owned,
-	Owner, Pallet, SelfWeightOf, TokenData, weights::WeightInfo,
+	AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,
+	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,
 };
 
 pub struct CommonWeights<T: Config>(PhantomData<T>);
@@ -39,8 +39,8 @@
 		<SelfWeightOf<T>>::transfer_from()
 	}
 
-	fn set_variable_metadata(_bytes: u32) -> Weight {
-		<SelfWeightOf<T>>::set_variable_metadata()
+	fn set_variable_metadata(bytes: u32) -> Weight {
+		<SelfWeightOf<T>>::set_variable_metadata(bytes)
 	}
 }
 
@@ -67,7 +67,7 @@
 	) -> DispatchResultWithPostInfo {
 		with_weight(
 			<Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),
-			<SelfWeightOf<T>>::create_item(),
+			<CommonWeights<T>>::create_item(),
 		)
 	}
 
@@ -85,7 +85,7 @@
 		let amount = data.len();
 		with_weight(
 			<Pallet<T>>::create_multiple_items(self, &sender, data),
-			<SelfWeightOf<T>>::create_multiple_items(amount as u32),
+			<CommonWeights<T>>::create_multiple_items(amount as u32),
 		)
 	}
 
@@ -99,7 +99,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::burn(&self, &sender, token),
-				<SelfWeightOf<T>>::burn_item(),
+				<CommonWeights<T>>::burn_item(),
 			)
 		} else {
 			Ok(().into())
@@ -117,7 +117,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::transfer(&self, &from, &to, token),
-				<SelfWeightOf<T>>::transfer(),
+				<CommonWeights<T>>::transfer(),
 			)
 		} else {
 			Ok(().into())
@@ -139,7 +139,7 @@
 			} else {
 				<Pallet<T>>::set_allowance(&self, &sender, token, None)
 			},
-			<SelfWeightOf<T>>::approve(),
+			<CommonWeights<T>>::approve(),
 		)
 	}
 
@@ -156,7 +156,7 @@
 		if amount == 1 {
 			with_weight(
 				<Pallet<T>>::transfer_from(&self, &sender, &from, &to, token),
-				<SelfWeightOf<T>>::transfer_from(),
+				<CommonWeights<T>>::transfer_from(),
 			)
 		} else {
 			Ok(().into())
@@ -169,9 +169,10 @@
 		token: TokenId,
 		data: Vec<u8>,
 	) -> DispatchResultWithPostInfo {
+		let len = data.len();
 		with_weight(
 			<Pallet<T>>::set_variable_metadata(&self, &sender, token, data),
-			<SelfWeightOf<T>>::set_variable_metadata(),
+			<CommonWeights<T>>::set_variable_metadata(len as u32),
 		)
 	}
 
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
after · pallets/nonfungible/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23use erc::ERC721Events;4use frame_support::{BoundedVec, ensure};5use nft_data_structs::{6	AccessMode, CUSTOM_DATA_LIMIT, Collection, CollectionId, CustomDataLimit, TokenId,7};8use pallet_common::{9	Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, account::CrossAccountId,10};11use sp_core::H160;12use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};13use sp_std::{vec::Vec, vec};14use core::ops::Deref;15use sp_std::collections::btree_map::BTreeMap;16use codec::{Encode, Decode};1718pub use pallet::*;19#[cfg(feature = "runtime-benchmarks")]20pub mod benchmarking;21pub mod common;22pub mod erc;23pub mod weights;2425pub struct CreateItemData<T: Config> {26	pub const_data: BoundedVec<u8, CustomDataLimit>,27	pub variable_data: BoundedVec<u8, CustomDataLimit>,28	pub owner: T::CrossAccountId,29}30pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;3132#[derive(Encode, Decode)]33pub struct ItemData<T: Config> {34	pub const_data: Vec<u8>,35	pub variable_data: Vec<u8>,36	pub owner: T::CrossAccountId,37}3839#[frame_support::pallet]40pub mod pallet {41	use super::*;42	use frame_support::{Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};43	use nft_data_structs::{CollectionId, TokenId};44	use super::weights::WeightInfo;4546	#[pallet::error]47	pub enum Error<T> {48		/// Not Nonfungible item data used to mint in Nonfungible collection.49		NotNonfungibleDataUsedToMintFungibleCollectionToken,50		/// Used amount > 1 with NFT51		NonfungibleItemsHaveNoAmount,52	}5354	#[pallet::config]55	pub trait Config: frame_system::Config + pallet_common::Config {56		type WeightInfo: WeightInfo;57	}5859	#[pallet::pallet]60	#[pallet::generate_store(pub(super) trait Store)]61	pub struct Pallet<T>(_);6263	#[pallet::storage]64	pub(super) type TokensMinted<T: Config> =65		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;66	#[pallet::storage]67	pub(super) type TokensBurnt<T: Config> =68		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;6970	#[pallet::storage]71	pub(super) type TokenData<T: Config> = StorageNMap<72		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),73		Value = ItemData<T>,74		QueryKind = OptionQuery,75	>;7677	/// Used to enumerate tokens owned by account78	#[pallet::storage]79	pub(super) type Owned<T: Config> = StorageNMap<80		Key = (81			Key<Twox64Concat, CollectionId>,82			Key<Blake2_128Concat, T::AccountId>,83			Key<Twox64Concat, TokenId>,84		),85		Value = bool,86		QueryKind = ValueQuery,87	>;8889	#[pallet::storage]90	pub(super) type AccountBalance<T: Config> = StorageNMap<91		Key = (92			Key<Twox64Concat, CollectionId>,93			Key<Blake2_128Concat, T::AccountId>,94		),95		Value = u32,96		QueryKind = ValueQuery,97	>;9899	#[pallet::storage]100	pub(super) type Allowance<T: Config> = StorageNMap<101		Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),102		Value = T::CrossAccountId,103		QueryKind = OptionQuery,104	>;105}106107pub struct NonfungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);108impl<T: Config> NonfungibleHandle<T> {109	pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {110		Self(inner)111	}112	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {113		self.0114	}115}116impl<T: Config> Deref for NonfungibleHandle<T> {117	type Target = pallet_common::CollectionHandle<T>;118119	fn deref(&self) -> &Self::Target {120		&self.0121	}122}123124impl<T: Config> Pallet<T> {125	pub fn total_supply(collection: &NonfungibleHandle<T>) -> u32 {126		<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)127	}128	pub fn token_exists(collection: &NonfungibleHandle<T>, token: TokenId) -> bool {129		<TokenData<T>>::contains_key((collection.id, token))130	}131}132133// unchecked calls skips any permission checks134impl<T: Config> Pallet<T> {135	pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {136		PalletCommon::init_collection(data)137	}138	pub fn destroy_collection(139		collection: NonfungibleHandle<T>,140		sender: &T::CrossAccountId,141	) -> DispatchResult {142		let id = collection.id;143144		// =========145146		PalletCommon::destroy_collection(collection.0, sender)?;147148		<TokenData<T>>::remove_prefix((id,), None);149		<Owned<T>>::remove_prefix((id,), None);150		<TokensMinted<T>>::remove(id);151		<TokensBurnt<T>>::remove(id);152		<Allowance<T>>::remove_prefix((id,), None);153		<AccountBalance<T>>::remove_prefix((id,), None);154		Ok(())155	}156157	pub fn burn(158		collection: &NonfungibleHandle<T>,159		sender: &T::CrossAccountId,160		token: TokenId,161	) -> DispatchResult {162		let token_data = <TokenData<T>>::get((collection.id, token))163			.ok_or_else(|| <CommonError<T>>::TokenNotFound)?;164		ensure!(165			&token_data.owner == sender166				|| (collection.limits.owner_can_transfer167					&& collection.is_owner_or_admin(sender)?),168			<CommonError<T>>::NoPermission169		);170171		if collection.access == AccessMode::WhiteList {172			collection.check_allowlist(sender)?;173		}174175		let burnt = <TokensBurnt<T>>::get(collection.id)176			.checked_add(1)177			.ok_or(ArithmeticError::Overflow)?;178179		// =========180181		<Owned<T>>::remove((collection.id, token_data.owner.as_sub(), token));182		<TokensBurnt<T>>::insert(collection.id, burnt);183		<TokenData<T>>::remove((collection.id, token));184		let old_spender = <Allowance<T>>::take((collection.id, token));185186		if let Some(old_spender) = old_spender {187			<PalletCommon<T>>::deposit_event(CommonEvent::Approved(188				collection.id,189				token,190				sender.clone(),191				old_spender.clone(),192				0,193			));194		}195196		collection.log_infallible(ERC721Events::Transfer {197			from: *token_data.owner.as_eth(),198			to: H160::default(),199			token_id: token.into(),200		});201		<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(202			collection.id,203			token,204			token_data.owner,205			1,206		));207		return Ok(());208	}209210	pub fn transfer(211		collection: &NonfungibleHandle<T>,212		from: &T::CrossAccountId,213		to: &T::CrossAccountId,214		token: TokenId,215	) -> DispatchResult {216		ensure!(217			collection.transfers_enabled,218			<CommonError<T>>::TransferNotAllowed219		);220221		let token_data = <TokenData<T>>::get((collection.id, token))222			.ok_or_else(|| <CommonError<T>>::TokenNotFound)?;223		ensure!(224			&token_data.owner == from225				|| (collection.limits.owner_can_transfer && collection.is_owner_or_admin(from)?),226			<CommonError<T>>::NoPermission227		);228229		if collection.access == AccessMode::WhiteList {230			collection.check_allowlist(from)?;231			collection.check_allowlist(to)?;232		}233		<PalletCommon<T>>::ensure_correct_receiver(to)?;234235		let balance_from = <AccountBalance<T>>::get((collection.id, from.as_sub()))236			.checked_sub(1)237			.ok_or(<CommonError<T>>::TokenValueTooLow)?;238		let balance_to = if from != to {239			let balance_to = <AccountBalance<T>>::get((collection.id, to.as_sub()))240				.checked_add(1)241				.ok_or(ArithmeticError::Overflow)?;242243			ensure!(244				balance_to < collection.limits.account_token_ownership_limit(),245				<CommonError<T>>::AccountTokenLimitExceeded,246			);247248			Some(balance_to)249		} else {250			None251		};252253		collection.consume_sstores(4)?;254		collection.consume_log(3, 0)?;255256		// =========257258		<TokenData<T>>::insert(259			(collection.id, token),260			ItemData {261				owner: to.clone(),262				..token_data263			},264		);265266		if let Some(balance_to) = balance_to {267			// from != to268			if balance_from == 0 {269				<AccountBalance<T>>::remove((collection.id, from.as_sub()));270			} else {271				<AccountBalance<T>>::insert((collection.id, from.as_sub()), balance_from);272			}273			<AccountBalance<T>>::insert((collection.id, to.as_sub()), balance_to);274			<Owned<T>>::remove((collection.id, from.as_sub(), token));275			<Owned<T>>::insert((collection.id, to.as_sub(), token), true);276		}277		Self::set_allowance_unchecked(collection, from, token, None);278279		collection.log_infallible(ERC721Events::Transfer {280			from: *from.as_eth(),281			to: *to.as_eth(),282			token_id: token.into(),283		});284		<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(285			collection.id,286			token,287			from.clone(),288			to.clone(),289			1,290		));291		Ok(())292	}293294	pub fn create_multiple_items(295		collection: &NonfungibleHandle<T>,296		sender: &T::CrossAccountId,297		data: Vec<CreateItemData<T>>,298	) -> DispatchResult {299		let unrestricted_minting = collection.is_owner_or_admin(sender)?;300		if !unrestricted_minting {301			ensure!(302				collection.mint_mode,303				<CommonError<T>>::PublicMintingNotAllowed304			);305			collection.check_allowlist(sender)?;306307			for item in data.iter() {308				collection.check_allowlist(&item.owner)?;309			}310		}311312		for data in data.iter() {313			<PalletCommon<T>>::ensure_correct_receiver(&data.owner)?;314			if !data.const_data.is_empty() {315				collection.consume_sstore()?;316			}317			if !data.variable_data.is_empty() {318				collection.consume_sstore()?;319			}320			collection.consume_sstore()?;321			collection.consume_log(3, 0)?;322		}323324		let first_token = <TokensMinted<T>>::get(collection.id);325		let tokens_minted = first_token326			.checked_add(data.len() as u32)327			.ok_or(ArithmeticError::Overflow)?;328		ensure!(329			tokens_minted < collection.limits.token_limit,330			<CommonError<T>>::CollectionTokenLimitExceeded331		);332		collection.consume_sstore()?;333334		let mut balances = BTreeMap::new();335		for data in &data {336			let balance = balances337				.entry(data.owner.as_sub())338				.or_insert_with(|| <AccountBalance<T>>::get((collection.id, data.owner.as_sub())));339			*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;340341			ensure!(342				*balance <= collection.limits.account_token_ownership_limit(),343				<CommonError<T>>::AccountTokenLimitExceeded,344			);345		}346		collection.consume_sstores(balances.len())?;347348		// =========349350		<TokensMinted<T>>::insert(collection.id, tokens_minted);351		for (account, balance) in balances {352			<AccountBalance<T>>::insert((collection.id, account), balance);353		}354		for (i, data) in data.into_iter().enumerate() {355			let token = first_token + i as u32 + 1;356357			<TokenData<T>>::insert(358				(collection.id, token),359				ItemData {360					const_data: data.const_data.into(),361					variable_data: data.variable_data.into(),362					owner: data.owner.clone(),363				},364			);365			<Owned<T>>::insert((collection.id, data.owner.as_sub(), token), true);366367			collection.log_infallible(ERC721Events::Transfer {368				from: H160::default(),369				to: *data.owner.as_eth(),370				token_id: token.into(),371			});372			<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(373				collection.id,374				TokenId(token),375				data.owner.clone(),376				1,377			));378		}379		Ok(())380	}381382	pub fn set_allowance_unchecked(383		collection: &NonfungibleHandle<T>,384		sender: &T::CrossAccountId,385		token: TokenId,386		spender: Option<&T::CrossAccountId>,387	) {388		if let Some(spender) = spender {389			let old_spender = <Allowance<T>>::get((collection.id, token));390			<Allowance<T>>::insert((collection.id, token), spender);391			// In ERC721 there is only one possible approved user of token, so we set392			// approved user to spender393			collection.log_infallible(ERC721Events::Approval {394				owner: *sender.as_eth(),395				approved: *spender.as_eth(),396				token_id: token.into(),397			});398			// In Unique chain, any token can have any amount of approved users, so we need to399			// set allowance of old owner to 0, and allowance of new owner to 1400			if old_spender.as_ref() != Some(spender) {401				if let Some(old_owner) = old_spender {402					<PalletCommon<T>>::deposit_event(CommonEvent::Approved(403						collection.id,404						token,405						sender.clone(),406						old_owner.clone(),407						0,408					));409				}410				<PalletCommon<T>>::deposit_event(CommonEvent::Approved(411					collection.id,412					token,413					sender.clone(),414					spender.clone(),415					1,416				));417			}418		} else {419			let old_spender = <Allowance<T>>::take((collection.id, token));420			// In ERC721 there is only one possible approved user of token, so we set421			// approved user to zero address422			collection.log_infallible(ERC721Events::Approval {423				owner: *sender.as_eth(),424				approved: H160::default(),425				token_id: token.into(),426			});427			// In Unique chain, any token can have any amount of approved users, so we need to428			// set allowance of old owner to 0429			if let Some(old_spender) = old_spender {430				<PalletCommon<T>>::deposit_event(CommonEvent::Approved(431					collection.id,432					token,433					sender.clone(),434					old_spender.clone(),435					0,436				));437			}438		}439	}440441	pub fn set_allowance(442		collection: &NonfungibleHandle<T>,443		sender: &T::CrossAccountId,444		token: TokenId,445		spender: Option<&T::CrossAccountId>,446	) -> DispatchResult {447		if collection.access == AccessMode::WhiteList {448			collection.check_allowlist(&sender)?;449			if let Some(spender) = spender {450				collection.check_allowlist(&spender)?;451			}452		}453454		if let Some(spender) = spender {455			<PalletCommon<T>>::ensure_correct_receiver(spender)?;456		}457		let token_data =458			<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;459		if &token_data.owner != sender {460			ensure!(461				collection.ignores_owned_amount(sender)?,462				<CommonError<T>>::CantApproveMoreThanOwned463			);464		}465466		// =========467468		Self::set_allowance_unchecked(collection, sender, token, spender);469		Ok(())470	}471472	pub fn transfer_from(473		collection: &NonfungibleHandle<T>,474		spender: &T::CrossAccountId,475		from: &T::CrossAccountId,476		to: &T::CrossAccountId,477		token: TokenId,478	) -> DispatchResult {479		if spender == from {480			return Self::transfer(collection, from, to, token);481		}482		if collection.access == AccessMode::WhiteList {483			// `from`, `to` checked in [`transfer`]484			collection.check_allowlist(spender)?;485		}486487		if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {488			ensure!(489				collection.ignores_allowance(spender)?,490				<CommonError<T>>::TokenValueNotEnough491			);492		}493494		// =========495496		Self::transfer(collection, &from, to, token)?;497		// Allowance is reset in [`transfer`]498		Ok(())499	}500501	pub fn set_variable_metadata(502		collection: &NonfungibleHandle<T>,503		sender: &T::CrossAccountId,504		token: TokenId,505		data: Vec<u8>,506	) -> DispatchResult {507		ensure!(508			data.len() as u32 <= CUSTOM_DATA_LIMIT,509			<CommonError<T>>::TokenVariableDataLimitExceeded510		);511		let token_data =512			<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;513		collection.check_can_update_meta(sender, &token_data.owner)?;514515		collection.consume_sstore()?;516517		// =========518519		<TokenData<T>>::insert(520			(collection.id, token),521			ItemData {522				variable_data: data,523				..token_data524			},525		);526		Ok(())527	}528529	/// Delegated to `create_multiple_items`530	pub fn create_item(531		collection: &NonfungibleHandle<T>,532		sender: &T::CrossAccountId,533		data: CreateItemData<T>,534	) -> DispatchResult {535		Self::create_multiple_items(collection, sender, vec![data])536	}537}
modifiedpallets/nonfungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -1,3 +1,27 @@
+// Template adopted from https://github.com/paritytech/substrate/blob/master/.maintain/frame-weight-template.hbs
+
+//! Autogenerated weights for pallet_nonfungible
+//!
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
+//! DATE: 2021-10-21, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 128
+
+// Executed Command:
+// target/release/nft
+// benchmark
+// --pallet
+// pallet-nonfungible
+// --wasm-execution
+// compiled
+// --extrinsic
+// *
+// --template
+// .maintain/frame-weight-template.hbs
+// --steps=50
+// --repeat=20
+// --output=./pallets/nonfungible/src/weights.rs
+
+
 #![cfg_attr(rustfmt, rustfmt_skip)]
 #![allow(unused_parens)]
 #![allow(unused_imports)]
@@ -5,33 +29,144 @@
 use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
 use sp_std::marker::PhantomData;
 
+/// Weight functions needed for pallet_nonfungible.
 pub trait WeightInfo {
 	fn create_item() -> Weight;
-	fn create_multiple_items(b: u32) -> Weight;
+	fn create_multiple_items(b: u32, ) -> Weight;
 	fn burn_item() -> Weight;
 	fn transfer() -> Weight;
 	fn approve() -> Weight;
 	fn transfer_from() -> Weight;
-	fn set_variable_metadata() -> Weight;
+	fn set_variable_metadata(b: u32, ) -> Weight;
 }
 
+/// Weights for pallet_nonfungible using the Substrate node and recommended hardware.
 pub struct SubstrateWeight<T>(PhantomData<T>);
 impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
-    fn create_item() -> Weight {0}
-	fn create_multiple_items(_b: u32) -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
-	fn set_variable_metadata() -> Weight {0}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:1)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn create_item() -> Weight {
+		(16_902_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(4 as Weight))
+	}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:4)
+	// Storage: Nonfungible Owned (r:0 w:4)
+	fn create_multiple_items(b: u32, ) -> Weight {
+		(15_860_000 as Weight)
+			// Standard Error: 5_000
+			.saturating_add((3_916_000 as Weight).saturating_mul(b as Weight))
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes((2 as Weight).saturating_mul(b as Weight)))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible TokensBurnt (r:1 w:1)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn burn_item() -> Weight {
+		(17_966_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(3 as Weight))
+			.saturating_add(T::DbWeight::get().writes(3 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer() -> Weight {
+		(23_886_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(4 as Weight))
+			.saturating_add(T::DbWeight::get().writes(5 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:0)
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	fn approve() -> Weight {
+		(14_697_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(2 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer_from() -> Weight {
+		(28_001_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(4 as Weight))
+			.saturating_add(T::DbWeight::get().writes(6 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	fn set_variable_metadata(_b: u32, ) -> Weight {
+		(6_380_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
+	}
 }
 
+// For backwards compatibility and tests
 impl WeightInfo for () {
-    fn create_item() -> Weight {0}
-	fn create_multiple_items(_b: u32) -> Weight {0}
-	fn burn_item() -> Weight {0}
-	fn transfer() -> Weight {0}
-	fn approve() -> Weight {0}
-	fn transfer_from() -> Weight {0}
-	fn set_variable_metadata() -> Weight {0}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:1)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn create_item() -> Weight {
+		(16_902_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
+	}
+	// Storage: Nonfungible TokensMinted (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:0 w:4)
+	// Storage: Nonfungible Owned (r:0 w:4)
+	fn create_multiple_items(b: u32, ) -> Weight {
+		(15_860_000 as Weight)
+			// Standard Error: 5_000
+			.saturating_add((3_916_000 as Weight).saturating_mul(b as Weight))
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes((2 as Weight).saturating_mul(b as Weight)))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible TokensBurnt (r:1 w:1)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:1)
+	fn burn_item() -> Weight {
+		(17_966_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Allowance (r:1 w:0)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer() -> Weight {
+		(23_886_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(5 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:0)
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	fn approve() -> Weight {
+		(14_697_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
+	}
+	// Storage: Nonfungible Allowance (r:1 w:1)
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	// Storage: Nonfungible AccountBalance (r:2 w:2)
+	// Storage: Nonfungible Owned (r:0 w:2)
+	fn transfer_from() -> Weight {
+		(28_001_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(6 as Weight))
+	}
+	// Storage: Nonfungible TokenData (r:1 w:1)
+	fn set_variable_metadata(_b: u32, ) -> Weight {
+		(6_380_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
+	}
 }