difftreelog
feat createMultipleItemsEx call
in: master
16 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -17,7 +17,7 @@
TokenId, Weight, WithdrawReasons, CollectionStats, MAX_TOKEN_OWNERSHIP, CollectionMode,
NFT_SPONSOR_TRANSFER_TIMEOUT, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,
REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MAX_SPONSOR_TIMEOUT, CUSTOM_DATA_LIMIT, CollectionLimits,
- CustomDataLimit, CreateCollectionData, SponsorshipState,
+ CustomDataLimit, CreateCollectionData, SponsorshipState, CreateItemExData,
};
pub use pallet::*;
use sp_core::H160;
@@ -624,9 +624,10 @@
}
/// Worst cases
-pub trait CommonWeightInfo {
+pub trait CommonWeightInfo<CrossAccountId> {
fn create_item() -> Weight;
fn create_multiple_items(amount: u32) -> Weight;
+ fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;
fn burn_item() -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
@@ -648,6 +649,11 @@
to: T::CrossAccountId,
data: Vec<CreateItemData>,
) -> DispatchResultWithPostInfo;
+ fn create_multiple_items_ex(
+ &self,
+ sender: T::CrossAccountId,
+ data: CreateItemExData<T::CrossAccountId>,
+ ) -> DispatchResultWithPostInfo;
fn burn_item(
&self,
sender: T::CrossAccountId,
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.rsdiffbeforeafterboth1use core::marker::PhantomData;23use sp_std::collections::btree_map::BTreeMap;4use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};5use up_data_structs::{TokenId, CustomDataLimit};6use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};7use sp_runtime::DispatchError;8use sp_std::vec::Vec;910use crate::{11 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,12 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,13};1415macro_rules! max_weight_of {16 ($($method:ident ($($args:tt)*)),*) => {17 018 $(19 .max(<SelfWeightOf<T>>::$method($($args)*))20 )*21 };22}2324pub struct CommonWeights<T: Config>(PhantomData<T>);25impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {26 fn create_item() -> Weight {27 <SelfWeightOf<T>>::create_item()28 }2930 fn create_multiple_items(amount: u32) -> Weight {31 <SelfWeightOf<T>>::create_multiple_items(amount)32 }3334 fn burn_item() -> Weight {35 max_weight_of!(burn_item_partial(), burn_item_fully())36 }3738 fn transfer() -> Weight {39 max_weight_of!(40 transfer_normal(),41 transfer_creating(),42 transfer_removing(),43 transfer_creating_removing()44 )45 }4647 fn approve() -> Weight {48 <SelfWeightOf<T>>::approve()49 }5051 fn transfer_from() -> Weight {52 max_weight_of!(53 transfer_from_normal(),54 transfer_from_creating(),55 transfer_from_removing(),56 transfer_from_creating_removing()57 )58 }5960 fn burn_from() -> Weight {61 <SelfWeightOf<T>>::burn_from()62 }6364 fn set_variable_metadata(bytes: u32) -> Weight {65 <SelfWeightOf<T>>::set_variable_metadata(bytes)66 }67}6869fn map_create_data<T: Config>(70 data: up_data_structs::CreateItemData,71 to: &T::CrossAccountId,72) -> Result<CreateItemData<T>, DispatchError> {73 match data {74 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData {75 const_data: data.const_data,76 variable_data: data.variable_data,77 users: {78 let mut out = BTreeMap::new();79 out.insert(to.clone(), data.pieces);80 out81 },82 }),83 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),84 }85}8687impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {88 fn create_item(89 &self,90 sender: T::CrossAccountId,91 to: T::CrossAccountId,92 data: up_data_structs::CreateItemData,93 ) -> DispatchResultWithPostInfo {94 with_weight(95 <Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),96 <CommonWeights<T>>::create_item(),97 )98 }99100 fn create_multiple_items(101 &self,102 sender: T::CrossAccountId,103 to: T::CrossAccountId,104 data: Vec<up_data_structs::CreateItemData>,105 ) -> DispatchResultWithPostInfo {106 let data = data107 .into_iter()108 .map(|d| map_create_data::<T>(d, &to))109 .collect::<Result<Vec<_>, DispatchError>>()?;110111 let amount = data.len();112 with_weight(113 <Pallet<T>>::create_multiple_items(self, &sender, data),114 <CommonWeights<T>>::create_multiple_items(amount as u32),115 )116 }117118 fn burn_item(119 &self,120 sender: T::CrossAccountId,121 token: TokenId,122 amount: u128,123 ) -> DispatchResultWithPostInfo {124 with_weight(125 <Pallet<T>>::burn(self, &sender, token, amount),126 <CommonWeights<T>>::burn_item(),127 )128 }129130 fn transfer(131 &self,132 from: T::CrossAccountId,133 to: T::CrossAccountId,134 token: TokenId,135 amount: u128,136 ) -> DispatchResultWithPostInfo {137 with_weight(138 <Pallet<T>>::transfer(self, &from, &to, token, amount),139 <CommonWeights<T>>::transfer(),140 )141 }142143 fn approve(144 &self,145 sender: T::CrossAccountId,146 spender: T::CrossAccountId,147 token: TokenId,148 amount: u128,149 ) -> DispatchResultWithPostInfo {150 with_weight(151 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),152 <CommonWeights<T>>::approve(),153 )154 }155156 fn transfer_from(157 &self,158 sender: T::CrossAccountId,159 from: T::CrossAccountId,160 to: T::CrossAccountId,161 token: TokenId,162 amount: u128,163 ) -> DispatchResultWithPostInfo {164 with_weight(165 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount),166 <CommonWeights<T>>::transfer_from(),167 )168 }169170 fn burn_from(171 &self,172 sender: T::CrossAccountId,173 from: T::CrossAccountId,174 token: TokenId,175 amount: u128,176 ) -> DispatchResultWithPostInfo {177 with_weight(178 <Pallet<T>>::burn_from(self, &sender, &from, token, amount),179 <CommonWeights<T>>::burn_from(),180 )181 }182183 fn set_variable_metadata(184 &self,185 sender: T::CrossAccountId,186 token: TokenId,187 data: BoundedVec<u8, CustomDataLimit>,188 ) -> DispatchResultWithPostInfo {189 let len = data.len();190 with_weight(191 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),192 <CommonWeights<T>>::set_variable_metadata(len as u32),193 )194 }195196 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {197 <Owned<T>>::iter_prefix((self.id, account))198 .map(|(id, _)| id)199 .collect()200 }201202 fn token_exists(&self, token: TokenId) -> bool {203 <Pallet<T>>::token_exists(self, token)204 }205206 fn last_token_id(&self) -> TokenId {207 TokenId(<TokensMinted<T>>::get(self.id))208 }209210 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {211 None212 }213 fn const_metadata(&self, token: TokenId) -> Vec<u8> {214 <TokenData<T>>::get((self.id, token))215 .const_data216 .into_inner()217 }218 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {219 <TokenData<T>>::get((self.id, token))220 .variable_data221 .into_inner()222 }223224 fn collection_tokens(&self) -> u32 {225 <Pallet<T>>::total_supply(self)226 }227228 fn account_balance(&self, account: T::CrossAccountId) -> u32 {229 <AccountBalance<T>>::get((self.id, account))230 }231232 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {233 <Balance<T>>::get((self.id, token, account))234 }235236 fn allowance(237 &self,238 sender: T::CrossAccountId,239 spender: T::CrossAccountId,240 token: TokenId,241 ) -> u128 {242 <Allowance<T>>::get((self.id, token, sender, spender))243 }244}1use core::marker::PhantomData;23use sp_std::collections::btree_map::BTreeMap;4use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};5use up_data_structs::{TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData};6use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};7use sp_runtime::DispatchError;8use sp_std::{vec::Vec, vec};910use crate::{11 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,12 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,13};1415macro_rules! max_weight_of {16 ($($method:ident ($($args:tt)*)),*) => {17 018 $(19 .max(<SelfWeightOf<T>>::$method($($args)*))20 )*21 };22}2324pub struct CommonWeights<T: Config>(PhantomData<T>);25impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {26 fn create_item() -> Weight {27 <SelfWeightOf<T>>::create_item()28 }2930 fn create_multiple_items(amount: u32) -> Weight {31 <SelfWeightOf<T>>::create_multiple_items(amount)32 }3334 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {35 match call {36 CreateItemExData::RefungibleMultipleOwners(i) => {37 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)38 }39 CreateItemExData::RefungibleMultipleItems(i) => {40 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)41 }42 _ => 0,43 }44 }4546 fn burn_item() -> Weight {47 max_weight_of!(burn_item_partial(), burn_item_fully())48 }4950 fn transfer() -> Weight {51 max_weight_of!(52 transfer_normal(),53 transfer_creating(),54 transfer_removing(),55 transfer_creating_removing()56 )57 }5859 fn approve() -> Weight {60 <SelfWeightOf<T>>::approve()61 }6263 fn transfer_from() -> Weight {64 max_weight_of!(65 transfer_from_normal(),66 transfer_from_creating(),67 transfer_from_removing(),68 transfer_from_creating_removing()69 )70 }7172 fn burn_from() -> Weight {73 <SelfWeightOf<T>>::burn_from()74 }7576 fn set_variable_metadata(bytes: u32) -> Weight {77 <SelfWeightOf<T>>::set_variable_metadata(bytes)78 }79}8081fn map_create_data<T: Config>(82 data: up_data_structs::CreateItemData,83 to: &T::CrossAccountId,84) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {85 match data {86 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {87 const_data: data.const_data,88 variable_data: data.variable_data,89 users: {90 let mut out = BTreeMap::new();91 out.insert(to.clone(), data.pieces);92 out.try_into().expect("limit > 0")93 },94 }),95 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),96 }97}9899impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {100 fn create_item(101 &self,102 sender: T::CrossAccountId,103 to: T::CrossAccountId,104 data: up_data_structs::CreateItemData,105 ) -> DispatchResultWithPostInfo {106 with_weight(107 <Pallet<T>>::create_item(self, &sender, map_create_data::<T>(data, &to)?),108 <CommonWeights<T>>::create_item(),109 )110 }111112 fn create_multiple_items(113 &self,114 sender: T::CrossAccountId,115 to: T::CrossAccountId,116 data: Vec<up_data_structs::CreateItemData>,117 ) -> DispatchResultWithPostInfo {118 let data = data119 .into_iter()120 .map(|d| map_create_data::<T>(d, &to))121 .collect::<Result<Vec<_>, DispatchError>>()?;122123 let amount = data.len();124 with_weight(125 <Pallet<T>>::create_multiple_items(self, &sender, data),126 <CommonWeights<T>>::create_multiple_items(amount as u32),127 )128 }129130 fn create_multiple_items_ex(131 &self,132 sender: <T>::CrossAccountId,133 data: CreateItemExData<T::CrossAccountId>,134 ) -> DispatchResultWithPostInfo {135 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);136 let data = match data {137 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],138 CreateItemExData::RefungibleMultipleItems(r)139 if r.iter().all(|i| i.users.len() == 1) =>140 {141 r.into_inner()142 }143 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),144 };145146 with_weight(147 <Pallet<T>>::create_multiple_items(self, &sender, data),148 weight,149 )150 }151152 fn burn_item(153 &self,154 sender: T::CrossAccountId,155 token: TokenId,156 amount: u128,157 ) -> DispatchResultWithPostInfo {158 with_weight(159 <Pallet<T>>::burn(self, &sender, token, amount),160 <CommonWeights<T>>::burn_item(),161 )162 }163164 fn transfer(165 &self,166 from: T::CrossAccountId,167 to: T::CrossAccountId,168 token: TokenId,169 amount: u128,170 ) -> DispatchResultWithPostInfo {171 with_weight(172 <Pallet<T>>::transfer(self, &from, &to, token, amount),173 <CommonWeights<T>>::transfer(),174 )175 }176177 fn approve(178 &self,179 sender: T::CrossAccountId,180 spender: T::CrossAccountId,181 token: TokenId,182 amount: u128,183 ) -> DispatchResultWithPostInfo {184 with_weight(185 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),186 <CommonWeights<T>>::approve(),187 )188 }189190 fn transfer_from(191 &self,192 sender: T::CrossAccountId,193 from: T::CrossAccountId,194 to: T::CrossAccountId,195 token: TokenId,196 amount: u128,197 ) -> DispatchResultWithPostInfo {198 with_weight(199 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount),200 <CommonWeights<T>>::transfer_from(),201 )202 }203204 fn burn_from(205 &self,206 sender: T::CrossAccountId,207 from: T::CrossAccountId,208 token: TokenId,209 amount: u128,210 ) -> DispatchResultWithPostInfo {211 with_weight(212 <Pallet<T>>::burn_from(self, &sender, &from, token, amount),213 <CommonWeights<T>>::burn_from(),214 )215 }216217 fn set_variable_metadata(218 &self,219 sender: T::CrossAccountId,220 token: TokenId,221 data: BoundedVec<u8, CustomDataLimit>,222 ) -> DispatchResultWithPostInfo {223 let len = data.len();224 with_weight(225 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),226 <CommonWeights<T>>::set_variable_metadata(len as u32),227 )228 }229230 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {231 <Owned<T>>::iter_prefix((self.id, account))232 .map(|(id, _)| id)233 .collect()234 }235236 fn token_exists(&self, token: TokenId) -> bool {237 <Pallet<T>>::token_exists(self, token)238 }239240 fn last_token_id(&self) -> TokenId {241 TokenId(<TokensMinted<T>>::get(self.id))242 }243244 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {245 None246 }247 fn const_metadata(&self, token: TokenId) -> Vec<u8> {248 <TokenData<T>>::get((self.id, token))249 .const_data250 .into_inner()251 }252 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {253 <TokenData<T>>::get((self.id, token))254 .variable_data255 .into_inner()256 }257258 fn collection_tokens(&self) -> u32 {259 <Pallet<T>>::total_supply(self)260 }261262 fn account_balance(&self, account: T::CrossAccountId) -> u32 {263 <AccountBalance<T>>::get((self.id, account))264 }265266 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {267 <Balance<T>>::get((self.id, token, account))268 }269270 fn allowance(271 &self,272 sender: T::CrossAccountId,273 spender: T::CrossAccountId,274 token: TokenId,275 ) -> u128 {276 <Allowance<T>>::get((self.id, token, sender, spender))277 }278}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 {