difftreelog
feat createMultipleItemsEx call
in: master
16 files changed
pallets/common/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23use core::ops::{Deref, DerefMut};4use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};5use sp_std::vec::Vec;6use account::CrossAccountId;7use frame_support::{8 dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo},9 ensure, fail,10 traits::{Imbalance, Get, Currency},11 BoundedVec,12};13use pallet_evm::GasWeightMapping;14use up_data_structs::{15 COLLECTION_NUMBER_LIMIT, Collection, CollectionId, CreateItemData, ExistenceRequirement,16 MAX_TOKEN_PREFIX_LENGTH, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission, Pays, PostDispatchInfo,17 TokenId, Weight, WithdrawReasons, CollectionStats, MAX_TOKEN_OWNERSHIP, CollectionMode,18 NFT_SPONSOR_TRANSFER_TIMEOUT, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,19 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT, CUSTOM_DATA_LIMIT, CollectionLimits,20 CustomDataLimit, CreateCollectionData, SponsorshipState,21};22pub use pallet::*;23use sp_core::H160;24use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};25pub mod account;26#[cfg(feature = "runtime-benchmarks")]27pub mod benchmarking;28pub mod erc;29pub mod eth;3031#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]32pub struct CollectionHandle<T: Config> {33 pub id: CollectionId,34 collection: Collection<T::AccountId>,35 pub recorder: SubstrateRecorder<T>,36}37impl<T: Config> WithRecorder<T> for CollectionHandle<T> {38 fn recorder(&self) -> &SubstrateRecorder<T> {39 &self.recorder40 }41 fn into_recorder(self) -> SubstrateRecorder<T> {42 self.recorder43 }44}45impl<T: Config> CollectionHandle<T> {46 pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {47 <CollectionById<T>>::get(id).map(|collection| Self {48 id,49 collection,50 recorder: SubstrateRecorder::new(eth::collection_id_to_address(id), gas_limit),51 })52 }53 pub fn new(id: CollectionId) -> Option<Self> {54 Self::new_with_gas_limit(id, u64::MAX)55 }56 pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {57 Ok(Self::new(id).ok_or(<Error<T>>::CollectionNotFound)?)58 }59 pub fn log_mirrored(&self, log: impl evm_coder::ToLog) {60 self.recorder.log_mirrored(log)61 }62 pub fn log_direct(&self, log: impl evm_coder::ToLog) {63 self.recorder.log_direct(log)64 }65 pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {66 self.recorder67 .consume_gas(T::GasWeightMapping::weight_to_gas(68 <T as frame_system::Config>::DbWeight::get()69 .read70 .saturating_mul(reads),71 ))72 }73 pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {74 self.recorder75 .consume_gas(T::GasWeightMapping::weight_to_gas(76 <T as frame_system::Config>::DbWeight::get()77 .write78 .saturating_mul(writes),79 ))80 }81 pub fn submit_logs(self) {82 self.recorder.submit_logs()83 }84 pub fn save(self) -> DispatchResult {85 self.recorder.submit_logs();86 <CollectionById<T>>::insert(self.id, self.collection);87 Ok(())88 }89}90impl<T: Config> Deref for CollectionHandle<T> {91 type Target = Collection<T::AccountId>;9293 fn deref(&self) -> &Self::Target {94 &self.collection95 }96}9798impl<T: Config> DerefMut for CollectionHandle<T> {99 fn deref_mut(&mut self) -> &mut Self::Target {100 &mut self.collection101 }102}103104impl<T: Config> CollectionHandle<T> {105 pub fn check_is_owner(&self, subject: &T::CrossAccountId) -> DispatchResult {106 ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);107 Ok(())108 }109 pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {110 *subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))111 }112 pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {113 ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);114 Ok(())115 }116 pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {117 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)118 }119 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {120 self.limits.owner_can_transfer() && self.is_owner_or_admin(user)121 }122 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {123 ensure!(124 <Allowlist<T>>::get((self.id, user)),125 <Error<T>>::AddressNotInAllowlist126 );127 Ok(())128 }129130 pub fn check_can_update_meta(131 &self,132 subject: &T::CrossAccountId,133 item_owner: &T::CrossAccountId,134 ) -> DispatchResult {135 match self.meta_update_permission {136 MetaUpdatePermission::ItemOwner => {137 ensure!(subject == item_owner, <Error<T>>::NoPermission);138 Ok(())139 }140 MetaUpdatePermission::Admin => self.check_is_owner_or_admin(subject),141 MetaUpdatePermission::None => fail!(<Error<T>>::NoPermission),142 }143 }144}145146#[frame_support::pallet]147pub mod pallet {148 use super::*;149 use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key};150 use account::CrossAccountId;151 use frame_support::traits::Currency;152 use up_data_structs::TokenId;153 use scale_info::TypeInfo;154155 #[pallet::config]156 pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config + TypeInfo {157 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;158159 type CrossAccountId: CrossAccountId<Self::AccountId>;160161 type EvmAddressMapping: pallet_evm::AddressMapping<Self::AccountId>;162 type EvmBackwardsAddressMapping: up_evm_mapping::EvmBackwardsAddressMapping<Self::AccountId>;163164 type Currency: Currency<Self::AccountId>;165166 #[pallet::constant]167 type CollectionCreationPrice: Get<168 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,169 >;170171 type TreasuryAccountId: Get<Self::AccountId>;172 }173174 #[pallet::pallet]175 #[pallet::generate_store(pub(super) trait Store)]176 pub struct Pallet<T>(_);177178 #[pallet::extra_constants]179 impl<T: Config> Pallet<T> {180 pub fn collection_admins_limit() -> u32 {181 COLLECTION_ADMINS_LIMIT182 }183 }184185 #[pallet::event]186 #[pallet::generate_deposit(pub fn deposit_event)]187 pub enum Event<T: Config> {188 /// New collection was created189 ///190 /// # Arguments191 ///192 /// * collection_id: Globally unique identifier of newly created collection.193 ///194 /// * mode: [CollectionMode] converted into u8.195 ///196 /// * account_id: Collection owner.197 CollectionCreated(CollectionId, u8, T::AccountId),198199 /// New collection was destroyed200 ///201 /// # Arguments202 ///203 /// * collection_id: Globally unique identifier of collection.204 CollectionDestroyed(CollectionId),205206 /// New item was created.207 ///208 /// # Arguments209 ///210 /// * collection_id: Id of the collection where item was created.211 ///212 /// * item_id: Id of an item. Unique within the collection.213 ///214 /// * recipient: Owner of newly created item215 ///216 /// * amount: Always 1 for NFT217 ItemCreated(CollectionId, TokenId, T::CrossAccountId, u128),218219 /// Collection item was burned.220 ///221 /// # Arguments222 ///223 /// * collection_id.224 ///225 /// * item_id: Identifier of burned NFT.226 ///227 /// * owner: which user has destroyed its tokens228 ///229 /// * amount: Always 1 for NFT230 ItemDestroyed(CollectionId, TokenId, T::CrossAccountId, u128),231232 /// Item was transferred233 ///234 /// * collection_id: Id of collection to which item is belong235 ///236 /// * item_id: Id of an item237 ///238 /// * sender: Original owner of item239 ///240 /// * recipient: New owner of item241 ///242 /// * amount: Always 1 for NFT243 Transfer(244 CollectionId,245 TokenId,246 T::CrossAccountId,247 T::CrossAccountId,248 u128,249 ),250251 /// * collection_id252 ///253 /// * item_id254 ///255 /// * sender256 ///257 /// * spender258 ///259 /// * amount260 Approved(261 CollectionId,262 TokenId,263 T::CrossAccountId,264 T::CrossAccountId,265 u128,266 ),267 }268269 #[pallet::error]270 pub enum Error<T> {271 /// This collection does not exist.272 CollectionNotFound,273 /// Sender parameter and item owner must be equal.274 MustBeTokenOwner,275 /// No permission to perform action276 NoPermission,277 /// Collection is not in mint mode.278 PublicMintingNotAllowed,279 /// Address is not in allow list.280 AddressNotInAllowlist,281282 /// Collection name can not be longer than 63 char.283 CollectionNameLimitExceeded,284 /// Collection description can not be longer than 255 char.285 CollectionDescriptionLimitExceeded,286 /// Token prefix can not be longer than 15 char.287 CollectionTokenPrefixLimitExceeded,288 /// Total collections bound exceeded.289 TotalCollectionsLimitExceeded,290 /// variable_data exceeded data limit.291 TokenVariableDataLimitExceeded,292 /// Exceeded max admin count293 CollectionAdminCountExceeded,294 /// Collection limit bounds per collection exceeded295 CollectionLimitBoundsExceeded,296 /// Tried to enable permissions which are only permitted to be disabled297 OwnerPermissionsCantBeReverted,298299 /// Collection settings not allowing items transferring300 TransferNotAllowed,301 /// Account token limit exceeded per collection302 AccountTokenLimitExceeded,303 /// Collection token limit exceeded304 CollectionTokenLimitExceeded,305 /// Metadata flag frozen306 MetadataFlagFrozen,307308 /// Item not exists.309 TokenNotFound,310 /// Item balance not enough.311 TokenValueTooLow,312 /// Requested value more than approved.313 ApprovedValueTooLow,314 /// Tried to approve more than owned315 CantApproveMoreThanOwned,316317 /// Can't transfer tokens to ethereum zero address318 AddressIsZero,319 /// Target collection doesn't supports this operation320 UnsupportedOperation,321 }322323 #[pallet::storage]324 pub type CreatedCollectionCount<T> = StorageValue<Value = CollectionId, QueryKind = ValueQuery>;325 #[pallet::storage]326 pub type DestroyedCollectionCount<T> =327 StorageValue<Value = CollectionId, QueryKind = ValueQuery>;328329 /// Collection info330 #[pallet::storage]331 pub type CollectionById<T> = StorageMap<332 Hasher = Blake2_128Concat,333 Key = CollectionId,334 Value = Collection<<T as frame_system::Config>::AccountId>,335 QueryKind = OptionQuery,336 >;337338 #[pallet::storage]339 pub type AdminAmount<T> = StorageMap<340 Hasher = Blake2_128Concat,341 Key = CollectionId,342 Value = u32,343 QueryKind = ValueQuery,344 >;345346 /// List of collection admins347 #[pallet::storage]348 pub type IsAdmin<T: Config> = StorageNMap<349 Key = (350 Key<Blake2_128Concat, CollectionId>,351 Key<Blake2_128Concat, T::CrossAccountId>,352 ),353 Value = bool,354 QueryKind = ValueQuery,355 >;356357 /// Allowlisted collection users358 #[pallet::storage]359 pub type Allowlist<T: Config> = StorageNMap<360 Key = (361 Key<Blake2_128Concat, CollectionId>,362 Key<Blake2_128Concat, T::CrossAccountId>,363 ),364 Value = bool,365 QueryKind = ValueQuery,366 >;367368 /// Not used by code, exists only to provide some types to metadata369 #[pallet::storage]370 pub type DummyStorageValue<T> =371 StorageValue<Value = (CollectionStats, CollectionId, TokenId), QueryKind = OptionQuery>;372}373374impl<T: Config> Pallet<T> {375 /// Ethereum receiver 0x0000000000000000000000000000000000000000 is reserved, and shouldn't own tokens376 pub fn ensure_correct_receiver(receiver: &T::CrossAccountId) -> DispatchResult {377 ensure!(378 &T::CrossAccountId::from_eth(H160([0; 20])) != receiver,379 <Error<T>>::AddressIsZero380 );381 Ok(())382 }383 pub fn adminlist(collection: CollectionId) -> Vec<T::CrossAccountId> {384 <IsAdmin<T>>::iter_prefix((collection,))385 .map(|(a, _)| a)386 .collect()387 }388 pub fn allowlist(collection: CollectionId) -> Vec<T::CrossAccountId> {389 <Allowlist<T>>::iter_prefix((collection,))390 .map(|(a, _)| a)391 .collect()392 }393 pub fn allowed(collection: CollectionId, user: T::CrossAccountId) -> bool {394 <Allowlist<T>>::get((collection, user))395 }396 pub fn collection_stats() -> CollectionStats {397 let created = <CreatedCollectionCount<T>>::get();398 let destroyed = <DestroyedCollectionCount<T>>::get();399 CollectionStats {400 created: created.0,401 destroyed: destroyed.0,402 alive: created.0 - destroyed.0,403 }404 }405}406407impl<T: Config> Pallet<T> {408 pub fn init_collection(409 owner: T::AccountId,410 data: CreateCollectionData<T::AccountId>,411 ) -> Result<CollectionId, DispatchError> {412 {413 ensure!(414 data.token_prefix.len() <= MAX_TOKEN_PREFIX_LENGTH as usize,415 Error::<T>::CollectionTokenPrefixLimitExceeded416 );417 }418419 let created_count = <CreatedCollectionCount<T>>::get()420 .0421 .checked_add(1)422 .ok_or(ArithmeticError::Overflow)?;423 let destroyed_count = <DestroyedCollectionCount<T>>::get().0;424 let id = CollectionId(created_count);425426 // bound Total number of collections427 ensure!(428 created_count - destroyed_count <= COLLECTION_NUMBER_LIMIT,429 <Error<T>>::TotalCollectionsLimitExceeded430 );431432 // =========433434 let collection = Collection {435 owner: owner.clone(),436 name: data.name,437 mode: data.mode.clone(),438 mint_mode: false,439 access: data.access.unwrap_or_default(),440 description: data.description,441 token_prefix: data.token_prefix,442 offchain_schema: data.offchain_schema,443 schema_version: data.schema_version.unwrap_or_default(),444 sponsorship: data445 .pending_sponsor446 .map(SponsorshipState::Unconfirmed)447 .unwrap_or_default(),448 variable_on_chain_schema: data.variable_on_chain_schema,449 const_on_chain_schema: data.const_on_chain_schema,450 limits: data451 .limits452 .map(|limits| Self::clamp_limits(data.mode.clone(), &Default::default(), limits))453 .unwrap_or_else(|| Ok(CollectionLimits::default()))?,454 meta_update_permission: data.meta_update_permission.unwrap_or_default(),455 };456457 // Take a (non-refundable) deposit of collection creation458 {459 let mut imbalance =460 <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();461 imbalance.subsume(462 <<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(463 &T::TreasuryAccountId::get(),464 T::CollectionCreationPrice::get(),465 ),466 );467 <T as Config>::Currency::settle(468 &owner,469 imbalance,470 WithdrawReasons::TRANSFER,471 ExistenceRequirement::KeepAlive,472 )473 .map_err(|_| Error::<T>::NoPermission)?;474 }475476 <CreatedCollectionCount<T>>::put(created_count);477 <Pallet<T>>::deposit_event(Event::CollectionCreated(id, data.mode.id(), owner.clone()));478 <CollectionById<T>>::insert(id, collection);479 Ok(id)480 }481482 pub fn destroy_collection(483 collection: CollectionHandle<T>,484 sender: &T::CrossAccountId,485 ) -> DispatchResult {486 ensure!(487 collection.limits.owner_can_destroy(),488 <Error<T>>::NoPermission,489 );490 collection.check_is_owner(sender)?;491492 let destroyed_collections = <DestroyedCollectionCount<T>>::get()493 .0494 .checked_add(1)495 .ok_or(ArithmeticError::Overflow)?;496497 // =========498499 <DestroyedCollectionCount<T>>::put(destroyed_collections);500 <CollectionById<T>>::remove(collection.id);501 <AdminAmount<T>>::remove(collection.id);502 <IsAdmin<T>>::remove_prefix((collection.id,), None);503 <Allowlist<T>>::remove_prefix((collection.id,), None);504505 <Pallet<T>>::deposit_event(Event::CollectionDestroyed(collection.id));506 Ok(())507 }508509 pub fn toggle_allowlist(510 collection: &CollectionHandle<T>,511 sender: &T::CrossAccountId,512 user: &T::CrossAccountId,513 allowed: bool,514 ) -> DispatchResult {515 collection.check_is_owner_or_admin(sender)?;516517 // =========518519 if allowed {520 <Allowlist<T>>::insert((collection.id, user), true);521 } else {522 <Allowlist<T>>::remove((collection.id, user));523 }524525 Ok(())526 }527528 pub fn toggle_admin(529 collection: &CollectionHandle<T>,530 sender: &T::CrossAccountId,531 user: &T::CrossAccountId,532 admin: bool,533 ) -> DispatchResult {534 collection.check_is_owner_or_admin(sender)?;535536 let was_admin = <IsAdmin<T>>::get((collection.id, user));537 if was_admin == admin {538 return Ok(());539 }540 let amount = <AdminAmount<T>>::get(collection.id);541542 if admin {543 let amount = amount544 .checked_add(1)545 .ok_or(<Error<T>>::CollectionAdminCountExceeded)?;546 ensure!(547 amount <= Self::collection_admins_limit(),548 <Error<T>>::CollectionAdminCountExceeded,549 );550551 // =========552553 <AdminAmount<T>>::insert(collection.id, amount);554 <IsAdmin<T>>::insert((collection.id, user), true);555 } else {556 <AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));557 <IsAdmin<T>>::remove((collection.id, user));558 }559560 Ok(())561 }562563 pub fn clamp_limits(564 mode: CollectionMode,565 old_limit: &CollectionLimits,566 mut new_limit: CollectionLimits,567 ) -> Result<CollectionLimits, DispatchError> {568 macro_rules! limit_default {569 ($old:ident, $new:ident, $($field:ident $(($arg:expr))? => $check:expr),* $(,)?) => {{570 $(571 if let Some($new) = $new.$field {572 let $old = $old.$field($($arg)?);573 let _ = $new;574 let _ = $old;575 $check576 } else {577 $new.$field = $old.$field578 }579 )*580 }};581 }582583 limit_default!(old_limit, new_limit,584 account_token_ownership_limit => ensure!(585 new_limit <= MAX_TOKEN_OWNERSHIP,586 <Error<T>>::CollectionLimitBoundsExceeded,587 ),588 sponsor_transfer_timeout(match mode {589 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,590 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,591 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,592 }) => ensure!(593 new_limit <= MAX_SPONSOR_TIMEOUT,594 <Error<T>>::CollectionLimitBoundsExceeded,595 ),596 sponsored_data_size => ensure!(597 new_limit <= CUSTOM_DATA_LIMIT,598 <Error<T>>::CollectionLimitBoundsExceeded,599 ),600 token_limit => ensure!(601 old_limit >= new_limit && new_limit > 0,602 <Error<T>>::CollectionTokenLimitExceeded603 ),604 owner_can_transfer => ensure!(605 old_limit || !new_limit,606 <Error<T>>::OwnerPermissionsCantBeReverted,607 ),608 owner_can_destroy => ensure!(609 old_limit || !new_limit,610 <Error<T>>::OwnerPermissionsCantBeReverted,611 ),612 sponsored_data_rate_limit => {},613 transfers_enabled => {},614 );615 Ok(new_limit)616 }617}618619#[macro_export]620macro_rules! unsupported {621 () => {622 Err(<Error<T>>::UnsupportedOperation.into())623 };624}625626/// Worst cases627pub trait CommonWeightInfo {628 fn create_item() -> Weight;629 fn create_multiple_items(amount: u32) -> Weight;630 fn burn_item() -> Weight;631 fn transfer() -> Weight;632 fn approve() -> Weight;633 fn transfer_from() -> Weight;634 fn burn_from() -> Weight;635 fn set_variable_metadata(bytes: u32) -> Weight;636}637638pub trait CommonCollectionOperations<T: Config> {639 fn create_item(640 &self,641 sender: T::CrossAccountId,642 to: T::CrossAccountId,643 data: CreateItemData,644 ) -> DispatchResultWithPostInfo;645 fn create_multiple_items(646 &self,647 sender: T::CrossAccountId,648 to: T::CrossAccountId,649 data: Vec<CreateItemData>,650 ) -> DispatchResultWithPostInfo;651 fn burn_item(652 &self,653 sender: T::CrossAccountId,654 token: TokenId,655 amount: u128,656 ) -> DispatchResultWithPostInfo;657658 fn transfer(659 &self,660 sender: T::CrossAccountId,661 to: T::CrossAccountId,662 token: TokenId,663 amount: u128,664 ) -> DispatchResultWithPostInfo;665 fn approve(666 &self,667 sender: T::CrossAccountId,668 spender: T::CrossAccountId,669 token: TokenId,670 amount: u128,671 ) -> DispatchResultWithPostInfo;672 fn transfer_from(673 &self,674 sender: T::CrossAccountId,675 from: T::CrossAccountId,676 to: T::CrossAccountId,677 token: TokenId,678 amount: u128,679 ) -> DispatchResultWithPostInfo;680 fn burn_from(681 &self,682 sender: T::CrossAccountId,683 from: T::CrossAccountId,684 token: TokenId,685 amount: u128,686 ) -> DispatchResultWithPostInfo;687688 fn set_variable_metadata(689 &self,690 sender: T::CrossAccountId,691 token: TokenId,692 data: BoundedVec<u8, CustomDataLimit>,693 ) -> DispatchResultWithPostInfo;694695 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;696 fn token_exists(&self, token: TokenId) -> bool;697 fn last_token_id(&self) -> TokenId;698699 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;700 fn const_metadata(&self, token: TokenId) -> Vec<u8>;701 fn variable_metadata(&self, token: TokenId) -> Vec<u8>;702703 /// How many tokens collection contains (Applicable to nonfungible/refungible)704 fn collection_tokens(&self) -> u32;705 /// Amount of different tokens account has (Applicable to nonfungible/refungible)706 fn account_balance(&self, account: T::CrossAccountId) -> u32;707 /// Amount of specific token account have (Applicable to fungible/refungible)708 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;709 fn allowance(710 &self,711 sender: T::CrossAccountId,712 spender: T::CrossAccountId,713 token: TokenId,714 ) -> u128;715}716717// Flexible enough for implementing CommonCollectionOperations718pub fn with_weight(res: DispatchResult, weight: Weight) -> DispatchResultWithPostInfo {719 let post_info = PostDispatchInfo {720 actual_weight: Some(weight),721 pays_fee: Pays::Yes,722 };723 match res {724 Ok(()) => Ok(post_info),725 Err(error) => Err(DispatchErrorWithPostInfo { post_info, error }),726 }727}pallets/fungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/fungible/src/benchmarking.rs
+++ b/pallets/fungible/src/benchmarking.rs
@@ -4,7 +4,7 @@
use sp_std::prelude::*;
use pallet_common::benchmarking::create_collection_raw;
use frame_benchmarking::{benchmarks, account};
-use up_data_structs::{CollectionMode};
+use up_data_structs::{CollectionMode, MAX_ITEMS_PER_BATCH};
use pallet_common::bench_init;
const SEED: u32 = 1;
@@ -26,6 +26,18 @@
};
}: {<Pallet<T>>::create_item(&collection, &sender, (to, 200))?}
+ create_multiple_items_ex {
+ let b in 0..MAX_ITEMS_PER_BATCH;
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ sender: cross_from_sub(owner);
+ };
+ let data = (0..b).map(|i| {
+ bench_init!(to: cross_sub(i););
+ (to, 200)
+ }).collect::<BTreeMap<_, _>>().try_into().unwrap();
+ }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)}
+
burn_item {
bench_init!{
owner: sub; collection: collection(owner);
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -1,7 +1,7 @@
use core::marker::PhantomData;
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
-use up_data_structs::TokenId;
+use up_data_structs::{TokenId, CreateItemExData};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::ArithmeticError;
use sp_std::{vec::Vec, vec};
@@ -21,6 +21,15 @@
Self::create_item()
}
+ fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {
+ match data {
+ CreateItemExData::Fungible(f) => {
+ <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)
+ }
+ _ => 0,
+ }
+ }
+
fn burn_item() -> Weight {
<SelfWeightOf<T>>::burn_item()
}
@@ -87,6 +96,23 @@
)
}
+ fn create_multiple_items_ex(
+ &self,
+ sender: <T>::CrossAccountId,
+ data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);
+ let data = match data {
+ up_data_structs::CreateItemExData::Fungible(f) => f,
+ _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),
+ };
+
+ with_weight(
+ <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),
+ weight,
+ )
+ }
+
fn burn_item(
&self,
sender: T::CrossAccountId,
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -392,6 +392,6 @@
sender: &T::CrossAccountId,
data: CreateItemData<T>,
) -> DispatchResult {
- Self::create_multiple_items(collection, sender, vec![data])
+ Self::create_multiple_items(collection, sender, [(data.0, data.1)].into_iter().collect())
}
}
pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -33,6 +33,7 @@
/// Weight functions needed for pallet_fungible.
pub trait WeightInfo {
fn create_item() -> Weight;
+ fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
@@ -51,6 +52,17 @@
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Fungible TotalSupply (r:1 w:1)
+ // Storage: Fungible Balance (r:4 w:4)
+ fn create_multiple_items_ex(b: u32, ) -> Weight {
+ (1_055_000 as Weight)
+ // Standard Error: 22_000
+ .saturating_add((5_273_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(T::DbWeight::get().writes(1 as Weight))
+ .saturating_add(T::DbWeight::get().writes((1 as Weight).saturating_mul(b 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)
@@ -97,6 +109,17 @@
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Fungible TotalSupply (r:1 w:1)
+ // Storage: Fungible Balance (r:4 w:4)
+ fn create_multiple_items_ex(b: u32, ) -> Weight {
+ (1_055_000 as Weight)
+ // Standard Error: 22_000
+ .saturating_add((5_273_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(RocksDbWeight::get().writes(1 as Weight))
+ .saturating_add(RocksDbWeight::get().writes((1 as Weight).saturating_mul(b 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)
pallets/nonfungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/benchmarking.rs
+++ b/pallets/nonfungible/src/benchmarking.rs
@@ -56,6 +56,16 @@
let data = (0..b).map(|_| create_max_item_data::<T>(to.clone())).collect();
}: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
+ create_multiple_items_ex {
+ let b in 0..MAX_ITEMS_PER_BATCH;
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ sender: cross_from_sub(owner);
+ };
+ let data = (0..b).map(|i| {
+ bench_init!(to: cross_sub(i););
+ create_max_item_data::<T>(to)
+ }).collect();
}: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
burn_item {
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -1,7 +1,7 @@
use core::marker::PhantomData;
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
-use up_data_structs::{TokenId, CustomDataLimit};
+use up_data_structs::{TokenId, CustomDataLimit, CreateItemExData};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::DispatchError;
use sp_std::vec::Vec;
@@ -17,6 +17,13 @@
<SelfWeightOf<T>>::create_item()
}
+ fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {
+ match data {
+ CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),
+ _ => 0,
+ }
+ }
+
fn create_multiple_items(amount: u32) -> Weight {
<SelfWeightOf<T>>::create_multiple_items(amount)
}
@@ -91,6 +98,23 @@
)
}
+ fn create_multiple_items_ex(
+ &self,
+ sender: <T>::CrossAccountId,
+ data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);
+ let data = match data {
+ up_data_structs::CreateItemExData::NFT(nft) => nft,
+ _ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),
+ };
+
+ with_weight(
+ <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),
+ weight,
+ )
+ }
+
fn burn_item(
&self,
sender: T::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -2,7 +2,9 @@
use erc::ERC721Events;
use frame_support::{BoundedVec, ensure};
-use up_data_structs::{AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData};
+use up_data_structs::{
+ AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
+};
use pallet_common::{
Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, account::CrossAccountId,
};
@@ -22,11 +24,7 @@
pub mod erc;
pub mod weights;
-pub struct CreateItemData<T: Config> {
- pub const_data: BoundedVec<u8, CustomDataLimit>,
- pub variable_data: BoundedVec<u8, CustomDataLimit>,
- pub owner: T::CrossAccountId,
-}
+pub type CreateItemData<T> = CreateNftExData<<T as pallet_common::Config>::CrossAccountId>;
pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen)]
pallets/nonfungible/src/weights.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -34,6 +34,7 @@
pub trait WeightInfo {
fn create_item() -> Weight;
fn create_multiple_items(b: u32, ) -> Weight;
+ fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
@@ -66,6 +67,19 @@
.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 TokensMinted (r:1 w:1)
+ // Storage: Nonfungible AccountBalance (r:4 w:4)
+ // Storage: Nonfungible TokenData (r:0 w:4)
+ // Storage: Nonfungible Owned (r:0 w:4)
+ fn create_multiple_items_ex(b: u32, ) -> Weight {
+ (2_090_000 as Weight)
+ // Standard Error: 10_000
+ .saturating_add((9_230_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(T::DbWeight::get().writes(1 as Weight))
+ .saturating_add(T::DbWeight::get().writes((3 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)
@@ -140,6 +154,19 @@
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
.saturating_add(RocksDbWeight::get().writes((2 as Weight).saturating_mul(b as Weight)))
}
+ // Storage: Nonfungible TokensMinted (r:1 w:1)
+ // Storage: Nonfungible AccountBalance (r:4 w:4)
+ // Storage: Nonfungible TokenData (r:0 w:4)
+ // Storage: Nonfungible Owned (r:0 w:4)
+ fn create_multiple_items_ex(b: u32, ) -> Weight {
+ (2_090_000 as Weight)
+ // Standard Error: 10_000
+ .saturating_add((9_230_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(RocksDbWeight::get().writes(1 as Weight))
+ .saturating_add(RocksDbWeight::get().writes((3 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)
pallets/refungible/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/refungible/src/benchmarking.rs
+++ b/pallets/refungible/src/benchmarking.rs
@@ -31,7 +31,8 @@
sender: &T::CrossAccountId,
users: impl IntoIterator<Item = (T::CrossAccountId, u128)>,
) -> Result<TokenId, DispatchError> {
- <Pallet<T>>::create_item(&collection, sender, create_max_item_data(users))?;
+ let data: CreateRefungibleExData<T::CrossAccountId> = create_max_item_data(users);
+ <Pallet<T>>::create_item(&collection, sender, data)?;
Ok(TokenId(<TokensMinted<T>>::get(&collection.id)))
}
@@ -60,6 +61,30 @@
let data = (0..b).map(|_| create_max_item_data([(to.clone(), 200)])).collect();
}: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
+ create_multiple_items_ex_multiple_items {
+ let b in 0..MAX_ITEMS_PER_BATCH;
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ sender: cross_from_sub(owner);
+ };
+ let data = (0..b).map(|t| {
+ bench_init!(to: cross_sub(t););
+ create_max_item_data([(to, 200)])
+ }).collect();
+ }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
+
+ create_multiple_items_ex_multiple_owners {
+ let b in 0..MAX_ITEMS_PER_BATCH;
+ bench_init!{
+ owner: sub; collection: collection(owner);
+ sender: cross_from_sub(owner);
+ };
+ let data = vec![create_max_item_data((0..b).map(|u| {
+ bench_init!(to: cross_sub(u););
+ (to, 200)
+ }))].try_into().unwrap();
+ }: {<Pallet<T>>::create_multiple_items(&collection, &sender, data)?}
+
// Other user left, token data is kept
burn_item_partial {
bench_init!{
@@ -170,6 +195,6 @@
sender: cross_from_sub(owner);
};
let item = create_max_item(&collection, &sender, [(sender.clone(), 200)])?;
- let data = create_data(b as usize);
+ let data = create_var_data(b).try_into().unwrap();
}: {<Pallet<T>>::set_variable_metadata(&collection, &sender, item, data)?}
}
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -2,10 +2,10 @@
use sp_std::collections::btree_map::BTreeMap;
use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};
-use up_data_structs::{TokenId, CustomDataLimit};
+use up_data_structs::{TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::DispatchError;
-use sp_std::vec::Vec;
+use sp_std::{vec::Vec, vec};
use crate::{
AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,
@@ -31,6 +31,18 @@
<SelfWeightOf<T>>::create_multiple_items(amount)
}
+ fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {
+ match call {
+ CreateItemExData::RefungibleMultipleOwners(i) => {
+ <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)
+ }
+ CreateItemExData::RefungibleMultipleItems(i) => {
+ <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)
+ }
+ _ => 0,
+ }
+ }
+
fn burn_item() -> Weight {
max_weight_of!(burn_item_partial(), burn_item_fully())
}
@@ -69,15 +81,15 @@
fn map_create_data<T: Config>(
data: up_data_structs::CreateItemData,
to: &T::CrossAccountId,
-) -> Result<CreateItemData<T>, DispatchError> {
+) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {
match data {
- up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData {
+ up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {
const_data: data.const_data,
variable_data: data.variable_data,
users: {
let mut out = BTreeMap::new();
out.insert(to.clone(), data.pieces);
- out
+ out.try_into().expect("limit > 0")
},
}),
_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),
@@ -92,7 +104,7 @@
data: up_data_structs::CreateItemData,
) -> DispatchResultWithPostInfo {
with_weight(
- <Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),
+ <Pallet<T>>::create_item(self, &sender, map_create_data::<T>(data, &to)?),
<CommonWeights<T>>::create_item(),
)
}
@@ -115,6 +127,28 @@
)
}
+ fn create_multiple_items_ex(
+ &self,
+ sender: <T>::CrossAccountId,
+ data: CreateItemExData<T::CrossAccountId>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);
+ let data = match data {
+ CreateItemExData::RefungibleMultipleOwners(r) => vec![r],
+ CreateItemExData::RefungibleMultipleItems(r)
+ if r.iter().all(|i| i.users.len() == 1) =>
+ {
+ r.into_inner()
+ }
+ _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),
+ };
+
+ with_weight(
+ <Pallet<T>>::create_multiple_items(self, &sender, data),
+ weight,
+ )
+ }
+
fn burn_item(
&self,
sender: T::CrossAccountId,
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -2,7 +2,8 @@
use frame_support::{ensure, BoundedVec};
use up_data_structs::{
- AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId, CreateCollectionData,
+ AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,
+ CreateCollectionData, CreateRefungibleExData,
};
use pallet_common::{
Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,
@@ -19,11 +20,6 @@
pub mod common;
pub mod erc;
pub mod weights;
-pub struct CreateItemData<T: Config> {
- pub const_data: BoundedVec<u8, CustomDataLimit>,
- pub variable_data: BoundedVec<u8, CustomDataLimit>,
- pub users: BTreeMap<T::CrossAccountId, u128>,
-}
pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)]
@@ -361,7 +357,7 @@
pub fn create_multiple_items(
collection: &RefungibleHandle<T>,
sender: &T::CrossAccountId,
- data: Vec<CreateItemData<T>>,
+ data: Vec<CreateRefungibleExData<T::CrossAccountId>>,
) -> DispatchResult {
if !collection.is_owner_or_admin(sender) {
ensure!(
@@ -606,7 +602,7 @@
pub fn create_item(
collection: &RefungibleHandle<T>,
sender: &T::CrossAccountId,
- data: CreateItemData<T>,
+ data: CreateRefungibleExData<T::CrossAccountId>,
) -> DispatchResult {
Self::create_multiple_items(collection, sender, vec![data])
}
pallets/refungible/src/weights.rsdiffbeforeafterboth--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -34,6 +34,8 @@
pub trait WeightInfo {
fn create_item() -> Weight;
fn create_multiple_items(b: u32, ) -> Weight;
+ fn create_multiple_items_ex_multiple_items(b: u32, ) -> Weight;
+ fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight;
fn burn_item_partial() -> Weight;
fn burn_item_fully() -> Weight;
fn transfer_normal() -> Weight;
@@ -77,6 +79,36 @@
.saturating_add(T::DbWeight::get().writes(2 as Weight))
.saturating_add(T::DbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
}
+ // Storage: Refungible TokensMinted (r:1 w:1)
+ // Storage: Refungible AccountBalance (r:4 w:4)
+ // Storage: Refungible Balance (r:0 w:4)
+ // Storage: Refungible TotalSupply (r:0 w:4)
+ // Storage: Refungible TokenData (r:0 w:4)
+ // Storage: Refungible Owned (r:0 w:4)
+ fn create_multiple_items_ex_multiple_items(b: u32, ) -> Weight {
+ (11_953_000 as Weight)
+ // Standard Error: 27_000
+ .saturating_add((10_775_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(T::DbWeight::get().writes(1 as Weight))
+ .saturating_add(T::DbWeight::get().writes((5 as Weight).saturating_mul(b as Weight)))
+ }
+ // Storage: Refungible TokensMinted (r:1 w:1)
+ // Storage: Refungible TotalSupply (r:0 w:1)
+ // Storage: Refungible TokenData (r:0 w:1)
+ // Storage: Refungible AccountBalance (r:4 w:4)
+ // Storage: Refungible Balance (r:0 w:4)
+ // Storage: Refungible Owned (r:0 w:4)
+ fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight {
+ (0 as Weight)
+ // Standard Error: 13_000
+ .saturating_add((8_528_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(T::DbWeight::get().reads(1 as Weight))
+ .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(T::DbWeight::get().writes(3 as Weight))
+ .saturating_add(T::DbWeight::get().writes((3 as Weight).saturating_mul(b as Weight)))
+ }
// Storage: Refungible TotalSupply (r:1 w:1)
// Storage: Refungible Balance (r:1 w:1)
// Storage: Refungible AccountBalance (r:1 w:1)
@@ -215,6 +247,36 @@
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
.saturating_add(RocksDbWeight::get().writes((4 as Weight).saturating_mul(b as Weight)))
}
+ // Storage: Refungible TokensMinted (r:1 w:1)
+ // Storage: Refungible AccountBalance (r:4 w:4)
+ // Storage: Refungible Balance (r:0 w:4)
+ // Storage: Refungible TotalSupply (r:0 w:4)
+ // Storage: Refungible TokenData (r:0 w:4)
+ // Storage: Refungible Owned (r:0 w:4)
+ fn create_multiple_items_ex_multiple_items(b: u32, ) -> Weight {
+ (11_953_000 as Weight)
+ // Standard Error: 27_000
+ .saturating_add((10_775_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(RocksDbWeight::get().writes(1 as Weight))
+ .saturating_add(RocksDbWeight::get().writes((5 as Weight).saturating_mul(b as Weight)))
+ }
+ // Storage: Refungible TokensMinted (r:1 w:1)
+ // Storage: Refungible TotalSupply (r:0 w:1)
+ // Storage: Refungible TokenData (r:0 w:1)
+ // Storage: Refungible AccountBalance (r:4 w:4)
+ // Storage: Refungible Balance (r:0 w:4)
+ // Storage: Refungible Owned (r:0 w:4)
+ fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight {
+ (0 as Weight)
+ // Standard Error: 13_000
+ .saturating_add((8_528_000 as Weight).saturating_mul(b as Weight))
+ .saturating_add(RocksDbWeight::get().reads(1 as Weight))
+ .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
+ .saturating_add(RocksDbWeight::get().writes(3 as Weight))
+ .saturating_add(RocksDbWeight::get().writes((3 as Weight).saturating_mul(b as Weight)))
+ }
// Storage: Refungible TotalSupply (r:1 w:1)
// Storage: Refungible Balance (r:1 w:1)
// Storage: Refungible AccountBalance (r:1 w:1)
pallets/unique/src/common.rsdiffbeforeafterboth--- a/pallets/unique/src/common.rs
+++ b/pallets/unique/src/common.rs
@@ -5,6 +5,7 @@
use pallet_fungible::{common::CommonWeights as FungibleWeights};
use pallet_nonfungible::{common::CommonWeights as NonfungibleWeights};
use pallet_refungible::{common::CommonWeights as RefungibleWeights};
+use up_data_structs::CreateItemExData;
use crate::{Config, dispatch::dispatch_weight};
@@ -17,7 +18,7 @@
}
pub struct CommonWeights<T: Config>(PhantomData<T>);
-impl<T: Config> CommonWeightInfo for CommonWeights<T> {
+impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
fn create_item() -> up_data_structs::Weight {
dispatch_weight::<T>() + max_weight_of!(create_item())
}
@@ -26,6 +27,10 @@
dispatch_weight::<T>() + max_weight_of!(create_multiple_items(amount))
}
+ fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(create_multiple_items_ex(data))
+ }
+
fn burn_item() -> Weight {
dispatch_weight::<T>() + max_weight_of!(burn_item())
}
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -40,7 +40,7 @@
OFFCHAIN_SCHEMA_LIMIT, MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH,
MAX_TOKEN_PREFIX_LENGTH, AccessMode, CreateItemData, CollectionLimits, CollectionId,
CollectionMode, TokenId, SchemaVersion, SponsorshipState, MetaUpdatePermission,
- CreateCollectionData, CustomDataLimit,
+ CreateCollectionData, CustomDataLimit, CreateItemExData,
};
use pallet_common::{
account::CrossAccountId, CollectionHandle, Pallet as PalletCommon, Error as CommonError,
@@ -735,6 +735,14 @@
dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items(sender, owner, items_data))
}
+ #[weight = <CommonWeights<T>>::create_multiple_items_ex(&data)]
+ #[transactional]
+ pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+ dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items_ex(sender, data))
+ }
+
// TODO! transaction weight
/// Set transfers_enabled value for particular collection
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -1,6 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
-use core::convert::{TryFrom, TryInto};
+use core::{
+ convert::{TryFrom, TryInto},
+ fmt,
+};
+use frame_support::storage::bounded_btree_map::BoundedBTreeMap;
+use sp_std::collections::btree_map::BTreeMap;
#[cfg(feature = "serde")]
pub use serde::{Serialize, Deserialize};
@@ -525,6 +530,47 @@
ReFungible(CreateReFungibleData),
}
+#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]
+#[derivative(Debug)]
+pub struct CreateNftExData<CrossAccountId> {
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ pub const_data: BoundedVec<u8, CustomDataLimit>,
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ pub variable_data: BoundedVec<u8, CustomDataLimit>,
+ pub owner: CrossAccountId,
+}
+
+#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]
+#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]
+pub struct CreateRefungibleExData<CrossAccountId> {
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ pub const_data: BoundedVec<u8, CustomDataLimit>,
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ pub variable_data: BoundedVec<u8, CustomDataLimit>,
+ #[derivative(Debug(format_with = "bounded_map_debug"))]
+ pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,
+}
+
+#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]
+#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]
+pub enum CreateItemExData<CrossAccountId> {
+ NFT(
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ BoundedVec<CreateNftExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,
+ ),
+ Fungible(
+ #[derivative(Debug(format_with = "bounded_map_debug"))]
+ BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,
+ ),
+ /// Many tokens, each may have only one owner
+ RefungibleMultipleItems(
+ #[derivative(Debug(format_with = "bounded_debug"))]
+ BoundedVec<CreateRefungibleExData<CrossAccountId>, ConstU32<MAX_ITEMS_PER_BATCH>>,
+ ),
+ /// Single token, which may have many owners
+ RefungibleMultipleOwners(CreateRefungibleExData<CrossAccountId>),
+}
+
impl CreateItemData {
pub fn data_size(&self) -> usize {
match self {