difftreelog
feat(fungible) benchmarking
in: master
5 files changed
pallets/fungible/Cargo.tomldiffbeforeafterboth--- a/pallets/fungible/Cargo.toml
+++ b/pallets/fungible/Cargo.toml
@@ -20,6 +20,7 @@
evm-coder = { default-features = false, path = '../../crates/evm-coder' }
ethereum = { default-features = false, version = "0.9.0" }
pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
+frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
[features]
default = ["std"]
@@ -32,5 +33,9 @@
"pallet-common/std",
"evm-coder/std",
"ethereum/std",
+ 'frame-benchmarking/std',
]
-runtime-benchmarks = []
+runtime-benchmarks = [
+ 'frame-benchmarking',
+ 'pallet-common/runtime-benchmarks',
+]
pallets/fungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/fungible/src/benchmarking.rs
+++ b/pallets/fungible/src/benchmarking.rs
@@ -1 +1,61 @@
-#![cfg(feature = "runtime-benchmarking")]
+use super::*;
+use crate::{Pallet, Config, FungibleHandle};
+
+use sp_std::prelude::*;
+use pallet_common::benchmarking::create_collection_raw;
+use frame_benchmarking::{benchmarks, account};
+use nft_data_structs::{CollectionMode};
+use pallet_common::bench_init;
+
+const SEED: u32 = 1;
+
+fn create_collection<T: Config>(owner: T::AccountId) -> Result<FungibleHandle<T>, DispatchError> {
+ create_collection_raw(
+ owner,
+ CollectionMode::Fungible(0),
+ <Pallet<T>>::init_collection,
+ FungibleHandle::cast,
+ )
+}
+
+benchmarks! {
+ create_item {
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ sender: cross_from_sub(owner); to: cross_sub;
+ };
+ }: {<Pallet<T>>::create_item(&collection, &sender, (to, 200))?}
+
+ burn_item {
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ owner: cross_from_sub; burner: cross_sub;
+ };
+ <Pallet<T>>::create_item(&collection, &owner, (burner.clone(), 200))?;
+ }: {<Pallet<T>>::burn(&collection, &burner, 100)?}
+
+ transfer {
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ owner: cross_from_sub; sender: cross_sub; to: cross_sub;
+ };
+ <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+ }: {<Pallet<T>>::transfer(&collection, &sender, &to, 200)?}
+
+ approve {
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ owner: cross_from_sub; sender: cross_sub; spender: cross_sub;
+ };
+ <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+ }: {<Pallet<T>>::set_allowance(&collection, &sender, &spender, 100)?}
+
+ transfer_from {
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ owner: cross_from_sub; sender: cross_sub; spender: cross_sub; receiver: cross_sub;
+ };
+ <Pallet<T>>::create_item(&collection, &owner, (sender.clone(), 200))?;
+ <Pallet<T>>::set_allowance(&collection, &sender, &spender, 200)?;
+ }: {<Pallet<T>>::transfer_from(&collection, &spender, &sender, &receiver, 100)?}
+}
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -54,7 +54,7 @@
match data {
nft_data_structs::CreateItemData::Fungible(data) => with_weight(
<Pallet<T>>::create_item(self, &sender, (to, data.value)),
- <SelfWeightOf<T>>::create_item(),
+ <CommonWeights<T>>::create_item(),
),
_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),
}
@@ -80,7 +80,7 @@
with_weight(
<Pallet<T>>::create_item(self, &sender, (to, sum)),
- <SelfWeightOf<T>>::create_item(),
+ <CommonWeights<T>>::create_item(),
)
}
@@ -97,7 +97,7 @@
with_weight(
<Pallet<T>>::burn(self, &sender, amount),
- <SelfWeightOf<T>>::burn_item(),
+ <CommonWeights<T>>::burn_item(),
)
}
@@ -115,7 +115,7 @@
with_weight(
<Pallet<T>>::transfer(&self, &from, &to, amount),
- <SelfWeightOf<T>>::transfer(),
+ <CommonWeights<T>>::transfer(),
)
}
@@ -133,7 +133,7 @@
with_weight(
<Pallet<T>>::set_allowance(&self, &sender, &spender, amount),
- <SelfWeightOf<T>>::approve(),
+ <CommonWeights<T>>::approve(),
)
}
@@ -152,7 +152,7 @@
with_weight(
<Pallet<T>>::transfer_from(&self, &sender, &from, &to, amount),
- <SelfWeightOf<T>>::transfer_from(),
+ <CommonWeights<T>>::transfer_from(),
)
}
pallets/fungible/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23use core::ops::Deref;4use frame_support::{ensure};5use nft_data_structs::{AccessMode, Collection, CollectionId, TokenId};6use pallet_common::{7 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,8};9use sp_core::H160;10use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};11use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};1213pub use pallet::*;1415use crate::erc::ERC20Events;16pub mod benchmarking;17pub mod common;18pub mod erc;19pub mod weights;2021pub type CreateItemData<T> = (<T as pallet_common::Config>::CrossAccountId, u128);22pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;2324#[frame_support::pallet]25pub mod pallet {26 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};27 use nft_data_structs::CollectionId;28 use super::weights::WeightInfo;2930 #[pallet::error]31 pub enum Error<T> {32 /// Not Fungible item data used to mint in Fungible collection.33 NotFungibleDataUsedToMintFungibleCollectionToken,34 /// Not default id passed as TokenId argument35 FungibleItemsHaveNoId,36 /// Tried to set data for fungible item37 FungibleItemsHaveData,38 }3940 #[pallet::config]41 pub trait Config: frame_system::Config + pallet_common::Config {42 type WeightInfo: WeightInfo;43 }4445 #[pallet::pallet]46 #[pallet::generate_store(pub(super) trait Store)]47 pub struct Pallet<T>(_);4849 #[pallet::storage]50 pub(super) type TotalSupply<T: Config> =51 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;5253 #[pallet::storage]54 pub(super) type Balance<T: Config> = StorageNMap<55 Key = (56 Key<Twox64Concat, CollectionId>,57 Key<Blake2_128Concat, T::AccountId>,58 ),59 Value = u128,60 QueryKind = ValueQuery,61 >;6263 #[pallet::storage]64 pub(super) type Allowance<T: Config> = StorageNMap<65 Key = (66 Key<Twox64Concat, CollectionId>,67 Key<Blake2_128, T::AccountId>,68 Key<Blake2_128Concat, T::AccountId>,69 ),70 Value = u128,71 QueryKind = ValueQuery,72 >;73}7475pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);76impl<T: Config> FungibleHandle<T> {77 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {78 Self(inner)79 }80 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {81 self.082 }83}84impl<T: Config> Deref for FungibleHandle<T> {85 type Target = pallet_common::CollectionHandle<T>;8687 fn deref(&self) -> &Self::Target {88 &self.089 }90}9192impl<T: Config> Pallet<T> {93 pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {94 PalletCommon::init_collection(data)95 }96 pub fn destroy_collection(97 collection: FungibleHandle<T>,98 sender: &T::CrossAccountId,99 ) -> DispatchResult {100 let id = collection.id;101102 // =========103104 PalletCommon::destroy_collection(collection.0, sender)?;105106 <TotalSupply<T>>::remove(id);107 <Balance<T>>::remove_prefix((id,), None);108 <Allowance<T>>::remove_prefix((id,), None);109 Ok(())110 }111112 pub fn burn(113 collection: &FungibleHandle<T>,114 owner: &T::CrossAccountId,115 amount: u128,116 ) -> DispatchResult {117 let total_supply = <TotalSupply<T>>::get(collection.id)118 .checked_sub(amount)119 .ok_or(<CommonError<T>>::TokenValueTooLow)?;120121 let balance = <Balance<T>>::get((collection.id, owner.as_sub()))122 .checked_sub(amount)123 .ok_or(<CommonError<T>>::TokenValueTooLow)?;124125 if collection.access == AccessMode::WhiteList {126 collection.check_allowlist(owner)?;127 }128129 // =========130131 if balance == 0 {132 <Balance<T>>::remove((collection.id, owner.as_sub()));133 } else {134 <Balance<T>>::insert((collection.id, owner.as_sub()), balance);135 }136 <TotalSupply<T>>::insert(collection.id, total_supply);137138 collection.log_infallible(ERC20Events::Transfer {139 from: *owner.as_eth(),140 to: H160::default(),141 value: amount.into(),142 });143 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(144 collection.id,145 TokenId::default(),146 owner.clone(),147 amount,148 ));149 Ok(())150 }151152 pub fn transfer(153 collection: &FungibleHandle<T>,154 from: &T::CrossAccountId,155 to: &T::CrossAccountId,156 amount: u128,157 ) -> DispatchResult {158 ensure!(159 collection.transfers_enabled,160 <CommonError<T>>::TransferNotAllowed161 );162163 if collection.access == AccessMode::WhiteList {164 collection.check_allowlist(from)?;165 collection.check_allowlist(to)?;166 }167 <PalletCommon<T>>::ensure_correct_receiver(to)?;168169 let balance_from = <Balance<T>>::get((collection.id, from.as_sub()))170 .checked_sub(amount)171 .ok_or(<CommonError<T>>::TokenValueTooLow)?;172 let balance_to = if from != to {173 Some(174 <Balance<T>>::get((collection.id, to.as_sub()))175 .checked_add(amount)176 .ok_or(ArithmeticError::Overflow)?,177 )178 } else {179 None180 };181182 collection.consume_sstore()?;183 collection.consume_sstore()?;184 collection.consume_log(2, 32)?;185 collection.consume_sstore()?;186187 // =========188189 if let Some(balance_to) = balance_to {190 // from != to191 if balance_from == 0 {192 <Balance<T>>::remove((collection.id, from.as_sub()));193 } else {194 <Balance<T>>::insert((collection.id, from.as_sub()), balance_from);195 }196 <Balance<T>>::insert((collection.id, to.as_sub()), balance_to);197 }198199 collection.log_infallible(ERC20Events::Transfer {200 from: *from.as_eth(),201 to: *to.as_eth(),202 value: amount.into(),203 });204 <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(205 collection.id,206 TokenId::default(),207 from.clone(),208 to.clone(),209 amount,210 ));211 Ok(())212 }213214 pub fn create_multiple_items(215 collection: &FungibleHandle<T>,216 sender: &T::CrossAccountId,217 data: Vec<CreateItemData<T>>,218 ) -> DispatchResult {219 let unrestricted_minting = collection.is_owner_or_admin(sender)?;220 if !unrestricted_minting {221 ensure!(222 collection.mint_mode,223 <CommonError<T>>::PublicMintingNotAllowed224 );225 collection.check_allowlist(sender)?;226227 for (owner, _) in data.iter() {228 collection.check_allowlist(owner)?;229 }230 }231232 let mut balances = BTreeMap::new();233234 let total_supply = data235 .iter()236 .map(|u| u.1)237 .try_fold(0u128, |acc, v| acc.checked_add(v))238 .ok_or(ArithmeticError::Overflow)?;239240 for (user, amount) in data.into_iter() {241 collection.consume_sload()?;242 let balance = balances243 .entry(user.clone())244 .or_insert_with(|| <Balance<T>>::get((collection.id, user.as_sub())));245 *balance = (*balance)246 .checked_add(amount)247 .ok_or(ArithmeticError::Overflow)?;248 }249250 collection.consume_sstore()?;251 for _ in &balances {252 collection.consume_sstore()?;253 collection.consume_log(2, 32)?;254 collection.consume_sstore()?;255 }256257 // =========258259 <TotalSupply<T>>::insert(collection.id, total_supply);260 for (user, amount) in balances {261 <Balance<T>>::insert((collection.id, user.as_sub()), amount);262263 collection.log_infallible(ERC20Events::Transfer {264 from: H160::default(),265 to: *user.as_eth(),266 value: amount.into(),267 });268 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(269 collection.id,270 TokenId::default(),271 user.clone(),272 amount,273 ));274 }275276 Ok(())277 }278279 fn set_allowance_unchecked(280 collection: &FungibleHandle<T>,281 owner: &T::CrossAccountId,282 spender: &T::CrossAccountId,283 amount: u128,284 ) {285 <Allowance<T>>::insert((collection.id, owner.as_sub(), spender.as_sub()), amount);286287 collection.log_infallible(ERC20Events::Approval {288 owner: *owner.as_eth(),289 spender: *spender.as_eth(),290 value: amount.into(),291 });292 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(293 collection.id,294 TokenId(0),295 owner.clone(),296 spender.clone(),297 amount,298 ));299 }300301 pub fn set_allowance(302 collection: &FungibleHandle<T>,303 owner: &T::CrossAccountId,304 spender: &T::CrossAccountId,305 amount: u128,306 ) -> DispatchResult {307 if collection.access == AccessMode::WhiteList {308 collection.check_allowlist(&owner)?;309 collection.check_allowlist(&spender)?;310 }311312 if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {313 ensure!(314 collection.ignores_owned_amount(owner)?,315 <CommonError<T>>::CantApproveMoreThanOwned316 );317 }318319 // =========320321 Self::set_allowance_unchecked(collection, owner, spender, amount);322 Ok(())323 }324325 pub fn transfer_from(326 collection: &FungibleHandle<T>,327 spender: &T::CrossAccountId,328 from: &T::CrossAccountId,329 to: &T::CrossAccountId,330 amount: u128,331 ) -> DispatchResult {332 if spender == from {333 return Self::transfer(collection, from, to, amount);334 }335 if collection.access == AccessMode::WhiteList {336 // `from`, `to` checked in [`transfer`]337 collection.check_allowlist(spender)?;338 }339340 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))341 .checked_sub(amount);342 if allowance.is_none() {343 ensure!(344 collection.ignores_allowance(spender)?,345 <CommonError<T>>::TokenValueNotEnough346 );347 }348349 // =========350351 Self::transfer(collection, from, to, amount)?;352 if let Some(allowance) = allowance {353 Self::set_allowance_unchecked(collection, from, spender, allowance);354 }355 Ok(())356 }357358 /// Delegated to `create_multiple_items`359 pub fn create_item(360 collection: &FungibleHandle<T>,361 sender: &T::CrossAccountId,362 data: CreateItemData<T>,363 ) -> DispatchResult {364 Self::create_multiple_items(collection, sender, vec![data])365 }366}1#![cfg_attr(not(feature = "std"), no_std)]23use core::ops::Deref;4use frame_support::{ensure};5use nft_data_structs::{AccessMode, Collection, CollectionId, TokenId};6use pallet_common::{7 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,8};9use sp_core::H160;10use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};11use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};1213pub use pallet::*;1415use crate::erc::ERC20Events;16#[cfg(feature = "runtime-benchmarks")]17pub mod benchmarking;18pub mod common;19pub mod erc;20pub mod weights;2122pub type CreateItemData<T> = (<T as pallet_common::Config>::CrossAccountId, u128);23pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;2425#[frame_support::pallet]26pub mod pallet {27 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};28 use nft_data_structs::CollectionId;29 use super::weights::WeightInfo;3031 #[pallet::error]32 pub enum Error<T> {33 /// Not Fungible item data used to mint in Fungible collection.34 NotFungibleDataUsedToMintFungibleCollectionToken,35 /// Not default id passed as TokenId argument36 FungibleItemsHaveNoId,37 /// Tried to set data for fungible item38 FungibleItemsHaveData,39 }4041 #[pallet::config]42 pub trait Config: frame_system::Config + pallet_common::Config {43 type WeightInfo: WeightInfo;44 }4546 #[pallet::pallet]47 #[pallet::generate_store(pub(super) trait Store)]48 pub struct Pallet<T>(_);4950 #[pallet::storage]51 pub(super) type TotalSupply<T: Config> =52 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;5354 #[pallet::storage]55 pub(super) type Balance<T: Config> = StorageNMap<56 Key = (57 Key<Twox64Concat, CollectionId>,58 Key<Blake2_128Concat, T::AccountId>,59 ),60 Value = u128,61 QueryKind = ValueQuery,62 >;6364 #[pallet::storage]65 pub(super) type Allowance<T: Config> = StorageNMap<66 Key = (67 Key<Twox64Concat, CollectionId>,68 Key<Blake2_128, T::AccountId>,69 Key<Blake2_128Concat, T::AccountId>,70 ),71 Value = u128,72 QueryKind = ValueQuery,73 >;74}7576pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);77impl<T: Config> FungibleHandle<T> {78 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {79 Self(inner)80 }81 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {82 self.083 }84}85impl<T: Config> Deref for FungibleHandle<T> {86 type Target = pallet_common::CollectionHandle<T>;8788 fn deref(&self) -> &Self::Target {89 &self.090 }91}9293impl<T: Config> Pallet<T> {94 pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {95 PalletCommon::init_collection(data)96 }97 pub fn destroy_collection(98 collection: FungibleHandle<T>,99 sender: &T::CrossAccountId,100 ) -> DispatchResult {101 let id = collection.id;102103 // =========104105 PalletCommon::destroy_collection(collection.0, sender)?;106107 <TotalSupply<T>>::remove(id);108 <Balance<T>>::remove_prefix((id,), None);109 <Allowance<T>>::remove_prefix((id,), None);110 Ok(())111 }112113 pub fn burn(114 collection: &FungibleHandle<T>,115 owner: &T::CrossAccountId,116 amount: u128,117 ) -> DispatchResult {118 let total_supply = <TotalSupply<T>>::get(collection.id)119 .checked_sub(amount)120 .ok_or(<CommonError<T>>::TokenValueTooLow)?;121122 let balance = <Balance<T>>::get((collection.id, owner.as_sub()))123 .checked_sub(amount)124 .ok_or(<CommonError<T>>::TokenValueTooLow)?;125126 if collection.access == AccessMode::WhiteList {127 collection.check_allowlist(owner)?;128 }129130 // =========131132 if balance == 0 {133 <Balance<T>>::remove((collection.id, owner.as_sub()));134 } else {135 <Balance<T>>::insert((collection.id, owner.as_sub()), balance);136 }137 <TotalSupply<T>>::insert(collection.id, total_supply);138139 collection.log_infallible(ERC20Events::Transfer {140 from: *owner.as_eth(),141 to: H160::default(),142 value: amount.into(),143 });144 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(145 collection.id,146 TokenId::default(),147 owner.clone(),148 amount,149 ));150 Ok(())151 }152153 pub fn transfer(154 collection: &FungibleHandle<T>,155 from: &T::CrossAccountId,156 to: &T::CrossAccountId,157 amount: u128,158 ) -> DispatchResult {159 ensure!(160 collection.transfers_enabled,161 <CommonError<T>>::TransferNotAllowed162 );163164 if collection.access == AccessMode::WhiteList {165 collection.check_allowlist(from)?;166 collection.check_allowlist(to)?;167 }168 <PalletCommon<T>>::ensure_correct_receiver(to)?;169170 let balance_from = <Balance<T>>::get((collection.id, from.as_sub()))171 .checked_sub(amount)172 .ok_or(<CommonError<T>>::TokenValueTooLow)?;173 let balance_to = if from != to {174 Some(175 <Balance<T>>::get((collection.id, to.as_sub()))176 .checked_add(amount)177 .ok_or(ArithmeticError::Overflow)?,178 )179 } else {180 None181 };182183 collection.consume_sstore()?;184 collection.consume_sstore()?;185 collection.consume_log(2, 32)?;186 collection.consume_sstore()?;187188 // =========189190 if let Some(balance_to) = balance_to {191 // from != to192 if balance_from == 0 {193 <Balance<T>>::remove((collection.id, from.as_sub()));194 } else {195 <Balance<T>>::insert((collection.id, from.as_sub()), balance_from);196 }197 <Balance<T>>::insert((collection.id, to.as_sub()), balance_to);198 }199200 collection.log_infallible(ERC20Events::Transfer {201 from: *from.as_eth(),202 to: *to.as_eth(),203 value: amount.into(),204 });205 <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(206 collection.id,207 TokenId::default(),208 from.clone(),209 to.clone(),210 amount,211 ));212 Ok(())213 }214215 pub fn create_multiple_items(216 collection: &FungibleHandle<T>,217 sender: &T::CrossAccountId,218 data: Vec<CreateItemData<T>>,219 ) -> DispatchResult {220 let unrestricted_minting = collection.is_owner_or_admin(sender)?;221 if !unrestricted_minting {222 ensure!(223 collection.mint_mode,224 <CommonError<T>>::PublicMintingNotAllowed225 );226 collection.check_allowlist(sender)?;227228 for (owner, _) in data.iter() {229 collection.check_allowlist(owner)?;230 }231 }232233 let mut balances = BTreeMap::new();234235 let total_supply = data236 .iter()237 .map(|u| u.1)238 .try_fold(0u128, |acc, v| acc.checked_add(v))239 .ok_or(ArithmeticError::Overflow)?;240241 for (user, amount) in data.into_iter() {242 collection.consume_sload()?;243 let balance = balances244 .entry(user.clone())245 .or_insert_with(|| <Balance<T>>::get((collection.id, user.as_sub())));246 *balance = (*balance)247 .checked_add(amount)248 .ok_or(ArithmeticError::Overflow)?;249 }250251 collection.consume_sstore()?;252 for _ in &balances {253 collection.consume_sstore()?;254 collection.consume_log(2, 32)?;255 collection.consume_sstore()?;256 }257258 // =========259260 <TotalSupply<T>>::insert(collection.id, total_supply);261 for (user, amount) in balances {262 <Balance<T>>::insert((collection.id, user.as_sub()), amount);263264 collection.log_infallible(ERC20Events::Transfer {265 from: H160::default(),266 to: *user.as_eth(),267 value: amount.into(),268 });269 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(270 collection.id,271 TokenId::default(),272 user.clone(),273 amount,274 ));275 }276277 Ok(())278 }279280 fn set_allowance_unchecked(281 collection: &FungibleHandle<T>,282 owner: &T::CrossAccountId,283 spender: &T::CrossAccountId,284 amount: u128,285 ) {286 <Allowance<T>>::insert((collection.id, owner.as_sub(), spender.as_sub()), amount);287288 collection.log_infallible(ERC20Events::Approval {289 owner: *owner.as_eth(),290 spender: *spender.as_eth(),291 value: amount.into(),292 });293 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(294 collection.id,295 TokenId(0),296 owner.clone(),297 spender.clone(),298 amount,299 ));300 }301302 pub fn set_allowance(303 collection: &FungibleHandle<T>,304 owner: &T::CrossAccountId,305 spender: &T::CrossAccountId,306 amount: u128,307 ) -> DispatchResult {308 if collection.access == AccessMode::WhiteList {309 collection.check_allowlist(&owner)?;310 collection.check_allowlist(&spender)?;311 }312313 if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {314 ensure!(315 collection.ignores_owned_amount(owner)?,316 <CommonError<T>>::CantApproveMoreThanOwned317 );318 }319320 // =========321322 Self::set_allowance_unchecked(collection, owner, spender, amount);323 Ok(())324 }325326 pub fn transfer_from(327 collection: &FungibleHandle<T>,328 spender: &T::CrossAccountId,329 from: &T::CrossAccountId,330 to: &T::CrossAccountId,331 amount: u128,332 ) -> DispatchResult {333 if spender == from {334 return Self::transfer(collection, from, to, amount);335 }336 if collection.access == AccessMode::WhiteList {337 // `from`, `to` checked in [`transfer`]338 collection.check_allowlist(spender)?;339 }340341 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))342 .checked_sub(amount);343 if allowance.is_none() {344 ensure!(345 collection.ignores_allowance(spender)?,346 <CommonError<T>>::TokenValueNotEnough347 );348 }349350 // =========351352 Self::transfer(collection, from, to, amount)?;353 if let Some(allowance) = allowance {354 Self::set_allowance_unchecked(collection, from, spender, allowance);355 }356 Ok(())357 }358359 /// Delegated to `create_multiple_items`360 pub fn create_item(361 collection: &FungibleHandle<T>,362 sender: &T::CrossAccountId,363 data: CreateItemData<T>,364 ) -> DispatchResult {365 Self::create_multiple_items(collection, sender, vec![data])366 }367}pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/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_fungible
+//!
+//! 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-fungible
+// --wasm-execution
+// compiled
+// --extrinsic
+// *
+// --template
+// .maintain/frame-weight-template.hbs
+// --steps=50
+// --repeat=20
+// --output=./pallets/fungible/src/weights.rs
+
+
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
@@ -5,6 +29,7 @@
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
+/// Weight functions needed for pallet_fungible.
pub trait WeightInfo {
fn create_item() -> Weight;
fn burn_item() -> Weight;
@@ -13,19 +38,79 @@
fn transfer_from() -> Weight;
}
+/// Weights for pallet_fungible 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 burn_item() -> Weight {0}
- fn transfer() -> Weight {0}
- fn approve() -> Weight {0}
- fn transfer_from() -> Weight {0}
+ // Storage: Fungible Balance (r:1 w:1)
+ // Storage: Fungible TotalSupply (r:0 w:1)
+ fn create_item() -> Weight {
+ (12_069_000 as Weight)
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible TotalSupply (r:1 w:1)
+ // Storage: Fungible Balance (r:1 w:1)
+ fn burn_item() -> Weight {
+ (14_096_000 as Weight)
+ .saturating_add(T::DbWeight::get().reads(2 as Weight))
+ .saturating_add(T::DbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible Balance (r:2 w:2)
+ fn transfer() -> Weight {
+ (15_436_000 as Weight)
+ .saturating_add(T::DbWeight::get().reads(2 as Weight))
+ .saturating_add(T::DbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible Balance (r:1 w:0)
+ // Storage: Fungible Allowance (r:0 w:1)
+ fn approve() -> Weight {
+ (12_867_000 as Weight)
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().writes(1 as Weight))
+ }
+ // Storage: Fungible Allowance (r:1 w:1)
+ // Storage: Fungible Balance (r:2 w:2)
+ fn transfer_from() -> Weight {
+ (21_462_000 as Weight)
+ .saturating_add(T::DbWeight::get().reads(3 as Weight))
+ .saturating_add(T::DbWeight::get().writes(3 as Weight))
+ }
}
+// For backwards compatibility and tests
impl WeightInfo for () {
- fn create_item() -> Weight {0}
- fn burn_item() -> Weight {0}
- fn transfer() -> Weight {0}
- fn approve() -> Weight {0}
- fn transfer_from() -> Weight {0}
+ // Storage: Fungible Balance (r:1 w:1)
+ // Storage: Fungible TotalSupply (r:0 w:1)
+ fn create_item() -> Weight {
+ (12_069_000 as Weight)
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible TotalSupply (r:1 w:1)
+ // Storage: Fungible Balance (r:1 w:1)
+ fn burn_item() -> Weight {
+ (14_096_000 as Weight)
+ .saturating_add(RocksDbWeight::get().reads(2 as Weight))
+ .saturating_add(RocksDbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible Balance (r:2 w:2)
+ fn transfer() -> Weight {
+ (15_436_000 as Weight)
+ .saturating_add(RocksDbWeight::get().reads(2 as Weight))
+ .saturating_add(RocksDbWeight::get().writes(2 as Weight))
+ }
+ // Storage: Fungible Balance (r:1 w:0)
+ // Storage: Fungible Allowance (r:0 w:1)
+ fn approve() -> Weight {
+ (12_867_000 as Weight)
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().writes(1 as Weight))
+ }
+ // Storage: Fungible Allowance (r:1 w:1)
+ // Storage: Fungible Balance (r:2 w:2)
+ fn transfer_from() -> Weight {
+ (21_462_000 as Weight)
+ .saturating_add(RocksDbWeight::get().reads(3 as Weight))
+ .saturating_add(RocksDbWeight::get().writes(3 as Weight))
+ }
}